blob: 50e785a0ea7343dc1e2ea17391eb14e7c437bbae [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/tty.h>
26#include <linux/ioport.h>
27#include <linux/slab.h>
28#include <linux/init.h>
29#include <linux/serial.h>
Andrew Victorafefc412006-06-19 19:53:19 +010030#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000031#include <linux/console.h>
32#include <linux/sysrq.h>
33#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010034#include <linux/platform_device.h>
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +020035#include <linux/of.h>
36#include <linux/of_device.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010037#include <linux/of_gpio.h>
Chip Coldwella6670612008-02-08 04:21:06 -080038#include <linux/dma-mapping.h>
Vinod Koul6b997ba2014-10-16 12:59:06 +053039#include <linux/dmaengine.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010040#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010041#include <linux/atmel_serial.h>
Claudio Scordinoe8faff72010-05-03 13:31:28 +010042#include <linux/uaccess.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080043#include <linux/platform_data/atmel.h>
Elen Song2e68c222013-07-22 16:30:30 +080044#include <linux/timer.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010045#include <linux/gpio.h>
Richard Genoude0b0baa2014-05-13 20:20:44 +020046#include <linux/gpio/consumer.h>
47#include <linux/err.h>
Richard Genoudab5e4e42014-05-13 20:20:45 +020048#include <linux/irq.h>
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +010049#include <linux/suspend.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
Cyrille Pitchenb5199d42015-07-02 15:18:12 +020058/* The minium number of data FIFOs should be able to contain */
59#define ATMEL_MIN_FIFO_SIZE 8
60/*
61 * These two offsets are substracted from the RX FIFO size to define the RTS
62 * high and low thresholds
63 */
64#define ATMEL_RTS_HIGH_OFFSET 16
65#define ATMEL_RTS_LOW_OFFSET 20
66
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020067#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000068#define SUPPORT_SYSRQ
69#endif
70
71#include <linux/serial_core.h>
72
Richard Genoude0b0baa2014-05-13 20:20:44 +020073#include "serial_mctrl_gpio.h"
74
Claudio Scordinoe8faff72010-05-03 13:31:28 +010075static void atmel_start_rx(struct uart_port *port);
76static void atmel_stop_rx(struct uart_port *port);
77
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020078#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000079
80/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
81 * should coexist with the 8250 driver, such as if we have an external 16C550
82 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020083#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000084#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020085#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000086
87#else
88
89/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
90 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020091#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000092#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020093#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000094
95#endif
96
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020097#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000098
Chip Coldwella6670612008-02-08 04:21:06 -080099struct atmel_dma_buffer {
100 unsigned char *buf;
101 dma_addr_t dma_addr;
102 unsigned int dma_size;
103 unsigned int ofs;
104};
105
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800106struct atmel_uart_char {
107 u16 status;
108 u16 ch;
109};
110
111#define ATMEL_SERIAL_RINGSIZE 1024
112
Andrew Victorafefc412006-06-19 19:53:19 +0100113/*
Alexandre Belloni9af92fb2015-09-10 11:29:03 +0200114 * at91: 6 USARTs and one DBGU port (SAM9260)
115 * avr32: 4
116 */
117#define ATMEL_MAX_UART 7
118
119/*
Andrew Victorafefc412006-06-19 19:53:19 +0100120 * We wrap our port structure around the generic uart_port.
121 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200122struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100123 struct uart_port uart; /* uart */
124 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700125 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
126 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700127 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800128
Elen Song34df42f2013-07-22 16:30:27 +0800129 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800130 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800131 short pdc_rx_idx; /* current PDC RX buffer */
132 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
133
Elen Song08f738b2013-07-22 16:30:26 +0800134 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800135 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800136 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
137
Elen Song08f738b2013-07-22 16:30:26 +0800138 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800139 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800140 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800141 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800142 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800143 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800144 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800145 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800146 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800147 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800148 struct tasklet_struct tasklet;
149 unsigned int irq_status;
150 unsigned int irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +0800151 unsigned int status_change;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200152 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800153
154 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100155
Richard Genoude0b0baa2014-05-13 20:20:44 +0200156 struct mctrl_gpios *gpios;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100157 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200158 u32 fifo_size;
159 u32 rts_high;
160 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200161 bool ms_irq_enabled;
Elen Song055560b2013-07-22 16:30:29 +0800162 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800163 struct timer_list uart_timer; /* uart timer */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100164
165 bool suspended;
166 unsigned int pending;
167 unsigned int pending_status;
168 spinlock_t lock_suspended;
169
Elen Songa930e522013-07-22 16:30:25 +0800170 int (*prepare_rx)(struct uart_port *port);
171 int (*prepare_tx)(struct uart_port *port);
172 void (*schedule_rx)(struct uart_port *port);
173 void (*schedule_tx)(struct uart_port *port);
174 void (*release_rx)(struct uart_port *port);
175 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100176};
177
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200178static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100179static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100180
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000181#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200182static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000183#endif
184
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200185#if defined(CONFIG_OF)
186static const struct of_device_id atmel_serial_dt_ids[] = {
187 { .compatible = "atmel,at91rm9200-usart" },
188 { .compatible = "atmel,at91sam9260-usart" },
189 { /* sentinel */ }
190};
191
192MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
193#endif
194
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800195static inline struct atmel_uart_port *
196to_atmel_uart_port(struct uart_port *uart)
197{
198 return container_of(uart, struct atmel_uart_port, uart);
199}
200
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200201static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
202{
203 return __raw_readl(port->membase + reg);
204}
205
206static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
207{
208 __raw_writel(value, port->membase + reg);
209}
210
Cyrille Pitchena6499432015-07-30 16:33:38 +0200211#ifdef CONFIG_AVR32
212
213/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
214static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200215{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200216 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200217}
218
Cyrille Pitchena6499432015-07-30 16:33:38 +0200219static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200220{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200221 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200222}
223
Cyrille Pitchena6499432015-07-30 16:33:38 +0200224#else
225
226static inline u8 atmel_uart_read_char(struct uart_port *port)
227{
228 return __raw_readb(port->membase + ATMEL_US_RHR);
229}
230
231static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
232{
233 __raw_writeb(value, port->membase + ATMEL_US_THR);
234}
235
236#endif
237
Chip Coldwella6670612008-02-08 04:21:06 -0800238#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800239static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800240{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800241 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800242
Elen Song64e22eb2013-07-22 16:30:24 +0800243 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800244}
245
Elen Song64e22eb2013-07-22 16:30:24 +0800246static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800247{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800248 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800249
Elen Song64e22eb2013-07-22 16:30:24 +0800250 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800251}
252#else
Elen Song64e22eb2013-07-22 16:30:24 +0800253static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800254{
255 return false;
256}
257
Elen Song64e22eb2013-07-22 16:30:24 +0800258static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800259{
260 return false;
261}
262#endif
263
Elen Song08f738b2013-07-22 16:30:26 +0800264static bool atmel_use_dma_tx(struct uart_port *port)
265{
266 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
267
268 return atmel_port->use_dma_tx;
269}
270
Elen Song34df42f2013-07-22 16:30:27 +0800271static bool atmel_use_dma_rx(struct uart_port *port)
272{
273 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
274
275 return atmel_port->use_dma_rx;
276}
277
Richard Genoude0b0baa2014-05-13 20:20:44 +0200278static unsigned int atmel_get_lines_status(struct uart_port *port)
279{
280 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
281 unsigned int status, ret = 0;
282
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200283 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200284
285 mctrl_gpio_get(atmel_port->gpios, &ret);
286
287 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
288 UART_GPIO_CTS))) {
289 if (ret & TIOCM_CTS)
290 status &= ~ATMEL_US_CTS;
291 else
292 status |= ATMEL_US_CTS;
293 }
294
295 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
296 UART_GPIO_DSR))) {
297 if (ret & TIOCM_DSR)
298 status &= ~ATMEL_US_DSR;
299 else
300 status |= ATMEL_US_DSR;
301 }
302
303 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
304 UART_GPIO_RI))) {
305 if (ret & TIOCM_RI)
306 status &= ~ATMEL_US_RI;
307 else
308 status |= ATMEL_US_RI;
309 }
310
311 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
312 UART_GPIO_DCD))) {
313 if (ret & TIOCM_CD)
314 status &= ~ATMEL_US_DCD;
315 else
316 status |= ATMEL_US_DCD;
317 }
318
319 return status;
320}
321
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100322/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100323static int atmel_config_rs485(struct uart_port *port,
324 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100325{
326 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
327 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100328
329 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200330 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100331
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200332 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100333
334 /* Resetting serial mode to RS232 (0x0) */
335 mode &= ~ATMEL_US_USMODE;
336
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100337 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100338
339 if (rs485conf->flags & SER_RS485_ENABLED) {
340 dev_dbg(port->dev, "Setting UART to RS485\n");
341 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200342 atmel_uart_writel(port, ATMEL_US_TTGR,
343 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100344 mode |= ATMEL_US_USMODE_RS485;
345 } else {
346 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800347 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100348 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
349 ATMEL_US_TXBUFE;
350 else
351 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
352 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200353 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100354
355 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200356 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100357
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100358 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100359}
360
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000361/*
362 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
363 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200364static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000365{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200366 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
367 TIOCSER_TEMT :
368 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000369}
370
371/*
372 * Set state of the modem control output lines
373 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200374static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000375{
376 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200377 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100378 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100379 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000380
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100381 /* override mode to RS485 if needed, otherwise keep the current mode */
382 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200383 atmel_uart_writel(port, ATMEL_US_TTGR,
384 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100385 mode &= ~ATMEL_US_USMODE;
386 mode |= ATMEL_US_USMODE_RS485;
387 }
388
389 /* set the RTS line state according to the mode */
390 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
391 /* force RTS line to high level */
392 rts_paused = ATMEL_US_RTSEN;
393
394 /* give the control of the RTS line back to the hardware */
395 rts_ready = ATMEL_US_RTSDIS;
396 } else {
397 /* force RTS line to high level */
398 rts_paused = ATMEL_US_RTSDIS;
399
400 /* force RTS line to low level */
401 rts_ready = ATMEL_US_RTSEN;
402 }
403
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000404 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100405 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000406 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100407 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000408
409 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200410 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000411 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200412 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000413
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200414 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100415
Richard Genoude0b0baa2014-05-13 20:20:44 +0200416 mctrl_gpio_set(atmel_port->gpios, mctrl);
417
Andrew Victorafefc412006-06-19 19:53:19 +0100418 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100419 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100420 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200421 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100422 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200423 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100424
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200425 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000426}
427
428/*
429 * Get state of the modem control input lines
430 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200431static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000432{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200433 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
434 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000435
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200436 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437
438 /*
439 * The control signals are active low.
440 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200441 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000442 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200443 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000444 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200445 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000446 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200447 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000448 ret |= TIOCM_RI;
449
Richard Genoude0b0baa2014-05-13 20:20:44 +0200450 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000451}
452
453/*
454 * Stop transmitting.
455 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200456static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100458 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
459
Elen Song64e22eb2013-07-22 16:30:24 +0800460 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800461 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200462 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100463 }
464 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200465 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100466
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100467 if ((port->rs485.flags & SER_RS485_ENABLED) &&
468 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100469 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000470}
471
472/*
473 * Start transmitting.
474 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200475static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000476{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100477 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
478
Elen Song64e22eb2013-07-22 16:30:24 +0800479 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200480 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800481 /* The transmitter is already running. Yes, we
482 really need this.*/
483 return;
484
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100485 if ((port->rs485.flags & SER_RS485_ENABLED) &&
486 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100487 atmel_stop_rx(port);
488
Chip Coldwella6670612008-02-08 04:21:06 -0800489 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200490 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100491 }
492 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200493 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100494}
495
496/*
497 * start receiving - port is in process of being opened.
498 */
499static void atmel_start_rx(struct uart_port *port)
500{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200501 /* reset status and receiver */
502 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100503
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200504 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200505
Elen Song64e22eb2013-07-22 16:30:24 +0800506 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100507 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200508 atmel_uart_writel(port, ATMEL_US_IER,
509 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
510 port->read_status_mask);
511 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100512 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200513 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100514 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000515}
516
517/*
518 * Stop receiving - port is in process of being closed.
519 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200520static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000521{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200522 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200523
Elen Song64e22eb2013-07-22 16:30:24 +0800524 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800525 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200526 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
527 atmel_uart_writel(port, ATMEL_US_IDR,
528 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
529 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100530 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200531 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100532 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000533}
534
535/*
536 * Enable modem status interrupts
537 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200538static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000539{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200540 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
541 uint32_t ier = 0;
542
543 /*
544 * Interrupt should not be enabled twice
545 */
546 if (atmel_port->ms_irq_enabled)
547 return;
548
549 atmel_port->ms_irq_enabled = true;
550
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200551 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200552 ier |= ATMEL_US_CTSIC;
553
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200554 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200555 ier |= ATMEL_US_DSRIC;
556
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200557 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200558 ier |= ATMEL_US_RIIC;
559
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200560 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200561 ier |= ATMEL_US_DCDIC;
562
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200563 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200564
565 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000566}
567
568/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200569 * Disable modem status interrupts
570 */
571static void atmel_disable_ms(struct uart_port *port)
572{
573 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
574 uint32_t idr = 0;
575
576 /*
577 * Interrupt should not be disabled twice
578 */
579 if (!atmel_port->ms_irq_enabled)
580 return;
581
582 atmel_port->ms_irq_enabled = false;
583
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200584 mctrl_gpio_disable_ms(atmel_port->gpios);
585
586 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200587 idr |= ATMEL_US_CTSIC;
588
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200589 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200590 idr |= ATMEL_US_DSRIC;
591
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200592 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200593 idr |= ATMEL_US_RIIC;
594
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200595 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200596 idr |= ATMEL_US_DCDIC;
597
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200598 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200599}
600
601/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000602 * Control the transmission of a break signal
603 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200604static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000605{
606 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200607 /* start break */
608 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000609 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200610 /* stop break */
611 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000612}
613
614/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800615 * Stores the incoming character in the ring buffer
616 */
617static void
618atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
619 unsigned int ch)
620{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800621 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800622 struct circ_buf *ring = &atmel_port->rx_ring;
623 struct atmel_uart_char *c;
624
625 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
626 /* Buffer overflow, ignore char */
627 return;
628
629 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
630 c->status = status;
631 c->ch = ch;
632
633 /* Make sure the character is stored before we update head. */
634 smp_wmb();
635
636 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
637}
638
639/*
Chip Coldwella6670612008-02-08 04:21:06 -0800640 * Deal with parity, framing and overrun errors.
641 */
642static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
643{
644 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200645 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800646
647 if (status & ATMEL_US_RXBRK) {
648 /* ignore side-effect */
649 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
650 port->icount.brk++;
651 }
652 if (status & ATMEL_US_PARE)
653 port->icount.parity++;
654 if (status & ATMEL_US_FRAME)
655 port->icount.frame++;
656 if (status & ATMEL_US_OVRE)
657 port->icount.overrun++;
658}
659
660/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000661 * Characters received (called from interrupt handler)
662 */
David Howells7d12e782006-10-05 14:55:46 +0100663static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000664{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800665 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800666 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000667
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200668 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200669 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200670 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000671
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672 /*
673 * note that the error handling code is
674 * out of the main execution path
675 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700676 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
677 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
678 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800679
Remy Bohmerb843aa22008-02-08 04:21:01 -0800680 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200681 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800682
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700683 if (status & ATMEL_US_RXBRK
684 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700685 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200686 atmel_uart_writel(port, ATMEL_US_IER,
687 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700688 } else {
689 /*
690 * This is either the end-of-break
691 * condition or we've received at
692 * least one character without RXBRK
693 * being set. In both cases, the next
694 * RXBRK will indicate start-of-break.
695 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200696 atmel_uart_writel(port, ATMEL_US_IDR,
697 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700698 status &= ~ATMEL_US_RXBRK;
699 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100700 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000701 }
702
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800703 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200704 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000705 }
706
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800707 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000708}
709
710/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800711 * Transmit characters (called from tasklet with TXRDY interrupt
712 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000713 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200714static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000715{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700716 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100717 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000718
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200719 if (port->x_char &&
720 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200721 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000722 port->icount.tx++;
723 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000724 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800725 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000726 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000727
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200728 while (atmel_uart_readl(port, ATMEL_US_CSR) &
729 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200730 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000731 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
732 port->icount.tx++;
733 if (uart_circ_empty(xmit))
734 break;
735 }
736
737 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
738 uart_write_wakeup(port);
739
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800740 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100741 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200742 atmel_uart_writel(port, ATMEL_US_IER,
743 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000744}
745
Elen Song08f738b2013-07-22 16:30:26 +0800746static void atmel_complete_tx_dma(void *arg)
747{
748 struct atmel_uart_port *atmel_port = arg;
749 struct uart_port *port = &atmel_port->uart;
750 struct circ_buf *xmit = &port->state->xmit;
751 struct dma_chan *chan = atmel_port->chan_tx;
752 unsigned long flags;
753
754 spin_lock_irqsave(&port->lock, flags);
755
756 if (chan)
757 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200758 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800759 xmit->tail &= UART_XMIT_SIZE - 1;
760
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200761 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800762
763 spin_lock_irq(&atmel_port->lock_tx);
764 async_tx_ack(atmel_port->desc_tx);
765 atmel_port->cookie_tx = -EINVAL;
766 atmel_port->desc_tx = NULL;
767 spin_unlock_irq(&atmel_port->lock_tx);
768
769 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
770 uart_write_wakeup(port);
771
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100772 /*
773 * xmit is a circular buffer so, if we have just send data from
774 * xmit->tail to the end of xmit->buf, now we have to transmit the
775 * remaining data from the beginning of xmit->buf to xmit->head.
776 */
Elen Song08f738b2013-07-22 16:30:26 +0800777 if (!uart_circ_empty(xmit))
778 tasklet_schedule(&atmel_port->tasklet);
779
780 spin_unlock_irqrestore(&port->lock, flags);
781}
782
783static void atmel_release_tx_dma(struct uart_port *port)
784{
785 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
786 struct dma_chan *chan = atmel_port->chan_tx;
787
788 if (chan) {
789 dmaengine_terminate_all(chan);
790 dma_release_channel(chan);
791 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200792 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800793 }
794
795 atmel_port->desc_tx = NULL;
796 atmel_port->chan_tx = NULL;
797 atmel_port->cookie_tx = -EINVAL;
798}
799
800/*
801 * Called from tasklet with TXRDY interrupt is disabled.
802 */
803static void atmel_tx_dma(struct uart_port *port)
804{
805 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
806 struct circ_buf *xmit = &port->state->xmit;
807 struct dma_chan *chan = atmel_port->chan_tx;
808 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200809 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
810 unsigned int tx_len, part1_len, part2_len, sg_len;
811 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800812
813 /* Make sure we have an idle channel */
814 if (atmel_port->desc_tx != NULL)
815 return;
816
817 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
818 /*
819 * DMA is idle now.
820 * Port xmit buffer is already mapped,
821 * and it is one page... Just adjust
822 * offsets and lengths. Since it is a circular buffer,
823 * we have to transmit till the end, and then the rest.
824 * Take the port lock to get a
825 * consistent xmit buffer state.
826 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200827 tx_len = CIRC_CNT_TO_END(xmit->head,
828 xmit->tail,
829 UART_XMIT_SIZE);
830
831 if (atmel_port->fifo_size) {
832 /* multi data mode */
833 part1_len = (tx_len & ~0x3); /* DWORD access */
834 part2_len = (tx_len & 0x3); /* BYTE access */
835 } else {
836 /* single data (legacy) mode */
837 part1_len = 0;
838 part2_len = tx_len; /* BYTE access only */
839 }
840
841 sg_init_table(sgl, 2);
842 sg_len = 0;
843 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
844 if (part1_len) {
845 sg = &sgl[sg_len++];
846 sg_dma_address(sg) = phys_addr;
847 sg_dma_len(sg) = part1_len;
848
849 phys_addr += part1_len;
850 }
851
852 if (part2_len) {
853 sg = &sgl[sg_len++];
854 sg_dma_address(sg) = phys_addr;
855 sg_dma_len(sg) = part2_len;
856 }
857
858 /*
859 * save tx_len so atmel_complete_tx_dma() will increase
860 * xmit->tail correctly
861 */
862 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800863
864 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200865 sgl,
866 sg_len,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100867 DMA_MEM_TO_DEV,
868 DMA_PREP_INTERRUPT |
869 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800870 if (!desc) {
871 dev_err(port->dev, "Failed to send via dma!\n");
872 return;
873 }
874
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200875 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800876
877 atmel_port->desc_tx = desc;
878 desc->callback = atmel_complete_tx_dma;
879 desc->callback_param = atmel_port;
880 atmel_port->cookie_tx = dmaengine_submit(desc);
881
882 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100883 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800884 /* DMA done, stop TX, start RX for RS485 */
885 atmel_start_rx(port);
886 }
887 }
888
889 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
890 uart_write_wakeup(port);
891}
892
893static int atmel_prepare_tx_dma(struct uart_port *port)
894{
895 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
896 dma_cap_mask_t mask;
897 struct dma_slave_config config;
898 int ret, nent;
899
900 dma_cap_zero(mask);
901 dma_cap_set(DMA_SLAVE, mask);
902
903 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
904 if (atmel_port->chan_tx == NULL)
905 goto chan_err;
906 dev_info(port->dev, "using %s for tx DMA transfers\n",
907 dma_chan_name(atmel_port->chan_tx));
908
909 spin_lock_init(&atmel_port->lock_tx);
910 sg_init_table(&atmel_port->sg_tx, 1);
911 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800912 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800913 sg_set_page(&atmel_port->sg_tx,
914 virt_to_page(port->state->xmit.buf),
915 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200916 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800917 nent = dma_map_sg(port->dev,
918 &atmel_port->sg_tx,
919 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200920 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800921
922 if (!nent) {
923 dev_dbg(port->dev, "need to release resource of dma\n");
924 goto chan_err;
925 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200926 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800927 sg_dma_len(&atmel_port->sg_tx),
928 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200929 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800930 }
931
932 /* Configure the slave DMA */
933 memset(&config, 0, sizeof(config));
934 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200935 config.dst_addr_width = (atmel_port->fifo_size) ?
936 DMA_SLAVE_BUSWIDTH_4_BYTES :
937 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800938 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200939 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800940
Maxime Ripard5483c102014-10-22 17:43:16 +0200941 ret = dmaengine_slave_config(atmel_port->chan_tx,
942 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800943 if (ret) {
944 dev_err(port->dev, "DMA tx slave configuration failed\n");
945 goto chan_err;
946 }
947
948 return 0;
949
950chan_err:
951 dev_err(port->dev, "TX channel not available, switch to pio\n");
952 atmel_port->use_dma_tx = 0;
953 if (atmel_port->chan_tx)
954 atmel_release_tx_dma(port);
955 return -EINVAL;
956}
957
Elen Song34df42f2013-07-22 16:30:27 +0800958static void atmel_complete_rx_dma(void *arg)
959{
960 struct uart_port *port = arg;
961 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
962
963 tasklet_schedule(&atmel_port->tasklet);
964}
965
966static void atmel_release_rx_dma(struct uart_port *port)
967{
968 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
969 struct dma_chan *chan = atmel_port->chan_rx;
970
971 if (chan) {
972 dmaengine_terminate_all(chan);
973 dma_release_channel(chan);
974 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200975 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800976 }
977
978 atmel_port->desc_rx = NULL;
979 atmel_port->chan_rx = NULL;
980 atmel_port->cookie_rx = -EINVAL;
981}
982
983static void atmel_rx_from_dma(struct uart_port *port)
984{
985 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200986 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800987 struct circ_buf *ring = &atmel_port->rx_ring;
988 struct dma_chan *chan = atmel_port->chan_rx;
989 struct dma_tx_state state;
990 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200991 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +0800992
993
994 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200995 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +0800996 dmastat = dmaengine_tx_status(chan,
997 atmel_port->cookie_rx,
998 &state);
999 /* Restart a new tasklet if DMA status is error */
1000 if (dmastat == DMA_ERROR) {
1001 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001002 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001003 tasklet_schedule(&atmel_port->tasklet);
1004 return;
1005 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001006
1007 /* CPU claims ownership of RX DMA buffer */
1008 dma_sync_sg_for_cpu(port->dev,
1009 &atmel_port->sg_rx,
1010 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001011 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001012
1013 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001014 * ring->head points to the end of data already written by the DMA.
1015 * ring->tail points to the beginning of data to be read by the
1016 * framework.
1017 * The current transfer size should not be larger than the dma buffer
1018 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001019 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001020 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1021 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1022 /*
1023 * At this point ring->head may point to the first byte right after the
1024 * last byte of the dma buffer:
1025 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1026 *
1027 * However ring->tail must always points inside the dma buffer:
1028 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1029 *
1030 * Since we use a ring buffer, we have to handle the case
1031 * where head is lower than tail. In such a case, we first read from
1032 * tail to the end of the buffer then reset tail.
1033 */
1034 if (ring->head < ring->tail) {
1035 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001036
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001037 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1038 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001039 port->icount.rx += count;
1040 }
1041
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001042 /* Finally we read data from tail to head */
1043 if (ring->tail < ring->head) {
1044 count = ring->head - ring->tail;
1045
1046 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1047 /* Wrap ring->head if needed */
1048 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1049 ring->head = 0;
1050 ring->tail = ring->head;
1051 port->icount.rx += count;
1052 }
1053
1054 /* USART retreives ownership of RX DMA buffer */
1055 dma_sync_sg_for_device(port->dev,
1056 &atmel_port->sg_rx,
1057 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001058 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001059
1060 /*
1061 * Drop the lock here since it might end up calling
1062 * uart_start(), which takes the lock.
1063 */
1064 spin_unlock(&port->lock);
1065 tty_flip_buffer_push(tport);
1066 spin_lock(&port->lock);
1067
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001068 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001069}
1070
1071static int atmel_prepare_rx_dma(struct uart_port *port)
1072{
1073 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1074 struct dma_async_tx_descriptor *desc;
1075 dma_cap_mask_t mask;
1076 struct dma_slave_config config;
1077 struct circ_buf *ring;
1078 int ret, nent;
1079
1080 ring = &atmel_port->rx_ring;
1081
1082 dma_cap_zero(mask);
1083 dma_cap_set(DMA_CYCLIC, mask);
1084
1085 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1086 if (atmel_port->chan_rx == NULL)
1087 goto chan_err;
1088 dev_info(port->dev, "using %s for rx DMA transfers\n",
1089 dma_chan_name(atmel_port->chan_rx));
1090
1091 spin_lock_init(&atmel_port->lock_rx);
1092 sg_init_table(&atmel_port->sg_rx, 1);
1093 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001094 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001095 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001096 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001097 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001098 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001099 nent = dma_map_sg(port->dev,
1100 &atmel_port->sg_rx,
1101 1,
1102 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001103
1104 if (!nent) {
1105 dev_dbg(port->dev, "need to release resource of dma\n");
1106 goto chan_err;
1107 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001108 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001109 sg_dma_len(&atmel_port->sg_rx),
1110 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001111 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001112 }
1113
1114 /* Configure the slave DMA */
1115 memset(&config, 0, sizeof(config));
1116 config.direction = DMA_DEV_TO_MEM;
1117 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1118 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001119 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001120
Maxime Ripard5483c102014-10-22 17:43:16 +02001121 ret = dmaengine_slave_config(atmel_port->chan_rx,
1122 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001123 if (ret) {
1124 dev_err(port->dev, "DMA rx slave configuration failed\n");
1125 goto chan_err;
1126 }
1127 /*
1128 * Prepare a cyclic dma transfer, assign 2 descriptors,
1129 * each one is half ring buffer size
1130 */
1131 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001132 sg_dma_address(&atmel_port->sg_rx),
1133 sg_dma_len(&atmel_port->sg_rx),
1134 sg_dma_len(&atmel_port->sg_rx)/2,
1135 DMA_DEV_TO_MEM,
1136 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001137 desc->callback = atmel_complete_rx_dma;
1138 desc->callback_param = port;
1139 atmel_port->desc_rx = desc;
1140 atmel_port->cookie_rx = dmaengine_submit(desc);
1141
1142 return 0;
1143
1144chan_err:
1145 dev_err(port->dev, "RX channel not available, switch to pio\n");
1146 atmel_port->use_dma_rx = 0;
1147 if (atmel_port->chan_rx)
1148 atmel_release_rx_dma(port);
1149 return -EINVAL;
1150}
1151
Elen Song2e68c222013-07-22 16:30:30 +08001152static void atmel_uart_timer_callback(unsigned long data)
1153{
1154 struct uart_port *port = (void *)data;
1155 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1156
1157 tasklet_schedule(&atmel_port->tasklet);
1158 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1159}
1160
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001161/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001162 * receive interrupt handler.
1163 */
1164static void
1165atmel_handle_receive(struct uart_port *port, unsigned int pending)
1166{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001167 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001168
Elen Song64e22eb2013-07-22 16:30:24 +08001169 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001170 /*
1171 * PDC receive. Just schedule the tasklet and let it
1172 * figure out the details.
1173 *
1174 * TODO: We're not handling error flags correctly at
1175 * the moment.
1176 */
1177 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001178 atmel_uart_writel(port, ATMEL_US_IDR,
1179 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Chip Coldwella6670612008-02-08 04:21:06 -08001180 tasklet_schedule(&atmel_port->tasklet);
1181 }
1182
1183 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1184 ATMEL_US_FRAME | ATMEL_US_PARE))
1185 atmel_pdc_rxerr(port, pending);
1186 }
1187
Elen Song34df42f2013-07-22 16:30:27 +08001188 if (atmel_use_dma_rx(port)) {
1189 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001190 atmel_uart_writel(port, ATMEL_US_IDR,
1191 ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001192 tasklet_schedule(&atmel_port->tasklet);
1193 }
1194 }
1195
Remy Bohmerb843aa22008-02-08 04:21:01 -08001196 /* Interrupt receive */
1197 if (pending & ATMEL_US_RXRDY)
1198 atmel_rx_chars(port);
1199 else if (pending & ATMEL_US_RXBRK) {
1200 /*
1201 * End of break detected. If it came along with a
1202 * character, atmel_rx_chars will handle it.
1203 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001204 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1205 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001206 atmel_port->break_active = 0;
1207 }
1208}
1209
1210/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001211 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001212 */
1213static void
1214atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1215{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001216 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001217
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001218 if (pending & atmel_port->tx_done_mask) {
1219 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001220 atmel_uart_writel(port, ATMEL_US_IDR,
1221 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001222 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001223 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001224}
1225
1226/*
1227 * status flags interrupt handler.
1228 */
1229static void
1230atmel_handle_status(struct uart_port *port, unsigned int pending,
1231 unsigned int status)
1232{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001233 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001234
Remy Bohmerb843aa22008-02-08 04:21:01 -08001235 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001236 | ATMEL_US_CTSIC)) {
1237 atmel_port->irq_status = status;
Leilei Zhaod033e822015-04-09 10:48:15 +08001238 atmel_port->status_change = atmel_port->irq_status ^
1239 atmel_port->irq_status_prev;
1240 atmel_port->irq_status_prev = status;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001241 tasklet_schedule(&atmel_port->tasklet);
1242 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001243}
1244
1245/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001246 * Interrupt handler
1247 */
David Howells7d12e782006-10-05 14:55:46 +01001248static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001249{
1250 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001251 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001252 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001253
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001254 spin_lock(&atmel_port->lock_suspended);
1255
Chip Coldwella6670612008-02-08 04:21:06 -08001256 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001257 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001258 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001259 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001260 if (!pending)
1261 break;
1262
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001263 if (atmel_port->suspended) {
1264 atmel_port->pending |= pending;
1265 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001266 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001267 pm_system_wakeup();
1268 break;
1269 }
1270
Remy Bohmerb843aa22008-02-08 04:21:01 -08001271 atmel_handle_receive(port, pending);
1272 atmel_handle_status(port, pending, status);
1273 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001274 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001275
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001276 spin_unlock(&atmel_port->lock_suspended);
1277
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001278 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001279}
1280
Elen Songa930e522013-07-22 16:30:25 +08001281static void atmel_release_tx_pdc(struct uart_port *port)
1282{
1283 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1284 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1285
1286 dma_unmap_single(port->dev,
1287 pdc->dma_addr,
1288 pdc->dma_size,
1289 DMA_TO_DEVICE);
1290}
1291
Chip Coldwella6670612008-02-08 04:21:06 -08001292/*
1293 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1294 */
Elen Song64e22eb2013-07-22 16:30:24 +08001295static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001296{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001297 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001298 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001299 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1300 int count;
1301
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001302 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001303 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001304 return;
1305
Chip Coldwella6670612008-02-08 04:21:06 -08001306 xmit->tail += pdc->ofs;
1307 xmit->tail &= UART_XMIT_SIZE - 1;
1308
1309 port->icount.tx += pdc->ofs;
1310 pdc->ofs = 0;
1311
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001312 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001313
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001314 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001315 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001316
Itai Levi1f140812009-01-15 13:50:43 -08001317 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001318 dma_sync_single_for_device(port->dev,
1319 pdc->dma_addr,
1320 pdc->dma_size,
1321 DMA_TO_DEVICE);
1322
1323 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1324 pdc->ofs = count;
1325
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001326 atmel_uart_writel(port, ATMEL_PDC_TPR,
1327 pdc->dma_addr + xmit->tail);
1328 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001329 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001330 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001331 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001332 atmel_uart_writel(port, ATMEL_US_IER,
1333 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001334 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001335 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1336 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001337 /* DMA done, stop TX, start RX for RS485 */
1338 atmel_start_rx(port);
1339 }
Chip Coldwella6670612008-02-08 04:21:06 -08001340 }
1341
1342 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1343 uart_write_wakeup(port);
1344}
1345
Elen Songa930e522013-07-22 16:30:25 +08001346static int atmel_prepare_tx_pdc(struct uart_port *port)
1347{
1348 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1349 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1350 struct circ_buf *xmit = &port->state->xmit;
1351
1352 pdc->buf = xmit->buf;
1353 pdc->dma_addr = dma_map_single(port->dev,
1354 pdc->buf,
1355 UART_XMIT_SIZE,
1356 DMA_TO_DEVICE);
1357 pdc->dma_size = UART_XMIT_SIZE;
1358 pdc->ofs = 0;
1359
1360 return 0;
1361}
1362
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001363static void atmel_rx_from_ring(struct uart_port *port)
1364{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001365 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001366 struct circ_buf *ring = &atmel_port->rx_ring;
1367 unsigned int flg;
1368 unsigned int status;
1369
1370 while (ring->head != ring->tail) {
1371 struct atmel_uart_char c;
1372
1373 /* Make sure c is loaded after head. */
1374 smp_rmb();
1375
1376 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1377
1378 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1379
1380 port->icount.rx++;
1381 status = c.status;
1382 flg = TTY_NORMAL;
1383
1384 /*
1385 * note that the error handling code is
1386 * out of the main execution path
1387 */
1388 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1389 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1390 if (status & ATMEL_US_RXBRK) {
1391 /* ignore side-effect */
1392 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1393
1394 port->icount.brk++;
1395 if (uart_handle_break(port))
1396 continue;
1397 }
1398 if (status & ATMEL_US_PARE)
1399 port->icount.parity++;
1400 if (status & ATMEL_US_FRAME)
1401 port->icount.frame++;
1402 if (status & ATMEL_US_OVRE)
1403 port->icount.overrun++;
1404
1405 status &= port->read_status_mask;
1406
1407 if (status & ATMEL_US_RXBRK)
1408 flg = TTY_BREAK;
1409 else if (status & ATMEL_US_PARE)
1410 flg = TTY_PARITY;
1411 else if (status & ATMEL_US_FRAME)
1412 flg = TTY_FRAME;
1413 }
1414
1415
1416 if (uart_handle_sysrq_char(port, c.ch))
1417 continue;
1418
1419 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1420 }
1421
1422 /*
1423 * Drop the lock here since it might end up calling
1424 * uart_start(), which takes the lock.
1425 */
1426 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001427 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001428 spin_lock(&port->lock);
1429}
1430
Elen Songa930e522013-07-22 16:30:25 +08001431static void atmel_release_rx_pdc(struct uart_port *port)
1432{
1433 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1434 int i;
1435
1436 for (i = 0; i < 2; i++) {
1437 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1438
1439 dma_unmap_single(port->dev,
1440 pdc->dma_addr,
1441 pdc->dma_size,
1442 DMA_FROM_DEVICE);
1443 kfree(pdc->buf);
1444 }
1445}
1446
Elen Song64e22eb2013-07-22 16:30:24 +08001447static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001448{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001449 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001450 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001451 struct atmel_dma_buffer *pdc;
1452 int rx_idx = atmel_port->pdc_rx_idx;
1453 unsigned int head;
1454 unsigned int tail;
1455 unsigned int count;
1456
1457 do {
1458 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001459 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001460
1461 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001462 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001463 tail = pdc->ofs;
1464
1465 /* If the PDC has switched buffers, RPR won't contain
1466 * any address within the current buffer. Since head
1467 * is unsigned, we just need a one-way comparison to
1468 * find out.
1469 *
1470 * In this case, we just need to consume the entire
1471 * buffer and resubmit it for DMA. This will clear the
1472 * ENDRX bit as well, so that we can safely re-enable
1473 * all interrupts below.
1474 */
1475 head = min(head, pdc->dma_size);
1476
1477 if (likely(head != tail)) {
1478 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1479 pdc->dma_size, DMA_FROM_DEVICE);
1480
1481 /*
1482 * head will only wrap around when we recycle
1483 * the DMA buffer, and when that happens, we
1484 * explicitly set tail to 0. So head will
1485 * always be greater than tail.
1486 */
1487 count = head - tail;
1488
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001489 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1490 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001491
1492 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1493 pdc->dma_size, DMA_FROM_DEVICE);
1494
1495 port->icount.rx += count;
1496 pdc->ofs = head;
1497 }
1498
1499 /*
1500 * If the current buffer is full, we need to check if
1501 * the next one contains any additional data.
1502 */
1503 if (head >= pdc->dma_size) {
1504 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001505 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1506 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001507
1508 rx_idx = !rx_idx;
1509 atmel_port->pdc_rx_idx = rx_idx;
1510 }
1511 } while (head >= pdc->dma_size);
1512
1513 /*
1514 * Drop the lock here since it might end up calling
1515 * uart_start(), which takes the lock.
1516 */
1517 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001518 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001519 spin_lock(&port->lock);
1520
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001521 atmel_uart_writel(port, ATMEL_US_IER,
1522 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001523}
1524
Elen Songa930e522013-07-22 16:30:25 +08001525static int atmel_prepare_rx_pdc(struct uart_port *port)
1526{
1527 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1528 int i;
1529
1530 for (i = 0; i < 2; i++) {
1531 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1532
1533 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1534 if (pdc->buf == NULL) {
1535 if (i != 0) {
1536 dma_unmap_single(port->dev,
1537 atmel_port->pdc_rx[0].dma_addr,
1538 PDC_BUFFER_SIZE,
1539 DMA_FROM_DEVICE);
1540 kfree(atmel_port->pdc_rx[0].buf);
1541 }
1542 atmel_port->use_pdc_rx = 0;
1543 return -ENOMEM;
1544 }
1545 pdc->dma_addr = dma_map_single(port->dev,
1546 pdc->buf,
1547 PDC_BUFFER_SIZE,
1548 DMA_FROM_DEVICE);
1549 pdc->dma_size = PDC_BUFFER_SIZE;
1550 pdc->ofs = 0;
1551 }
1552
1553 atmel_port->pdc_rx_idx = 0;
1554
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001555 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1556 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001557
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001558 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1559 atmel_port->pdc_rx[1].dma_addr);
1560 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001561
1562 return 0;
1563}
1564
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001565/*
1566 * tasklet handling tty stuff outside the interrupt handler.
1567 */
1568static void atmel_tasklet_func(unsigned long data)
1569{
1570 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001571 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Leilei Zhaod033e822015-04-09 10:48:15 +08001572 unsigned int status = atmel_port->irq_status;
1573 unsigned int status_change = atmel_port->status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001574
1575 /* The interrupt handler does not take the lock */
1576 spin_lock(&port->lock);
1577
Elen Songa930e522013-07-22 16:30:25 +08001578 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001579
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001580 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1581 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1582 /* TODO: All reads to CSR will clear these interrupts! */
1583 if (status_change & ATMEL_US_RI)
1584 port->icount.rng++;
1585 if (status_change & ATMEL_US_DSR)
1586 port->icount.dsr++;
1587 if (status_change & ATMEL_US_DCD)
1588 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1589 if (status_change & ATMEL_US_CTS)
1590 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1591
Alan Coxbdc04e32009-09-19 13:13:31 -07001592 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001593
Leilei Zhaod033e822015-04-09 10:48:15 +08001594 atmel_port->status_change = 0;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001595 }
1596
Elen Songa930e522013-07-22 16:30:25 +08001597 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001598
1599 spin_unlock(&port->lock);
1600}
1601
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001602static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001603 struct platform_device *pdev)
1604{
1605 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001606 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001607
1608 if (np) {
1609 /* DMA/PDC usage specification */
1610 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1611 if (of_get_property(np, "dmas", NULL)) {
1612 atmel_port->use_dma_rx = true;
1613 atmel_port->use_pdc_rx = false;
1614 } else {
1615 atmel_port->use_dma_rx = false;
1616 atmel_port->use_pdc_rx = true;
1617 }
1618 } else {
1619 atmel_port->use_dma_rx = false;
1620 atmel_port->use_pdc_rx = false;
1621 }
1622
1623 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1624 if (of_get_property(np, "dmas", NULL)) {
1625 atmel_port->use_dma_tx = true;
1626 atmel_port->use_pdc_tx = false;
1627 } else {
1628 atmel_port->use_dma_tx = false;
1629 atmel_port->use_pdc_tx = true;
1630 }
1631 } else {
1632 atmel_port->use_dma_tx = false;
1633 atmel_port->use_pdc_tx = false;
1634 }
1635
1636 } else {
1637 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1638 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1639 atmel_port->use_dma_rx = false;
1640 atmel_port->use_dma_tx = false;
1641 }
1642
Elen Song33d64c42013-07-22 16:30:28 +08001643}
1644
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001645static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001646 struct platform_device *pdev)
1647{
1648 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001649 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001650
1651 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001652 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001653 u32 rs485_delay[2];
1654 /* rs485 properties */
1655 if (of_property_read_u32_array(np, "rs485-rts-delay",
1656 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001657 rs485conf->delay_rts_before_send = rs485_delay[0];
1658 rs485conf->delay_rts_after_send = rs485_delay[1];
1659 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001660 }
Elen Song33d64c42013-07-22 16:30:28 +08001661
1662 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1663 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1664
1665 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1666 NULL))
1667 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001668 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001669 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001670 }
1671
1672}
1673
Elen Songa930e522013-07-22 16:30:25 +08001674static void atmel_set_ops(struct uart_port *port)
1675{
1676 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1677
Elen Song34df42f2013-07-22 16:30:27 +08001678 if (atmel_use_dma_rx(port)) {
1679 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1680 atmel_port->schedule_rx = &atmel_rx_from_dma;
1681 atmel_port->release_rx = &atmel_release_rx_dma;
1682 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001683 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1684 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1685 atmel_port->release_rx = &atmel_release_rx_pdc;
1686 } else {
1687 atmel_port->prepare_rx = NULL;
1688 atmel_port->schedule_rx = &atmel_rx_from_ring;
1689 atmel_port->release_rx = NULL;
1690 }
1691
Elen Song08f738b2013-07-22 16:30:26 +08001692 if (atmel_use_dma_tx(port)) {
1693 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1694 atmel_port->schedule_tx = &atmel_tx_dma;
1695 atmel_port->release_tx = &atmel_release_tx_dma;
1696 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001697 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1698 atmel_port->schedule_tx = &atmel_tx_pdc;
1699 atmel_port->release_tx = &atmel_release_tx_pdc;
1700 } else {
1701 atmel_port->prepare_tx = NULL;
1702 atmel_port->schedule_tx = &atmel_tx_chars;
1703 atmel_port->release_tx = NULL;
1704 }
1705}
1706
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001707/*
Elen Song055560b2013-07-22 16:30:29 +08001708 * Get ip name usart or uart
1709 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001710static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001711{
1712 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001713 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001714 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001715 int usart, uart;
1716 /* usart and uart ascii */
1717 usart = 0x55534152;
1718 uart = 0x44424755;
1719
1720 atmel_port->is_usart = false;
1721
1722 if (name == usart) {
1723 dev_dbg(port->dev, "This is usart\n");
1724 atmel_port->is_usart = true;
1725 } else if (name == uart) {
1726 dev_dbg(port->dev, "This is uart\n");
1727 atmel_port->is_usart = false;
1728 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001729 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001730 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001731 switch (version) {
1732 case 0x302:
1733 case 0x10213:
1734 dev_dbg(port->dev, "This version is usart\n");
1735 atmel_port->is_usart = true;
1736 break;
1737 case 0x203:
1738 case 0x10202:
1739 dev_dbg(port->dev, "This version is uart\n");
1740 atmel_port->is_usart = false;
1741 break;
1742 default:
1743 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1744 }
Elen Song055560b2013-07-22 16:30:29 +08001745 }
Elen Song055560b2013-07-22 16:30:29 +08001746}
1747
1748/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001749 * Perform initialization and enable port for reception
1750 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001751static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001752{
Elen Song33d64c42013-07-22 16:30:28 +08001753 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001754 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001755 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001756 int retval;
1757
1758 /*
1759 * Ensure that no interrupts are enabled otherwise when
1760 * request_irq() is called we could get stuck trying to
1761 * handle an unexpected interrupt
1762 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001763 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001764 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001765
1766 /*
1767 * Allocate the IRQ
1768 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001769 retval = request_irq(port->irq, atmel_interrupt,
1770 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001771 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001772 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001773 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001774 return retval;
1775 }
1776
Leilei Zhao1e125782015-02-27 16:07:18 +08001777 tasklet_enable(&atmel_port->tasklet);
1778
Richard Genoudab5e4e42014-05-13 20:20:45 +02001779 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001780 * Initialize DMA (if necessary)
1781 */
Elen Song33d64c42013-07-22 16:30:28 +08001782 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001783 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001784
Elen Songa930e522013-07-22 16:30:25 +08001785 if (atmel_port->prepare_rx) {
1786 retval = atmel_port->prepare_rx(port);
1787 if (retval < 0)
1788 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001789 }
1790
Elen Songa930e522013-07-22 16:30:25 +08001791 if (atmel_port->prepare_tx) {
1792 retval = atmel_port->prepare_tx(port);
1793 if (retval < 0)
1794 atmel_set_ops(port);
1795 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001796
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001797 /*
1798 * Enable FIFO when available
1799 */
1800 if (atmel_port->fifo_size) {
1801 unsigned int txrdym = ATMEL_US_ONE_DATA;
1802 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1803 unsigned int fmr;
1804
1805 atmel_uart_writel(port, ATMEL_US_CR,
1806 ATMEL_US_FIFOEN |
1807 ATMEL_US_RXFCLR |
1808 ATMEL_US_TXFLCLR);
1809
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001810 if (atmel_use_dma_tx(port))
1811 txrdym = ATMEL_US_FOUR_DATA;
1812
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001813 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1814 if (atmel_port->rts_high &&
1815 atmel_port->rts_low)
1816 fmr |= ATMEL_US_FRTSC |
1817 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1818 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1819
1820 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1821 }
1822
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001823 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001824 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001825 atmel_port->irq_status = atmel_port->irq_status_prev;
1826
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001827 /*
1828 * Finally, enable the serial port
1829 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001830 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001831 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001832 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001833
Marek Roszko8bc661b2014-01-10 10:33:11 +01001834 setup_timer(&atmel_port->uart_timer,
1835 atmel_uart_timer_callback,
1836 (unsigned long)port);
1837
Elen Song64e22eb2013-07-22 16:30:24 +08001838 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001839 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001840 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001841 mod_timer(&atmel_port->uart_timer,
1842 jiffies + uart_poll_timeout(port));
1843 /* set USART timeout */
1844 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001845 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1846 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001847
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001848 atmel_uart_writel(port, ATMEL_US_IER,
1849 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001850 }
Chip Coldwella6670612008-02-08 04:21:06 -08001851 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001852 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001853 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001854 /* set UART timeout */
1855 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001856 mod_timer(&atmel_port->uart_timer,
1857 jiffies + uart_poll_timeout(port));
1858 /* set USART timeout */
1859 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001860 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1861 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001862
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001863 atmel_uart_writel(port, ATMEL_US_IER,
1864 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001865 }
Chip Coldwella6670612008-02-08 04:21:06 -08001866 } else {
1867 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001868 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001869 }
Andrew Victorafefc412006-06-19 19:53:19 +01001870
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001871 return 0;
1872}
1873
1874/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001875 * Flush any TX data submitted for DMA. Called when the TX circular
1876 * buffer is reset.
1877 */
1878static void atmel_flush_buffer(struct uart_port *port)
1879{
1880 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1881
1882 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001883 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001884 atmel_port->pdc_tx.ofs = 0;
1885 }
1886}
1887
1888/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001889 * Disable the port
1890 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001891static void atmel_shutdown(struct uart_port *port)
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);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001894
Chip Coldwella6670612008-02-08 04:21:06 -08001895 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001896 * Prevent any tasklets being scheduled during
1897 * cleanup
1898 */
1899 del_timer_sync(&atmel_port->uart_timer);
1900
1901 /*
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001902 * Clear out any scheduled tasklets before
1903 * we destroy the buffers
1904 */
Leilei Zhao1e125782015-02-27 16:07:18 +08001905 tasklet_disable(&atmel_port->tasklet);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001906 tasklet_kill(&atmel_port->tasklet);
1907
1908 /*
1909 * Ensure everything is stopped and
1910 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001911 */
1912 atmel_stop_rx(port);
1913 atmel_stop_tx(port);
1914
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001915 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1916 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001917
1918
Chip Coldwella6670612008-02-08 04:21:06 -08001919 /*
1920 * Shut-down the DMA.
1921 */
Elen Songa930e522013-07-22 16:30:25 +08001922 if (atmel_port->release_rx)
1923 atmel_port->release_rx(port);
1924 if (atmel_port->release_tx)
1925 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001926
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001927 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001928 * Reset ring buffer pointers
1929 */
1930 atmel_port->rx_ring.head = 0;
1931 atmel_port->rx_ring.tail = 0;
1932
1933 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001934 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001935 */
1936 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001937
1938 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001939
Peter Hurley479e9b92014-10-16 16:54:18 -04001940 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001941}
1942
1943/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001944 * Power / Clock management.
1945 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001946static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1947 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001948{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001949 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001950
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001952 case 0:
1953 /*
1954 * Enable the peripheral clock for this serial port.
1955 * This is called on uart_open() or a resume event.
1956 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001957 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001958
1959 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001960 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001961 break;
1962 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001963 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001964 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
1965 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07001966
Remy Bohmerb843aa22008-02-08 04:21:01 -08001967 /*
1968 * Disable the peripheral clock for this serial port.
1969 * This is called on uart_close() or a suspend event.
1970 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001971 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001972 break;
1973 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001974 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001975 }
1976}
1977
1978/*
1979 * Change the port parameters
1980 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001981static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1982 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001983{
1984 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001985 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001986
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001987 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001988 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001989
1990 /* reset the mode, clock divisor, parity, stop bits and data size */
1991 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
1992 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001993
Remy Bohmerb843aa22008-02-08 04:21:01 -08001994 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001995 quot = uart_get_divisor(port, baud);
1996
Remy Bohmerb843aa22008-02-08 04:21:01 -08001997 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001998 quot /= 8;
1999 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2000 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002001
2002 /* byte size */
2003 switch (termios->c_cflag & CSIZE) {
2004 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002005 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002006 break;
2007 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002008 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002009 break;
2010 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002011 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002012 break;
2013 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002014 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002015 break;
2016 }
2017
2018 /* stop bits */
2019 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002020 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002021
2022 /* parity */
2023 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002024 /* Mark or Space parity */
2025 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002026 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002027 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002028 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002029 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002030 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002031 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002032 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002033 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002034 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002035 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002036
2037 spin_lock_irqsave(&port->lock, flags);
2038
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002039 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002040 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002041 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002042 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002043 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002044
Elen Song64e22eb2013-07-22 16:30:24 +08002045 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002046 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002047 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002048
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002049 /*
2050 * Characters to ignore
2051 */
2052 port->ignore_status_mask = 0;
2053 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002054 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002055 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002056 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002057 /*
2058 * If we're ignoring parity and break indicators,
2059 * ignore overruns too (for real raw support).
2060 */
2061 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002062 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002063 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002064 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002065
2066 /* update the per-port timeout */
2067 uart_update_timeout(port, termios->c_cflag, baud);
2068
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002069 /*
2070 * save/disable interrupts. The tty layer will ensure that the
2071 * transmitter is empty if requested by the caller, so there's
2072 * no need to wait for it here.
2073 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002074 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2075 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002076
2077 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002078 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002079
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002080 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002081 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002082 atmel_uart_writel(port, ATMEL_US_TTGR,
2083 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002084 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002085 } else if (termios->c_cflag & CRTSCTS) {
2086 /* RS232 with hardware handshake (RTS/CTS) */
2087 mode |= ATMEL_US_USMODE_HWHS;
2088 } else {
2089 /* RS232 without hadware handshake */
2090 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002091 }
2092
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002093 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002094 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002095
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002096 /*
2097 * when switching the mode, set the RTS line state according to the
2098 * new mode, otherwise keep the former state
2099 */
2100 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2101 unsigned int rts_state;
2102
2103 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2104 /* let the hardware control the RTS line */
2105 rts_state = ATMEL_US_RTSDIS;
2106 } else {
2107 /* force RTS line to low level */
2108 rts_state = ATMEL_US_RTSEN;
2109 }
2110
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002111 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002112 }
2113
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002114 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002115 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2116 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2117 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002118
2119 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002120 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002121
2122 /* CTS flow-control and modem-status interrupts */
2123 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002124 atmel_enable_ms(port);
2125 else
2126 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002127
2128 spin_unlock_irqrestore(&port->lock, flags);
2129}
2130
Peter Hurley732a84a2014-11-05 13:11:43 -05002131static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002132{
Peter Hurley732a84a2014-11-05 13:11:43 -05002133 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002134 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002135 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002136 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002137 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002138 } else {
2139 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002140 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2141 spin_lock_irq(&port->lock);
2142 atmel_disable_ms(port);
2143 spin_unlock_irq(&port->lock);
2144 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002145 }
2146}
2147
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002148/*
2149 * Return string describing the specified port
2150 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002151static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002152{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002153 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002154}
2155
2156/*
2157 * Release the memory region(s) being used by 'port'.
2158 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002159static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002160{
Andrew Victorafefc412006-06-19 19:53:19 +01002161 struct platform_device *pdev = to_platform_device(port->dev);
2162 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2163
2164 release_mem_region(port->mapbase, size);
2165
2166 if (port->flags & UPF_IOREMAP) {
2167 iounmap(port->membase);
2168 port->membase = NULL;
2169 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002170}
2171
2172/*
2173 * Request the memory region(s) being used by 'port'.
2174 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002175static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002176{
Andrew Victorafefc412006-06-19 19:53:19 +01002177 struct platform_device *pdev = to_platform_device(port->dev);
2178 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002179
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002180 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002181 return -EBUSY;
2182
2183 if (port->flags & UPF_IOREMAP) {
2184 port->membase = ioremap(port->mapbase, size);
2185 if (port->membase == NULL) {
2186 release_mem_region(port->mapbase, size);
2187 return -ENOMEM;
2188 }
2189 }
2190
2191 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002192}
2193
2194/*
2195 * Configure/autoconfigure the port.
2196 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002197static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002198{
2199 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002200 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002201 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002202 }
2203}
2204
2205/*
2206 * Verify the new serial_struct (for TIOCSSERIAL).
2207 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002208static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002209{
2210 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002211 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002212 ret = -EINVAL;
2213 if (port->irq != ser->irq)
2214 ret = -EINVAL;
2215 if (ser->io_type != SERIAL_IO_MEM)
2216 ret = -EINVAL;
2217 if (port->uartclk / 16 != ser->baud_base)
2218 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002219 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002220 ret = -EINVAL;
2221 if (port->iobase != ser->port)
2222 ret = -EINVAL;
2223 if (ser->hub6 != 0)
2224 ret = -EINVAL;
2225 return ret;
2226}
2227
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002228#ifdef CONFIG_CONSOLE_POLL
2229static int atmel_poll_get_char(struct uart_port *port)
2230{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002231 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002232 cpu_relax();
2233
Cyrille Pitchena6499432015-07-30 16:33:38 +02002234 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002235}
2236
2237static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2238{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002239 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002240 cpu_relax();
2241
Cyrille Pitchena6499432015-07-30 16:33:38 +02002242 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002243}
2244#endif
2245
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002246static struct uart_ops atmel_pops = {
2247 .tx_empty = atmel_tx_empty,
2248 .set_mctrl = atmel_set_mctrl,
2249 .get_mctrl = atmel_get_mctrl,
2250 .stop_tx = atmel_stop_tx,
2251 .start_tx = atmel_start_tx,
2252 .stop_rx = atmel_stop_rx,
2253 .enable_ms = atmel_enable_ms,
2254 .break_ctl = atmel_break_ctl,
2255 .startup = atmel_startup,
2256 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002257 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002258 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002259 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002260 .type = atmel_type,
2261 .release_port = atmel_release_port,
2262 .request_port = atmel_request_port,
2263 .config_port = atmel_config_port,
2264 .verify_port = atmel_verify_port,
2265 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002266#ifdef CONFIG_CONSOLE_POLL
2267 .poll_get_char = atmel_poll_get_char,
2268 .poll_put_char = atmel_poll_put_char,
2269#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002270};
2271
Andrew Victorafefc412006-06-19 19:53:19 +01002272/*
2273 * Configure the port from the platform device resource info.
2274 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002275static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002276 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002277{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002278 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002279 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002280 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002281
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002282 atmel_init_property(atmel_port, pdev);
2283 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002284
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002285 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002286
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002287 port->iotype = UPIO_MEM;
2288 port->flags = UPF_BOOT_AUTOCONF;
2289 port->ops = &atmel_pops;
2290 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002291 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002292 port->mapbase = pdev->resource[0].start;
2293 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002294 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002295
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002296 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2297 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08002298 tasklet_disable(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002299
2300 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2301
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002302 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002303 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002304 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002305 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002306 port->flags |= UPF_IOREMAP;
2307 port->membase = NULL;
2308 }
2309
Remy Bohmerb843aa22008-02-08 04:21:01 -08002310 /* for console, the clock could already be configured */
2311 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002312 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002313 if (IS_ERR(atmel_port->clk)) {
2314 ret = PTR_ERR(atmel_port->clk);
2315 atmel_port->clk = NULL;
2316 return ret;
2317 }
2318 ret = clk_prepare_enable(atmel_port->clk);
2319 if (ret) {
2320 clk_put(atmel_port->clk);
2321 atmel_port->clk = NULL;
2322 return ret;
2323 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002324 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002325 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002326 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002327 }
Chip Coldwella6670612008-02-08 04:21:06 -08002328
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002329 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002330 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002331 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002332 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002333 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002334 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2335 } else {
2336 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2337 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002338
2339 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002340}
2341
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002342struct platform_device *atmel_default_console_device; /* the serial console device */
2343
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002344#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002345static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002346{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002347 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002348 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002349 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002350}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002351
2352/*
2353 * Interrupts are disabled on entering
2354 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002355static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002356{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002357 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002358 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002359 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002360 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002361
2362 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002363 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002364 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002365 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2366 atmel_uart_writel(port, ATMEL_US_IDR,
2367 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002368
Marc Pignat39d4c922008-04-02 13:04:42 -07002369 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002370 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2371 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002372
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002373 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002374
2375 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002376 * Finally, wait for transmitter to become empty
2377 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002378 */
2379 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002380 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002381 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002382
2383 /* Restore PDC transmit status */
2384 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002385 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002386
Remy Bohmerb843aa22008-02-08 04:21:01 -08002387 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002388 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002389}
2390
2391/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002392 * If the port was already initialised (eg, by a boot loader),
2393 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002394 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002395static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2396 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002397{
2398 unsigned int mr, quot;
2399
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002400 /*
2401 * If the baud rate generator isn't running, the port wasn't
2402 * initialized by the boot loader.
2403 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002404 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002405 if (!quot)
2406 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002407
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002408 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002409 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002410 *bits = 8;
2411 else
2412 *bits = 7;
2413
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002414 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002415 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002416 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002417 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002418 *parity = 'o';
2419
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002420 /*
2421 * The serial core only rounds down when matching this to a
2422 * supported baud rate. Make sure we don't end up slightly
2423 * lower than one of those, as it would make us fall through
2424 * to a much lower baud rate than we really want.
2425 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002426 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002427}
2428
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002429static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002430{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002431 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002432 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002433 int baud = 115200;
2434 int bits = 8;
2435 int parity = 'n';
2436 int flow = 'n';
2437
Remy Bohmerb843aa22008-02-08 04:21:01 -08002438 if (port->membase == NULL) {
2439 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002440 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002441 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002442
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002443 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2444 if (ret)
2445 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002446
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002447 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2448 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2449 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002450
2451 if (options)
2452 uart_parse_options(options, &baud, &parity, &bits, &flow);
2453 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002454 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002455
2456 return uart_set_options(port, co, baud, parity, bits, flow);
2457}
2458
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002459static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002460
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002461static struct console atmel_console = {
2462 .name = ATMEL_DEVICENAME,
2463 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002464 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002465 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002466 .flags = CON_PRINTBUFFER,
2467 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002468 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002469};
2470
David Brownell06a7f052008-11-06 12:53:40 -08002471#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002472
Andrew Victorafefc412006-06-19 19:53:19 +01002473/*
2474 * Early console initialization (before VM subsystem initialized).
2475 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002476static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002477{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002478 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002479 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002480 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002481 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002482 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002483 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002484
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002485 port->backup_imr = 0;
2486 port->uart.line = id;
2487
2488 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002489 ret = atmel_init_port(port, atmel_default_console_device);
2490 if (ret)
2491 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002492 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002493 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002494
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002495 return 0;
2496}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002497
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002498console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002499
Andrew Victorafefc412006-06-19 19:53:19 +01002500/*
2501 * Late console initialization.
2502 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002503static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002504{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002505 if (atmel_default_console_device
2506 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002507 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002508
2509 return 0;
2510}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002511
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002512core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002513
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002514static inline bool atmel_is_console_port(struct uart_port *port)
2515{
2516 return port->cons && port->cons->index == port->line;
2517}
2518
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002519#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002520#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002521
2522static inline bool atmel_is_console_port(struct uart_port *port)
2523{
2524 return false;
2525}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002526#endif
2527
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002528static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002529 .owner = THIS_MODULE,
2530 .driver_name = "atmel_serial",
2531 .dev_name = ATMEL_DEVICENAME,
2532 .major = SERIAL_ATMEL_MAJOR,
2533 .minor = MINOR_START,
2534 .nr = ATMEL_MAX_UART,
2535 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002536};
2537
Andrew Victorafefc412006-06-19 19:53:19 +01002538#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002539static bool atmel_serial_clk_will_stop(void)
2540{
2541#ifdef CONFIG_ARCH_AT91
2542 return at91_suspend_entering_slow_clock();
2543#else
2544 return false;
2545#endif
2546}
2547
Remy Bohmerb843aa22008-02-08 04:21:01 -08002548static int atmel_serial_suspend(struct platform_device *pdev,
2549 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002550{
Andrew Victorafefc412006-06-19 19:53:19 +01002551 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002552 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002553
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002554 if (atmel_is_console_port(port) && console_suspend_enabled) {
2555 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002556 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2557 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002558 cpu_relax();
2559 }
2560
Anti Sullinf05596d2008-09-22 13:57:54 -07002561 /* we can not wake up if we're running on slow clock */
2562 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002563 if (atmel_serial_clk_will_stop()) {
2564 unsigned long flags;
2565
2566 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2567 atmel_port->suspended = true;
2568 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002569 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002570 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002571
2572 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002573
2574 return 0;
2575}
2576
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002577static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002578{
2579 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002580 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002581 unsigned long flags;
2582
2583 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2584 if (atmel_port->pending) {
2585 atmel_handle_receive(port, atmel_port->pending);
2586 atmel_handle_status(port, atmel_port->pending,
2587 atmel_port->pending_status);
2588 atmel_handle_transmit(port, atmel_port->pending);
2589 atmel_port->pending = 0;
2590 }
2591 atmel_port->suspended = false;
2592 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002593
Anti Sullinf05596d2008-09-22 13:57:54 -07002594 uart_resume_port(&atmel_uart, port);
2595 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002596
2597 return 0;
2598}
2599#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002600#define atmel_serial_suspend NULL
2601#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002602#endif
2603
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002604static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2605 struct platform_device *pdev)
2606{
2607 port->fifo_size = 0;
2608 port->rts_low = 0;
2609 port->rts_high = 0;
2610
2611 if (of_property_read_u32(pdev->dev.of_node,
2612 "atmel,fifo-size",
2613 &port->fifo_size))
2614 return;
2615
2616 if (!port->fifo_size)
2617 return;
2618
2619 if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2620 port->fifo_size = 0;
2621 dev_err(&pdev->dev, "Invalid FIFO size\n");
2622 return;
2623 }
2624
2625 /*
2626 * 0 <= rts_low <= rts_high <= fifo_size
2627 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2628 * to flush their internal TX FIFO, commonly up to 16 data, before
2629 * actually stopping to send new data. So we try to set the RTS High
2630 * Threshold to a reasonably high value respecting this 16 data
2631 * empirical rule when possible.
2632 */
2633 port->rts_high = max_t(int, port->fifo_size >> 1,
2634 port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2635 port->rts_low = max_t(int, port->fifo_size >> 2,
2636 port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2637
2638 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2639 port->fifo_size);
2640 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2641 port->rts_high);
2642 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
2643 port->rts_low);
2644}
2645
Bill Pemberton9671f092012-11-19 13:21:50 -05002646static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002647{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002648 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002649 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002650 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002651 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002652 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002653 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002654
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002655 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002656
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002657 if (np)
2658 ret = of_alias_get_id(np, "serial");
2659 else
2660 if (pdata)
2661 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002662
2663 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002664 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002665 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002666 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002667
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002668 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002669 ret = -ENODEV;
2670 goto err;
2671 }
2672
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002673 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002674 /* port already in use */
2675 ret = -EBUSY;
2676 goto err;
2677 }
2678
2679 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002680 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002681 port->uart.line = ret;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002682 atmel_serial_probe_fifos(port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002683
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002684 spin_lock_init(&port->lock_suspended);
2685
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002686 ret = atmel_init_port(port, pdev);
2687 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002688 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002689
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002690 port->gpios = mctrl_gpio_init(&port->uart, 0);
2691 if (IS_ERR(port->gpios)) {
2692 ret = PTR_ERR(port->gpios);
2693 goto err_clear_bit;
2694 }
2695
Elen Song64e22eb2013-07-22 16:30:24 +08002696 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002697 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002698 data = kmalloc(sizeof(struct atmel_uart_char)
2699 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002700 if (!data)
2701 goto err_alloc_ring;
2702 port->rx_ring.buf = data;
2703 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002704
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002705 rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2706
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002707 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002708 if (ret)
2709 goto err_add_port;
2710
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002711#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002712 if (atmel_is_console_port(&port->uart)
2713 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2714 /*
2715 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002716 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002717 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002718 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002719 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002720#endif
David Brownell06a7f052008-11-06 12:53:40 -08002721
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002722 device_init_wakeup(&pdev->dev, 1);
2723 platform_set_drvdata(pdev, port);
2724
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002725 /*
2726 * The peripheral clock has been disabled by atmel_init_port():
2727 * enable it before accessing I/O registers
2728 */
2729 clk_prepare_enable(port->clk);
2730
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002731 if (rs485_enabled) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002732 atmel_uart_writel(&port->uart, ATMEL_US_MR,
2733 ATMEL_US_USMODE_NORMAL);
2734 atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002735 }
2736
Elen Song055560b2013-07-22 16:30:29 +08002737 /*
2738 * Get port name of usart or uart
2739 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002740 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002741
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002742 /*
2743 * The peripheral clock can now safely be disabled till the port
2744 * is used
2745 */
2746 clk_disable_unprepare(port->clk);
2747
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002748 return 0;
2749
2750err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002751 kfree(port->rx_ring.buf);
2752 port->rx_ring.buf = NULL;
2753err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002754 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002755 clk_put(port->clk);
2756 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002757 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002758err_clear_bit:
2759 clear_bit(port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002760err:
Andrew Victorafefc412006-06-19 19:53:19 +01002761 return ret;
2762}
2763
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002764static struct platform_driver atmel_serial_driver = {
2765 .probe = atmel_serial_probe,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002766 .suspend = atmel_serial_suspend,
2767 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002768 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002769 .name = "atmel_usart",
2770 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
2771 .suppress_bind_attrs = true,
Andrew Victorafefc412006-06-19 19:53:19 +01002772 },
2773};
2774
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002775static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002776{
2777 int ret;
2778
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002779 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002780 if (ret)
2781 return ret;
2782
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002783 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002784 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002785 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002786
2787 return ret;
2788}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002789device_initcall(atmel_serial_init);