blob: e67cb38312800ed9e2b8d0f67bcd8a0f85da186c [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/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200531 * Disable modem status interrupts
532 */
533static void atmel_disable_ms(struct uart_port *port)
534{
535 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
536 uint32_t idr = 0;
537
538 /*
539 * Interrupt should not be disabled twice
540 */
541 if (!atmel_port->ms_irq_enabled)
542 return;
543
544 atmel_port->ms_irq_enabled = false;
545
546 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
547 disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
548 else
549 idr |= ATMEL_US_CTSIC;
550
551 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
552 disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
553 else
554 idr |= ATMEL_US_DSRIC;
555
556 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
557 disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
558 else
559 idr |= ATMEL_US_RIIC;
560
561 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
562 disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
563 else
564 idr |= ATMEL_US_DCDIC;
565
566 UART_PUT_IDR(port, idr);
567}
568
569/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000570 * Control the transmission of a break signal
571 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200572static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000573{
574 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200575 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000576 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200577 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000578}
579
580/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800581 * Stores the incoming character in the ring buffer
582 */
583static void
584atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
585 unsigned int ch)
586{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800587 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800588 struct circ_buf *ring = &atmel_port->rx_ring;
589 struct atmel_uart_char *c;
590
591 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
592 /* Buffer overflow, ignore char */
593 return;
594
595 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
596 c->status = status;
597 c->ch = ch;
598
599 /* Make sure the character is stored before we update head. */
600 smp_wmb();
601
602 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
603}
604
605/*
Chip Coldwella6670612008-02-08 04:21:06 -0800606 * Deal with parity, framing and overrun errors.
607 */
608static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
609{
610 /* clear error */
611 UART_PUT_CR(port, ATMEL_US_RSTSTA);
612
613 if (status & ATMEL_US_RXBRK) {
614 /* ignore side-effect */
615 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
616 port->icount.brk++;
617 }
618 if (status & ATMEL_US_PARE)
619 port->icount.parity++;
620 if (status & ATMEL_US_FRAME)
621 port->icount.frame++;
622 if (status & ATMEL_US_OVRE)
623 port->icount.overrun++;
624}
625
626/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000627 * Characters received (called from interrupt handler)
628 */
David Howells7d12e782006-10-05 14:55:46 +0100629static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000630{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800631 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800632 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000633
Andrew Victorafefc412006-06-19 19:53:19 +0100634 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200635 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000636 ch = UART_GET_CHAR(port);
637
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000638 /*
639 * note that the error handling code is
640 * out of the main execution path
641 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700642 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
643 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
644 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800645
Remy Bohmerb843aa22008-02-08 04:21:01 -0800646 /* clear error */
647 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800648
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700649 if (status & ATMEL_US_RXBRK
650 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700651 atmel_port->break_active = 1;
652 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700653 } else {
654 /*
655 * This is either the end-of-break
656 * condition or we've received at
657 * least one character without RXBRK
658 * being set. In both cases, the next
659 * RXBRK will indicate start-of-break.
660 */
661 UART_PUT_IDR(port, ATMEL_US_RXBRK);
662 status &= ~ATMEL_US_RXBRK;
663 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100664 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000665 }
666
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800667 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100668 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000669 }
670
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800671 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672}
673
674/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800675 * Transmit characters (called from tasklet with TXRDY interrupt
676 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000677 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200678static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000679{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700680 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100681 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000682
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100683 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000684 UART_PUT_CHAR(port, port->x_char);
685 port->icount.tx++;
686 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000687 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800688 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000689 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000690
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100691 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000692 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
693 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
694 port->icount.tx++;
695 if (uart_circ_empty(xmit))
696 break;
697 }
698
699 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
700 uart_write_wakeup(port);
701
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800702 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100703 /* Enable interrupts */
704 UART_PUT_IER(port, atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000705}
706
Elen Song08f738b2013-07-22 16:30:26 +0800707static void atmel_complete_tx_dma(void *arg)
708{
709 struct atmel_uart_port *atmel_port = arg;
710 struct uart_port *port = &atmel_port->uart;
711 struct circ_buf *xmit = &port->state->xmit;
712 struct dma_chan *chan = atmel_port->chan_tx;
713 unsigned long flags;
714
715 spin_lock_irqsave(&port->lock, flags);
716
717 if (chan)
718 dmaengine_terminate_all(chan);
719 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
720 xmit->tail &= UART_XMIT_SIZE - 1;
721
722 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
723
724 spin_lock_irq(&atmel_port->lock_tx);
725 async_tx_ack(atmel_port->desc_tx);
726 atmel_port->cookie_tx = -EINVAL;
727 atmel_port->desc_tx = NULL;
728 spin_unlock_irq(&atmel_port->lock_tx);
729
730 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
731 uart_write_wakeup(port);
732
733 /* Do we really need this? */
734 if (!uart_circ_empty(xmit))
735 tasklet_schedule(&atmel_port->tasklet);
736
737 spin_unlock_irqrestore(&port->lock, flags);
738}
739
740static void atmel_release_tx_dma(struct uart_port *port)
741{
742 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
743 struct dma_chan *chan = atmel_port->chan_tx;
744
745 if (chan) {
746 dmaengine_terminate_all(chan);
747 dma_release_channel(chan);
748 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200749 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800750 }
751
752 atmel_port->desc_tx = NULL;
753 atmel_port->chan_tx = NULL;
754 atmel_port->cookie_tx = -EINVAL;
755}
756
757/*
758 * Called from tasklet with TXRDY interrupt is disabled.
759 */
760static void atmel_tx_dma(struct uart_port *port)
761{
762 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
763 struct circ_buf *xmit = &port->state->xmit;
764 struct dma_chan *chan = atmel_port->chan_tx;
765 struct dma_async_tx_descriptor *desc;
766 struct scatterlist *sg = &atmel_port->sg_tx;
767
768 /* Make sure we have an idle channel */
769 if (atmel_port->desc_tx != NULL)
770 return;
771
772 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
773 /*
774 * DMA is idle now.
775 * Port xmit buffer is already mapped,
776 * and it is one page... Just adjust
777 * offsets and lengths. Since it is a circular buffer,
778 * we have to transmit till the end, and then the rest.
779 * Take the port lock to get a
780 * consistent xmit buffer state.
781 */
782 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
783 sg_dma_address(sg) = (sg_dma_address(sg) &
784 ~(UART_XMIT_SIZE - 1))
785 + sg->offset;
786 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
787 xmit->tail,
788 UART_XMIT_SIZE);
789 BUG_ON(!sg_dma_len(sg));
790
791 desc = dmaengine_prep_slave_sg(chan,
792 sg,
793 1,
794 DMA_MEM_TO_DEV,
795 DMA_PREP_INTERRUPT |
796 DMA_CTRL_ACK);
797 if (!desc) {
798 dev_err(port->dev, "Failed to send via dma!\n");
799 return;
800 }
801
802 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
803
804 atmel_port->desc_tx = desc;
805 desc->callback = atmel_complete_tx_dma;
806 desc->callback_param = atmel_port;
807 atmel_port->cookie_tx = dmaengine_submit(desc);
808
809 } else {
810 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
811 /* DMA done, stop TX, start RX for RS485 */
812 atmel_start_rx(port);
813 }
814 }
815
816 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
817 uart_write_wakeup(port);
818}
819
820static int atmel_prepare_tx_dma(struct uart_port *port)
821{
822 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
823 dma_cap_mask_t mask;
824 struct dma_slave_config config;
825 int ret, nent;
826
827 dma_cap_zero(mask);
828 dma_cap_set(DMA_SLAVE, mask);
829
830 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
831 if (atmel_port->chan_tx == NULL)
832 goto chan_err;
833 dev_info(port->dev, "using %s for tx DMA transfers\n",
834 dma_chan_name(atmel_port->chan_tx));
835
836 spin_lock_init(&atmel_port->lock_tx);
837 sg_init_table(&atmel_port->sg_tx, 1);
838 /* UART circular tx buffer is an aligned page. */
839 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
840 sg_set_page(&atmel_port->sg_tx,
841 virt_to_page(port->state->xmit.buf),
842 UART_XMIT_SIZE,
843 (int)port->state->xmit.buf & ~PAGE_MASK);
844 nent = dma_map_sg(port->dev,
845 &atmel_port->sg_tx,
846 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200847 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800848
849 if (!nent) {
850 dev_dbg(port->dev, "need to release resource of dma\n");
851 goto chan_err;
852 } else {
853 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
854 sg_dma_len(&atmel_port->sg_tx),
855 port->state->xmit.buf,
856 sg_dma_address(&atmel_port->sg_tx));
857 }
858
859 /* Configure the slave DMA */
860 memset(&config, 0, sizeof(config));
861 config.direction = DMA_MEM_TO_DEV;
862 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
863 config.dst_addr = port->mapbase + ATMEL_US_THR;
864
Maxime Ripard5483c102014-10-22 17:43:16 +0200865 ret = dmaengine_slave_config(atmel_port->chan_tx,
866 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800867 if (ret) {
868 dev_err(port->dev, "DMA tx slave configuration failed\n");
869 goto chan_err;
870 }
871
872 return 0;
873
874chan_err:
875 dev_err(port->dev, "TX channel not available, switch to pio\n");
876 atmel_port->use_dma_tx = 0;
877 if (atmel_port->chan_tx)
878 atmel_release_tx_dma(port);
879 return -EINVAL;
880}
881
Elen Song34df42f2013-07-22 16:30:27 +0800882static void atmel_complete_rx_dma(void *arg)
883{
884 struct uart_port *port = arg;
885 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
886
887 tasklet_schedule(&atmel_port->tasklet);
888}
889
890static void atmel_release_rx_dma(struct uart_port *port)
891{
892 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
893 struct dma_chan *chan = atmel_port->chan_rx;
894
895 if (chan) {
896 dmaengine_terminate_all(chan);
897 dma_release_channel(chan);
898 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200899 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800900 }
901
902 atmel_port->desc_rx = NULL;
903 atmel_port->chan_rx = NULL;
904 atmel_port->cookie_rx = -EINVAL;
905}
906
907static void atmel_rx_from_dma(struct uart_port *port)
908{
909 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200910 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800911 struct circ_buf *ring = &atmel_port->rx_ring;
912 struct dma_chan *chan = atmel_port->chan_rx;
913 struct dma_tx_state state;
914 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200915 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +0800916
917
918 /* Reset the UART timeout early so that we don't miss one */
919 UART_PUT_CR(port, ATMEL_US_STTTO);
920 dmastat = dmaengine_tx_status(chan,
921 atmel_port->cookie_rx,
922 &state);
923 /* Restart a new tasklet if DMA status is error */
924 if (dmastat == DMA_ERROR) {
925 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
926 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
927 tasklet_schedule(&atmel_port->tasklet);
928 return;
929 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200930
931 /* CPU claims ownership of RX DMA buffer */
932 dma_sync_sg_for_cpu(port->dev,
933 &atmel_port->sg_rx,
934 1,
935 DMA_DEV_TO_MEM);
Elen Song34df42f2013-07-22 16:30:27 +0800936
937 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200938 * ring->head points to the end of data already written by the DMA.
939 * ring->tail points to the beginning of data to be read by the
940 * framework.
941 * The current transfer size should not be larger than the dma buffer
942 * length.
Elen Song34df42f2013-07-22 16:30:27 +0800943 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200944 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
945 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
946 /*
947 * At this point ring->head may point to the first byte right after the
948 * last byte of the dma buffer:
949 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
950 *
951 * However ring->tail must always points inside the dma buffer:
952 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
953 *
954 * Since we use a ring buffer, we have to handle the case
955 * where head is lower than tail. In such a case, we first read from
956 * tail to the end of the buffer then reset tail.
957 */
958 if (ring->head < ring->tail) {
959 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +0800960
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200961 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
962 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +0800963 port->icount.rx += count;
964 }
965
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200966 /* Finally we read data from tail to head */
967 if (ring->tail < ring->head) {
968 count = ring->head - ring->tail;
969
970 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
971 /* Wrap ring->head if needed */
972 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
973 ring->head = 0;
974 ring->tail = ring->head;
975 port->icount.rx += count;
976 }
977
978 /* USART retreives ownership of RX DMA buffer */
979 dma_sync_sg_for_device(port->dev,
980 &atmel_port->sg_rx,
981 1,
982 DMA_DEV_TO_MEM);
983
984 /*
985 * Drop the lock here since it might end up calling
986 * uart_start(), which takes the lock.
987 */
988 spin_unlock(&port->lock);
989 tty_flip_buffer_push(tport);
990 spin_lock(&port->lock);
991
Elen Song34df42f2013-07-22 16:30:27 +0800992 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
993}
994
995static int atmel_prepare_rx_dma(struct uart_port *port)
996{
997 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
998 struct dma_async_tx_descriptor *desc;
999 dma_cap_mask_t mask;
1000 struct dma_slave_config config;
1001 struct circ_buf *ring;
1002 int ret, nent;
1003
1004 ring = &atmel_port->rx_ring;
1005
1006 dma_cap_zero(mask);
1007 dma_cap_set(DMA_CYCLIC, mask);
1008
1009 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1010 if (atmel_port->chan_rx == NULL)
1011 goto chan_err;
1012 dev_info(port->dev, "using %s for rx DMA transfers\n",
1013 dma_chan_name(atmel_port->chan_rx));
1014
1015 spin_lock_init(&atmel_port->lock_rx);
1016 sg_init_table(&atmel_port->sg_rx, 1);
1017 /* UART circular rx buffer is an aligned page. */
1018 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
1019 sg_set_page(&atmel_port->sg_rx,
1020 virt_to_page(ring->buf),
1021 ATMEL_SERIAL_RINGSIZE,
1022 (int)ring->buf & ~PAGE_MASK);
1023 nent = dma_map_sg(port->dev,
1024 &atmel_port->sg_rx,
1025 1,
Wolfram Sang48479142014-07-21 11:42:04 +02001026 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001027
1028 if (!nent) {
1029 dev_dbg(port->dev, "need to release resource of dma\n");
1030 goto chan_err;
1031 } else {
1032 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1033 sg_dma_len(&atmel_port->sg_rx),
1034 ring->buf,
1035 sg_dma_address(&atmel_port->sg_rx));
1036 }
1037
1038 /* Configure the slave DMA */
1039 memset(&config, 0, sizeof(config));
1040 config.direction = DMA_DEV_TO_MEM;
1041 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1042 config.src_addr = port->mapbase + ATMEL_US_RHR;
1043
Maxime Ripard5483c102014-10-22 17:43:16 +02001044 ret = dmaengine_slave_config(atmel_port->chan_rx,
1045 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001046 if (ret) {
1047 dev_err(port->dev, "DMA rx slave configuration failed\n");
1048 goto chan_err;
1049 }
1050 /*
1051 * Prepare a cyclic dma transfer, assign 2 descriptors,
1052 * each one is half ring buffer size
1053 */
1054 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
1055 sg_dma_address(&atmel_port->sg_rx),
1056 sg_dma_len(&atmel_port->sg_rx),
1057 sg_dma_len(&atmel_port->sg_rx)/2,
1058 DMA_DEV_TO_MEM,
1059 DMA_PREP_INTERRUPT);
1060 desc->callback = atmel_complete_rx_dma;
1061 desc->callback_param = port;
1062 atmel_port->desc_rx = desc;
1063 atmel_port->cookie_rx = dmaengine_submit(desc);
1064
1065 return 0;
1066
1067chan_err:
1068 dev_err(port->dev, "RX channel not available, switch to pio\n");
1069 atmel_port->use_dma_rx = 0;
1070 if (atmel_port->chan_rx)
1071 atmel_release_rx_dma(port);
1072 return -EINVAL;
1073}
1074
Elen Song2e68c222013-07-22 16:30:30 +08001075static void atmel_uart_timer_callback(unsigned long data)
1076{
1077 struct uart_port *port = (void *)data;
1078 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1079
1080 tasklet_schedule(&atmel_port->tasklet);
1081 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1082}
1083
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001084/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001085 * receive interrupt handler.
1086 */
1087static void
1088atmel_handle_receive(struct uart_port *port, unsigned int pending)
1089{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001090 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001091
Elen Song64e22eb2013-07-22 16:30:24 +08001092 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001093 /*
1094 * PDC receive. Just schedule the tasklet and let it
1095 * figure out the details.
1096 *
1097 * TODO: We're not handling error flags correctly at
1098 * the moment.
1099 */
1100 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1101 UART_PUT_IDR(port, (ATMEL_US_ENDRX
1102 | ATMEL_US_TIMEOUT));
1103 tasklet_schedule(&atmel_port->tasklet);
1104 }
1105
1106 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1107 ATMEL_US_FRAME | ATMEL_US_PARE))
1108 atmel_pdc_rxerr(port, pending);
1109 }
1110
Elen Song34df42f2013-07-22 16:30:27 +08001111 if (atmel_use_dma_rx(port)) {
1112 if (pending & ATMEL_US_TIMEOUT) {
1113 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
1114 tasklet_schedule(&atmel_port->tasklet);
1115 }
1116 }
1117
Remy Bohmerb843aa22008-02-08 04:21:01 -08001118 /* Interrupt receive */
1119 if (pending & ATMEL_US_RXRDY)
1120 atmel_rx_chars(port);
1121 else if (pending & ATMEL_US_RXBRK) {
1122 /*
1123 * End of break detected. If it came along with a
1124 * character, atmel_rx_chars will handle it.
1125 */
1126 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1127 UART_PUT_IDR(port, ATMEL_US_RXBRK);
1128 atmel_port->break_active = 0;
1129 }
1130}
1131
1132/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001133 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001134 */
1135static void
1136atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1137{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001138 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001139
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001140 if (pending & atmel_port->tx_done_mask) {
1141 /* Either PDC or interrupt transmission */
1142 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1143 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001144 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001145}
1146
1147/*
1148 * status flags interrupt handler.
1149 */
1150static void
1151atmel_handle_status(struct uart_port *port, unsigned int pending,
1152 unsigned int status)
1153{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001154 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001155
Remy Bohmerb843aa22008-02-08 04:21:01 -08001156 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001157 | ATMEL_US_CTSIC)) {
1158 atmel_port->irq_status = status;
1159 tasklet_schedule(&atmel_port->tasklet);
1160 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001161}
1162
1163/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001164 * Interrupt handler
1165 */
David Howells7d12e782006-10-05 14:55:46 +01001166static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001167{
1168 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001169 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001170 unsigned int status, pending, pass_counter = 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001171 bool gpio_handled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001172
Chip Coldwella6670612008-02-08 04:21:06 -08001173 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001174 status = atmel_get_lines_status(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001175 pending = status & UART_GET_IMR(port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001176 if (!gpio_handled) {
1177 /*
1178 * Dealing with GPIO interrupt
1179 */
1180 if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1181 pending |= ATMEL_US_CTSIC;
1182
1183 if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1184 pending |= ATMEL_US_DSRIC;
1185
1186 if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1187 pending |= ATMEL_US_RIIC;
1188
1189 if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1190 pending |= ATMEL_US_DCDIC;
1191
1192 gpio_handled = true;
1193 }
Chip Coldwella6670612008-02-08 04:21:06 -08001194 if (!pending)
1195 break;
1196
Remy Bohmerb843aa22008-02-08 04:21:01 -08001197 atmel_handle_receive(port, pending);
1198 atmel_handle_status(port, pending, status);
1199 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001200 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001201
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001202 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001203}
1204
Elen Songa930e522013-07-22 16:30:25 +08001205static void atmel_release_tx_pdc(struct uart_port *port)
1206{
1207 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1208 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1209
1210 dma_unmap_single(port->dev,
1211 pdc->dma_addr,
1212 pdc->dma_size,
1213 DMA_TO_DEVICE);
1214}
1215
Chip Coldwella6670612008-02-08 04:21:06 -08001216/*
1217 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1218 */
Elen Song64e22eb2013-07-22 16:30:24 +08001219static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001220{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001221 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001222 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001223 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1224 int count;
1225
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001226 /* nothing left to transmit? */
1227 if (UART_GET_TCR(port))
1228 return;
1229
Chip Coldwella6670612008-02-08 04:21:06 -08001230 xmit->tail += pdc->ofs;
1231 xmit->tail &= UART_XMIT_SIZE - 1;
1232
1233 port->icount.tx += pdc->ofs;
1234 pdc->ofs = 0;
1235
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001236 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001237
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001238 /* disable PDC transmit */
1239 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1240
Itai Levi1f140812009-01-15 13:50:43 -08001241 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001242 dma_sync_single_for_device(port->dev,
1243 pdc->dma_addr,
1244 pdc->dma_size,
1245 DMA_TO_DEVICE);
1246
1247 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1248 pdc->ofs = count;
1249
1250 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1251 UART_PUT_TCR(port, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001252 /* re-enable PDC transmit */
Chip Coldwella6670612008-02-08 04:21:06 -08001253 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001254 /* Enable interrupts */
1255 UART_PUT_IER(port, atmel_port->tx_done_mask);
1256 } else {
Bernhard Roth83cac9f2011-08-24 09:48:23 +02001257 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1258 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001259 /* DMA done, stop TX, start RX for RS485 */
1260 atmel_start_rx(port);
1261 }
Chip Coldwella6670612008-02-08 04:21:06 -08001262 }
1263
1264 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1265 uart_write_wakeup(port);
1266}
1267
Elen Songa930e522013-07-22 16:30:25 +08001268static int atmel_prepare_tx_pdc(struct uart_port *port)
1269{
1270 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1271 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1272 struct circ_buf *xmit = &port->state->xmit;
1273
1274 pdc->buf = xmit->buf;
1275 pdc->dma_addr = dma_map_single(port->dev,
1276 pdc->buf,
1277 UART_XMIT_SIZE,
1278 DMA_TO_DEVICE);
1279 pdc->dma_size = UART_XMIT_SIZE;
1280 pdc->ofs = 0;
1281
1282 return 0;
1283}
1284
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001285static void atmel_rx_from_ring(struct uart_port *port)
1286{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001287 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001288 struct circ_buf *ring = &atmel_port->rx_ring;
1289 unsigned int flg;
1290 unsigned int status;
1291
1292 while (ring->head != ring->tail) {
1293 struct atmel_uart_char c;
1294
1295 /* Make sure c is loaded after head. */
1296 smp_rmb();
1297
1298 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1299
1300 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1301
1302 port->icount.rx++;
1303 status = c.status;
1304 flg = TTY_NORMAL;
1305
1306 /*
1307 * note that the error handling code is
1308 * out of the main execution path
1309 */
1310 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1311 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1312 if (status & ATMEL_US_RXBRK) {
1313 /* ignore side-effect */
1314 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1315
1316 port->icount.brk++;
1317 if (uart_handle_break(port))
1318 continue;
1319 }
1320 if (status & ATMEL_US_PARE)
1321 port->icount.parity++;
1322 if (status & ATMEL_US_FRAME)
1323 port->icount.frame++;
1324 if (status & ATMEL_US_OVRE)
1325 port->icount.overrun++;
1326
1327 status &= port->read_status_mask;
1328
1329 if (status & ATMEL_US_RXBRK)
1330 flg = TTY_BREAK;
1331 else if (status & ATMEL_US_PARE)
1332 flg = TTY_PARITY;
1333 else if (status & ATMEL_US_FRAME)
1334 flg = TTY_FRAME;
1335 }
1336
1337
1338 if (uart_handle_sysrq_char(port, c.ch))
1339 continue;
1340
1341 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1342 }
1343
1344 /*
1345 * Drop the lock here since it might end up calling
1346 * uart_start(), which takes the lock.
1347 */
1348 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001349 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001350 spin_lock(&port->lock);
1351}
1352
Elen Songa930e522013-07-22 16:30:25 +08001353static void atmel_release_rx_pdc(struct uart_port *port)
1354{
1355 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1356 int i;
1357
1358 for (i = 0; i < 2; i++) {
1359 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1360
1361 dma_unmap_single(port->dev,
1362 pdc->dma_addr,
1363 pdc->dma_size,
1364 DMA_FROM_DEVICE);
1365 kfree(pdc->buf);
1366 }
1367}
1368
Elen Song64e22eb2013-07-22 16:30:24 +08001369static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001370{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001371 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001372 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001373 struct atmel_dma_buffer *pdc;
1374 int rx_idx = atmel_port->pdc_rx_idx;
1375 unsigned int head;
1376 unsigned int tail;
1377 unsigned int count;
1378
1379 do {
1380 /* Reset the UART timeout early so that we don't miss one */
1381 UART_PUT_CR(port, ATMEL_US_STTTO);
1382
1383 pdc = &atmel_port->pdc_rx[rx_idx];
1384 head = UART_GET_RPR(port) - pdc->dma_addr;
1385 tail = pdc->ofs;
1386
1387 /* If the PDC has switched buffers, RPR won't contain
1388 * any address within the current buffer. Since head
1389 * is unsigned, we just need a one-way comparison to
1390 * find out.
1391 *
1392 * In this case, we just need to consume the entire
1393 * buffer and resubmit it for DMA. This will clear the
1394 * ENDRX bit as well, so that we can safely re-enable
1395 * all interrupts below.
1396 */
1397 head = min(head, pdc->dma_size);
1398
1399 if (likely(head != tail)) {
1400 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1401 pdc->dma_size, DMA_FROM_DEVICE);
1402
1403 /*
1404 * head will only wrap around when we recycle
1405 * the DMA buffer, and when that happens, we
1406 * explicitly set tail to 0. So head will
1407 * always be greater than tail.
1408 */
1409 count = head - tail;
1410
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001411 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1412 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001413
1414 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1415 pdc->dma_size, DMA_FROM_DEVICE);
1416
1417 port->icount.rx += count;
1418 pdc->ofs = head;
1419 }
1420
1421 /*
1422 * If the current buffer is full, we need to check if
1423 * the next one contains any additional data.
1424 */
1425 if (head >= pdc->dma_size) {
1426 pdc->ofs = 0;
1427 UART_PUT_RNPR(port, pdc->dma_addr);
1428 UART_PUT_RNCR(port, pdc->dma_size);
1429
1430 rx_idx = !rx_idx;
1431 atmel_port->pdc_rx_idx = rx_idx;
1432 }
1433 } while (head >= pdc->dma_size);
1434
1435 /*
1436 * Drop the lock here since it might end up calling
1437 * uart_start(), which takes the lock.
1438 */
1439 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001440 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001441 spin_lock(&port->lock);
1442
1443 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1444}
1445
Elen Songa930e522013-07-22 16:30:25 +08001446static int atmel_prepare_rx_pdc(struct uart_port *port)
1447{
1448 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1449 int i;
1450
1451 for (i = 0; i < 2; i++) {
1452 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1453
1454 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1455 if (pdc->buf == NULL) {
1456 if (i != 0) {
1457 dma_unmap_single(port->dev,
1458 atmel_port->pdc_rx[0].dma_addr,
1459 PDC_BUFFER_SIZE,
1460 DMA_FROM_DEVICE);
1461 kfree(atmel_port->pdc_rx[0].buf);
1462 }
1463 atmel_port->use_pdc_rx = 0;
1464 return -ENOMEM;
1465 }
1466 pdc->dma_addr = dma_map_single(port->dev,
1467 pdc->buf,
1468 PDC_BUFFER_SIZE,
1469 DMA_FROM_DEVICE);
1470 pdc->dma_size = PDC_BUFFER_SIZE;
1471 pdc->ofs = 0;
1472 }
1473
1474 atmel_port->pdc_rx_idx = 0;
1475
1476 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1477 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1478
1479 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1480 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1481
1482 return 0;
1483}
1484
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001485/*
1486 * tasklet handling tty stuff outside the interrupt handler.
1487 */
1488static void atmel_tasklet_func(unsigned long data)
1489{
1490 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001491 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001492 unsigned int status;
1493 unsigned int status_change;
1494
1495 /* The interrupt handler does not take the lock */
1496 spin_lock(&port->lock);
1497
Elen Songa930e522013-07-22 16:30:25 +08001498 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001499
1500 status = atmel_port->irq_status;
1501 status_change = status ^ atmel_port->irq_status_prev;
1502
1503 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1504 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1505 /* TODO: All reads to CSR will clear these interrupts! */
1506 if (status_change & ATMEL_US_RI)
1507 port->icount.rng++;
1508 if (status_change & ATMEL_US_DSR)
1509 port->icount.dsr++;
1510 if (status_change & ATMEL_US_DCD)
1511 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1512 if (status_change & ATMEL_US_CTS)
1513 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1514
Alan Coxbdc04e32009-09-19 13:13:31 -07001515 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001516
1517 atmel_port->irq_status_prev = status;
1518 }
1519
Elen Songa930e522013-07-22 16:30:25 +08001520 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001521
1522 spin_unlock(&port->lock);
1523}
1524
Elen Song33d64c42013-07-22 16:30:28 +08001525static int atmel_init_property(struct atmel_uart_port *atmel_port,
1526 struct platform_device *pdev)
1527{
1528 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001529 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001530
1531 if (np) {
1532 /* DMA/PDC usage specification */
1533 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1534 if (of_get_property(np, "dmas", NULL)) {
1535 atmel_port->use_dma_rx = true;
1536 atmel_port->use_pdc_rx = false;
1537 } else {
1538 atmel_port->use_dma_rx = false;
1539 atmel_port->use_pdc_rx = true;
1540 }
1541 } else {
1542 atmel_port->use_dma_rx = false;
1543 atmel_port->use_pdc_rx = false;
1544 }
1545
1546 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1547 if (of_get_property(np, "dmas", NULL)) {
1548 atmel_port->use_dma_tx = true;
1549 atmel_port->use_pdc_tx = false;
1550 } else {
1551 atmel_port->use_dma_tx = false;
1552 atmel_port->use_pdc_tx = true;
1553 }
1554 } else {
1555 atmel_port->use_dma_tx = false;
1556 atmel_port->use_pdc_tx = false;
1557 }
1558
1559 } else {
1560 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1561 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1562 atmel_port->use_dma_rx = false;
1563 atmel_port->use_dma_tx = false;
1564 }
1565
1566 return 0;
1567}
1568
1569static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1570 struct platform_device *pdev)
1571{
1572 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001573 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001574
1575 if (np) {
1576 u32 rs485_delay[2];
1577 /* rs485 properties */
1578 if (of_property_read_u32_array(np, "rs485-rts-delay",
1579 rs485_delay, 2) == 0) {
1580 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1581
1582 rs485conf->delay_rts_before_send = rs485_delay[0];
1583 rs485conf->delay_rts_after_send = rs485_delay[1];
1584 rs485conf->flags = 0;
1585
1586 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1587 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1588
1589 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1590 NULL))
1591 rs485conf->flags |= SER_RS485_ENABLED;
1592 }
1593 } else {
1594 atmel_port->rs485 = pdata->rs485;
1595 }
1596
1597}
1598
Elen Songa930e522013-07-22 16:30:25 +08001599static void atmel_set_ops(struct uart_port *port)
1600{
1601 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1602
Elen Song34df42f2013-07-22 16:30:27 +08001603 if (atmel_use_dma_rx(port)) {
1604 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1605 atmel_port->schedule_rx = &atmel_rx_from_dma;
1606 atmel_port->release_rx = &atmel_release_rx_dma;
1607 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001608 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1609 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1610 atmel_port->release_rx = &atmel_release_rx_pdc;
1611 } else {
1612 atmel_port->prepare_rx = NULL;
1613 atmel_port->schedule_rx = &atmel_rx_from_ring;
1614 atmel_port->release_rx = NULL;
1615 }
1616
Elen Song08f738b2013-07-22 16:30:26 +08001617 if (atmel_use_dma_tx(port)) {
1618 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1619 atmel_port->schedule_tx = &atmel_tx_dma;
1620 atmel_port->release_tx = &atmel_release_tx_dma;
1621 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001622 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1623 atmel_port->schedule_tx = &atmel_tx_pdc;
1624 atmel_port->release_tx = &atmel_release_tx_pdc;
1625 } else {
1626 atmel_port->prepare_tx = NULL;
1627 atmel_port->schedule_tx = &atmel_tx_chars;
1628 atmel_port->release_tx = NULL;
1629 }
1630}
1631
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001632/*
Elen Song055560b2013-07-22 16:30:29 +08001633 * Get ip name usart or uart
1634 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001635static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001636{
1637 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1638 int name = UART_GET_IP_NAME(port);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001639 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001640 int usart, uart;
1641 /* usart and uart ascii */
1642 usart = 0x55534152;
1643 uart = 0x44424755;
1644
1645 atmel_port->is_usart = false;
1646
1647 if (name == usart) {
1648 dev_dbg(port->dev, "This is usart\n");
1649 atmel_port->is_usart = true;
1650 } else if (name == uart) {
1651 dev_dbg(port->dev, "This is uart\n");
1652 atmel_port->is_usart = false;
1653 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001654 /* fallback for older SoCs: use version field */
1655 version = UART_GET_IP_VERSION(port);
1656 switch (version) {
1657 case 0x302:
1658 case 0x10213:
1659 dev_dbg(port->dev, "This version is usart\n");
1660 atmel_port->is_usart = true;
1661 break;
1662 case 0x203:
1663 case 0x10202:
1664 dev_dbg(port->dev, "This version is uart\n");
1665 atmel_port->is_usart = false;
1666 break;
1667 default:
1668 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1669 }
Elen Song055560b2013-07-22 16:30:29 +08001670 }
Elen Song055560b2013-07-22 16:30:29 +08001671}
1672
Richard Genoudab5e4e42014-05-13 20:20:45 +02001673static void atmel_free_gpio_irq(struct uart_port *port)
1674{
1675 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1676 enum mctrl_gpio_idx i;
1677
1678 for (i = 0; i < UART_GPIO_MAX; i++)
1679 if (atmel_port->gpio_irq[i] >= 0)
1680 free_irq(atmel_port->gpio_irq[i], port);
1681}
1682
1683static int atmel_request_gpio_irq(struct uart_port *port)
1684{
1685 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1686 int *irq = atmel_port->gpio_irq;
1687 enum mctrl_gpio_idx i;
1688 int err = 0;
1689
1690 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1691 if (irq[i] < 0)
1692 continue;
1693
1694 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1695 err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1696 "atmel_serial", port);
1697 if (err)
1698 dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1699 irq[i]);
1700 }
1701
1702 /*
1703 * If something went wrong, rollback.
1704 */
1705 while (err && (--i >= 0))
1706 if (irq[i] >= 0)
1707 free_irq(irq[i], port);
1708
1709 return err;
1710}
1711
Elen Song055560b2013-07-22 16:30:29 +08001712/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001713 * Perform initialization and enable port for reception
1714 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001715static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001716{
Elen Song33d64c42013-07-22 16:30:28 +08001717 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001718 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001719 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001720 int retval;
1721
1722 /*
1723 * Ensure that no interrupts are enabled otherwise when
1724 * request_irq() is called we could get stuck trying to
1725 * handle an unexpected interrupt
1726 */
1727 UART_PUT_IDR(port, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001728 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001729
1730 /*
1731 * Allocate the IRQ
1732 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001733 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001734 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001735 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001736 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001737 return retval;
1738 }
1739
1740 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001741 * Get the GPIO lines IRQ
1742 */
1743 retval = atmel_request_gpio_irq(port);
1744 if (retval)
1745 goto free_irq;
1746
1747 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001748 * Initialize DMA (if necessary)
1749 */
Elen Song33d64c42013-07-22 16:30:28 +08001750 atmel_init_property(atmel_port, pdev);
1751
Elen Songa930e522013-07-22 16:30:25 +08001752 if (atmel_port->prepare_rx) {
1753 retval = atmel_port->prepare_rx(port);
1754 if (retval < 0)
1755 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001756 }
1757
Elen Songa930e522013-07-22 16:30:25 +08001758 if (atmel_port->prepare_tx) {
1759 retval = atmel_port->prepare_tx(port);
1760 if (retval < 0)
1761 atmel_set_ops(port);
1762 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001763
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001764 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001765 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001766 atmel_port->irq_status = atmel_port->irq_status_prev;
1767
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001768 /*
1769 * Finally, enable the serial port
1770 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001771 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001772 /* enable xmit & rcvr */
1773 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001774
Marek Roszko8bc661b2014-01-10 10:33:11 +01001775 setup_timer(&atmel_port->uart_timer,
1776 atmel_uart_timer_callback,
1777 (unsigned long)port);
1778
Elen Song64e22eb2013-07-22 16:30:24 +08001779 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001780 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001781 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001782 mod_timer(&atmel_port->uart_timer,
1783 jiffies + uart_poll_timeout(port));
1784 /* set USART timeout */
1785 } else {
1786 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1787 UART_PUT_CR(port, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001788
Elen Song2e68c222013-07-22 16:30:30 +08001789 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1790 }
Chip Coldwella6670612008-02-08 04:21:06 -08001791 /* enable PDC controller */
1792 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001793 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001794 /* set UART timeout */
1795 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001796 mod_timer(&atmel_port->uart_timer,
1797 jiffies + uart_poll_timeout(port));
1798 /* set USART timeout */
1799 } else {
1800 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1801 UART_PUT_CR(port, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001802
Elen Song2e68c222013-07-22 16:30:30 +08001803 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1804 }
Chip Coldwella6670612008-02-08 04:21:06 -08001805 } else {
1806 /* enable receive only */
1807 UART_PUT_IER(port, ATMEL_US_RXRDY);
1808 }
Andrew Victorafefc412006-06-19 19:53:19 +01001809
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001810 return 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001811
1812free_irq:
1813 free_irq(port->irq, port);
1814
1815 return retval;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001816}
1817
1818/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001819 * 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
1826 if (atmel_use_pdc_tx(port)) {
1827 UART_PUT_TCR(port, 0);
1828 atmel_port->pdc_tx.ofs = 0;
1829 }
1830}
1831
1832/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001833 * Disable the port
1834 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001835static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001836{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001837 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001838
Chip Coldwella6670612008-02-08 04:21:06 -08001839 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001840 * Prevent any tasklets being scheduled during
1841 * cleanup
1842 */
1843 del_timer_sync(&atmel_port->uart_timer);
1844
1845 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001846 * Clear out any scheduled tasklets before
1847 * we destroy the buffers
1848 */
1849 tasklet_kill(&atmel_port->tasklet);
1850
1851 /*
1852 * Ensure everything is stopped and
1853 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001854 */
1855 atmel_stop_rx(port);
1856 atmel_stop_tx(port);
1857
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001858 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1859 UART_PUT_IDR(port, -1);
1860
1861
Chip Coldwella6670612008-02-08 04:21:06 -08001862 /*
1863 * Shut-down the DMA.
1864 */
Elen Songa930e522013-07-22 16:30:25 +08001865 if (atmel_port->release_rx)
1866 atmel_port->release_rx(port);
1867 if (atmel_port->release_tx)
1868 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001869
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001870 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001871 * Reset ring buffer pointers
1872 */
1873 atmel_port->rx_ring.head = 0;
1874 atmel_port->rx_ring.tail = 0;
1875
1876 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001877 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001878 */
1879 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001880 atmel_free_gpio_irq(port);
1881
1882 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001883
Peter Hurley479e9b92014-10-16 16:54:18 -04001884 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001885}
1886
1887/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001888 * Power / Clock management.
1889 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001890static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1891 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001892{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001893 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001894
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001895 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001896 case 0:
1897 /*
1898 * Enable the peripheral clock for this serial port.
1899 * This is called on uart_open() or a resume event.
1900 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001901 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001902
1903 /* re-enable interrupts if we disabled some on suspend */
1904 UART_PUT_IER(port, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001905 break;
1906 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001907 /* Back up the interrupt mask and disable all interrupts */
1908 atmel_port->backup_imr = UART_GET_IMR(port);
1909 UART_PUT_IDR(port, -1);
1910
Remy Bohmerb843aa22008-02-08 04:21:01 -08001911 /*
1912 * Disable the peripheral clock for this serial port.
1913 * This is called on uart_close() or a suspend event.
1914 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001915 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001916 break;
1917 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001918 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001919 }
1920}
1921
1922/*
1923 * Change the port parameters
1924 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001925static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1926 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001927{
1928 unsigned long flags;
1929 unsigned int mode, imr, quot, baud;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001930 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001931
Andrew Victor03abeac2007-05-03 12:26:24 +01001932 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001933 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
Peter Ma8e706c42009-03-31 10:31:02 -07001934 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1935 | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001936
Remy Bohmerb843aa22008-02-08 04:21:01 -08001937 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001938 quot = uart_get_divisor(port, baud);
1939
Remy Bohmerb843aa22008-02-08 04:21:01 -08001940 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001941 quot /= 8;
1942 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1943 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001944
1945 /* byte size */
1946 switch (termios->c_cflag & CSIZE) {
1947 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001948 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001949 break;
1950 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001951 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001952 break;
1953 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001954 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001955 break;
1956 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001957 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001958 break;
1959 }
1960
1961 /* stop bits */
1962 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001963 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001964
1965 /* parity */
1966 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001967 /* Mark or Space parity */
1968 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001969 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001970 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001971 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001972 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001973 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001974 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001975 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001976 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001977 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001978 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001979
Peter Ma8e706c42009-03-31 10:31:02 -07001980 /* hardware handshake (RTS/CTS) */
1981 if (termios->c_cflag & CRTSCTS)
1982 mode |= ATMEL_US_USMODE_HWHS;
1983 else
1984 mode |= ATMEL_US_USMODE_NORMAL;
1985
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001986 spin_lock_irqsave(&port->lock, flags);
1987
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001988 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001989 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001990 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04001991 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001992 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001993
Elen Song64e22eb2013-07-22 16:30:24 +08001994 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08001995 /* need to enable error interrupts */
1996 UART_PUT_IER(port, port->read_status_mask);
1997
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001998 /*
1999 * Characters to ignore
2000 */
2001 port->ignore_status_mask = 0;
2002 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002003 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002004 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002005 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002006 /*
2007 * If we're ignoring parity and break indicators,
2008 * ignore overruns too (for real raw support).
2009 */
2010 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002011 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002012 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002013 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002014
2015 /* update the per-port timeout */
2016 uart_update_timeout(port, termios->c_cflag, baud);
2017
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002018 /*
2019 * save/disable interrupts. The tty layer will ensure that the
2020 * transmitter is empty if requested by the caller, so there's
2021 * no need to wait for it here.
2022 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002023 imr = UART_GET_IMR(port);
2024 UART_PUT_IDR(port, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002025
2026 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002027 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002028
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002029 /* Resetting serial mode to RS232 (0x0) */
2030 mode &= ~ATMEL_US_USMODE;
2031
2032 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
Claudio Scordino93f33502011-11-09 15:51:49 +01002033 if ((atmel_port->rs485.delay_rts_after_send) > 0)
Claudio Scordino1b633182010-07-20 15:26:47 -07002034 UART_PUT_TTGR(port,
2035 atmel_port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002036 mode |= ATMEL_US_USMODE_RS485;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002037 }
2038
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002039 /* set the parity, stop bits and data size */
2040 UART_PUT_MR(port, mode);
2041
2042 /* set the baud rate */
2043 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002044 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2045 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002046
2047 /* restore interrupts */
2048 UART_PUT_IER(port, imr);
2049
2050 /* CTS flow-control and modem-status interrupts */
2051 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002052 atmel_enable_ms(port);
2053 else
2054 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002055
2056 spin_unlock_irqrestore(&port->lock, flags);
2057}
2058
Peter Hurley732a84a2014-11-05 13:11:43 -05002059static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002060{
Peter Hurley732a84a2014-11-05 13:11:43 -05002061 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002062 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002063 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002064 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002065 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002066 } else {
2067 port->flags &= ~UPF_HARDPPS_CD;
2068 }
2069}
2070
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002071/*
2072 * Return string describing the specified port
2073 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002074static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002075{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002076 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002077}
2078
2079/*
2080 * Release the memory region(s) being used by 'port'.
2081 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002082static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002083{
Andrew Victorafefc412006-06-19 19:53:19 +01002084 struct platform_device *pdev = to_platform_device(port->dev);
2085 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2086
2087 release_mem_region(port->mapbase, size);
2088
2089 if (port->flags & UPF_IOREMAP) {
2090 iounmap(port->membase);
2091 port->membase = NULL;
2092 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002093}
2094
2095/*
2096 * Request the memory region(s) being used by 'port'.
2097 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002098static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002099{
Andrew Victorafefc412006-06-19 19:53:19 +01002100 struct platform_device *pdev = to_platform_device(port->dev);
2101 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002102
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002103 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002104 return -EBUSY;
2105
2106 if (port->flags & UPF_IOREMAP) {
2107 port->membase = ioremap(port->mapbase, size);
2108 if (port->membase == NULL) {
2109 release_mem_region(port->mapbase, size);
2110 return -ENOMEM;
2111 }
2112 }
2113
2114 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002115}
2116
2117/*
2118 * Configure/autoconfigure the port.
2119 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002120static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002121{
2122 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002123 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002124 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002125 }
2126}
2127
2128/*
2129 * Verify the new serial_struct (for TIOCSSERIAL).
2130 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002131static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002132{
2133 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002134 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002135 ret = -EINVAL;
2136 if (port->irq != ser->irq)
2137 ret = -EINVAL;
2138 if (ser->io_type != SERIAL_IO_MEM)
2139 ret = -EINVAL;
2140 if (port->uartclk / 16 != ser->baud_base)
2141 ret = -EINVAL;
2142 if ((void *)port->mapbase != ser->iomem_base)
2143 ret = -EINVAL;
2144 if (port->iobase != ser->port)
2145 ret = -EINVAL;
2146 if (ser->hub6 != 0)
2147 ret = -EINVAL;
2148 return ret;
2149}
2150
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002151#ifdef CONFIG_CONSOLE_POLL
2152static int atmel_poll_get_char(struct uart_port *port)
2153{
2154 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
2155 cpu_relax();
2156
2157 return UART_GET_CHAR(port);
2158}
2159
2160static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2161{
2162 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2163 cpu_relax();
2164
2165 UART_PUT_CHAR(port, ch);
2166}
2167#endif
2168
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002169static int
2170atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
2171{
2172 struct serial_rs485 rs485conf;
2173
2174 switch (cmd) {
2175 case TIOCSRS485:
2176 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
2177 sizeof(rs485conf)))
2178 return -EFAULT;
2179
2180 atmel_config_rs485(port, &rs485conf);
2181 break;
2182
2183 case TIOCGRS485:
2184 if (copy_to_user((struct serial_rs485 *) arg,
2185 &(to_atmel_uart_port(port)->rs485),
2186 sizeof(rs485conf)))
2187 return -EFAULT;
2188 break;
2189
2190 default:
2191 return -ENOIOCTLCMD;
2192 }
2193 return 0;
2194}
2195
2196
2197
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002198static struct uart_ops atmel_pops = {
2199 .tx_empty = atmel_tx_empty,
2200 .set_mctrl = atmel_set_mctrl,
2201 .get_mctrl = atmel_get_mctrl,
2202 .stop_tx = atmel_stop_tx,
2203 .start_tx = atmel_start_tx,
2204 .stop_rx = atmel_stop_rx,
2205 .enable_ms = atmel_enable_ms,
2206 .break_ctl = atmel_break_ctl,
2207 .startup = atmel_startup,
2208 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002209 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002210 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002211 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002212 .type = atmel_type,
2213 .release_port = atmel_release_port,
2214 .request_port = atmel_request_port,
2215 .config_port = atmel_config_port,
2216 .verify_port = atmel_verify_port,
2217 .pm = atmel_serial_pm,
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002218 .ioctl = atmel_ioctl,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002219#ifdef CONFIG_CONSOLE_POLL
2220 .poll_get_char = atmel_poll_get_char,
2221 .poll_put_char = atmel_poll_put_char,
2222#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002223};
2224
Andrew Victorafefc412006-06-19 19:53:19 +01002225/*
2226 * Configure the port from the platform device resource info.
2227 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002228static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002229 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002230{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002231 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002232 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002233 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002234
Elen Song33d64c42013-07-22 16:30:28 +08002235 if (!atmel_init_property(atmel_port, pdev))
2236 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002237
Elen Song33d64c42013-07-22 16:30:28 +08002238 atmel_init_rs485(atmel_port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002239
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002240 port->iotype = UPIO_MEM;
2241 port->flags = UPF_BOOT_AUTOCONF;
2242 port->ops = &atmel_pops;
2243 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002244 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002245 port->mapbase = pdev->resource[0].start;
2246 port->irq = pdev->resource[1].start;
2247
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002248 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2249 (unsigned long)port);
2250
2251 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2252
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002253 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002254 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002255 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002256 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002257 port->flags |= UPF_IOREMAP;
2258 port->membase = NULL;
2259 }
2260
Remy Bohmerb843aa22008-02-08 04:21:01 -08002261 /* for console, the clock could already be configured */
2262 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002263 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002264 if (IS_ERR(atmel_port->clk)) {
2265 ret = PTR_ERR(atmel_port->clk);
2266 atmel_port->clk = NULL;
2267 return ret;
2268 }
2269 ret = clk_prepare_enable(atmel_port->clk);
2270 if (ret) {
2271 clk_put(atmel_port->clk);
2272 atmel_port->clk = NULL;
2273 return ret;
2274 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002275 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002276 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002277 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002278 }
Chip Coldwella6670612008-02-08 04:21:06 -08002279
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002280 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2281 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2282 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002283 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002284 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002285 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2286 } else {
2287 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2288 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002289
2290 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002291}
2292
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002293struct platform_device *atmel_default_console_device; /* the serial console device */
2294
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002295#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002296static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002297{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002298 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002299 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00002300 UART_PUT_CHAR(port, ch);
2301}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002302
2303/*
2304 * Interrupts are disabled on entering
2305 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002306static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002307{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002308 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002309 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002310 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002311 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002312
2313 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002314 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002315 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002316 imr = UART_GET_IMR(port);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002317 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002318
Marc Pignat39d4c922008-04-02 13:04:42 -07002319 /* Store PDC transmit status and disable it */
2320 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2321 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2322
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002323 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002324
2325 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002326 * Finally, wait for transmitter to become empty
2327 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002328 */
2329 do {
2330 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002331 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002332
2333 /* Restore PDC transmit status */
2334 if (pdc_tx)
2335 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2336
Remy Bohmerb843aa22008-02-08 04:21:01 -08002337 /* set interrupts back the way they were */
2338 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002339}
2340
2341/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002342 * If the port was already initialised (eg, by a boot loader),
2343 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002344 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002345static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2346 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002347{
2348 unsigned int mr, quot;
2349
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002350 /*
2351 * If the baud rate generator isn't running, the port wasn't
2352 * initialized by the boot loader.
2353 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07002354 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002355 if (!quot)
2356 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002357
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002358 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2359 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002360 *bits = 8;
2361 else
2362 *bits = 7;
2363
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002364 mr = UART_GET_MR(port) & ATMEL_US_PAR;
2365 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002366 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002367 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002368 *parity = 'o';
2369
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002370 /*
2371 * The serial core only rounds down when matching this to a
2372 * supported baud rate. Make sure we don't end up slightly
2373 * lower than one of those, as it would make us fall through
2374 * to a much lower baud rate than we really want.
2375 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002376 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002377}
2378
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002379static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002380{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002381 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002382 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002383 int baud = 115200;
2384 int bits = 8;
2385 int parity = 'n';
2386 int flow = 'n';
2387
Remy Bohmerb843aa22008-02-08 04:21:01 -08002388 if (port->membase == NULL) {
2389 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002390 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002391 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002392
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002393 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2394 if (ret)
2395 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002396
Remy Bohmerb843aa22008-02-08 04:21:01 -08002397 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002398 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2399 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002400
2401 if (options)
2402 uart_parse_options(options, &baud, &parity, &bits, &flow);
2403 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002404 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002405
2406 return uart_set_options(port, co, baud, parity, bits, flow);
2407}
2408
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002409static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002410
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002411static struct console atmel_console = {
2412 .name = ATMEL_DEVICENAME,
2413 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002414 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002415 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002416 .flags = CON_PRINTBUFFER,
2417 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002418 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002419};
2420
David Brownell06a7f052008-11-06 12:53:40 -08002421#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002422
Andrew Victorafefc412006-06-19 19:53:19 +01002423/*
2424 * Early console initialization (before VM subsystem initialized).
2425 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002426static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002427{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002428 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002429 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002430 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002431 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002432 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002433 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002434
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002435 port->backup_imr = 0;
2436 port->uart.line = id;
2437
2438 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002439 ret = atmel_init_port(port, atmel_default_console_device);
2440 if (ret)
2441 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002442 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002443 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002444
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002445 return 0;
2446}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002447
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002448console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002449
Andrew Victorafefc412006-06-19 19:53:19 +01002450/*
2451 * Late console initialization.
2452 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002453static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002454{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002455 if (atmel_default_console_device
2456 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002457 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002458
2459 return 0;
2460}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002461
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002462core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002463
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002464static inline bool atmel_is_console_port(struct uart_port *port)
2465{
2466 return port->cons && port->cons->index == port->line;
2467}
2468
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002469#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002470#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002471
2472static inline bool atmel_is_console_port(struct uart_port *port)
2473{
2474 return false;
2475}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002476#endif
2477
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002478static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002479 .owner = THIS_MODULE,
2480 .driver_name = "atmel_serial",
2481 .dev_name = ATMEL_DEVICENAME,
2482 .major = SERIAL_ATMEL_MAJOR,
2483 .minor = MINOR_START,
2484 .nr = ATMEL_MAX_UART,
2485 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002486};
2487
Andrew Victorafefc412006-06-19 19:53:19 +01002488#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002489static bool atmel_serial_clk_will_stop(void)
2490{
2491#ifdef CONFIG_ARCH_AT91
2492 return at91_suspend_entering_slow_clock();
2493#else
2494 return false;
2495#endif
2496}
2497
Remy Bohmerb843aa22008-02-08 04:21:01 -08002498static int atmel_serial_suspend(struct platform_device *pdev,
2499 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002500{
Andrew Victorafefc412006-06-19 19:53:19 +01002501 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002502 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002503
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002504 if (atmel_is_console_port(port) && console_suspend_enabled) {
2505 /* Drain the TX shifter */
2506 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2507 cpu_relax();
2508 }
2509
Anti Sullinf05596d2008-09-22 13:57:54 -07002510 /* we can not wake up if we're running on slow clock */
2511 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2512 if (atmel_serial_clk_will_stop())
2513 device_set_wakeup_enable(&pdev->dev, 0);
2514
2515 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002516
2517 return 0;
2518}
2519
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002520static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002521{
2522 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002523 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002524
Anti Sullinf05596d2008-09-22 13:57:54 -07002525 uart_resume_port(&atmel_uart, port);
2526 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002527
2528 return 0;
2529}
2530#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002531#define atmel_serial_suspend NULL
2532#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002533#endif
2534
Richard Genoude0b0baa2014-05-13 20:20:44 +02002535static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2536{
Richard Genoudab5e4e42014-05-13 20:20:45 +02002537 enum mctrl_gpio_idx i;
2538 struct gpio_desc *gpiod;
2539
Richard Genoude0b0baa2014-05-13 20:20:44 +02002540 p->gpios = mctrl_gpio_init(dev, 0);
2541 if (IS_ERR_OR_NULL(p->gpios))
2542 return -1;
2543
Richard Genoudab5e4e42014-05-13 20:20:45 +02002544 for (i = 0; i < UART_GPIO_MAX; i++) {
2545 gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2546 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2547 p->gpio_irq[i] = gpiod_to_irq(gpiod);
2548 else
2549 p->gpio_irq[i] = -EINVAL;
2550 }
2551
Richard Genoude0b0baa2014-05-13 20:20:44 +02002552 return 0;
2553}
2554
Bill Pemberton9671f092012-11-19 13:21:50 -05002555static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002556{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002557 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002558 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002559 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002560 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002561 int ret = -ENODEV;
Andrew Victorafefc412006-06-19 19:53:19 +01002562
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002563 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002564
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002565 if (np)
2566 ret = of_alias_get_id(np, "serial");
2567 else
2568 if (pdata)
2569 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002570
2571 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002572 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002573 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002574 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002575
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002576 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002577 ret = -ENODEV;
2578 goto err;
2579 }
2580
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002581 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002582 /* port already in use */
2583 ret = -EBUSY;
2584 goto err;
2585 }
2586
2587 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002588 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002589 port->uart.line = ret;
Linus Walleij354e57f2013-11-07 10:25:55 +01002590
Richard Genoude0b0baa2014-05-13 20:20:44 +02002591 ret = atmel_init_gpios(port, &pdev->dev);
2592 if (ret < 0)
2593 dev_err(&pdev->dev, "%s",
2594 "Failed to initialize GPIOs. The serial port may not work as expected");
Anti Sullinf05596d2008-09-22 13:57:54 -07002595
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002596 ret = atmel_init_port(port, pdev);
2597 if (ret)
2598 goto err;
Andrew Victorafefc412006-06-19 19:53:19 +01002599
Elen Song64e22eb2013-07-22 16:30:24 +08002600 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002601 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002602 data = kmalloc(sizeof(struct atmel_uart_char)
2603 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002604 if (!data)
2605 goto err_alloc_ring;
2606 port->rx_ring.buf = data;
2607 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002608
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002609 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002610 if (ret)
2611 goto err_add_port;
2612
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002613#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002614 if (atmel_is_console_port(&port->uart)
2615 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2616 /*
2617 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002618 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002619 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002620 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002621 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002622#endif
David Brownell06a7f052008-11-06 12:53:40 -08002623
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002624 device_init_wakeup(&pdev->dev, 1);
2625 platform_set_drvdata(pdev, port);
2626
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002627 if (port->rs485.flags & SER_RS485_ENABLED) {
2628 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2629 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2630 }
2631
Elen Song055560b2013-07-22 16:30:29 +08002632 /*
2633 * Get port name of usart or uart
2634 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002635 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002636
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002637 return 0;
2638
2639err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002640 kfree(port->rx_ring.buf);
2641 port->rx_ring.buf = NULL;
2642err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002643 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002644 clk_put(port->clk);
2645 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002646 }
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002647err:
Andrew Victorafefc412006-06-19 19:53:19 +01002648 return ret;
2649}
2650
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002651static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002652{
2653 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002654 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002655 int ret = 0;
2656
Marek Roszkof50c995f2014-01-07 11:45:07 +01002657 tasklet_kill(&atmel_port->tasklet);
2658
Andrew Victorafefc412006-06-19 19:53:19 +01002659 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002660
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002661 ret = uart_remove_one_port(&atmel_uart, port);
2662
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002663 kfree(atmel_port->rx_ring.buf);
2664
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002665 /* "port" is allocated statically, so we shouldn't free it */
2666
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002667 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002668
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002669 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002670
2671 return ret;
2672}
2673
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002674static struct platform_driver atmel_serial_driver = {
2675 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002676 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002677 .suspend = atmel_serial_suspend,
2678 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002679 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002680 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01002681 .owner = THIS_MODULE,
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002682 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002683 },
2684};
2685
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002686static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002687{
2688 int ret;
2689
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002690 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002691 if (ret)
2692 return ret;
2693
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002694 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002695 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002696 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002697
2698 return ret;
2699}
2700
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002701static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002702{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002703 platform_driver_unregister(&atmel_serial_driver);
2704 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002705}
2706
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002707module_init(atmel_serial_init);
2708module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002709
2710MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002711MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002712MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002713MODULE_ALIAS("platform:atmel_usart");