blob: 954941dd812478e4eb9120b40412dd8ba9fadaef [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;
Ludovic Desroches2958cce2016-02-22 15:18:55 +0100162 u32 rtor; /* address of receiver timeout register if it exists */
Nicolas Ferre4b769372016-01-26 11:26:14 +0100163 bool has_hw_timer;
164 struct timer_list uart_timer;
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100165
166 bool suspended;
167 unsigned int pending;
168 unsigned int pending_status;
169 spinlock_t lock_suspended;
170
Elen Songa930e522013-07-22 16:30:25 +0800171 int (*prepare_rx)(struct uart_port *port);
172 int (*prepare_tx)(struct uart_port *port);
173 void (*schedule_rx)(struct uart_port *port);
174 void (*schedule_tx)(struct uart_port *port);
175 void (*release_rx)(struct uart_port *port);
176 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100177};
178
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200179static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100180static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100181
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000182#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200183static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000184#endif
185
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200186#if defined(CONFIG_OF)
187static const struct of_device_id atmel_serial_dt_ids[] = {
188 { .compatible = "atmel,at91rm9200-usart" },
189 { .compatible = "atmel,at91sam9260-usart" },
190 { /* sentinel */ }
191};
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200192#endif
193
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800194static inline struct atmel_uart_port *
195to_atmel_uart_port(struct uart_port *uart)
196{
197 return container_of(uart, struct atmel_uart_port, uart);
198}
199
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200200static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
201{
202 return __raw_readl(port->membase + reg);
203}
204
205static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
206{
207 __raw_writel(value, port->membase + reg);
208}
209
Cyrille Pitchena6499432015-07-30 16:33:38 +0200210#ifdef CONFIG_AVR32
211
212/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
213static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200214{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200215 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200216}
217
Cyrille Pitchena6499432015-07-30 16:33:38 +0200218static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200219{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200220 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200221}
222
Cyrille Pitchena6499432015-07-30 16:33:38 +0200223#else
224
225static inline u8 atmel_uart_read_char(struct uart_port *port)
226{
227 return __raw_readb(port->membase + ATMEL_US_RHR);
228}
229
230static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
231{
232 __raw_writeb(value, port->membase + ATMEL_US_THR);
233}
234
235#endif
236
Chip Coldwella6670612008-02-08 04:21:06 -0800237#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800238static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800239{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800240 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800241
Elen Song64e22eb2013-07-22 16:30:24 +0800242 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800243}
244
Elen Song64e22eb2013-07-22 16:30:24 +0800245static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800246{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800247 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800248
Elen Song64e22eb2013-07-22 16:30:24 +0800249 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800250}
251#else
Elen Song64e22eb2013-07-22 16:30:24 +0800252static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800253{
254 return false;
255}
256
Elen Song64e22eb2013-07-22 16:30:24 +0800257static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800258{
259 return false;
260}
261#endif
262
Elen Song08f738b2013-07-22 16:30:26 +0800263static bool atmel_use_dma_tx(struct uart_port *port)
264{
265 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
266
267 return atmel_port->use_dma_tx;
268}
269
Elen Song34df42f2013-07-22 16:30:27 +0800270static bool atmel_use_dma_rx(struct uart_port *port)
271{
272 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
273
274 return atmel_port->use_dma_rx;
275}
276
Alexandre Belloni5be605a2016-04-12 14:51:40 +0200277static bool atmel_use_fifo(struct uart_port *port)
278{
279 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
280
281 return atmel_port->fifo_size;
282}
283
Richard Genoude0b0baa2014-05-13 20:20:44 +0200284static unsigned int atmel_get_lines_status(struct uart_port *port)
285{
286 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
287 unsigned int status, ret = 0;
288
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200289 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200290
291 mctrl_gpio_get(atmel_port->gpios, &ret);
292
293 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
294 UART_GPIO_CTS))) {
295 if (ret & TIOCM_CTS)
296 status &= ~ATMEL_US_CTS;
297 else
298 status |= ATMEL_US_CTS;
299 }
300
301 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
302 UART_GPIO_DSR))) {
303 if (ret & TIOCM_DSR)
304 status &= ~ATMEL_US_DSR;
305 else
306 status |= ATMEL_US_DSR;
307 }
308
309 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
310 UART_GPIO_RI))) {
311 if (ret & TIOCM_RI)
312 status &= ~ATMEL_US_RI;
313 else
314 status |= ATMEL_US_RI;
315 }
316
317 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
318 UART_GPIO_DCD))) {
319 if (ret & TIOCM_CD)
320 status &= ~ATMEL_US_DCD;
321 else
322 status |= ATMEL_US_DCD;
323 }
324
325 return status;
326}
327
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100328/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100329static int atmel_config_rs485(struct uart_port *port,
330 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100331{
332 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
333 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100334
335 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200336 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100337
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200338 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100339
340 /* Resetting serial mode to RS232 (0x0) */
341 mode &= ~ATMEL_US_USMODE;
342
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100343 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100344
345 if (rs485conf->flags & SER_RS485_ENABLED) {
346 dev_dbg(port->dev, "Setting UART to RS485\n");
347 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200348 atmel_uart_writel(port, ATMEL_US_TTGR,
349 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100350 mode |= ATMEL_US_USMODE_RS485;
351 } else {
352 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800353 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100354 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
355 ATMEL_US_TXBUFE;
356 else
357 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
358 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200359 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100360
361 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200362 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100363
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100364 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100365}
366
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000367/*
368 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
369 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200370static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000371{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200372 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
373 TIOCSER_TEMT :
374 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000375}
376
377/*
378 * Set state of the modem control output lines
379 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200380static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000381{
382 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200383 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100384 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100385 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000386
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100387 /* override mode to RS485 if needed, otherwise keep the current mode */
388 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200389 atmel_uart_writel(port, ATMEL_US_TTGR,
390 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100391 mode &= ~ATMEL_US_USMODE;
392 mode |= ATMEL_US_USMODE_RS485;
393 }
394
395 /* set the RTS line state according to the mode */
396 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
397 /* force RTS line to high level */
398 rts_paused = ATMEL_US_RTSEN;
399
400 /* give the control of the RTS line back to the hardware */
401 rts_ready = ATMEL_US_RTSDIS;
402 } else {
403 /* force RTS line to high level */
404 rts_paused = ATMEL_US_RTSDIS;
405
406 /* force RTS line to low level */
407 rts_ready = ATMEL_US_RTSEN;
408 }
409
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000410 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100411 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000412 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100413 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000414
415 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200416 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000417 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200418 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000419
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200420 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100421
Richard Genoude0b0baa2014-05-13 20:20:44 +0200422 mctrl_gpio_set(atmel_port->gpios, mctrl);
423
Andrew Victorafefc412006-06-19 19:53:19 +0100424 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100425 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100426 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200427 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100428 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200429 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100430
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200431 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000432}
433
434/*
435 * Get state of the modem control input lines
436 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200437static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000438{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200439 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
440 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000441
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200442 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000443
444 /*
445 * The control signals are active low.
446 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200447 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000448 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200449 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000450 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200451 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000452 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200453 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000454 ret |= TIOCM_RI;
455
Richard Genoude0b0baa2014-05-13 20:20:44 +0200456 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457}
458
459/*
460 * Stop transmitting.
461 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200462static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000463{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100464 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
465
Elen Song64e22eb2013-07-22 16:30:24 +0800466 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800467 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200468 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100469 }
470 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200471 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100472
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100473 if ((port->rs485.flags & SER_RS485_ENABLED) &&
474 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100475 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000476}
477
478/*
479 * Start transmitting.
480 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200481static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000482{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100483 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
484
Elen Song64e22eb2013-07-22 16:30:24 +0800485 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200486 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800487 /* The transmitter is already running. Yes, we
488 really need this.*/
489 return;
490
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100491 if ((port->rs485.flags & SER_RS485_ENABLED) &&
492 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100493 atmel_stop_rx(port);
494
Chip Coldwella6670612008-02-08 04:21:06 -0800495 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200496 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100497 }
498 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200499 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100500}
501
502/*
503 * start receiving - port is in process of being opened.
504 */
505static void atmel_start_rx(struct uart_port *port)
506{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200507 /* reset status and receiver */
508 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100509
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200510 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200511
Elen Song64e22eb2013-07-22 16:30:24 +0800512 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100513 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200514 atmel_uart_writel(port, ATMEL_US_IER,
515 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
516 port->read_status_mask);
517 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100518 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200519 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100520 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000521}
522
523/*
524 * Stop receiving - port is in process of being closed.
525 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200526static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000527{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200528 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200529
Elen Song64e22eb2013-07-22 16:30:24 +0800530 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800531 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200532 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
533 atmel_uart_writel(port, ATMEL_US_IDR,
534 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
535 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100536 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200537 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100538 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000539}
540
541/*
542 * Enable modem status interrupts
543 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200544static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000545{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200546 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
547 uint32_t ier = 0;
548
549 /*
550 * Interrupt should not be enabled twice
551 */
552 if (atmel_port->ms_irq_enabled)
553 return;
554
555 atmel_port->ms_irq_enabled = true;
556
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200557 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200558 ier |= ATMEL_US_CTSIC;
559
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200560 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200561 ier |= ATMEL_US_DSRIC;
562
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200563 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200564 ier |= ATMEL_US_RIIC;
565
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200566 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200567 ier |= ATMEL_US_DCDIC;
568
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200569 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200570
571 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000572}
573
574/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200575 * Disable modem status interrupts
576 */
577static void atmel_disable_ms(struct uart_port *port)
578{
579 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
580 uint32_t idr = 0;
581
582 /*
583 * Interrupt should not be disabled twice
584 */
585 if (!atmel_port->ms_irq_enabled)
586 return;
587
588 atmel_port->ms_irq_enabled = false;
589
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200590 mctrl_gpio_disable_ms(atmel_port->gpios);
591
592 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200593 idr |= ATMEL_US_CTSIC;
594
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200595 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200596 idr |= ATMEL_US_DSRIC;
597
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200598 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200599 idr |= ATMEL_US_RIIC;
600
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200601 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200602 idr |= ATMEL_US_DCDIC;
603
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200604 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200605}
606
607/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000608 * Control the transmission of a break signal
609 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200610static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000611{
612 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200613 /* start break */
614 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000615 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200616 /* stop break */
617 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000618}
619
620/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800621 * Stores the incoming character in the ring buffer
622 */
623static void
624atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
625 unsigned int ch)
626{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800627 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800628 struct circ_buf *ring = &atmel_port->rx_ring;
629 struct atmel_uart_char *c;
630
631 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
632 /* Buffer overflow, ignore char */
633 return;
634
635 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
636 c->status = status;
637 c->ch = ch;
638
639 /* Make sure the character is stored before we update head. */
640 smp_wmb();
641
642 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
643}
644
645/*
Chip Coldwella6670612008-02-08 04:21:06 -0800646 * Deal with parity, framing and overrun errors.
647 */
648static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
649{
650 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200651 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800652
653 if (status & ATMEL_US_RXBRK) {
654 /* ignore side-effect */
655 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
656 port->icount.brk++;
657 }
658 if (status & ATMEL_US_PARE)
659 port->icount.parity++;
660 if (status & ATMEL_US_FRAME)
661 port->icount.frame++;
662 if (status & ATMEL_US_OVRE)
663 port->icount.overrun++;
664}
665
666/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000667 * Characters received (called from interrupt handler)
668 */
David Howells7d12e782006-10-05 14:55:46 +0100669static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000670{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800671 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800672 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000673
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200674 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200675 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200676 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000677
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000678 /*
679 * note that the error handling code is
680 * out of the main execution path
681 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700682 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
683 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
684 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800685
Remy Bohmerb843aa22008-02-08 04:21:01 -0800686 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200687 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800688
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700689 if (status & ATMEL_US_RXBRK
690 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700691 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200692 atmel_uart_writel(port, ATMEL_US_IER,
693 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700694 } else {
695 /*
696 * This is either the end-of-break
697 * condition or we've received at
698 * least one character without RXBRK
699 * being set. In both cases, the next
700 * RXBRK will indicate start-of-break.
701 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200702 atmel_uart_writel(port, ATMEL_US_IDR,
703 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700704 status &= ~ATMEL_US_RXBRK;
705 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100706 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000707 }
708
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800709 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200710 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000711 }
712
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800713 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000714}
715
716/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800717 * Transmit characters (called from tasklet with TXRDY interrupt
718 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000719 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200720static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000721{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700722 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100723 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000724
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200725 if (port->x_char &&
726 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200727 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000728 port->icount.tx++;
729 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000730 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800731 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000732 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000733
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200734 while (atmel_uart_readl(port, ATMEL_US_CSR) &
735 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200736 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000737 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
738 port->icount.tx++;
739 if (uart_circ_empty(xmit))
740 break;
741 }
742
743 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
744 uart_write_wakeup(port);
745
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800746 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100747 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200748 atmel_uart_writel(port, ATMEL_US_IER,
749 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000750}
751
Elen Song08f738b2013-07-22 16:30:26 +0800752static void atmel_complete_tx_dma(void *arg)
753{
754 struct atmel_uart_port *atmel_port = arg;
755 struct uart_port *port = &atmel_port->uart;
756 struct circ_buf *xmit = &port->state->xmit;
757 struct dma_chan *chan = atmel_port->chan_tx;
758 unsigned long flags;
759
760 spin_lock_irqsave(&port->lock, flags);
761
762 if (chan)
763 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200764 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800765 xmit->tail &= UART_XMIT_SIZE - 1;
766
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200767 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800768
769 spin_lock_irq(&atmel_port->lock_tx);
770 async_tx_ack(atmel_port->desc_tx);
771 atmel_port->cookie_tx = -EINVAL;
772 atmel_port->desc_tx = NULL;
773 spin_unlock_irq(&atmel_port->lock_tx);
774
775 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
776 uart_write_wakeup(port);
777
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100778 /*
779 * xmit is a circular buffer so, if we have just send data from
780 * xmit->tail to the end of xmit->buf, now we have to transmit the
781 * remaining data from the beginning of xmit->buf to xmit->head.
782 */
Elen Song08f738b2013-07-22 16:30:26 +0800783 if (!uart_circ_empty(xmit))
784 tasklet_schedule(&atmel_port->tasklet);
785
786 spin_unlock_irqrestore(&port->lock, flags);
787}
788
789static void atmel_release_tx_dma(struct uart_port *port)
790{
791 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
792 struct dma_chan *chan = atmel_port->chan_tx;
793
794 if (chan) {
795 dmaengine_terminate_all(chan);
796 dma_release_channel(chan);
797 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200798 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800799 }
800
801 atmel_port->desc_tx = NULL;
802 atmel_port->chan_tx = NULL;
803 atmel_port->cookie_tx = -EINVAL;
804}
805
806/*
807 * Called from tasklet with TXRDY interrupt is disabled.
808 */
809static void atmel_tx_dma(struct uart_port *port)
810{
811 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
812 struct circ_buf *xmit = &port->state->xmit;
813 struct dma_chan *chan = atmel_port->chan_tx;
814 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200815 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
816 unsigned int tx_len, part1_len, part2_len, sg_len;
817 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800818
819 /* Make sure we have an idle channel */
820 if (atmel_port->desc_tx != NULL)
821 return;
822
823 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
824 /*
825 * DMA is idle now.
826 * Port xmit buffer is already mapped,
827 * and it is one page... Just adjust
828 * offsets and lengths. Since it is a circular buffer,
829 * we have to transmit till the end, and then the rest.
830 * Take the port lock to get a
831 * consistent xmit buffer state.
832 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200833 tx_len = CIRC_CNT_TO_END(xmit->head,
834 xmit->tail,
835 UART_XMIT_SIZE);
836
837 if (atmel_port->fifo_size) {
838 /* multi data mode */
839 part1_len = (tx_len & ~0x3); /* DWORD access */
840 part2_len = (tx_len & 0x3); /* BYTE access */
841 } else {
842 /* single data (legacy) mode */
843 part1_len = 0;
844 part2_len = tx_len; /* BYTE access only */
845 }
846
847 sg_init_table(sgl, 2);
848 sg_len = 0;
849 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
850 if (part1_len) {
851 sg = &sgl[sg_len++];
852 sg_dma_address(sg) = phys_addr;
853 sg_dma_len(sg) = part1_len;
854
855 phys_addr += part1_len;
856 }
857
858 if (part2_len) {
859 sg = &sgl[sg_len++];
860 sg_dma_address(sg) = phys_addr;
861 sg_dma_len(sg) = part2_len;
862 }
863
864 /*
865 * save tx_len so atmel_complete_tx_dma() will increase
866 * xmit->tail correctly
867 */
868 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800869
870 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200871 sgl,
872 sg_len,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100873 DMA_MEM_TO_DEV,
874 DMA_PREP_INTERRUPT |
875 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800876 if (!desc) {
877 dev_err(port->dev, "Failed to send via dma!\n");
878 return;
879 }
880
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200881 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800882
883 atmel_port->desc_tx = desc;
884 desc->callback = atmel_complete_tx_dma;
885 desc->callback_param = atmel_port;
886 atmel_port->cookie_tx = dmaengine_submit(desc);
887
888 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100889 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800890 /* DMA done, stop TX, start RX for RS485 */
891 atmel_start_rx(port);
892 }
893 }
894
895 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
896 uart_write_wakeup(port);
897}
898
899static int atmel_prepare_tx_dma(struct uart_port *port)
900{
901 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
902 dma_cap_mask_t mask;
903 struct dma_slave_config config;
904 int ret, nent;
905
906 dma_cap_zero(mask);
907 dma_cap_set(DMA_SLAVE, mask);
908
909 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
910 if (atmel_port->chan_tx == NULL)
911 goto chan_err;
912 dev_info(port->dev, "using %s for tx DMA transfers\n",
913 dma_chan_name(atmel_port->chan_tx));
914
915 spin_lock_init(&atmel_port->lock_tx);
916 sg_init_table(&atmel_port->sg_tx, 1);
917 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800918 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800919 sg_set_page(&atmel_port->sg_tx,
920 virt_to_page(port->state->xmit.buf),
921 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200922 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800923 nent = dma_map_sg(port->dev,
924 &atmel_port->sg_tx,
925 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200926 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800927
928 if (!nent) {
929 dev_dbg(port->dev, "need to release resource of dma\n");
930 goto chan_err;
931 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200932 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800933 sg_dma_len(&atmel_port->sg_tx),
934 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200935 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800936 }
937
938 /* Configure the slave DMA */
939 memset(&config, 0, sizeof(config));
940 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200941 config.dst_addr_width = (atmel_port->fifo_size) ?
942 DMA_SLAVE_BUSWIDTH_4_BYTES :
943 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800944 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200945 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800946
Maxime Ripard5483c102014-10-22 17:43:16 +0200947 ret = dmaengine_slave_config(atmel_port->chan_tx,
948 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800949 if (ret) {
950 dev_err(port->dev, "DMA tx slave configuration failed\n");
951 goto chan_err;
952 }
953
954 return 0;
955
956chan_err:
957 dev_err(port->dev, "TX channel not available, switch to pio\n");
958 atmel_port->use_dma_tx = 0;
959 if (atmel_port->chan_tx)
960 atmel_release_tx_dma(port);
961 return -EINVAL;
962}
963
Elen Song34df42f2013-07-22 16:30:27 +0800964static void atmel_complete_rx_dma(void *arg)
965{
966 struct uart_port *port = arg;
967 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
968
969 tasklet_schedule(&atmel_port->tasklet);
970}
971
972static void atmel_release_rx_dma(struct uart_port *port)
973{
974 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
975 struct dma_chan *chan = atmel_port->chan_rx;
976
977 if (chan) {
978 dmaengine_terminate_all(chan);
979 dma_release_channel(chan);
980 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200981 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800982 }
983
984 atmel_port->desc_rx = NULL;
985 atmel_port->chan_rx = NULL;
986 atmel_port->cookie_rx = -EINVAL;
987}
988
989static void atmel_rx_from_dma(struct uart_port *port)
990{
991 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200992 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800993 struct circ_buf *ring = &atmel_port->rx_ring;
994 struct dma_chan *chan = atmel_port->chan_rx;
995 struct dma_tx_state state;
996 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200997 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +0800998
999
1000 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001001 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001002 dmastat = dmaengine_tx_status(chan,
1003 atmel_port->cookie_rx,
1004 &state);
1005 /* Restart a new tasklet if DMA status is error */
1006 if (dmastat == DMA_ERROR) {
1007 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001008 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001009 tasklet_schedule(&atmel_port->tasklet);
1010 return;
1011 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001012
1013 /* CPU claims ownership of RX DMA buffer */
1014 dma_sync_sg_for_cpu(port->dev,
1015 &atmel_port->sg_rx,
1016 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001017 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001018
1019 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001020 * ring->head points to the end of data already written by the DMA.
1021 * ring->tail points to the beginning of data to be read by the
1022 * framework.
1023 * The current transfer size should not be larger than the dma buffer
1024 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001025 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001026 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1027 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1028 /*
1029 * At this point ring->head may point to the first byte right after the
1030 * last byte of the dma buffer:
1031 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1032 *
1033 * However ring->tail must always points inside the dma buffer:
1034 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1035 *
1036 * Since we use a ring buffer, we have to handle the case
1037 * where head is lower than tail. In such a case, we first read from
1038 * tail to the end of the buffer then reset tail.
1039 */
1040 if (ring->head < ring->tail) {
1041 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001042
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001043 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1044 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001045 port->icount.rx += count;
1046 }
1047
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001048 /* Finally we read data from tail to head */
1049 if (ring->tail < ring->head) {
1050 count = ring->head - ring->tail;
1051
1052 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1053 /* Wrap ring->head if needed */
1054 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1055 ring->head = 0;
1056 ring->tail = ring->head;
1057 port->icount.rx += count;
1058 }
1059
1060 /* USART retreives ownership of RX DMA buffer */
1061 dma_sync_sg_for_device(port->dev,
1062 &atmel_port->sg_rx,
1063 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001064 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001065
1066 /*
1067 * Drop the lock here since it might end up calling
1068 * uart_start(), which takes the lock.
1069 */
1070 spin_unlock(&port->lock);
1071 tty_flip_buffer_push(tport);
1072 spin_lock(&port->lock);
1073
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001074 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001075}
1076
1077static int atmel_prepare_rx_dma(struct uart_port *port)
1078{
1079 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1080 struct dma_async_tx_descriptor *desc;
1081 dma_cap_mask_t mask;
1082 struct dma_slave_config config;
1083 struct circ_buf *ring;
1084 int ret, nent;
1085
1086 ring = &atmel_port->rx_ring;
1087
1088 dma_cap_zero(mask);
1089 dma_cap_set(DMA_CYCLIC, mask);
1090
1091 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1092 if (atmel_port->chan_rx == NULL)
1093 goto chan_err;
1094 dev_info(port->dev, "using %s for rx DMA transfers\n",
1095 dma_chan_name(atmel_port->chan_rx));
1096
1097 spin_lock_init(&atmel_port->lock_rx);
1098 sg_init_table(&atmel_port->sg_rx, 1);
1099 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001100 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001101 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001102 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001103 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001104 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001105 nent = dma_map_sg(port->dev,
1106 &atmel_port->sg_rx,
1107 1,
1108 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001109
1110 if (!nent) {
1111 dev_dbg(port->dev, "need to release resource of dma\n");
1112 goto chan_err;
1113 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001114 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001115 sg_dma_len(&atmel_port->sg_rx),
1116 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001117 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001118 }
1119
1120 /* Configure the slave DMA */
1121 memset(&config, 0, sizeof(config));
1122 config.direction = DMA_DEV_TO_MEM;
1123 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1124 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001125 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001126
Maxime Ripard5483c102014-10-22 17:43:16 +02001127 ret = dmaengine_slave_config(atmel_port->chan_rx,
1128 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001129 if (ret) {
1130 dev_err(port->dev, "DMA rx slave configuration failed\n");
1131 goto chan_err;
1132 }
1133 /*
1134 * Prepare a cyclic dma transfer, assign 2 descriptors,
1135 * each one is half ring buffer size
1136 */
1137 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001138 sg_dma_address(&atmel_port->sg_rx),
1139 sg_dma_len(&atmel_port->sg_rx),
1140 sg_dma_len(&atmel_port->sg_rx)/2,
1141 DMA_DEV_TO_MEM,
1142 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001143 desc->callback = atmel_complete_rx_dma;
1144 desc->callback_param = port;
1145 atmel_port->desc_rx = desc;
1146 atmel_port->cookie_rx = dmaengine_submit(desc);
1147
1148 return 0;
1149
1150chan_err:
1151 dev_err(port->dev, "RX channel not available, switch to pio\n");
1152 atmel_port->use_dma_rx = 0;
1153 if (atmel_port->chan_rx)
1154 atmel_release_rx_dma(port);
1155 return -EINVAL;
1156}
1157
Elen Song2e68c222013-07-22 16:30:30 +08001158static void atmel_uart_timer_callback(unsigned long data)
1159{
1160 struct uart_port *port = (void *)data;
1161 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1162
1163 tasklet_schedule(&atmel_port->tasklet);
1164 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1165}
1166
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001167/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001168 * receive interrupt handler.
1169 */
1170static void
1171atmel_handle_receive(struct uart_port *port, unsigned int pending)
1172{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001173 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001174
Elen Song64e22eb2013-07-22 16:30:24 +08001175 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001176 /*
1177 * PDC receive. Just schedule the tasklet and let it
1178 * figure out the details.
1179 *
1180 * TODO: We're not handling error flags correctly at
1181 * the moment.
1182 */
1183 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001184 atmel_uart_writel(port, ATMEL_US_IDR,
1185 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Chip Coldwella6670612008-02-08 04:21:06 -08001186 tasklet_schedule(&atmel_port->tasklet);
1187 }
1188
1189 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1190 ATMEL_US_FRAME | ATMEL_US_PARE))
1191 atmel_pdc_rxerr(port, pending);
1192 }
1193
Elen Song34df42f2013-07-22 16:30:27 +08001194 if (atmel_use_dma_rx(port)) {
1195 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001196 atmel_uart_writel(port, ATMEL_US_IDR,
1197 ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001198 tasklet_schedule(&atmel_port->tasklet);
1199 }
1200 }
1201
Remy Bohmerb843aa22008-02-08 04:21:01 -08001202 /* Interrupt receive */
1203 if (pending & ATMEL_US_RXRDY)
1204 atmel_rx_chars(port);
1205 else if (pending & ATMEL_US_RXBRK) {
1206 /*
1207 * End of break detected. If it came along with a
1208 * character, atmel_rx_chars will handle it.
1209 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001210 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1211 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001212 atmel_port->break_active = 0;
1213 }
1214}
1215
1216/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001217 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001218 */
1219static void
1220atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1221{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001222 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001223
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001224 if (pending & atmel_port->tx_done_mask) {
1225 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001226 atmel_uart_writel(port, ATMEL_US_IDR,
1227 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001228 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001229 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001230}
1231
1232/*
1233 * status flags interrupt handler.
1234 */
1235static void
1236atmel_handle_status(struct uart_port *port, unsigned int pending,
1237 unsigned int status)
1238{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001239 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001240
Remy Bohmerb843aa22008-02-08 04:21:01 -08001241 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001242 | ATMEL_US_CTSIC)) {
1243 atmel_port->irq_status = status;
Leilei Zhaod033e822015-04-09 10:48:15 +08001244 atmel_port->status_change = atmel_port->irq_status ^
1245 atmel_port->irq_status_prev;
1246 atmel_port->irq_status_prev = status;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001247 tasklet_schedule(&atmel_port->tasklet);
1248 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001249}
1250
1251/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001252 * Interrupt handler
1253 */
David Howells7d12e782006-10-05 14:55:46 +01001254static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001255{
1256 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001257 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001258 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001259
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001260 spin_lock(&atmel_port->lock_suspended);
1261
Chip Coldwella6670612008-02-08 04:21:06 -08001262 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001263 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001264 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001265 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001266 if (!pending)
1267 break;
1268
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001269 if (atmel_port->suspended) {
1270 atmel_port->pending |= pending;
1271 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001272 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001273 pm_system_wakeup();
1274 break;
1275 }
1276
Remy Bohmerb843aa22008-02-08 04:21:01 -08001277 atmel_handle_receive(port, pending);
1278 atmel_handle_status(port, pending, status);
1279 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001280 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001281
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001282 spin_unlock(&atmel_port->lock_suspended);
1283
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001284 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001285}
1286
Elen Songa930e522013-07-22 16:30:25 +08001287static void atmel_release_tx_pdc(struct uart_port *port)
1288{
1289 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1290 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1291
1292 dma_unmap_single(port->dev,
1293 pdc->dma_addr,
1294 pdc->dma_size,
1295 DMA_TO_DEVICE);
1296}
1297
Chip Coldwella6670612008-02-08 04:21:06 -08001298/*
1299 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1300 */
Elen Song64e22eb2013-07-22 16:30:24 +08001301static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001302{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001303 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001304 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001305 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1306 int count;
1307
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001308 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001309 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001310 return;
1311
Chip Coldwella6670612008-02-08 04:21:06 -08001312 xmit->tail += pdc->ofs;
1313 xmit->tail &= UART_XMIT_SIZE - 1;
1314
1315 port->icount.tx += pdc->ofs;
1316 pdc->ofs = 0;
1317
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001318 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001319
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001320 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001321 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001322
Itai Levi1f140812009-01-15 13:50:43 -08001323 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001324 dma_sync_single_for_device(port->dev,
1325 pdc->dma_addr,
1326 pdc->dma_size,
1327 DMA_TO_DEVICE);
1328
1329 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1330 pdc->ofs = count;
1331
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001332 atmel_uart_writel(port, ATMEL_PDC_TPR,
1333 pdc->dma_addr + xmit->tail);
1334 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001335 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001336 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001337 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001338 atmel_uart_writel(port, ATMEL_US_IER,
1339 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001340 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001341 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1342 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001343 /* DMA done, stop TX, start RX for RS485 */
1344 atmel_start_rx(port);
1345 }
Chip Coldwella6670612008-02-08 04:21:06 -08001346 }
1347
1348 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1349 uart_write_wakeup(port);
1350}
1351
Elen Songa930e522013-07-22 16:30:25 +08001352static int atmel_prepare_tx_pdc(struct uart_port *port)
1353{
1354 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1355 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1356 struct circ_buf *xmit = &port->state->xmit;
1357
1358 pdc->buf = xmit->buf;
1359 pdc->dma_addr = dma_map_single(port->dev,
1360 pdc->buf,
1361 UART_XMIT_SIZE,
1362 DMA_TO_DEVICE);
1363 pdc->dma_size = UART_XMIT_SIZE;
1364 pdc->ofs = 0;
1365
1366 return 0;
1367}
1368
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001369static void atmel_rx_from_ring(struct uart_port *port)
1370{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001371 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001372 struct circ_buf *ring = &atmel_port->rx_ring;
1373 unsigned int flg;
1374 unsigned int status;
1375
1376 while (ring->head != ring->tail) {
1377 struct atmel_uart_char c;
1378
1379 /* Make sure c is loaded after head. */
1380 smp_rmb();
1381
1382 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1383
1384 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1385
1386 port->icount.rx++;
1387 status = c.status;
1388 flg = TTY_NORMAL;
1389
1390 /*
1391 * note that the error handling code is
1392 * out of the main execution path
1393 */
1394 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1395 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1396 if (status & ATMEL_US_RXBRK) {
1397 /* ignore side-effect */
1398 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1399
1400 port->icount.brk++;
1401 if (uart_handle_break(port))
1402 continue;
1403 }
1404 if (status & ATMEL_US_PARE)
1405 port->icount.parity++;
1406 if (status & ATMEL_US_FRAME)
1407 port->icount.frame++;
1408 if (status & ATMEL_US_OVRE)
1409 port->icount.overrun++;
1410
1411 status &= port->read_status_mask;
1412
1413 if (status & ATMEL_US_RXBRK)
1414 flg = TTY_BREAK;
1415 else if (status & ATMEL_US_PARE)
1416 flg = TTY_PARITY;
1417 else if (status & ATMEL_US_FRAME)
1418 flg = TTY_FRAME;
1419 }
1420
1421
1422 if (uart_handle_sysrq_char(port, c.ch))
1423 continue;
1424
1425 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1426 }
1427
1428 /*
1429 * Drop the lock here since it might end up calling
1430 * uart_start(), which takes the lock.
1431 */
1432 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001433 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001434 spin_lock(&port->lock);
1435}
1436
Elen Songa930e522013-07-22 16:30:25 +08001437static void atmel_release_rx_pdc(struct uart_port *port)
1438{
1439 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1440 int i;
1441
1442 for (i = 0; i < 2; i++) {
1443 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1444
1445 dma_unmap_single(port->dev,
1446 pdc->dma_addr,
1447 pdc->dma_size,
1448 DMA_FROM_DEVICE);
1449 kfree(pdc->buf);
1450 }
1451}
1452
Elen Song64e22eb2013-07-22 16:30:24 +08001453static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001454{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001455 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001456 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001457 struct atmel_dma_buffer *pdc;
1458 int rx_idx = atmel_port->pdc_rx_idx;
1459 unsigned int head;
1460 unsigned int tail;
1461 unsigned int count;
1462
1463 do {
1464 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001465 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001466
1467 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001468 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001469 tail = pdc->ofs;
1470
1471 /* If the PDC has switched buffers, RPR won't contain
1472 * any address within the current buffer. Since head
1473 * is unsigned, we just need a one-way comparison to
1474 * find out.
1475 *
1476 * In this case, we just need to consume the entire
1477 * buffer and resubmit it for DMA. This will clear the
1478 * ENDRX bit as well, so that we can safely re-enable
1479 * all interrupts below.
1480 */
1481 head = min(head, pdc->dma_size);
1482
1483 if (likely(head != tail)) {
1484 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1485 pdc->dma_size, DMA_FROM_DEVICE);
1486
1487 /*
1488 * head will only wrap around when we recycle
1489 * the DMA buffer, and when that happens, we
1490 * explicitly set tail to 0. So head will
1491 * always be greater than tail.
1492 */
1493 count = head - tail;
1494
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001495 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1496 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001497
1498 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1499 pdc->dma_size, DMA_FROM_DEVICE);
1500
1501 port->icount.rx += count;
1502 pdc->ofs = head;
1503 }
1504
1505 /*
1506 * If the current buffer is full, we need to check if
1507 * the next one contains any additional data.
1508 */
1509 if (head >= pdc->dma_size) {
1510 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001511 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1512 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001513
1514 rx_idx = !rx_idx;
1515 atmel_port->pdc_rx_idx = rx_idx;
1516 }
1517 } while (head >= pdc->dma_size);
1518
1519 /*
1520 * Drop the lock here since it might end up calling
1521 * uart_start(), which takes the lock.
1522 */
1523 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001524 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001525 spin_lock(&port->lock);
1526
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001527 atmel_uart_writel(port, ATMEL_US_IER,
1528 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001529}
1530
Elen Songa930e522013-07-22 16:30:25 +08001531static int atmel_prepare_rx_pdc(struct uart_port *port)
1532{
1533 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1534 int i;
1535
1536 for (i = 0; i < 2; i++) {
1537 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1538
1539 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1540 if (pdc->buf == NULL) {
1541 if (i != 0) {
1542 dma_unmap_single(port->dev,
1543 atmel_port->pdc_rx[0].dma_addr,
1544 PDC_BUFFER_SIZE,
1545 DMA_FROM_DEVICE);
1546 kfree(atmel_port->pdc_rx[0].buf);
1547 }
1548 atmel_port->use_pdc_rx = 0;
1549 return -ENOMEM;
1550 }
1551 pdc->dma_addr = dma_map_single(port->dev,
1552 pdc->buf,
1553 PDC_BUFFER_SIZE,
1554 DMA_FROM_DEVICE);
1555 pdc->dma_size = PDC_BUFFER_SIZE;
1556 pdc->ofs = 0;
1557 }
1558
1559 atmel_port->pdc_rx_idx = 0;
1560
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001561 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1562 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001563
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001564 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1565 atmel_port->pdc_rx[1].dma_addr);
1566 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001567
1568 return 0;
1569}
1570
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001571/*
1572 * tasklet handling tty stuff outside the interrupt handler.
1573 */
1574static void atmel_tasklet_func(unsigned long data)
1575{
1576 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001577 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Leilei Zhaod033e822015-04-09 10:48:15 +08001578 unsigned int status = atmel_port->irq_status;
1579 unsigned int status_change = atmel_port->status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001580
1581 /* The interrupt handler does not take the lock */
1582 spin_lock(&port->lock);
1583
Elen Songa930e522013-07-22 16:30:25 +08001584 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001585
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001586 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1587 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1588 /* TODO: All reads to CSR will clear these interrupts! */
1589 if (status_change & ATMEL_US_RI)
1590 port->icount.rng++;
1591 if (status_change & ATMEL_US_DSR)
1592 port->icount.dsr++;
1593 if (status_change & ATMEL_US_DCD)
1594 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1595 if (status_change & ATMEL_US_CTS)
1596 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1597
Alan Coxbdc04e32009-09-19 13:13:31 -07001598 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001599
Leilei Zhaod033e822015-04-09 10:48:15 +08001600 atmel_port->status_change = 0;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001601 }
1602
Elen Songa930e522013-07-22 16:30:25 +08001603 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001604
1605 spin_unlock(&port->lock);
1606}
1607
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001608static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001609 struct platform_device *pdev)
1610{
1611 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001612 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001613
1614 if (np) {
1615 /* DMA/PDC usage specification */
1616 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1617 if (of_get_property(np, "dmas", NULL)) {
1618 atmel_port->use_dma_rx = true;
1619 atmel_port->use_pdc_rx = false;
1620 } else {
1621 atmel_port->use_dma_rx = false;
1622 atmel_port->use_pdc_rx = true;
1623 }
1624 } else {
1625 atmel_port->use_dma_rx = false;
1626 atmel_port->use_pdc_rx = false;
1627 }
1628
1629 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1630 if (of_get_property(np, "dmas", NULL)) {
1631 atmel_port->use_dma_tx = true;
1632 atmel_port->use_pdc_tx = false;
1633 } else {
1634 atmel_port->use_dma_tx = false;
1635 atmel_port->use_pdc_tx = true;
1636 }
1637 } else {
1638 atmel_port->use_dma_tx = false;
1639 atmel_port->use_pdc_tx = false;
1640 }
1641
1642 } else {
1643 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1644 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1645 atmel_port->use_dma_rx = false;
1646 atmel_port->use_dma_tx = false;
1647 }
1648
Elen Song33d64c42013-07-22 16:30:28 +08001649}
1650
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001651static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001652 struct platform_device *pdev)
1653{
1654 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001655 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001656
1657 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001658 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001659 u32 rs485_delay[2];
1660 /* rs485 properties */
1661 if (of_property_read_u32_array(np, "rs485-rts-delay",
1662 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001663 rs485conf->delay_rts_before_send = rs485_delay[0];
1664 rs485conf->delay_rts_after_send = rs485_delay[1];
1665 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001666 }
Elen Song33d64c42013-07-22 16:30:28 +08001667
1668 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1669 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1670
1671 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1672 NULL))
1673 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001674 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001675 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001676 }
1677
1678}
1679
Elen Songa930e522013-07-22 16:30:25 +08001680static void atmel_set_ops(struct uart_port *port)
1681{
1682 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1683
Elen Song34df42f2013-07-22 16:30:27 +08001684 if (atmel_use_dma_rx(port)) {
1685 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1686 atmel_port->schedule_rx = &atmel_rx_from_dma;
1687 atmel_port->release_rx = &atmel_release_rx_dma;
1688 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001689 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1690 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1691 atmel_port->release_rx = &atmel_release_rx_pdc;
1692 } else {
1693 atmel_port->prepare_rx = NULL;
1694 atmel_port->schedule_rx = &atmel_rx_from_ring;
1695 atmel_port->release_rx = NULL;
1696 }
1697
Elen Song08f738b2013-07-22 16:30:26 +08001698 if (atmel_use_dma_tx(port)) {
1699 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1700 atmel_port->schedule_tx = &atmel_tx_dma;
1701 atmel_port->release_tx = &atmel_release_tx_dma;
1702 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001703 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1704 atmel_port->schedule_tx = &atmel_tx_pdc;
1705 atmel_port->release_tx = &atmel_release_tx_pdc;
1706 } else {
1707 atmel_port->prepare_tx = NULL;
1708 atmel_port->schedule_tx = &atmel_tx_chars;
1709 atmel_port->release_tx = NULL;
1710 }
1711}
1712
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001713/*
Elen Song055560b2013-07-22 16:30:29 +08001714 * Get ip name usart or uart
1715 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001716static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001717{
1718 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001719 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001720 u32 version;
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001721 u32 usart, dbgu_uart, new_uart;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001722 /* ASCII decoding for IP version */
1723 usart = 0x55534152; /* USAR(T) */
1724 dbgu_uart = 0x44424755; /* DBGU */
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001725 new_uart = 0x55415254; /* UART */
Elen Song055560b2013-07-22 16:30:29 +08001726
Nicolas Ferre4b769372016-01-26 11:26:14 +01001727 atmel_port->has_hw_timer = false;
Elen Song055560b2013-07-22 16:30:29 +08001728
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001729 if (name == new_uart) {
1730 dev_dbg(port->dev, "Uart with hw timer");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001731 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001732 atmel_port->rtor = ATMEL_UA_RTOR;
1733 } else if (name == usart) {
1734 dev_dbg(port->dev, "Usart\n");
1735 atmel_port->has_hw_timer = true;
1736 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001737 } else if (name == dbgu_uart) {
1738 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
Elen Song055560b2013-07-22 16:30:29 +08001739 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001740 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001741 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001742 switch (version) {
1743 case 0x302:
1744 case 0x10213:
1745 dev_dbg(port->dev, "This version is usart\n");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001746 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001747 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001748 break;
1749 case 0x203:
1750 case 0x10202:
1751 dev_dbg(port->dev, "This version is uart\n");
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001752 break;
1753 default:
1754 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1755 }
Elen Song055560b2013-07-22 16:30:29 +08001756 }
Elen Song055560b2013-07-22 16:30:29 +08001757}
1758
1759/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001760 * Perform initialization and enable port for reception
1761 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001762static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001763{
Elen Song33d64c42013-07-22 16:30:28 +08001764 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001765 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001766 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001767 int retval;
1768
1769 /*
1770 * Ensure that no interrupts are enabled otherwise when
1771 * request_irq() is called we could get stuck trying to
1772 * handle an unexpected interrupt
1773 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001774 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001775 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001776
1777 /*
1778 * Allocate the IRQ
1779 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001780 retval = request_irq(port->irq, atmel_interrupt,
1781 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001782 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001783 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001784 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001785 return retval;
1786 }
1787
Leilei Zhao1e125782015-02-27 16:07:18 +08001788 tasklet_enable(&atmel_port->tasklet);
1789
Richard Genoudab5e4e42014-05-13 20:20:45 +02001790 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001791 * Initialize DMA (if necessary)
1792 */
Elen Song33d64c42013-07-22 16:30:28 +08001793 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001794 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001795
Elen Songa930e522013-07-22 16:30:25 +08001796 if (atmel_port->prepare_rx) {
1797 retval = atmel_port->prepare_rx(port);
1798 if (retval < 0)
1799 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001800 }
1801
Elen Songa930e522013-07-22 16:30:25 +08001802 if (atmel_port->prepare_tx) {
1803 retval = atmel_port->prepare_tx(port);
1804 if (retval < 0)
1805 atmel_set_ops(port);
1806 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001807
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001808 /*
1809 * Enable FIFO when available
1810 */
1811 if (atmel_port->fifo_size) {
1812 unsigned int txrdym = ATMEL_US_ONE_DATA;
1813 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1814 unsigned int fmr;
1815
1816 atmel_uart_writel(port, ATMEL_US_CR,
1817 ATMEL_US_FIFOEN |
1818 ATMEL_US_RXFCLR |
1819 ATMEL_US_TXFLCLR);
1820
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001821 if (atmel_use_dma_tx(port))
1822 txrdym = ATMEL_US_FOUR_DATA;
1823
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001824 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1825 if (atmel_port->rts_high &&
1826 atmel_port->rts_low)
1827 fmr |= ATMEL_US_FRTSC |
1828 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1829 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1830
1831 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1832 }
1833
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001834 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001835 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001836 atmel_port->irq_status = atmel_port->irq_status_prev;
1837
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001838 /*
1839 * Finally, enable the serial port
1840 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001841 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001842 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001843 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001844
Marek Roszko8bc661b2014-01-10 10:33:11 +01001845 setup_timer(&atmel_port->uart_timer,
1846 atmel_uart_timer_callback,
1847 (unsigned long)port);
1848
Elen Song64e22eb2013-07-22 16:30:24 +08001849 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001850 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001851 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001852 mod_timer(&atmel_port->uart_timer,
1853 jiffies + uart_poll_timeout(port));
1854 /* set USART timeout */
1855 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001856 atmel_uart_writel(port, atmel_port->rtor,
1857 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001858 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001859
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001860 atmel_uart_writel(port, ATMEL_US_IER,
1861 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001862 }
Chip Coldwella6670612008-02-08 04:21:06 -08001863 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001864 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001865 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001866 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001867 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001868 mod_timer(&atmel_port->uart_timer,
1869 jiffies + uart_poll_timeout(port));
1870 /* set USART timeout */
1871 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001872 atmel_uart_writel(port, atmel_port->rtor,
1873 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001874 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001875
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001876 atmel_uart_writel(port, ATMEL_US_IER,
1877 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001878 }
Chip Coldwella6670612008-02-08 04:21:06 -08001879 } else {
1880 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001881 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001882 }
Andrew Victorafefc412006-06-19 19:53:19 +01001883
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001884 return 0;
1885}
1886
1887/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001888 * Flush any TX data submitted for DMA. Called when the TX circular
1889 * buffer is reset.
1890 */
1891static void atmel_flush_buffer(struct uart_port *port)
1892{
1893 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1894
1895 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001896 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001897 atmel_port->pdc_tx.ofs = 0;
1898 }
1899}
1900
1901/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001902 * Disable the port
1903 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001904static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001905{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001906 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001907
Chip Coldwella6670612008-02-08 04:21:06 -08001908 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001909 * Prevent any tasklets being scheduled during
1910 * cleanup
1911 */
1912 del_timer_sync(&atmel_port->uart_timer);
1913
1914 /*
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001915 * Clear out any scheduled tasklets before
1916 * we destroy the buffers
1917 */
Leilei Zhao1e125782015-02-27 16:07:18 +08001918 tasklet_disable(&atmel_port->tasklet);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001919 tasklet_kill(&atmel_port->tasklet);
1920
1921 /*
1922 * Ensure everything is stopped and
1923 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001924 */
1925 atmel_stop_rx(port);
1926 atmel_stop_tx(port);
1927
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001928 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1929 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001930
1931
Chip Coldwella6670612008-02-08 04:21:06 -08001932 /*
1933 * Shut-down the DMA.
1934 */
Elen Songa930e522013-07-22 16:30:25 +08001935 if (atmel_port->release_rx)
1936 atmel_port->release_rx(port);
1937 if (atmel_port->release_tx)
1938 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001939
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001940 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001941 * Reset ring buffer pointers
1942 */
1943 atmel_port->rx_ring.head = 0;
1944 atmel_port->rx_ring.tail = 0;
1945
1946 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001947 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001948 */
1949 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001950
1951 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001952
Peter Hurley479e9b92014-10-16 16:54:18 -04001953 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001954}
1955
1956/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001957 * Power / Clock management.
1958 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001959static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1960 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001961{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001962 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001963
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001964 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001965 case 0:
1966 /*
1967 * Enable the peripheral clock for this serial port.
1968 * This is called on uart_open() or a resume event.
1969 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001970 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001971
1972 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001973 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001974 break;
1975 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001976 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001977 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
1978 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07001979
Remy Bohmerb843aa22008-02-08 04:21:01 -08001980 /*
1981 * Disable the peripheral clock for this serial port.
1982 * This is called on uart_close() or a suspend event.
1983 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001984 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001985 break;
1986 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001987 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001988 }
1989}
1990
1991/*
1992 * Change the port parameters
1993 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001994static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1995 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001996{
1997 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001998 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001999
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002000 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002001 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002002
2003 /* reset the mode, clock divisor, parity, stop bits and data size */
2004 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2005 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002006
Remy Bohmerb843aa22008-02-08 04:21:01 -08002007 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002008 quot = uart_get_divisor(port, baud);
2009
Remy Bohmerb843aa22008-02-08 04:21:01 -08002010 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01002011 quot /= 8;
2012 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2013 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002014
2015 /* byte size */
2016 switch (termios->c_cflag & CSIZE) {
2017 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002018 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002019 break;
2020 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002021 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002022 break;
2023 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002024 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002025 break;
2026 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002027 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002028 break;
2029 }
2030
2031 /* stop bits */
2032 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002033 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002034
2035 /* parity */
2036 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002037 /* Mark or Space parity */
2038 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002039 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002040 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002041 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002042 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002043 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002044 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002045 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002046 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002047 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002048 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002049
2050 spin_lock_irqsave(&port->lock, flags);
2051
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002052 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002053 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002054 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002055 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002056 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002057
Elen Song64e22eb2013-07-22 16:30:24 +08002058 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002059 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002060 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002061
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002062 /*
2063 * Characters to ignore
2064 */
2065 port->ignore_status_mask = 0;
2066 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002067 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002068 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002069 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002070 /*
2071 * If we're ignoring parity and break indicators,
2072 * ignore overruns too (for real raw support).
2073 */
2074 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002075 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002076 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002077 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002078
2079 /* update the per-port timeout */
2080 uart_update_timeout(port, termios->c_cflag, baud);
2081
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002082 /*
2083 * save/disable interrupts. The tty layer will ensure that the
2084 * transmitter is empty if requested by the caller, so there's
2085 * no need to wait for it here.
2086 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002087 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2088 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002089
2090 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002091 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002092
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002093 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002094 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002095 atmel_uart_writel(port, ATMEL_US_TTGR,
2096 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002097 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002098 } else if (termios->c_cflag & CRTSCTS) {
2099 /* RS232 with hardware handshake (RTS/CTS) */
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002100 if (atmel_use_dma_rx(port) && !atmel_use_fifo(port)) {
2101 dev_info(port->dev, "not enabling hardware flow control because DMA is used");
2102 termios->c_cflag &= ~CRTSCTS;
2103 } else {
2104 mode |= ATMEL_US_USMODE_HWHS;
2105 }
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002106 } else {
2107 /* RS232 without hadware handshake */
2108 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002109 }
2110
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002111 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002112 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002113
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002114 /*
2115 * when switching the mode, set the RTS line state according to the
2116 * new mode, otherwise keep the former state
2117 */
2118 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2119 unsigned int rts_state;
2120
2121 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2122 /* let the hardware control the RTS line */
2123 rts_state = ATMEL_US_RTSDIS;
2124 } else {
2125 /* force RTS line to low level */
2126 rts_state = ATMEL_US_RTSEN;
2127 }
2128
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002129 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002130 }
2131
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002132 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002133 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2134 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2135 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002136
2137 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002138 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002139
2140 /* CTS flow-control and modem-status interrupts */
2141 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002142 atmel_enable_ms(port);
2143 else
2144 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002145
2146 spin_unlock_irqrestore(&port->lock, flags);
2147}
2148
Peter Hurley732a84a2014-11-05 13:11:43 -05002149static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002150{
Peter Hurley732a84a2014-11-05 13:11:43 -05002151 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002152 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002153 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002154 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002155 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002156 } else {
2157 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002158 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2159 spin_lock_irq(&port->lock);
2160 atmel_disable_ms(port);
2161 spin_unlock_irq(&port->lock);
2162 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002163 }
2164}
2165
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002166/*
2167 * Return string describing the specified port
2168 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002169static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002170{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002171 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002172}
2173
2174/*
2175 * Release the memory region(s) being used by 'port'.
2176 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002177static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002178{
Andrew Victorafefc412006-06-19 19:53:19 +01002179 struct platform_device *pdev = to_platform_device(port->dev);
2180 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2181
2182 release_mem_region(port->mapbase, size);
2183
2184 if (port->flags & UPF_IOREMAP) {
2185 iounmap(port->membase);
2186 port->membase = NULL;
2187 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002188}
2189
2190/*
2191 * Request the memory region(s) being used by 'port'.
2192 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002193static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002194{
Andrew Victorafefc412006-06-19 19:53:19 +01002195 struct platform_device *pdev = to_platform_device(port->dev);
2196 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002197
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002198 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002199 return -EBUSY;
2200
2201 if (port->flags & UPF_IOREMAP) {
2202 port->membase = ioremap(port->mapbase, size);
2203 if (port->membase == NULL) {
2204 release_mem_region(port->mapbase, size);
2205 return -ENOMEM;
2206 }
2207 }
2208
2209 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002210}
2211
2212/*
2213 * Configure/autoconfigure the port.
2214 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002215static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002216{
2217 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002218 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002219 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002220 }
2221}
2222
2223/*
2224 * Verify the new serial_struct (for TIOCSSERIAL).
2225 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002226static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002227{
2228 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002229 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002230 ret = -EINVAL;
2231 if (port->irq != ser->irq)
2232 ret = -EINVAL;
2233 if (ser->io_type != SERIAL_IO_MEM)
2234 ret = -EINVAL;
2235 if (port->uartclk / 16 != ser->baud_base)
2236 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002237 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002238 ret = -EINVAL;
2239 if (port->iobase != ser->port)
2240 ret = -EINVAL;
2241 if (ser->hub6 != 0)
2242 ret = -EINVAL;
2243 return ret;
2244}
2245
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002246#ifdef CONFIG_CONSOLE_POLL
2247static int atmel_poll_get_char(struct uart_port *port)
2248{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002249 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002250 cpu_relax();
2251
Cyrille Pitchena6499432015-07-30 16:33:38 +02002252 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002253}
2254
2255static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2256{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002257 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002258 cpu_relax();
2259
Cyrille Pitchena6499432015-07-30 16:33:38 +02002260 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002261}
2262#endif
2263
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002264static struct uart_ops atmel_pops = {
2265 .tx_empty = atmel_tx_empty,
2266 .set_mctrl = atmel_set_mctrl,
2267 .get_mctrl = atmel_get_mctrl,
2268 .stop_tx = atmel_stop_tx,
2269 .start_tx = atmel_start_tx,
2270 .stop_rx = atmel_stop_rx,
2271 .enable_ms = atmel_enable_ms,
2272 .break_ctl = atmel_break_ctl,
2273 .startup = atmel_startup,
2274 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002275 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002276 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002277 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002278 .type = atmel_type,
2279 .release_port = atmel_release_port,
2280 .request_port = atmel_request_port,
2281 .config_port = atmel_config_port,
2282 .verify_port = atmel_verify_port,
2283 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002284#ifdef CONFIG_CONSOLE_POLL
2285 .poll_get_char = atmel_poll_get_char,
2286 .poll_put_char = atmel_poll_put_char,
2287#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002288};
2289
Andrew Victorafefc412006-06-19 19:53:19 +01002290/*
2291 * Configure the port from the platform device resource info.
2292 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002293static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002294 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002295{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002296 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002297 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002298 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002299
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002300 atmel_init_property(atmel_port, pdev);
2301 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002302
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002303 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002304
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002305 port->iotype = UPIO_MEM;
2306 port->flags = UPF_BOOT_AUTOCONF;
2307 port->ops = &atmel_pops;
2308 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002309 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002310 port->mapbase = pdev->resource[0].start;
2311 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002312 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002313
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002314 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2315 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08002316 tasklet_disable(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002317
2318 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2319
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002320 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002321 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002322 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002323 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002324 port->flags |= UPF_IOREMAP;
2325 port->membase = NULL;
2326 }
2327
Remy Bohmerb843aa22008-02-08 04:21:01 -08002328 /* for console, the clock could already be configured */
2329 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002330 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002331 if (IS_ERR(atmel_port->clk)) {
2332 ret = PTR_ERR(atmel_port->clk);
2333 atmel_port->clk = NULL;
2334 return ret;
2335 }
2336 ret = clk_prepare_enable(atmel_port->clk);
2337 if (ret) {
2338 clk_put(atmel_port->clk);
2339 atmel_port->clk = NULL;
2340 return ret;
2341 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002342 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002343 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002344 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002345 }
Chip Coldwella6670612008-02-08 04:21:06 -08002346
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002347 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002348 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002349 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002350 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002351 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002352 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2353 } else {
2354 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2355 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002356
2357 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002358}
2359
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002360struct platform_device *atmel_default_console_device; /* the serial console device */
2361
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002362#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002363static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002364{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002365 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002366 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002367 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002368}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002369
2370/*
2371 * Interrupts are disabled on entering
2372 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002373static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002374{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002375 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002376 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002377 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002378 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002379
2380 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002381 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002382 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002383 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2384 atmel_uart_writel(port, ATMEL_US_IDR,
2385 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002386
Marc Pignat39d4c922008-04-02 13:04:42 -07002387 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002388 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2389 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002390
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002391 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002392
2393 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002394 * Finally, wait for transmitter to become empty
2395 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002396 */
2397 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002398 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002399 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002400
2401 /* Restore PDC transmit status */
2402 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002403 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002404
Remy Bohmerb843aa22008-02-08 04:21:01 -08002405 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002406 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002407}
2408
2409/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002410 * If the port was already initialised (eg, by a boot loader),
2411 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002412 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002413static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2414 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002415{
2416 unsigned int mr, quot;
2417
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002418 /*
2419 * If the baud rate generator isn't running, the port wasn't
2420 * initialized by the boot loader.
2421 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002422 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002423 if (!quot)
2424 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002425
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002426 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002427 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002428 *bits = 8;
2429 else
2430 *bits = 7;
2431
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002432 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002433 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002434 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002435 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002436 *parity = 'o';
2437
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002438 /*
2439 * The serial core only rounds down when matching this to a
2440 * supported baud rate. Make sure we don't end up slightly
2441 * lower than one of those, as it would make us fall through
2442 * to a much lower baud rate than we really want.
2443 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002444 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002445}
2446
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002447static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002448{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002449 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002450 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002451 int baud = 115200;
2452 int bits = 8;
2453 int parity = 'n';
2454 int flow = 'n';
2455
Remy Bohmerb843aa22008-02-08 04:21:01 -08002456 if (port->membase == NULL) {
2457 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002458 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002459 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002460
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002461 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2462 if (ret)
2463 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002464
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002465 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2466 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2467 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002468
2469 if (options)
2470 uart_parse_options(options, &baud, &parity, &bits, &flow);
2471 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002472 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002473
2474 return uart_set_options(port, co, baud, parity, bits, flow);
2475}
2476
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002477static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002478
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002479static struct console atmel_console = {
2480 .name = ATMEL_DEVICENAME,
2481 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002482 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002483 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002484 .flags = CON_PRINTBUFFER,
2485 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002486 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002487};
2488
David Brownell06a7f052008-11-06 12:53:40 -08002489#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002490
Andrew Victorafefc412006-06-19 19:53:19 +01002491/*
2492 * Early console initialization (before VM subsystem initialized).
2493 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002494static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002495{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002496 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002497 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002498 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002499 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002500 int id = pdata->num;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002501 struct atmel_uart_port *atmel_port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002502
Jaeden Amerob78cd162016-01-26 12:34:49 +01002503 atmel_port->backup_imr = 0;
2504 atmel_port->uart.line = id;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002505
2506 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002507 ret = atmel_init_port(atmel_port, atmel_default_console_device);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002508 if (ret)
2509 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002510 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002511 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002512
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002513 return 0;
2514}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002515
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002516console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002517
Andrew Victorafefc412006-06-19 19:53:19 +01002518/*
2519 * Late console initialization.
2520 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002521static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002522{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002523 if (atmel_default_console_device
2524 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002525 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002526
2527 return 0;
2528}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002529
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002530core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002531
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002532static inline bool atmel_is_console_port(struct uart_port *port)
2533{
2534 return port->cons && port->cons->index == port->line;
2535}
2536
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002537#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002538#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002539
2540static inline bool atmel_is_console_port(struct uart_port *port)
2541{
2542 return false;
2543}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002544#endif
2545
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002546static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002547 .owner = THIS_MODULE,
2548 .driver_name = "atmel_serial",
2549 .dev_name = ATMEL_DEVICENAME,
2550 .major = SERIAL_ATMEL_MAJOR,
2551 .minor = MINOR_START,
2552 .nr = ATMEL_MAX_UART,
2553 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002554};
2555
Andrew Victorafefc412006-06-19 19:53:19 +01002556#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002557static bool atmel_serial_clk_will_stop(void)
2558{
2559#ifdef CONFIG_ARCH_AT91
2560 return at91_suspend_entering_slow_clock();
2561#else
2562 return false;
2563#endif
2564}
2565
Remy Bohmerb843aa22008-02-08 04:21:01 -08002566static int atmel_serial_suspend(struct platform_device *pdev,
2567 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002568{
Andrew Victorafefc412006-06-19 19:53:19 +01002569 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002570 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002571
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002572 if (atmel_is_console_port(port) && console_suspend_enabled) {
2573 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002574 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2575 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002576 cpu_relax();
2577 }
2578
Anti Sullinf05596d2008-09-22 13:57:54 -07002579 /* we can not wake up if we're running on slow clock */
2580 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002581 if (atmel_serial_clk_will_stop()) {
2582 unsigned long flags;
2583
2584 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2585 atmel_port->suspended = true;
2586 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002587 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002588 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002589
2590 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002591
2592 return 0;
2593}
2594
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002595static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002596{
2597 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002598 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002599 unsigned long flags;
2600
2601 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2602 if (atmel_port->pending) {
2603 atmel_handle_receive(port, atmel_port->pending);
2604 atmel_handle_status(port, atmel_port->pending,
2605 atmel_port->pending_status);
2606 atmel_handle_transmit(port, atmel_port->pending);
2607 atmel_port->pending = 0;
2608 }
2609 atmel_port->suspended = false;
2610 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002611
Anti Sullinf05596d2008-09-22 13:57:54 -07002612 uart_resume_port(&atmel_uart, port);
2613 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002614
2615 return 0;
2616}
2617#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002618#define atmel_serial_suspend NULL
2619#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002620#endif
2621
Jaeden Amerob78cd162016-01-26 12:34:49 +01002622static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002623 struct platform_device *pdev)
2624{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002625 atmel_port->fifo_size = 0;
2626 atmel_port->rts_low = 0;
2627 atmel_port->rts_high = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002628
2629 if (of_property_read_u32(pdev->dev.of_node,
2630 "atmel,fifo-size",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002631 &atmel_port->fifo_size))
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002632 return;
2633
Jaeden Amerob78cd162016-01-26 12:34:49 +01002634 if (!atmel_port->fifo_size)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002635 return;
2636
Jaeden Amerob78cd162016-01-26 12:34:49 +01002637 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2638 atmel_port->fifo_size = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002639 dev_err(&pdev->dev, "Invalid FIFO size\n");
2640 return;
2641 }
2642
2643 /*
2644 * 0 <= rts_low <= rts_high <= fifo_size
2645 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2646 * to flush their internal TX FIFO, commonly up to 16 data, before
2647 * actually stopping to send new data. So we try to set the RTS High
2648 * Threshold to a reasonably high value respecting this 16 data
2649 * empirical rule when possible.
2650 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002651 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2652 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2653 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2,
2654 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002655
2656 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002657 atmel_port->fifo_size);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002658 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002659 atmel_port->rts_high);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002660 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002661 atmel_port->rts_low);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002662}
2663
Bill Pemberton9671f092012-11-19 13:21:50 -05002664static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002665{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002666 struct atmel_uart_port *atmel_port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002667 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002668 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002669 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002670 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002671 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002672
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002673 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002674
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002675 if (np)
2676 ret = of_alias_get_id(np, "serial");
2677 else
2678 if (pdata)
2679 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002680
2681 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002682 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002683 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002684 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002685
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002686 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002687 ret = -ENODEV;
2688 goto err;
2689 }
2690
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002691 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002692 /* port already in use */
2693 ret = -EBUSY;
2694 goto err;
2695 }
2696
Jaeden Amerob78cd162016-01-26 12:34:49 +01002697 atmel_port = &atmel_ports[ret];
2698 atmel_port->backup_imr = 0;
2699 atmel_port->uart.line = ret;
2700 atmel_serial_probe_fifos(atmel_port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002701
Jaeden Amerob78cd162016-01-26 12:34:49 +01002702 spin_lock_init(&atmel_port->lock_suspended);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002703
Jaeden Amerob78cd162016-01-26 12:34:49 +01002704 ret = atmel_init_port(atmel_port, pdev);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002705 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002706 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002707
Jaeden Amerob78cd162016-01-26 12:34:49 +01002708 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2709 if (IS_ERR(atmel_port->gpios)) {
2710 ret = PTR_ERR(atmel_port->gpios);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002711 goto err_clear_bit;
2712 }
2713
Jaeden Amerob78cd162016-01-26 12:34:49 +01002714 if (!atmel_use_pdc_rx(&atmel_port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002715 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002716 data = kmalloc(sizeof(struct atmel_uart_char)
2717 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002718 if (!data)
2719 goto err_alloc_ring;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002720 atmel_port->rx_ring.buf = data;
Chip Coldwella6670612008-02-08 04:21:06 -08002721 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002722
Jaeden Amerob78cd162016-01-26 12:34:49 +01002723 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002724
Jaeden Amerob78cd162016-01-26 12:34:49 +01002725 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002726 if (ret)
2727 goto err_add_port;
2728
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002729#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Jaeden Amerob78cd162016-01-26 12:34:49 +01002730 if (atmel_is_console_port(&atmel_port->uart)
David Brownell06a7f052008-11-06 12:53:40 -08002731 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2732 /*
2733 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002734 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002735 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002736 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002737 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002738#endif
David Brownell06a7f052008-11-06 12:53:40 -08002739
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002740 device_init_wakeup(&pdev->dev, 1);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002741 platform_set_drvdata(pdev, atmel_port);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002742
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002743 /*
2744 * The peripheral clock has been disabled by atmel_init_port():
2745 * enable it before accessing I/O registers
2746 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002747 clk_prepare_enable(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002748
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002749 if (rs485_enabled) {
Jaeden Amerob78cd162016-01-26 12:34:49 +01002750 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002751 ATMEL_US_USMODE_NORMAL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002752 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2753 ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002754 }
2755
Elen Song055560b2013-07-22 16:30:29 +08002756 /*
2757 * Get port name of usart or uart
2758 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002759 atmel_get_ip_name(&atmel_port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002760
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002761 /*
2762 * The peripheral clock can now safely be disabled till the port
2763 * is used
2764 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002765 clk_disable_unprepare(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002766
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002767 return 0;
2768
2769err_add_port:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002770 kfree(atmel_port->rx_ring.buf);
2771 atmel_port->rx_ring.buf = NULL;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002772err_alloc_ring:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002773 if (!atmel_is_console_port(&atmel_port->uart)) {
2774 clk_put(atmel_port->clk);
2775 atmel_port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002776 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002777err_clear_bit:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002778 clear_bit(atmel_port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002779err:
Andrew Victorafefc412006-06-19 19:53:19 +01002780 return ret;
2781}
2782
Romain Izardf4a8ab042016-02-26 11:15:04 +01002783/*
2784 * Even if the driver is not modular, it makes sense to be able to
2785 * unbind a device: there can be many bound devices, and there are
2786 * situations where dynamic binding and unbinding can be useful.
2787 *
2788 * For example, a connected device can require a specific firmware update
2789 * protocol that needs bitbanging on IO lines, but use the regular serial
2790 * port in the normal case.
2791 */
2792static int atmel_serial_remove(struct platform_device *pdev)
2793{
2794 struct uart_port *port = platform_get_drvdata(pdev);
2795 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2796 int ret = 0;
2797
2798 tasklet_kill(&atmel_port->tasklet);
2799
2800 device_init_wakeup(&pdev->dev, 0);
2801
2802 ret = uart_remove_one_port(&atmel_uart, port);
2803
2804 kfree(atmel_port->rx_ring.buf);
2805
2806 /* "port" is allocated statically, so we shouldn't free it */
2807
2808 clear_bit(port->line, atmel_ports_in_use);
2809
2810 clk_put(atmel_port->clk);
2811 atmel_port->clk = NULL;
2812
2813 return ret;
2814}
2815
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002816static struct platform_driver atmel_serial_driver = {
2817 .probe = atmel_serial_probe,
Romain Izardf4a8ab042016-02-26 11:15:04 +01002818 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002819 .suspend = atmel_serial_suspend,
2820 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002821 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002822 .name = "atmel_usart",
2823 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002824 },
2825};
2826
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002827static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002828{
2829 int ret;
2830
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002831 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002832 if (ret)
2833 return ret;
2834
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002835 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002836 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002837 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002838
2839 return ret;
2840}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002841device_initcall(atmel_serial_init);