blob: 4a7eb85f7c8577556927fc99ff822697975c399f [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
Ludovic Desroches637ba542016-06-17 12:05:48 +0200111/*
112 * Be careful, the real size of the ring buffer is
113 * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
114 * can contain up to 1024 characters in PIO mode and up to 4096 characters in
115 * DMA mode.
116 */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800117#define ATMEL_SERIAL_RINGSIZE 1024
118
Andrew Victorafefc412006-06-19 19:53:19 +0100119/*
Alexandre Belloni9af92fb2015-09-10 11:29:03 +0200120 * at91: 6 USARTs and one DBGU port (SAM9260)
121 * avr32: 4
122 */
123#define ATMEL_MAX_UART 7
124
125/*
Andrew Victorafefc412006-06-19 19:53:19 +0100126 * We wrap our port structure around the generic uart_port.
127 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200128struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100129 struct uart_port uart; /* uart */
130 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700131 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
132 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700133 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800134
Elen Song34df42f2013-07-22 16:30:27 +0800135 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800136 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800137 short pdc_rx_idx; /* current PDC RX buffer */
138 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
139
Elen Song08f738b2013-07-22 16:30:26 +0800140 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800141 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800142 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
143
Elen Song08f738b2013-07-22 16:30:26 +0800144 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800145 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800146 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800147 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800148 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800149 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800150 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800151 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800152 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800153 struct scatterlist sg_rx;
Nicolas Ferre00e8e6582016-06-17 12:05:47 +0200154 struct tasklet_struct tasklet_rx;
155 struct tasklet_struct tasklet_tx;
Nicolas Ferre98f20822016-06-26 09:44:49 +0200156 atomic_t tasklet_shutdown;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800157 unsigned int irq_status_prev;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200158 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800159
160 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100161
Richard Genoude0b0baa2014-05-13 20:20:44 +0200162 struct mctrl_gpios *gpios;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100163 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200164 u32 fifo_size;
165 u32 rts_high;
166 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200167 bool ms_irq_enabled;
Ludovic Desroches2958cce2016-02-22 15:18:55 +0100168 u32 rtor; /* address of receiver timeout register if it exists */
Ludovic Desroches5bf56352016-08-25 15:47:56 +0200169 bool has_frac_baudrate;
Nicolas Ferre4b769372016-01-26 11:26:14 +0100170 bool has_hw_timer;
171 struct timer_list uart_timer;
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100172
173 bool suspended;
174 unsigned int pending;
175 unsigned int pending_status;
176 spinlock_t lock_suspended;
177
Razvan Stefanescu0668f0d2019-03-19 15:20:35 +0200178 bool hd_start_rx; /* can start RX during half-duplex operation */
179
Elen Songa930e522013-07-22 16:30:25 +0800180 int (*prepare_rx)(struct uart_port *port);
181 int (*prepare_tx)(struct uart_port *port);
182 void (*schedule_rx)(struct uart_port *port);
183 void (*schedule_tx)(struct uart_port *port);
184 void (*release_rx)(struct uart_port *port);
185 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100186};
187
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200188static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100189static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100190
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000191#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200192static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000193#endif
194
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200195#if defined(CONFIG_OF)
196static const struct of_device_id atmel_serial_dt_ids[] = {
197 { .compatible = "atmel,at91rm9200-usart" },
198 { .compatible = "atmel,at91sam9260-usart" },
199 { /* sentinel */ }
200};
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200201#endif
202
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800203static inline struct atmel_uart_port *
204to_atmel_uart_port(struct uart_port *uart)
205{
206 return container_of(uart, struct atmel_uart_port, uart);
207}
208
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200209static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
210{
211 return __raw_readl(port->membase + reg);
212}
213
214static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
215{
216 __raw_writel(value, port->membase + reg);
217}
218
Cyrille Pitchena6499432015-07-30 16:33:38 +0200219#ifdef CONFIG_AVR32
220
221/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
222static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200223{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200224 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200225}
226
Cyrille Pitchena6499432015-07-30 16:33:38 +0200227static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200228{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200229 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200230}
231
Cyrille Pitchena6499432015-07-30 16:33:38 +0200232#else
233
234static inline u8 atmel_uart_read_char(struct uart_port *port)
235{
236 return __raw_readb(port->membase + ATMEL_US_RHR);
237}
238
239static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
240{
241 __raw_writeb(value, port->membase + ATMEL_US_THR);
242}
243
244#endif
245
Razvan Stefanescuab18aa02019-03-19 15:20:34 +0200246static inline int atmel_uart_is_half_duplex(struct uart_port *port)
247{
248 return (port->rs485.flags & SER_RS485_ENABLED) &&
249 !(port->rs485.flags & SER_RS485_RX_DURING_TX);
250}
251
Chip Coldwella6670612008-02-08 04:21:06 -0800252#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800253static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800254{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800255 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800256
Elen Song64e22eb2013-07-22 16:30:24 +0800257 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800258}
259
Elen Song64e22eb2013-07-22 16:30:24 +0800260static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800261{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800262 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800263
Elen Song64e22eb2013-07-22 16:30:24 +0800264 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800265}
266#else
Elen Song64e22eb2013-07-22 16:30:24 +0800267static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800268{
269 return false;
270}
271
Elen Song64e22eb2013-07-22 16:30:24 +0800272static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800273{
274 return false;
275}
276#endif
277
Elen Song08f738b2013-07-22 16:30:26 +0800278static bool atmel_use_dma_tx(struct uart_port *port)
279{
280 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
281
282 return atmel_port->use_dma_tx;
283}
284
Elen Song34df42f2013-07-22 16:30:27 +0800285static bool atmel_use_dma_rx(struct uart_port *port)
286{
287 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
288
289 return atmel_port->use_dma_rx;
290}
291
Alexandre Belloni5be605a2016-04-12 14:51:40 +0200292static bool atmel_use_fifo(struct uart_port *port)
293{
294 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
295
296 return atmel_port->fifo_size;
297}
298
Nicolas Ferre98f20822016-06-26 09:44:49 +0200299static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
300 struct tasklet_struct *t)
301{
302 if (!atomic_read(&atmel_port->tasklet_shutdown))
303 tasklet_schedule(t);
304}
305
Richard Genoude0b0baa2014-05-13 20:20:44 +0200306static unsigned int atmel_get_lines_status(struct uart_port *port)
307{
308 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
309 unsigned int status, ret = 0;
310
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200311 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200312
313 mctrl_gpio_get(atmel_port->gpios, &ret);
314
315 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
316 UART_GPIO_CTS))) {
317 if (ret & TIOCM_CTS)
318 status &= ~ATMEL_US_CTS;
319 else
320 status |= ATMEL_US_CTS;
321 }
322
323 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
324 UART_GPIO_DSR))) {
325 if (ret & TIOCM_DSR)
326 status &= ~ATMEL_US_DSR;
327 else
328 status |= ATMEL_US_DSR;
329 }
330
331 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
332 UART_GPIO_RI))) {
333 if (ret & TIOCM_RI)
334 status &= ~ATMEL_US_RI;
335 else
336 status |= ATMEL_US_RI;
337 }
338
339 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
340 UART_GPIO_DCD))) {
341 if (ret & TIOCM_CD)
342 status &= ~ATMEL_US_DCD;
343 else
344 status |= ATMEL_US_DCD;
345 }
346
347 return status;
348}
349
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100350/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100351static int atmel_config_rs485(struct uart_port *port,
352 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100353{
354 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
355 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100356
357 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200358 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100359
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200360 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100361
362 /* Resetting serial mode to RS232 (0x0) */
363 mode &= ~ATMEL_US_USMODE;
364
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100365 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100366
367 if (rs485conf->flags & SER_RS485_ENABLED) {
368 dev_dbg(port->dev, "Setting UART to RS485\n");
369 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200370 atmel_uart_writel(port, ATMEL_US_TTGR,
371 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100372 mode |= ATMEL_US_USMODE_RS485;
373 } else {
374 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800375 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100376 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
377 ATMEL_US_TXBUFE;
378 else
379 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
380 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200381 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100382
383 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200384 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100385
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100386 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100387}
388
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000389/*
390 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
391 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200392static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000393{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200394 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
395 TIOCSER_TEMT :
396 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000397}
398
399/*
400 * Set state of the modem control output lines
401 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200402static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000403{
404 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200405 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100406 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100407 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000408
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100409 /* override mode to RS485 if needed, otherwise keep the current mode */
410 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200411 atmel_uart_writel(port, ATMEL_US_TTGR,
412 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100413 mode &= ~ATMEL_US_USMODE;
414 mode |= ATMEL_US_USMODE_RS485;
415 }
416
417 /* set the RTS line state according to the mode */
418 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
419 /* force RTS line to high level */
420 rts_paused = ATMEL_US_RTSEN;
421
422 /* give the control of the RTS line back to the hardware */
423 rts_ready = ATMEL_US_RTSDIS;
424 } else {
425 /* force RTS line to high level */
426 rts_paused = ATMEL_US_RTSDIS;
427
428 /* force RTS line to low level */
429 rts_ready = ATMEL_US_RTSEN;
430 }
431
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000432 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100433 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000434 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100435 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000436
437 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200438 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000439 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200440 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000441
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200442 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100443
Richard Genoude0b0baa2014-05-13 20:20:44 +0200444 mctrl_gpio_set(atmel_port->gpios, mctrl);
445
Andrew Victorafefc412006-06-19 19:53:19 +0100446 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100447 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100448 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200449 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100450 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200451 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100452
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200453 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000454}
455
456/*
457 * Get state of the modem control input lines
458 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200459static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000460{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200461 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
462 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000463
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200464 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000465
466 /*
467 * The control signals are active low.
468 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200469 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000470 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200471 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000472 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200473 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000474 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200475 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000476 ret |= TIOCM_RI;
477
Richard Genoude0b0baa2014-05-13 20:20:44 +0200478 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000479}
480
481/*
482 * Stop transmitting.
483 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200484static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000485{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100486 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
487
Elen Song64e22eb2013-07-22 16:30:24 +0800488 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800489 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200490 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100491 }
Richard Genoudba04d862016-12-13 17:27:56 +0100492
493 /*
494 * Disable the transmitter.
495 * This is mandatory when DMA is used, otherwise the DMA buffer
496 * is fully transmitted.
497 */
498 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
499
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100500 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200501 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100502
Razvan Stefanescuab18aa02019-03-19 15:20:34 +0200503 if (atmel_uart_is_half_duplex(port))
Nicolas Ferre84486252020-02-10 16:20:53 +0100504 if (!atomic_read(&atmel_port->tasklet_shutdown))
505 atmel_start_rx(port);
Razvan Stefanescuab18aa02019-03-19 15:20:34 +0200506
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000507}
508
509/*
510 * Start transmitting.
511 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200512static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000513{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100514 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
515
Alexandre Belloni0058f082016-05-28 00:54:08 +0200516 if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
517 & ATMEL_PDC_TXTEN))
518 /* The transmitter is already running. Yes, we
519 really need this.*/
520 return;
Chip Coldwella6670612008-02-08 04:21:06 -0800521
Alexandre Belloni0058f082016-05-28 00:54:08 +0200522 if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
Razvan Stefanescuab18aa02019-03-19 15:20:34 +0200523 if (atmel_uart_is_half_duplex(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100524 atmel_stop_rx(port);
525
Alexandre Belloni0058f082016-05-28 00:54:08 +0200526 if (atmel_use_pdc_tx(port))
Chip Coldwella6670612008-02-08 04:21:06 -0800527 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200528 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Alexandre Belloni0058f082016-05-28 00:54:08 +0200529
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100530 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200531 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Richard Genoudba04d862016-12-13 17:27:56 +0100532
533 /* re-enable the transmitter */
534 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100535}
536
537/*
538 * start receiving - port is in process of being opened.
539 */
540static void atmel_start_rx(struct uart_port *port)
541{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200542 /* reset status and receiver */
543 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100544
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200545 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200546
Elen Song64e22eb2013-07-22 16:30:24 +0800547 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100548 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200549 atmel_uart_writel(port, ATMEL_US_IER,
550 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
551 port->read_status_mask);
552 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100553 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200554 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100555 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000556}
557
558/*
559 * Stop receiving - port is in process of being closed.
560 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200561static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000562{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200563 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200564
Elen Song64e22eb2013-07-22 16:30:24 +0800565 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800566 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200567 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
568 atmel_uart_writel(port, ATMEL_US_IDR,
569 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
570 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100571 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200572 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100573 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000574}
575
576/*
577 * Enable modem status interrupts
578 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200579static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000580{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200581 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
582 uint32_t ier = 0;
583
584 /*
585 * Interrupt should not be enabled twice
586 */
587 if (atmel_port->ms_irq_enabled)
588 return;
589
590 atmel_port->ms_irq_enabled = true;
591
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200592 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200593 ier |= 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 Genoudab5e4e42014-05-13 20:20:45 +0200596 ier |= 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 Genoudab5e4e42014-05-13 20:20:45 +0200599 ier |= 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 Genoudab5e4e42014-05-13 20:20:45 +0200602 ier |= ATMEL_US_DCDIC;
603
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200604 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200605
606 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000607}
608
609/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200610 * Disable modem status interrupts
611 */
612static void atmel_disable_ms(struct uart_port *port)
613{
614 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
615 uint32_t idr = 0;
616
617 /*
618 * Interrupt should not be disabled twice
619 */
620 if (!atmel_port->ms_irq_enabled)
621 return;
622
623 atmel_port->ms_irq_enabled = false;
624
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200625 mctrl_gpio_disable_ms(atmel_port->gpios);
626
627 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200628 idr |= ATMEL_US_CTSIC;
629
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200630 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200631 idr |= ATMEL_US_DSRIC;
632
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200633 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200634 idr |= ATMEL_US_RIIC;
635
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200636 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200637 idr |= ATMEL_US_DCDIC;
638
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200639 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200640}
641
642/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000643 * Control the transmission of a break signal
644 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200645static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000646{
647 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200648 /* start break */
649 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000650 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200651 /* stop break */
652 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000653}
654
655/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800656 * Stores the incoming character in the ring buffer
657 */
658static void
659atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
660 unsigned int ch)
661{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800662 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800663 struct circ_buf *ring = &atmel_port->rx_ring;
664 struct atmel_uart_char *c;
665
666 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
667 /* Buffer overflow, ignore char */
668 return;
669
670 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
671 c->status = status;
672 c->ch = ch;
673
674 /* Make sure the character is stored before we update head. */
675 smp_wmb();
676
677 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
678}
679
680/*
Chip Coldwella6670612008-02-08 04:21:06 -0800681 * Deal with parity, framing and overrun errors.
682 */
683static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
684{
685 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200686 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800687
688 if (status & ATMEL_US_RXBRK) {
689 /* ignore side-effect */
690 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
691 port->icount.brk++;
692 }
693 if (status & ATMEL_US_PARE)
694 port->icount.parity++;
695 if (status & ATMEL_US_FRAME)
696 port->icount.frame++;
697 if (status & ATMEL_US_OVRE)
698 port->icount.overrun++;
699}
700
701/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000702 * Characters received (called from interrupt handler)
703 */
David Howells7d12e782006-10-05 14:55:46 +0100704static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000705{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800706 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800707 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000708
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200709 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200710 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200711 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000712
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000713 /*
714 * note that the error handling code is
715 * out of the main execution path
716 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700717 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
718 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
719 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800720
Remy Bohmerb843aa22008-02-08 04:21:01 -0800721 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200722 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800723
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700724 if (status & ATMEL_US_RXBRK
725 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700726 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200727 atmel_uart_writel(port, ATMEL_US_IER,
728 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700729 } else {
730 /*
731 * This is either the end-of-break
732 * condition or we've received at
733 * least one character without RXBRK
734 * being set. In both cases, the next
735 * RXBRK will indicate start-of-break.
736 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200737 atmel_uart_writel(port, ATMEL_US_IDR,
738 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700739 status &= ~ATMEL_US_RXBRK;
740 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100741 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000742 }
743
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800744 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200745 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000746 }
747
Nicolas Ferre98f20822016-06-26 09:44:49 +0200748 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000749}
750
751/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800752 * Transmit characters (called from tasklet with TXRDY interrupt
753 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000754 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200755static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000756{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700757 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100758 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000759
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200760 if (port->x_char &&
761 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200762 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000763 port->icount.tx++;
764 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000765 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800766 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000767 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000768
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200769 while (atmel_uart_readl(port, ATMEL_US_CSR) &
770 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200771 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000772 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
773 port->icount.tx++;
774 if (uart_circ_empty(xmit))
775 break;
776 }
777
778 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
779 uart_write_wakeup(port);
780
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800781 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100782 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200783 atmel_uart_writel(port, ATMEL_US_IER,
784 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000785}
786
Elen Song08f738b2013-07-22 16:30:26 +0800787static void atmel_complete_tx_dma(void *arg)
788{
789 struct atmel_uart_port *atmel_port = arg;
790 struct uart_port *port = &atmel_port->uart;
791 struct circ_buf *xmit = &port->state->xmit;
792 struct dma_chan *chan = atmel_port->chan_tx;
793 unsigned long flags;
794
795 spin_lock_irqsave(&port->lock, flags);
796
797 if (chan)
798 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200799 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800800 xmit->tail &= UART_XMIT_SIZE - 1;
801
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200802 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800803
804 spin_lock_irq(&atmel_port->lock_tx);
805 async_tx_ack(atmel_port->desc_tx);
806 atmel_port->cookie_tx = -EINVAL;
807 atmel_port->desc_tx = NULL;
808 spin_unlock_irq(&atmel_port->lock_tx);
809
810 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
811 uart_write_wakeup(port);
812
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100813 /*
814 * xmit is a circular buffer so, if we have just send data from
815 * xmit->tail to the end of xmit->buf, now we have to transmit the
816 * remaining data from the beginning of xmit->buf to xmit->head.
817 */
Elen Song08f738b2013-07-22 16:30:26 +0800818 if (!uart_circ_empty(xmit))
Nicolas Ferre98f20822016-06-26 09:44:49 +0200819 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Razvan Stefanescuab18aa02019-03-19 15:20:34 +0200820 else if (atmel_uart_is_half_duplex(port)) {
Razvan Stefanescu0668f0d2019-03-19 15:20:35 +0200821 /*
822 * DMA done, re-enable TXEMPTY and signal that we can stop
823 * TX and start RX for RS485
824 */
825 atmel_port->hd_start_rx = true;
826 atmel_uart_writel(port, ATMEL_US_IER,
827 atmel_port->tx_done_mask);
Richard Genoud2d789bd2016-12-06 13:05:33 +0100828 }
Elen Song08f738b2013-07-22 16:30:26 +0800829
830 spin_unlock_irqrestore(&port->lock, flags);
831}
832
833static void atmel_release_tx_dma(struct uart_port *port)
834{
835 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
836 struct dma_chan *chan = atmel_port->chan_tx;
837
838 if (chan) {
839 dmaengine_terminate_all(chan);
840 dma_release_channel(chan);
841 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200842 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800843 }
844
845 atmel_port->desc_tx = NULL;
846 atmel_port->chan_tx = NULL;
847 atmel_port->cookie_tx = -EINVAL;
848}
849
850/*
851 * Called from tasklet with TXRDY interrupt is disabled.
852 */
853static void atmel_tx_dma(struct uart_port *port)
854{
855 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
856 struct circ_buf *xmit = &port->state->xmit;
857 struct dma_chan *chan = atmel_port->chan_tx;
858 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200859 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
860 unsigned int tx_len, part1_len, part2_len, sg_len;
861 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800862
863 /* Make sure we have an idle channel */
864 if (atmel_port->desc_tx != NULL)
865 return;
866
867 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
868 /*
869 * DMA is idle now.
870 * Port xmit buffer is already mapped,
871 * and it is one page... Just adjust
872 * offsets and lengths. Since it is a circular buffer,
873 * we have to transmit till the end, and then the rest.
874 * Take the port lock to get a
875 * consistent xmit buffer state.
876 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200877 tx_len = CIRC_CNT_TO_END(xmit->head,
878 xmit->tail,
879 UART_XMIT_SIZE);
880
881 if (atmel_port->fifo_size) {
882 /* multi data mode */
883 part1_len = (tx_len & ~0x3); /* DWORD access */
884 part2_len = (tx_len & 0x3); /* BYTE access */
885 } else {
886 /* single data (legacy) mode */
887 part1_len = 0;
888 part2_len = tx_len; /* BYTE access only */
889 }
890
891 sg_init_table(sgl, 2);
892 sg_len = 0;
893 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
894 if (part1_len) {
895 sg = &sgl[sg_len++];
896 sg_dma_address(sg) = phys_addr;
897 sg_dma_len(sg) = part1_len;
898
899 phys_addr += part1_len;
900 }
901
902 if (part2_len) {
903 sg = &sgl[sg_len++];
904 sg_dma_address(sg) = phys_addr;
905 sg_dma_len(sg) = part2_len;
906 }
907
908 /*
909 * save tx_len so atmel_complete_tx_dma() will increase
910 * xmit->tail correctly
911 */
912 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800913
914 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200915 sgl,
916 sg_len,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100917 DMA_MEM_TO_DEV,
918 DMA_PREP_INTERRUPT |
919 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800920 if (!desc) {
921 dev_err(port->dev, "Failed to send via dma!\n");
922 return;
923 }
924
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200925 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800926
927 atmel_port->desc_tx = desc;
928 desc->callback = atmel_complete_tx_dma;
929 desc->callback_param = atmel_port;
930 atmel_port->cookie_tx = dmaengine_submit(desc);
Elen Song08f738b2013-07-22 16:30:26 +0800931 }
932
933 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
934 uart_write_wakeup(port);
935}
936
937static int atmel_prepare_tx_dma(struct uart_port *port)
938{
939 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
940 dma_cap_mask_t mask;
941 struct dma_slave_config config;
942 int ret, nent;
943
944 dma_cap_zero(mask);
945 dma_cap_set(DMA_SLAVE, mask);
946
947 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
948 if (atmel_port->chan_tx == NULL)
949 goto chan_err;
950 dev_info(port->dev, "using %s for tx DMA transfers\n",
951 dma_chan_name(atmel_port->chan_tx));
952
953 spin_lock_init(&atmel_port->lock_tx);
954 sg_init_table(&atmel_port->sg_tx, 1);
955 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800956 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800957 sg_set_page(&atmel_port->sg_tx,
958 virt_to_page(port->state->xmit.buf),
959 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200960 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800961 nent = dma_map_sg(port->dev,
962 &atmel_port->sg_tx,
963 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200964 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800965
966 if (!nent) {
967 dev_dbg(port->dev, "need to release resource of dma\n");
968 goto chan_err;
969 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200970 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800971 sg_dma_len(&atmel_port->sg_tx),
972 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200973 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800974 }
975
976 /* Configure the slave DMA */
977 memset(&config, 0, sizeof(config));
978 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200979 config.dst_addr_width = (atmel_port->fifo_size) ?
980 DMA_SLAVE_BUSWIDTH_4_BYTES :
981 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800982 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200983 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800984
Maxime Ripard5483c102014-10-22 17:43:16 +0200985 ret = dmaengine_slave_config(atmel_port->chan_tx,
986 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800987 if (ret) {
988 dev_err(port->dev, "DMA tx slave configuration failed\n");
989 goto chan_err;
990 }
991
992 return 0;
993
994chan_err:
995 dev_err(port->dev, "TX channel not available, switch to pio\n");
996 atmel_port->use_dma_tx = 0;
997 if (atmel_port->chan_tx)
998 atmel_release_tx_dma(port);
999 return -EINVAL;
1000}
1001
Elen Song34df42f2013-07-22 16:30:27 +08001002static void atmel_complete_rx_dma(void *arg)
1003{
1004 struct uart_port *port = arg;
1005 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1006
Nicolas Ferre98f20822016-06-26 09:44:49 +02001007 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001008}
1009
1010static void atmel_release_rx_dma(struct uart_port *port)
1011{
1012 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1013 struct dma_chan *chan = atmel_port->chan_rx;
1014
1015 if (chan) {
1016 dmaengine_terminate_all(chan);
1017 dma_release_channel(chan);
1018 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +02001019 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001020 }
1021
1022 atmel_port->desc_rx = NULL;
1023 atmel_port->chan_rx = NULL;
1024 atmel_port->cookie_rx = -EINVAL;
1025}
1026
1027static void atmel_rx_from_dma(struct uart_port *port)
1028{
1029 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001030 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +08001031 struct circ_buf *ring = &atmel_port->rx_ring;
1032 struct dma_chan *chan = atmel_port->chan_rx;
1033 struct dma_tx_state state;
1034 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001035 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001036
1037
1038 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001039 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001040 dmastat = dmaengine_tx_status(chan,
1041 atmel_port->cookie_rx,
1042 &state);
1043 /* Restart a new tasklet if DMA status is error */
1044 if (dmastat == DMA_ERROR) {
1045 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001046 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001047 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001048 return;
1049 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001050
1051 /* CPU claims ownership of RX DMA buffer */
1052 dma_sync_sg_for_cpu(port->dev,
1053 &atmel_port->sg_rx,
1054 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001055 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001056
1057 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001058 * ring->head points to the end of data already written by the DMA.
1059 * ring->tail points to the beginning of data to be read by the
1060 * framework.
1061 * The current transfer size should not be larger than the dma buffer
1062 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001063 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001064 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1065 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1066 /*
1067 * At this point ring->head may point to the first byte right after the
1068 * last byte of the dma buffer:
1069 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1070 *
1071 * However ring->tail must always points inside the dma buffer:
1072 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1073 *
1074 * Since we use a ring buffer, we have to handle the case
1075 * where head is lower than tail. In such a case, we first read from
1076 * tail to the end of the buffer then reset tail.
1077 */
1078 if (ring->head < ring->tail) {
1079 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001080
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001081 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1082 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001083 port->icount.rx += count;
1084 }
1085
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001086 /* Finally we read data from tail to head */
1087 if (ring->tail < ring->head) {
1088 count = ring->head - ring->tail;
1089
1090 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1091 /* Wrap ring->head if needed */
1092 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1093 ring->head = 0;
1094 ring->tail = ring->head;
1095 port->icount.rx += count;
1096 }
1097
1098 /* USART retreives ownership of RX DMA buffer */
1099 dma_sync_sg_for_device(port->dev,
1100 &atmel_port->sg_rx,
1101 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001102 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001103
1104 /*
1105 * Drop the lock here since it might end up calling
1106 * uart_start(), which takes the lock.
1107 */
1108 spin_unlock(&port->lock);
1109 tty_flip_buffer_push(tport);
1110 spin_lock(&port->lock);
1111
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001112 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001113}
1114
1115static int atmel_prepare_rx_dma(struct uart_port *port)
1116{
1117 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1118 struct dma_async_tx_descriptor *desc;
1119 dma_cap_mask_t mask;
1120 struct dma_slave_config config;
1121 struct circ_buf *ring;
1122 int ret, nent;
1123
1124 ring = &atmel_port->rx_ring;
1125
1126 dma_cap_zero(mask);
1127 dma_cap_set(DMA_CYCLIC, mask);
1128
1129 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1130 if (atmel_port->chan_rx == NULL)
1131 goto chan_err;
1132 dev_info(port->dev, "using %s for rx DMA transfers\n",
1133 dma_chan_name(atmel_port->chan_rx));
1134
1135 spin_lock_init(&atmel_port->lock_rx);
1136 sg_init_table(&atmel_port->sg_rx, 1);
1137 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001138 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001139 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001140 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001141 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001142 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001143 nent = dma_map_sg(port->dev,
1144 &atmel_port->sg_rx,
1145 1,
1146 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001147
1148 if (!nent) {
1149 dev_dbg(port->dev, "need to release resource of dma\n");
1150 goto chan_err;
1151 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001152 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001153 sg_dma_len(&atmel_port->sg_rx),
1154 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001155 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001156 }
1157
1158 /* Configure the slave DMA */
1159 memset(&config, 0, sizeof(config));
1160 config.direction = DMA_DEV_TO_MEM;
1161 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1162 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001163 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001164
Maxime Ripard5483c102014-10-22 17:43:16 +02001165 ret = dmaengine_slave_config(atmel_port->chan_rx,
1166 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001167 if (ret) {
1168 dev_err(port->dev, "DMA rx slave configuration failed\n");
1169 goto chan_err;
1170 }
1171 /*
1172 * Prepare a cyclic dma transfer, assign 2 descriptors,
1173 * each one is half ring buffer size
1174 */
1175 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001176 sg_dma_address(&atmel_port->sg_rx),
1177 sg_dma_len(&atmel_port->sg_rx),
1178 sg_dma_len(&atmel_port->sg_rx)/2,
1179 DMA_DEV_TO_MEM,
1180 DMA_PREP_INTERRUPT);
Kangjie Lufe186fd2019-03-15 12:16:06 -05001181 if (!desc) {
1182 dev_err(port->dev, "Preparing DMA cyclic failed\n");
1183 goto chan_err;
1184 }
Elen Song34df42f2013-07-22 16:30:27 +08001185 desc->callback = atmel_complete_rx_dma;
1186 desc->callback_param = port;
1187 atmel_port->desc_rx = desc;
1188 atmel_port->cookie_rx = dmaengine_submit(desc);
1189
1190 return 0;
1191
1192chan_err:
1193 dev_err(port->dev, "RX channel not available, switch to pio\n");
1194 atmel_port->use_dma_rx = 0;
1195 if (atmel_port->chan_rx)
1196 atmel_release_rx_dma(port);
1197 return -EINVAL;
1198}
1199
Elen Song2e68c222013-07-22 16:30:30 +08001200static void atmel_uart_timer_callback(unsigned long data)
1201{
1202 struct uart_port *port = (void *)data;
1203 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1204
Nicolas Ferre98f20822016-06-26 09:44:49 +02001205 if (!atomic_read(&atmel_port->tasklet_shutdown)) {
1206 tasklet_schedule(&atmel_port->tasklet_rx);
1207 mod_timer(&atmel_port->uart_timer,
1208 jiffies + uart_poll_timeout(port));
1209 }
Elen Song2e68c222013-07-22 16:30:30 +08001210}
1211
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001212/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001213 * receive interrupt handler.
1214 */
1215static void
1216atmel_handle_receive(struct uart_port *port, unsigned int pending)
1217{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001218 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001219
Elen Song64e22eb2013-07-22 16:30:24 +08001220 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001221 /*
1222 * PDC receive. Just schedule the tasklet and let it
1223 * figure out the details.
1224 *
1225 * TODO: We're not handling error flags correctly at
1226 * the moment.
1227 */
1228 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001229 atmel_uart_writel(port, ATMEL_US_IDR,
1230 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Nicolas Ferre98f20822016-06-26 09:44:49 +02001231 atmel_tasklet_schedule(atmel_port,
1232 &atmel_port->tasklet_rx);
Chip Coldwella6670612008-02-08 04:21:06 -08001233 }
1234
1235 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1236 ATMEL_US_FRAME | ATMEL_US_PARE))
1237 atmel_pdc_rxerr(port, pending);
1238 }
1239
Elen Song34df42f2013-07-22 16:30:27 +08001240 if (atmel_use_dma_rx(port)) {
1241 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001242 atmel_uart_writel(port, ATMEL_US_IDR,
1243 ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001244 atmel_tasklet_schedule(atmel_port,
1245 &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001246 }
1247 }
1248
Remy Bohmerb843aa22008-02-08 04:21:01 -08001249 /* Interrupt receive */
1250 if (pending & ATMEL_US_RXRDY)
1251 atmel_rx_chars(port);
1252 else if (pending & ATMEL_US_RXBRK) {
1253 /*
1254 * End of break detected. If it came along with a
1255 * character, atmel_rx_chars will handle it.
1256 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001257 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1258 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001259 atmel_port->break_active = 0;
1260 }
1261}
1262
1263/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001264 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001265 */
1266static void
1267atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1268{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001269 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001270
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001271 if (pending & atmel_port->tx_done_mask) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001272 atmel_uart_writel(port, ATMEL_US_IDR,
1273 atmel_port->tx_done_mask);
Razvan Stefanescu0668f0d2019-03-19 15:20:35 +02001274
1275 /* Start RX if flag was set and FIFO is empty */
1276 if (atmel_port->hd_start_rx) {
1277 if (!(atmel_uart_readl(port, ATMEL_US_CSR)
1278 & ATMEL_US_TXEMPTY))
1279 dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n");
1280
1281 atmel_port->hd_start_rx = false;
1282 atmel_start_rx(port);
Razvan Stefanescu0668f0d2019-03-19 15:20:35 +02001283 }
1284
Nicolas Ferre98f20822016-06-26 09:44:49 +02001285 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001286 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001287}
1288
1289/*
1290 * status flags interrupt handler.
1291 */
1292static void
1293atmel_handle_status(struct uart_port *port, unsigned int pending,
1294 unsigned int status)
1295{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001296 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Nicolas Ferre92052182016-06-17 12:05:46 +02001297 unsigned int status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001298
Remy Bohmerb843aa22008-02-08 04:21:01 -08001299 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001300 | ATMEL_US_CTSIC)) {
Nicolas Ferre92052182016-06-17 12:05:46 +02001301 status_change = status ^ atmel_port->irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +08001302 atmel_port->irq_status_prev = status;
Nicolas Ferre92052182016-06-17 12:05:46 +02001303
1304 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1305 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1306 /* TODO: All reads to CSR will clear these interrupts! */
1307 if (status_change & ATMEL_US_RI)
1308 port->icount.rng++;
1309 if (status_change & ATMEL_US_DSR)
1310 port->icount.dsr++;
1311 if (status_change & ATMEL_US_DCD)
1312 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1313 if (status_change & ATMEL_US_CTS)
1314 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1315
1316 wake_up_interruptible(&port->state->port.delta_msr_wait);
1317 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001318 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001319}
1320
1321/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001322 * Interrupt handler
1323 */
David Howells7d12e782006-10-05 14:55:46 +01001324static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001325{
1326 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001327 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001328 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001329
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001330 spin_lock(&atmel_port->lock_suspended);
1331
Chip Coldwella6670612008-02-08 04:21:06 -08001332 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001333 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001334 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001335 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001336 if (!pending)
1337 break;
1338
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001339 if (atmel_port->suspended) {
1340 atmel_port->pending |= pending;
1341 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001342 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001343 pm_system_wakeup();
1344 break;
1345 }
1346
Remy Bohmerb843aa22008-02-08 04:21:01 -08001347 atmel_handle_receive(port, pending);
1348 atmel_handle_status(port, pending, status);
1349 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001350 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001351
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001352 spin_unlock(&atmel_port->lock_suspended);
1353
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001354 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001355}
1356
Elen Songa930e522013-07-22 16:30:25 +08001357static void atmel_release_tx_pdc(struct uart_port *port)
1358{
1359 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1360 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1361
1362 dma_unmap_single(port->dev,
1363 pdc->dma_addr,
1364 pdc->dma_size,
1365 DMA_TO_DEVICE);
1366}
1367
Chip Coldwella6670612008-02-08 04:21:06 -08001368/*
1369 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1370 */
Elen Song64e22eb2013-07-22 16:30:24 +08001371static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001372{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001373 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001374 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001375 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1376 int count;
1377
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001378 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001379 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001380 return;
1381
Chip Coldwella6670612008-02-08 04:21:06 -08001382 xmit->tail += pdc->ofs;
1383 xmit->tail &= UART_XMIT_SIZE - 1;
1384
1385 port->icount.tx += pdc->ofs;
1386 pdc->ofs = 0;
1387
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001388 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001389
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001390 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001391 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001392
Itai Levi1f140812009-01-15 13:50:43 -08001393 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001394 dma_sync_single_for_device(port->dev,
1395 pdc->dma_addr,
1396 pdc->dma_size,
1397 DMA_TO_DEVICE);
1398
1399 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1400 pdc->ofs = count;
1401
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001402 atmel_uart_writel(port, ATMEL_PDC_TPR,
1403 pdc->dma_addr + xmit->tail);
1404 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001405 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001406 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001407 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001408 atmel_uart_writel(port, ATMEL_US_IER,
1409 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001410 } else {
Razvan Stefanescuab18aa02019-03-19 15:20:34 +02001411 if (atmel_uart_is_half_duplex(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001412 /* DMA done, stop TX, start RX for RS485 */
1413 atmel_start_rx(port);
1414 }
Chip Coldwella6670612008-02-08 04:21:06 -08001415 }
1416
1417 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1418 uart_write_wakeup(port);
1419}
1420
Elen Songa930e522013-07-22 16:30:25 +08001421static int atmel_prepare_tx_pdc(struct uart_port *port)
1422{
1423 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1424 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1425 struct circ_buf *xmit = &port->state->xmit;
1426
1427 pdc->buf = xmit->buf;
1428 pdc->dma_addr = dma_map_single(port->dev,
1429 pdc->buf,
1430 UART_XMIT_SIZE,
1431 DMA_TO_DEVICE);
1432 pdc->dma_size = UART_XMIT_SIZE;
1433 pdc->ofs = 0;
1434
1435 return 0;
1436}
1437
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001438static void atmel_rx_from_ring(struct uart_port *port)
1439{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001440 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001441 struct circ_buf *ring = &atmel_port->rx_ring;
1442 unsigned int flg;
1443 unsigned int status;
1444
1445 while (ring->head != ring->tail) {
1446 struct atmel_uart_char c;
1447
1448 /* Make sure c is loaded after head. */
1449 smp_rmb();
1450
1451 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1452
1453 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1454
1455 port->icount.rx++;
1456 status = c.status;
1457 flg = TTY_NORMAL;
1458
1459 /*
1460 * note that the error handling code is
1461 * out of the main execution path
1462 */
1463 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1464 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1465 if (status & ATMEL_US_RXBRK) {
1466 /* ignore side-effect */
1467 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1468
1469 port->icount.brk++;
1470 if (uart_handle_break(port))
1471 continue;
1472 }
1473 if (status & ATMEL_US_PARE)
1474 port->icount.parity++;
1475 if (status & ATMEL_US_FRAME)
1476 port->icount.frame++;
1477 if (status & ATMEL_US_OVRE)
1478 port->icount.overrun++;
1479
1480 status &= port->read_status_mask;
1481
1482 if (status & ATMEL_US_RXBRK)
1483 flg = TTY_BREAK;
1484 else if (status & ATMEL_US_PARE)
1485 flg = TTY_PARITY;
1486 else if (status & ATMEL_US_FRAME)
1487 flg = TTY_FRAME;
1488 }
1489
1490
1491 if (uart_handle_sysrq_char(port, c.ch))
1492 continue;
1493
1494 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1495 }
1496
1497 /*
1498 * Drop the lock here since it might end up calling
1499 * uart_start(), which takes the lock.
1500 */
1501 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001502 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001503 spin_lock(&port->lock);
1504}
1505
Elen Songa930e522013-07-22 16:30:25 +08001506static void atmel_release_rx_pdc(struct uart_port *port)
1507{
1508 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1509 int i;
1510
1511 for (i = 0; i < 2; i++) {
1512 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1513
1514 dma_unmap_single(port->dev,
1515 pdc->dma_addr,
1516 pdc->dma_size,
1517 DMA_FROM_DEVICE);
1518 kfree(pdc->buf);
1519 }
1520}
1521
Elen Song64e22eb2013-07-22 16:30:24 +08001522static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001523{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001524 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001525 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001526 struct atmel_dma_buffer *pdc;
1527 int rx_idx = atmel_port->pdc_rx_idx;
1528 unsigned int head;
1529 unsigned int tail;
1530 unsigned int count;
1531
1532 do {
1533 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001534 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001535
1536 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001537 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001538 tail = pdc->ofs;
1539
1540 /* If the PDC has switched buffers, RPR won't contain
1541 * any address within the current buffer. Since head
1542 * is unsigned, we just need a one-way comparison to
1543 * find out.
1544 *
1545 * In this case, we just need to consume the entire
1546 * buffer and resubmit it for DMA. This will clear the
1547 * ENDRX bit as well, so that we can safely re-enable
1548 * all interrupts below.
1549 */
1550 head = min(head, pdc->dma_size);
1551
1552 if (likely(head != tail)) {
1553 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1554 pdc->dma_size, DMA_FROM_DEVICE);
1555
1556 /*
1557 * head will only wrap around when we recycle
1558 * the DMA buffer, and when that happens, we
1559 * explicitly set tail to 0. So head will
1560 * always be greater than tail.
1561 */
1562 count = head - tail;
1563
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001564 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1565 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001566
1567 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1568 pdc->dma_size, DMA_FROM_DEVICE);
1569
1570 port->icount.rx += count;
1571 pdc->ofs = head;
1572 }
1573
1574 /*
1575 * If the current buffer is full, we need to check if
1576 * the next one contains any additional data.
1577 */
1578 if (head >= pdc->dma_size) {
1579 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001580 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1581 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001582
1583 rx_idx = !rx_idx;
1584 atmel_port->pdc_rx_idx = rx_idx;
1585 }
1586 } while (head >= pdc->dma_size);
1587
1588 /*
1589 * Drop the lock here since it might end up calling
1590 * uart_start(), which takes the lock.
1591 */
1592 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001593 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001594 spin_lock(&port->lock);
1595
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001596 atmel_uart_writel(port, ATMEL_US_IER,
1597 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001598}
1599
Elen Songa930e522013-07-22 16:30:25 +08001600static int atmel_prepare_rx_pdc(struct uart_port *port)
1601{
1602 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1603 int i;
1604
1605 for (i = 0; i < 2; i++) {
1606 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1607
1608 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1609 if (pdc->buf == NULL) {
1610 if (i != 0) {
1611 dma_unmap_single(port->dev,
1612 atmel_port->pdc_rx[0].dma_addr,
1613 PDC_BUFFER_SIZE,
1614 DMA_FROM_DEVICE);
1615 kfree(atmel_port->pdc_rx[0].buf);
1616 }
1617 atmel_port->use_pdc_rx = 0;
1618 return -ENOMEM;
1619 }
1620 pdc->dma_addr = dma_map_single(port->dev,
1621 pdc->buf,
1622 PDC_BUFFER_SIZE,
1623 DMA_FROM_DEVICE);
1624 pdc->dma_size = PDC_BUFFER_SIZE;
1625 pdc->ofs = 0;
1626 }
1627
1628 atmel_port->pdc_rx_idx = 0;
1629
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001630 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1631 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001632
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001633 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1634 atmel_port->pdc_rx[1].dma_addr);
1635 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001636
1637 return 0;
1638}
1639
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001640/*
1641 * tasklet handling tty stuff outside the interrupt handler.
1642 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001643static void atmel_tasklet_rx_func(unsigned long data)
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001644{
1645 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001646 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001647
1648 /* The interrupt handler does not take the lock */
1649 spin_lock(&port->lock);
Elen Songa930e522013-07-22 16:30:25 +08001650 atmel_port->schedule_rx(port);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001651 spin_unlock(&port->lock);
1652}
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001653
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001654static void atmel_tasklet_tx_func(unsigned long data)
1655{
1656 struct uart_port *port = (struct uart_port *)data;
1657 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1658
1659 /* The interrupt handler does not take the lock */
1660 spin_lock(&port->lock);
1661 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001662 spin_unlock(&port->lock);
1663}
1664
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001665static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001666 struct platform_device *pdev)
1667{
1668 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001669 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001670
1671 if (np) {
1672 /* DMA/PDC usage specification */
Julia Lawall490d5ce2016-08-05 10:56:45 +02001673 if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1674 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001675 atmel_port->use_dma_rx = true;
1676 atmel_port->use_pdc_rx = false;
1677 } else {
1678 atmel_port->use_dma_rx = false;
1679 atmel_port->use_pdc_rx = true;
1680 }
1681 } else {
1682 atmel_port->use_dma_rx = false;
1683 atmel_port->use_pdc_rx = false;
1684 }
1685
Julia Lawall490d5ce2016-08-05 10:56:45 +02001686 if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1687 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001688 atmel_port->use_dma_tx = true;
1689 atmel_port->use_pdc_tx = false;
1690 } else {
1691 atmel_port->use_dma_tx = false;
1692 atmel_port->use_pdc_tx = true;
1693 }
1694 } else {
1695 atmel_port->use_dma_tx = false;
1696 atmel_port->use_pdc_tx = false;
1697 }
1698
1699 } else {
1700 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1701 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1702 atmel_port->use_dma_rx = false;
1703 atmel_port->use_dma_tx = false;
1704 }
1705
Elen Song33d64c42013-07-22 16:30:28 +08001706}
1707
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001708static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001709 struct platform_device *pdev)
1710{
1711 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001712 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001713
1714 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001715 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001716 u32 rs485_delay[2];
1717 /* rs485 properties */
1718 if (of_property_read_u32_array(np, "rs485-rts-delay",
1719 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001720 rs485conf->delay_rts_before_send = rs485_delay[0];
1721 rs485conf->delay_rts_after_send = rs485_delay[1];
1722 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001723 }
Elen Song33d64c42013-07-22 16:30:28 +08001724
1725 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1726 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1727
1728 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1729 NULL))
1730 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001731 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001732 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001733 }
1734
1735}
1736
Elen Songa930e522013-07-22 16:30:25 +08001737static void atmel_set_ops(struct uart_port *port)
1738{
1739 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1740
Elen Song34df42f2013-07-22 16:30:27 +08001741 if (atmel_use_dma_rx(port)) {
1742 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1743 atmel_port->schedule_rx = &atmel_rx_from_dma;
1744 atmel_port->release_rx = &atmel_release_rx_dma;
1745 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001746 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1747 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1748 atmel_port->release_rx = &atmel_release_rx_pdc;
1749 } else {
1750 atmel_port->prepare_rx = NULL;
1751 atmel_port->schedule_rx = &atmel_rx_from_ring;
1752 atmel_port->release_rx = NULL;
1753 }
1754
Elen Song08f738b2013-07-22 16:30:26 +08001755 if (atmel_use_dma_tx(port)) {
1756 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1757 atmel_port->schedule_tx = &atmel_tx_dma;
1758 atmel_port->release_tx = &atmel_release_tx_dma;
1759 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001760 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1761 atmel_port->schedule_tx = &atmel_tx_pdc;
1762 atmel_port->release_tx = &atmel_release_tx_pdc;
1763 } else {
1764 atmel_port->prepare_tx = NULL;
1765 atmel_port->schedule_tx = &atmel_tx_chars;
1766 atmel_port->release_tx = NULL;
1767 }
1768}
1769
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001770/*
Elen Song055560b2013-07-22 16:30:29 +08001771 * Get ip name usart or uart
1772 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001773static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001774{
1775 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001776 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001777 u32 version;
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001778 u32 usart, dbgu_uart, new_uart;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001779 /* ASCII decoding for IP version */
1780 usart = 0x55534152; /* USAR(T) */
1781 dbgu_uart = 0x44424755; /* DBGU */
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001782 new_uart = 0x55415254; /* UART */
Elen Song055560b2013-07-22 16:30:29 +08001783
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001784 /*
1785 * Only USART devices from at91sam9260 SOC implement fractional
1786 * baudrate.
1787 */
1788 atmel_port->has_frac_baudrate = false;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001789 atmel_port->has_hw_timer = false;
Elen Song055560b2013-07-22 16:30:29 +08001790
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001791 if (name == new_uart) {
1792 dev_dbg(port->dev, "Uart with hw timer");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001793 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001794 atmel_port->rtor = ATMEL_UA_RTOR;
1795 } else if (name == usart) {
1796 dev_dbg(port->dev, "Usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001797 atmel_port->has_frac_baudrate = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001798 atmel_port->has_hw_timer = true;
1799 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001800 } else if (name == dbgu_uart) {
1801 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
Elen Song055560b2013-07-22 16:30:29 +08001802 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001803 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001804 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001805 switch (version) {
1806 case 0x302:
1807 case 0x10213:
Jonas Danielsson85afaf52018-01-29 12:39:15 +01001808 case 0x10302:
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001809 dev_dbg(port->dev, "This version is usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001810 atmel_port->has_frac_baudrate = true;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001811 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001812 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001813 break;
1814 case 0x203:
1815 case 0x10202:
1816 dev_dbg(port->dev, "This version is uart\n");
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001817 break;
1818 default:
1819 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1820 }
Elen Song055560b2013-07-22 16:30:29 +08001821 }
Elen Song055560b2013-07-22 16:30:29 +08001822}
1823
1824/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001825 * Perform initialization and enable port for reception
1826 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001827static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001828{
Elen Song33d64c42013-07-22 16:30:28 +08001829 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001830 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001831 int retval;
1832
1833 /*
1834 * Ensure that no interrupts are enabled otherwise when
1835 * request_irq() is called we could get stuck trying to
1836 * handle an unexpected interrupt
1837 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001838 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001839 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001840
1841 /*
1842 * Allocate the IRQ
1843 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001844 retval = request_irq(port->irq, atmel_interrupt,
Sebastian Andrzej Siewior41bdf972018-05-07 19:11:30 +02001845 IRQF_SHARED | IRQF_COND_SUSPEND,
1846 dev_name(&pdev->dev), port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001847 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001848 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001849 return retval;
1850 }
1851
Nicolas Ferre98f20822016-06-26 09:44:49 +02001852 atomic_set(&atmel_port->tasklet_shutdown, 0);
1853 tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
1854 (unsigned long)port);
1855 tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
1856 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08001857
Richard Genoudab5e4e42014-05-13 20:20:45 +02001858 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001859 * Initialize DMA (if necessary)
1860 */
Elen Song33d64c42013-07-22 16:30:28 +08001861 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001862 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001863
Elen Songa930e522013-07-22 16:30:25 +08001864 if (atmel_port->prepare_rx) {
1865 retval = atmel_port->prepare_rx(port);
1866 if (retval < 0)
1867 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001868 }
1869
Elen Songa930e522013-07-22 16:30:25 +08001870 if (atmel_port->prepare_tx) {
1871 retval = atmel_port->prepare_tx(port);
1872 if (retval < 0)
1873 atmel_set_ops(port);
1874 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001875
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001876 /*
1877 * Enable FIFO when available
1878 */
1879 if (atmel_port->fifo_size) {
1880 unsigned int txrdym = ATMEL_US_ONE_DATA;
1881 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1882 unsigned int fmr;
1883
1884 atmel_uart_writel(port, ATMEL_US_CR,
1885 ATMEL_US_FIFOEN |
1886 ATMEL_US_RXFCLR |
1887 ATMEL_US_TXFLCLR);
1888
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001889 if (atmel_use_dma_tx(port))
1890 txrdym = ATMEL_US_FOUR_DATA;
1891
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001892 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1893 if (atmel_port->rts_high &&
1894 atmel_port->rts_low)
1895 fmr |= ATMEL_US_FRTSC |
1896 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1897 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1898
1899 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1900 }
1901
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001902 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001903 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001904
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001905 /*
1906 * Finally, enable the serial port
1907 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001908 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001909 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001910 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001911
Marek Roszko8bc661b2014-01-10 10:33:11 +01001912 setup_timer(&atmel_port->uart_timer,
1913 atmel_uart_timer_callback,
1914 (unsigned long)port);
1915
Elen Song64e22eb2013-07-22 16:30:24 +08001916 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001917 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001918 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001919 mod_timer(&atmel_port->uart_timer,
1920 jiffies + uart_poll_timeout(port));
1921 /* set USART timeout */
1922 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001923 atmel_uart_writel(port, atmel_port->rtor,
1924 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001925 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001926
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001927 atmel_uart_writel(port, ATMEL_US_IER,
1928 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001929 }
Chip Coldwella6670612008-02-08 04:21:06 -08001930 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001931 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001932 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001933 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001934 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001935 mod_timer(&atmel_port->uart_timer,
1936 jiffies + uart_poll_timeout(port));
1937 /* set USART timeout */
1938 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001939 atmel_uart_writel(port, atmel_port->rtor,
1940 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001941 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001942
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001943 atmel_uart_writel(port, ATMEL_US_IER,
1944 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001945 }
Chip Coldwella6670612008-02-08 04:21:06 -08001946 } else {
1947 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001948 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001949 }
Andrew Victorafefc412006-06-19 19:53:19 +01001950
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951 return 0;
1952}
1953
1954/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001955 * Flush any TX data submitted for DMA. Called when the TX circular
1956 * buffer is reset.
1957 */
1958static void atmel_flush_buffer(struct uart_port *port)
1959{
1960 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1961
1962 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001963 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001964 atmel_port->pdc_tx.ofs = 0;
1965 }
Richard Genoude087ae62017-03-20 11:52:41 +01001966 /*
1967 * in uart_flush_buffer(), the xmit circular buffer has just
1968 * been cleared, so we have to reset tx_len accordingly.
1969 */
1970 atmel_port->tx_len = 0;
Peter Hurley479e9b92014-10-16 16:54:18 -04001971}
1972
1973/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001974 * Disable the port
1975 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001976static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001977{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001978 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001979
Richard Genoud0ae9fde2016-09-12 15:34:41 +02001980 /* Disable modem control lines interrupts */
1981 atmel_disable_ms(port);
1982
Nicolas Ferre98f20822016-06-26 09:44:49 +02001983 /* Disable interrupts at device level */
1984 atmel_uart_writel(port, ATMEL_US_IDR, -1);
1985
1986 /* Prevent spurious interrupts from scheduling the tasklet */
1987 atomic_inc(&atmel_port->tasklet_shutdown);
1988
Chip Coldwella6670612008-02-08 04:21:06 -08001989 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001990 * Prevent any tasklets being scheduled during
1991 * cleanup
1992 */
1993 del_timer_sync(&atmel_port->uart_timer);
1994
Nicolas Ferre98f20822016-06-26 09:44:49 +02001995 /* Make sure that no interrupt is on the fly */
1996 synchronize_irq(port->irq);
1997
Marek Roszko8bc661b2014-01-10 10:33:11 +01001998 /*
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001999 * Clear out any scheduled tasklets before
2000 * we destroy the buffers
2001 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002002 tasklet_kill(&atmel_port->tasklet_rx);
2003 tasklet_kill(&atmel_port->tasklet_tx);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01002004
2005 /*
2006 * Ensure everything is stopped and
Nicolas Ferre98f20822016-06-26 09:44:49 +02002007 * disable port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08002008 */
2009 atmel_stop_rx(port);
2010 atmel_stop_tx(port);
2011
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002012 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01002013
Chip Coldwella6670612008-02-08 04:21:06 -08002014 /*
2015 * Shut-down the DMA.
2016 */
Elen Songa930e522013-07-22 16:30:25 +08002017 if (atmel_port->release_rx)
2018 atmel_port->release_rx(port);
2019 if (atmel_port->release_tx)
2020 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08002021
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002022 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01002023 * Reset ring buffer pointers
2024 */
2025 atmel_port->rx_ring.head = 0;
2026 atmel_port->rx_ring.tail = 0;
2027
2028 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02002029 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002030 */
2031 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02002032
Peter Hurley479e9b92014-10-16 16:54:18 -04002033 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002034}
2035
2036/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002037 * Power / Clock management.
2038 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002039static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2040 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002041{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002042 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002043
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002044 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002045 case 0:
2046 /*
2047 * Enable the peripheral clock for this serial port.
2048 * This is called on uart_open() or a resume event.
2049 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002050 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07002051
2052 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002053 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002054 break;
2055 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07002056 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002057 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2058 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07002059
Remy Bohmerb843aa22008-02-08 04:21:01 -08002060 /*
2061 * Disable the peripheral clock for this serial port.
2062 * This is called on uart_close() or a suspend event.
2063 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002064 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002065 break;
2066 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01002067 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002068 }
2069}
2070
2071/*
2072 * Change the port parameters
2073 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002074static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2075 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002076{
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002077 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002078 unsigned long flags;
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002079 unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002080
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002081 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002082 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002083
2084 /* reset the mode, clock divisor, parity, stop bits and data size */
2085 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2086 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002087
Remy Bohmerb843aa22008-02-08 04:21:01 -08002088 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002089
2090 /* byte size */
2091 switch (termios->c_cflag & CSIZE) {
2092 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002093 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002094 break;
2095 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002096 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002097 break;
2098 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002099 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002100 break;
2101 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002102 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002103 break;
2104 }
2105
2106 /* stop bits */
2107 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002108 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002109
2110 /* parity */
2111 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002112 /* Mark or Space parity */
2113 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002114 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002115 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002116 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002117 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002118 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002119 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002120 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002121 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002122 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002123 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002124
2125 spin_lock_irqsave(&port->lock, flags);
2126
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002127 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002128 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002129 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002130 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002131 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002132
Elen Song64e22eb2013-07-22 16:30:24 +08002133 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002134 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002135 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002136
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002137 /*
2138 * Characters to ignore
2139 */
2140 port->ignore_status_mask = 0;
2141 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002142 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002143 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002144 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002145 /*
2146 * If we're ignoring parity and break indicators,
2147 * ignore overruns too (for real raw support).
2148 */
2149 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002150 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002151 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002152 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002153
2154 /* update the per-port timeout */
2155 uart_update_timeout(port, termios->c_cflag, baud);
2156
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002157 /*
2158 * save/disable interrupts. The tty layer will ensure that the
2159 * transmitter is empty if requested by the caller, so there's
2160 * no need to wait for it here.
2161 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002162 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2163 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002164
2165 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002166 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002167
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002168 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002169 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002170 atmel_uart_writel(port, ATMEL_US_TTGR,
2171 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002172 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002173 } else if (termios->c_cflag & CRTSCTS) {
2174 /* RS232 with hardware handshake (RTS/CTS) */
Richard Genoud9bcffe72016-10-27 18:04:06 +02002175 if (atmel_use_fifo(port) &&
2176 !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
2177 /*
2178 * with ATMEL_US_USMODE_HWHS set, the controller will
2179 * be able to drive the RTS pin high/low when the RX
2180 * FIFO is above RXFTHRES/below RXFTHRES2.
2181 * It will also disable the transmitter when the CTS
2182 * pin is high.
2183 * This mode is not activated if CTS pin is a GPIO
2184 * because in this case, the transmitter is always
2185 * disabled (there must be an internal pull-up
2186 * responsible for this behaviour).
2187 * If the RTS pin is a GPIO, the controller won't be
2188 * able to drive it according to the FIFO thresholds,
2189 * but it will be handled by the driver.
2190 */
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002191 mode |= ATMEL_US_USMODE_HWHS;
Richard Genoud9bcffe72016-10-27 18:04:06 +02002192 } else {
2193 /*
2194 * For platforms without FIFO, the flow control is
2195 * handled by the driver.
2196 */
2197 mode |= ATMEL_US_USMODE_NORMAL;
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002198 }
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002199 } else {
2200 /* RS232 without hadware handshake */
2201 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002202 }
2203
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002204 /*
2205 * Set the baud rate:
2206 * Fractional baudrate allows to setup output frequency more
2207 * accurately. This feature is enabled only when using normal mode.
2208 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
2209 * Currently, OVER is always set to 0 so we get
Alexey Starikovskiy36131cd2016-09-21 12:44:14 +02002210 * baudrate = selected clock / (16 * (CD + FP / 8))
2211 * then
2212 * 8 CD + FP = selected clock / (2 * baudrate)
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002213 */
2214 if (atmel_port->has_frac_baudrate &&
2215 (mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_NORMAL) {
Alexey Starikovskiy36131cd2016-09-21 12:44:14 +02002216 div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
2217 cd = div >> 3;
2218 fp = div & ATMEL_US_FP_MASK;
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002219 } else {
2220 cd = uart_get_divisor(port, baud);
2221 }
2222
2223 if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */
2224 cd /= 8;
2225 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2226 }
2227 quot = cd | fp << ATMEL_US_FP_OFFSET;
2228
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002229 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
David Engrafedad0c22019-12-16 09:54:03 +01002230
2231 /* set the mode, clock divisor, parity, stop bits and data size */
2232 atmel_uart_writel(port, ATMEL_US_MR, mode);
2233
2234 /*
2235 * when switching the mode, set the RTS line state according to the
2236 * new mode, otherwise keep the former state
2237 */
2238 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2239 unsigned int rts_state;
2240
2241 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2242 /* let the hardware control the RTS line */
2243 rts_state = ATMEL_US_RTSDIS;
2244 } else {
2245 /* force RTS line to low level */
2246 rts_state = ATMEL_US_RTSEN;
2247 }
2248
2249 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
2250 }
2251
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002252 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2253 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002254
2255 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002256 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002257
2258 /* CTS flow-control and modem-status interrupts */
2259 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002260 atmel_enable_ms(port);
2261 else
2262 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002263
2264 spin_unlock_irqrestore(&port->lock, flags);
2265}
2266
Peter Hurley732a84a2014-11-05 13:11:43 -05002267static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002268{
Peter Hurley732a84a2014-11-05 13:11:43 -05002269 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002270 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002271 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002272 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002273 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002274 } else {
2275 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002276 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2277 spin_lock_irq(&port->lock);
2278 atmel_disable_ms(port);
2279 spin_unlock_irq(&port->lock);
2280 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002281 }
2282}
2283
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002284/*
2285 * Return string describing the specified port
2286 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002287static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002288{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002289 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002290}
2291
2292/*
2293 * Release the memory region(s) being used by 'port'.
2294 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002295static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002296{
Andrew Victorafefc412006-06-19 19:53:19 +01002297 struct platform_device *pdev = to_platform_device(port->dev);
2298 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2299
2300 release_mem_region(port->mapbase, size);
2301
2302 if (port->flags & UPF_IOREMAP) {
2303 iounmap(port->membase);
2304 port->membase = NULL;
2305 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002306}
2307
2308/*
2309 * Request the memory region(s) being used by 'port'.
2310 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002311static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002312{
Andrew Victorafefc412006-06-19 19:53:19 +01002313 struct platform_device *pdev = to_platform_device(port->dev);
2314 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002315
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002316 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002317 return -EBUSY;
2318
2319 if (port->flags & UPF_IOREMAP) {
2320 port->membase = ioremap(port->mapbase, size);
2321 if (port->membase == NULL) {
2322 release_mem_region(port->mapbase, size);
2323 return -ENOMEM;
2324 }
2325 }
2326
2327 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002328}
2329
2330/*
2331 * Configure/autoconfigure the port.
2332 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002333static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002334{
2335 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002336 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002337 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002338 }
2339}
2340
2341/*
2342 * Verify the new serial_struct (for TIOCSSERIAL).
2343 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002344static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002345{
2346 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002347 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002348 ret = -EINVAL;
2349 if (port->irq != ser->irq)
2350 ret = -EINVAL;
2351 if (ser->io_type != SERIAL_IO_MEM)
2352 ret = -EINVAL;
2353 if (port->uartclk / 16 != ser->baud_base)
2354 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002355 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002356 ret = -EINVAL;
2357 if (port->iobase != ser->port)
2358 ret = -EINVAL;
2359 if (ser->hub6 != 0)
2360 ret = -EINVAL;
2361 return ret;
2362}
2363
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002364#ifdef CONFIG_CONSOLE_POLL
2365static int atmel_poll_get_char(struct uart_port *port)
2366{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002367 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002368 cpu_relax();
2369
Cyrille Pitchena6499432015-07-30 16:33:38 +02002370 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002371}
2372
2373static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2374{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002375 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002376 cpu_relax();
2377
Cyrille Pitchena6499432015-07-30 16:33:38 +02002378 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002379}
2380#endif
2381
Julia Lawall5c7dcdb2016-09-01 19:51:31 +02002382static const struct uart_ops atmel_pops = {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002383 .tx_empty = atmel_tx_empty,
2384 .set_mctrl = atmel_set_mctrl,
2385 .get_mctrl = atmel_get_mctrl,
2386 .stop_tx = atmel_stop_tx,
2387 .start_tx = atmel_start_tx,
2388 .stop_rx = atmel_stop_rx,
2389 .enable_ms = atmel_enable_ms,
2390 .break_ctl = atmel_break_ctl,
2391 .startup = atmel_startup,
2392 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002393 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002394 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002395 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002396 .type = atmel_type,
2397 .release_port = atmel_release_port,
2398 .request_port = atmel_request_port,
2399 .config_port = atmel_config_port,
2400 .verify_port = atmel_verify_port,
2401 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002402#ifdef CONFIG_CONSOLE_POLL
2403 .poll_get_char = atmel_poll_get_char,
2404 .poll_put_char = atmel_poll_put_char,
2405#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002406};
2407
Andrew Victorafefc412006-06-19 19:53:19 +01002408/*
2409 * Configure the port from the platform device resource info.
2410 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002411static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002412 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002413{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002414 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002415 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002416 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002417
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002418 atmel_init_property(atmel_port, pdev);
2419 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002420
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002421 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002422
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002423 port->iotype = UPIO_MEM;
2424 port->flags = UPF_BOOT_AUTOCONF;
2425 port->ops = &atmel_pops;
2426 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002427 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002428 port->mapbase = pdev->resource[0].start;
2429 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002430 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002431
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002432 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2433
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002434 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002435 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002436 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002437 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002438 port->flags |= UPF_IOREMAP;
2439 port->membase = NULL;
2440 }
2441
Remy Bohmerb843aa22008-02-08 04:21:01 -08002442 /* for console, the clock could already be configured */
2443 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002444 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002445 if (IS_ERR(atmel_port->clk)) {
2446 ret = PTR_ERR(atmel_port->clk);
2447 atmel_port->clk = NULL;
2448 return ret;
2449 }
2450 ret = clk_prepare_enable(atmel_port->clk);
2451 if (ret) {
2452 clk_put(atmel_port->clk);
2453 atmel_port->clk = NULL;
2454 return ret;
2455 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002456 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002457 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002458 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002459 }
Chip Coldwella6670612008-02-08 04:21:06 -08002460
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002461 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002462 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002463 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002464 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002465 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002466 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2467 } else {
2468 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2469 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002470
2471 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002472}
2473
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002474struct platform_device *atmel_default_console_device; /* the serial console device */
2475
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002476#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002477static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002478{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002479 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002480 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002481 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002482}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002483
2484/*
2485 * Interrupts are disabled on entering
2486 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002487static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002488{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002489 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002490 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002491 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002492 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002493
2494 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002495 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002496 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002497 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2498 atmel_uart_writel(port, ATMEL_US_IDR,
2499 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002500
Marc Pignat39d4c922008-04-02 13:04:42 -07002501 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002502 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2503 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002504
Nicolas Ferre1f1c9e22017-03-20 16:38:57 +01002505 /* Make sure that tx path is actually able to send characters */
2506 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2507
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002508 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002509
2510 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002511 * Finally, wait for transmitter to become empty
2512 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002513 */
2514 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002515 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002516 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002517
2518 /* Restore PDC transmit status */
2519 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002520 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002521
Remy Bohmerb843aa22008-02-08 04:21:01 -08002522 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002523 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002524}
2525
2526/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002527 * If the port was already initialised (eg, by a boot loader),
2528 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002529 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002530static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2531 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002532{
2533 unsigned int mr, quot;
2534
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002535 /*
2536 * If the baud rate generator isn't running, the port wasn't
2537 * initialized by the boot loader.
2538 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002539 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002540 if (!quot)
2541 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002542
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002543 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002544 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002545 *bits = 8;
2546 else
2547 *bits = 7;
2548
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002549 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002550 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002551 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002552 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002553 *parity = 'o';
2554
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002555 /*
2556 * The serial core only rounds down when matching this to a
2557 * supported baud rate. Make sure we don't end up slightly
2558 * lower than one of those, as it would make us fall through
2559 * to a much lower baud rate than we really want.
2560 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002561 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002562}
2563
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002564static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002565{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002566 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002567 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002568 int baud = 115200;
2569 int bits = 8;
2570 int parity = 'n';
2571 int flow = 'n';
2572
Remy Bohmerb843aa22008-02-08 04:21:01 -08002573 if (port->membase == NULL) {
2574 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002575 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002576 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002577
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002578 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2579 if (ret)
2580 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002581
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002582 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2583 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2584 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002585
2586 if (options)
2587 uart_parse_options(options, &baud, &parity, &bits, &flow);
2588 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002589 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002590
2591 return uart_set_options(port, co, baud, parity, bits, flow);
2592}
2593
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002594static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002595
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002596static struct console atmel_console = {
2597 .name = ATMEL_DEVICENAME,
2598 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002599 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002600 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002601 .flags = CON_PRINTBUFFER,
2602 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002603 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002604};
2605
David Brownell06a7f052008-11-06 12:53:40 -08002606#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002607
Andrew Victorafefc412006-06-19 19:53:19 +01002608/*
2609 * Early console initialization (before VM subsystem initialized).
2610 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002611static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002612{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002613 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002614 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002615 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002616 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002617 int id = pdata->num;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002618 struct atmel_uart_port *atmel_port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002619
Jaeden Amerob78cd162016-01-26 12:34:49 +01002620 atmel_port->backup_imr = 0;
2621 atmel_port->uart.line = id;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002622
2623 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002624 ret = atmel_init_port(atmel_port, atmel_default_console_device);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002625 if (ret)
2626 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002627 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002628 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002629
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002630 return 0;
2631}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002632
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002633console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002634
Andrew Victorafefc412006-06-19 19:53:19 +01002635/*
2636 * Late console initialization.
2637 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002638static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002639{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002640 if (atmel_default_console_device
2641 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002642 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002643
2644 return 0;
2645}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002646
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002647core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002648
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002649static inline bool atmel_is_console_port(struct uart_port *port)
2650{
2651 return port->cons && port->cons->index == port->line;
2652}
2653
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002654#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002655#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002656
2657static inline bool atmel_is_console_port(struct uart_port *port)
2658{
2659 return false;
2660}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002661#endif
2662
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002663static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002664 .owner = THIS_MODULE,
2665 .driver_name = "atmel_serial",
2666 .dev_name = ATMEL_DEVICENAME,
2667 .major = SERIAL_ATMEL_MAJOR,
2668 .minor = MINOR_START,
2669 .nr = ATMEL_MAX_UART,
2670 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002671};
2672
Andrew Victorafefc412006-06-19 19:53:19 +01002673#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002674static bool atmel_serial_clk_will_stop(void)
2675{
2676#ifdef CONFIG_ARCH_AT91
2677 return at91_suspend_entering_slow_clock();
2678#else
2679 return false;
2680#endif
2681}
2682
Remy Bohmerb843aa22008-02-08 04:21:01 -08002683static int atmel_serial_suspend(struct platform_device *pdev,
2684 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002685{
Andrew Victorafefc412006-06-19 19:53:19 +01002686 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002687 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002688
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002689 if (atmel_is_console_port(port) && console_suspend_enabled) {
2690 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002691 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2692 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002693 cpu_relax();
2694 }
2695
Anti Sullinf05596d2008-09-22 13:57:54 -07002696 /* we can not wake up if we're running on slow clock */
2697 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002698 if (atmel_serial_clk_will_stop()) {
2699 unsigned long flags;
2700
2701 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2702 atmel_port->suspended = true;
2703 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002704 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002705 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002706
2707 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002708
2709 return 0;
2710}
2711
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002712static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002713{
2714 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002715 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002716 unsigned long flags;
2717
2718 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2719 if (atmel_port->pending) {
2720 atmel_handle_receive(port, atmel_port->pending);
2721 atmel_handle_status(port, atmel_port->pending,
2722 atmel_port->pending_status);
2723 atmel_handle_transmit(port, atmel_port->pending);
2724 atmel_port->pending = 0;
2725 }
2726 atmel_port->suspended = false;
2727 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002728
Anti Sullinf05596d2008-09-22 13:57:54 -07002729 uart_resume_port(&atmel_uart, port);
2730 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002731
2732 return 0;
2733}
2734#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002735#define atmel_serial_suspend NULL
2736#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002737#endif
2738
Jaeden Amerob78cd162016-01-26 12:34:49 +01002739static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002740 struct platform_device *pdev)
2741{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002742 atmel_port->fifo_size = 0;
2743 atmel_port->rts_low = 0;
2744 atmel_port->rts_high = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002745
2746 if (of_property_read_u32(pdev->dev.of_node,
2747 "atmel,fifo-size",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002748 &atmel_port->fifo_size))
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002749 return;
2750
Jaeden Amerob78cd162016-01-26 12:34:49 +01002751 if (!atmel_port->fifo_size)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002752 return;
2753
Jaeden Amerob78cd162016-01-26 12:34:49 +01002754 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2755 atmel_port->fifo_size = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002756 dev_err(&pdev->dev, "Invalid FIFO size\n");
2757 return;
2758 }
2759
2760 /*
2761 * 0 <= rts_low <= rts_high <= fifo_size
2762 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2763 * to flush their internal TX FIFO, commonly up to 16 data, before
2764 * actually stopping to send new data. So we try to set the RTS High
2765 * Threshold to a reasonably high value respecting this 16 data
2766 * empirical rule when possible.
2767 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002768 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2769 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2770 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2,
2771 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002772
2773 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002774 atmel_port->fifo_size);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002775 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002776 atmel_port->rts_high);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002777 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002778 atmel_port->rts_low);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002779}
2780
Bill Pemberton9671f092012-11-19 13:21:50 -05002781static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002782{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002783 struct atmel_uart_port *atmel_port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002784 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002785 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002786 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002787 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002788 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002789
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002790 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002791
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002792 if (np)
2793 ret = of_alias_get_id(np, "serial");
2794 else
2795 if (pdata)
2796 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002797
2798 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002799 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002800 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002801 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002802
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002803 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002804 ret = -ENODEV;
2805 goto err;
2806 }
2807
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002808 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002809 /* port already in use */
2810 ret = -EBUSY;
2811 goto err;
2812 }
2813
Jaeden Amerob78cd162016-01-26 12:34:49 +01002814 atmel_port = &atmel_ports[ret];
2815 atmel_port->backup_imr = 0;
2816 atmel_port->uart.line = ret;
2817 atmel_serial_probe_fifos(atmel_port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002818
Nicolas Ferre98f20822016-06-26 09:44:49 +02002819 atomic_set(&atmel_port->tasklet_shutdown, 0);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002820 spin_lock_init(&atmel_port->lock_suspended);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002821
Jaeden Amerob78cd162016-01-26 12:34:49 +01002822 ret = atmel_init_port(atmel_port, pdev);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002823 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002824 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002825
Jaeden Amerob78cd162016-01-26 12:34:49 +01002826 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2827 if (IS_ERR(atmel_port->gpios)) {
2828 ret = PTR_ERR(atmel_port->gpios);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002829 goto err_clear_bit;
2830 }
2831
Jaeden Amerob78cd162016-01-26 12:34:49 +01002832 if (!atmel_use_pdc_rx(&atmel_port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002833 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002834 data = kmalloc(sizeof(struct atmel_uart_char)
2835 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002836 if (!data)
2837 goto err_alloc_ring;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002838 atmel_port->rx_ring.buf = data;
Chip Coldwella6670612008-02-08 04:21:06 -08002839 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002840
Jaeden Amerob78cd162016-01-26 12:34:49 +01002841 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002842
Jaeden Amerob78cd162016-01-26 12:34:49 +01002843 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002844 if (ret)
2845 goto err_add_port;
2846
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002847#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Jaeden Amerob78cd162016-01-26 12:34:49 +01002848 if (atmel_is_console_port(&atmel_port->uart)
David Brownell06a7f052008-11-06 12:53:40 -08002849 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2850 /*
2851 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002852 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002853 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002854 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002855 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002856#endif
David Brownell06a7f052008-11-06 12:53:40 -08002857
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002858 device_init_wakeup(&pdev->dev, 1);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002859 platform_set_drvdata(pdev, atmel_port);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002860
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002861 /*
2862 * The peripheral clock has been disabled by atmel_init_port():
2863 * enable it before accessing I/O registers
2864 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002865 clk_prepare_enable(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002866
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002867 if (rs485_enabled) {
Jaeden Amerob78cd162016-01-26 12:34:49 +01002868 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002869 ATMEL_US_USMODE_NORMAL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002870 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2871 ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002872 }
2873
Elen Song055560b2013-07-22 16:30:29 +08002874 /*
2875 * Get port name of usart or uart
2876 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002877 atmel_get_ip_name(&atmel_port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002878
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002879 /*
2880 * The peripheral clock can now safely be disabled till the port
2881 * is used
2882 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002883 clk_disable_unprepare(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002884
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002885 return 0;
2886
2887err_add_port:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002888 kfree(atmel_port->rx_ring.buf);
2889 atmel_port->rx_ring.buf = NULL;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002890err_alloc_ring:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002891 if (!atmel_is_console_port(&atmel_port->uart)) {
2892 clk_put(atmel_port->clk);
2893 atmel_port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002894 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002895err_clear_bit:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002896 clear_bit(atmel_port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002897err:
Andrew Victorafefc412006-06-19 19:53:19 +01002898 return ret;
2899}
2900
Romain Izardf4a8ab042016-02-26 11:15:04 +01002901/*
2902 * Even if the driver is not modular, it makes sense to be able to
2903 * unbind a device: there can be many bound devices, and there are
2904 * situations where dynamic binding and unbinding can be useful.
2905 *
2906 * For example, a connected device can require a specific firmware update
2907 * protocol that needs bitbanging on IO lines, but use the regular serial
2908 * port in the normal case.
2909 */
2910static int atmel_serial_remove(struct platform_device *pdev)
2911{
2912 struct uart_port *port = platform_get_drvdata(pdev);
2913 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2914 int ret = 0;
2915
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002916 tasklet_kill(&atmel_port->tasklet_rx);
2917 tasklet_kill(&atmel_port->tasklet_tx);
Romain Izardf4a8ab042016-02-26 11:15:04 +01002918
2919 device_init_wakeup(&pdev->dev, 0);
2920
2921 ret = uart_remove_one_port(&atmel_uart, port);
2922
2923 kfree(atmel_port->rx_ring.buf);
2924
2925 /* "port" is allocated statically, so we shouldn't free it */
2926
2927 clear_bit(port->line, atmel_ports_in_use);
2928
2929 clk_put(atmel_port->clk);
2930 atmel_port->clk = NULL;
2931
2932 return ret;
2933}
2934
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002935static struct platform_driver atmel_serial_driver = {
2936 .probe = atmel_serial_probe,
Romain Izardf4a8ab042016-02-26 11:15:04 +01002937 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002938 .suspend = atmel_serial_suspend,
2939 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002940 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002941 .name = "atmel_usart",
2942 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002943 },
2944};
2945
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002946static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002947{
2948 int ret;
2949
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002950 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002951 if (ret)
2952 return ret;
2953
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002954 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002955 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002956 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002957
2958 return ret;
2959}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002960device_initcall(atmel_serial_init);