blob: 72064b5e4f36d9b7fe279b13f32f613b7c13861d [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
Elen Songa930e522013-07-22 16:30:25 +0800178 int (*prepare_rx)(struct uart_port *port);
179 int (*prepare_tx)(struct uart_port *port);
180 void (*schedule_rx)(struct uart_port *port);
181 void (*schedule_tx)(struct uart_port *port);
182 void (*release_rx)(struct uart_port *port);
183 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100184};
185
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200186static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100187static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100188
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000189#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200190static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000191#endif
192
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200193#if defined(CONFIG_OF)
194static const struct of_device_id atmel_serial_dt_ids[] = {
195 { .compatible = "atmel,at91rm9200-usart" },
196 { .compatible = "atmel,at91sam9260-usart" },
197 { /* sentinel */ }
198};
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200199#endif
200
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800201static inline struct atmel_uart_port *
202to_atmel_uart_port(struct uart_port *uart)
203{
204 return container_of(uart, struct atmel_uart_port, uart);
205}
206
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200207static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
208{
209 return __raw_readl(port->membase + reg);
210}
211
212static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
213{
214 __raw_writel(value, port->membase + reg);
215}
216
Cyrille Pitchena6499432015-07-30 16:33:38 +0200217#ifdef CONFIG_AVR32
218
219/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
220static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200221{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200222 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200223}
224
Cyrille Pitchena6499432015-07-30 16:33:38 +0200225static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200226{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200227 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200228}
229
Cyrille Pitchena6499432015-07-30 16:33:38 +0200230#else
231
232static inline u8 atmel_uart_read_char(struct uart_port *port)
233{
234 return __raw_readb(port->membase + ATMEL_US_RHR);
235}
236
237static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
238{
239 __raw_writeb(value, port->membase + ATMEL_US_THR);
240}
241
242#endif
243
Chip Coldwella6670612008-02-08 04:21:06 -0800244#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800245static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800246{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800247 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800248
Elen Song64e22eb2013-07-22 16:30:24 +0800249 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800250}
251
Elen Song64e22eb2013-07-22 16:30:24 +0800252static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800253{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800254 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800255
Elen Song64e22eb2013-07-22 16:30:24 +0800256 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800257}
258#else
Elen Song64e22eb2013-07-22 16:30:24 +0800259static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800260{
261 return false;
262}
263
Elen Song64e22eb2013-07-22 16:30:24 +0800264static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800265{
266 return false;
267}
268#endif
269
Elen Song08f738b2013-07-22 16:30:26 +0800270static bool atmel_use_dma_tx(struct uart_port *port)
271{
272 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
273
274 return atmel_port->use_dma_tx;
275}
276
Elen Song34df42f2013-07-22 16:30:27 +0800277static bool atmel_use_dma_rx(struct uart_port *port)
278{
279 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
280
281 return atmel_port->use_dma_rx;
282}
283
Alexandre Belloni5be605a2016-04-12 14:51:40 +0200284static bool atmel_use_fifo(struct uart_port *port)
285{
286 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
287
288 return atmel_port->fifo_size;
289}
290
Nicolas Ferre98f20822016-06-26 09:44:49 +0200291static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
292 struct tasklet_struct *t)
293{
294 if (!atomic_read(&atmel_port->tasklet_shutdown))
295 tasklet_schedule(t);
296}
297
Richard Genoude0b0baa2014-05-13 20:20:44 +0200298static unsigned int atmel_get_lines_status(struct uart_port *port)
299{
300 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
301 unsigned int status, ret = 0;
302
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200303 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200304
305 mctrl_gpio_get(atmel_port->gpios, &ret);
306
307 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
308 UART_GPIO_CTS))) {
309 if (ret & TIOCM_CTS)
310 status &= ~ATMEL_US_CTS;
311 else
312 status |= ATMEL_US_CTS;
313 }
314
315 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
316 UART_GPIO_DSR))) {
317 if (ret & TIOCM_DSR)
318 status &= ~ATMEL_US_DSR;
319 else
320 status |= ATMEL_US_DSR;
321 }
322
323 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
324 UART_GPIO_RI))) {
325 if (ret & TIOCM_RI)
326 status &= ~ATMEL_US_RI;
327 else
328 status |= ATMEL_US_RI;
329 }
330
331 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
332 UART_GPIO_DCD))) {
333 if (ret & TIOCM_CD)
334 status &= ~ATMEL_US_DCD;
335 else
336 status |= ATMEL_US_DCD;
337 }
338
339 return status;
340}
341
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100342/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100343static int atmel_config_rs485(struct uart_port *port,
344 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100345{
346 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
347 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100348
349 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200350 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100351
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200352 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100353
354 /* Resetting serial mode to RS232 (0x0) */
355 mode &= ~ATMEL_US_USMODE;
356
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100357 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100358
359 if (rs485conf->flags & SER_RS485_ENABLED) {
360 dev_dbg(port->dev, "Setting UART to RS485\n");
361 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200362 atmel_uart_writel(port, ATMEL_US_TTGR,
363 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100364 mode |= ATMEL_US_USMODE_RS485;
365 } else {
366 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800367 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100368 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
369 ATMEL_US_TXBUFE;
370 else
371 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
372 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200373 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100374
375 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200376 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100377
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100378 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100379}
380
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000381/*
382 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
383 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200384static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000385{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200386 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
387 TIOCSER_TEMT :
388 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000389}
390
391/*
392 * Set state of the modem control output lines
393 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200394static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000395{
396 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200397 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100398 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100399 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000400
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100401 /* override mode to RS485 if needed, otherwise keep the current mode */
402 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200403 atmel_uart_writel(port, ATMEL_US_TTGR,
404 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100405 mode &= ~ATMEL_US_USMODE;
406 mode |= ATMEL_US_USMODE_RS485;
407 }
408
409 /* set the RTS line state according to the mode */
410 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
411 /* force RTS line to high level */
412 rts_paused = ATMEL_US_RTSEN;
413
414 /* give the control of the RTS line back to the hardware */
415 rts_ready = ATMEL_US_RTSDIS;
416 } else {
417 /* force RTS line to high level */
418 rts_paused = ATMEL_US_RTSDIS;
419
420 /* force RTS line to low level */
421 rts_ready = ATMEL_US_RTSEN;
422 }
423
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000424 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100425 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000426 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100427 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000428
429 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200430 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000431 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200432 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000433
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200434 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100435
Richard Genoude0b0baa2014-05-13 20:20:44 +0200436 mctrl_gpio_set(atmel_port->gpios, mctrl);
437
Andrew Victorafefc412006-06-19 19:53:19 +0100438 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100439 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100440 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200441 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100442 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200443 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100444
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200445 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000446}
447
448/*
449 * Get state of the modem control input lines
450 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200451static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000452{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200453 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
454 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000455
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200456 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457
458 /*
459 * The control signals are active low.
460 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200461 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000462 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200463 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000464 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200465 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000466 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200467 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000468 ret |= TIOCM_RI;
469
Richard Genoude0b0baa2014-05-13 20:20:44 +0200470 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000471}
472
473/*
474 * Stop transmitting.
475 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200476static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000477{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100478 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
479
Elen Song64e22eb2013-07-22 16:30:24 +0800480 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800481 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200482 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100483 }
484 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200485 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100486
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100487 if ((port->rs485.flags & SER_RS485_ENABLED) &&
488 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100489 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000490}
491
492/*
493 * Start transmitting.
494 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200495static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000496{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100497 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
498
Alexandre Belloni0058f082016-05-28 00:54:08 +0200499 if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
500 & ATMEL_PDC_TXTEN))
501 /* The transmitter is already running. Yes, we
502 really need this.*/
503 return;
Chip Coldwella6670612008-02-08 04:21:06 -0800504
Alexandre Belloni0058f082016-05-28 00:54:08 +0200505 if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100506 if ((port->rs485.flags & SER_RS485_ENABLED) &&
507 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100508 atmel_stop_rx(port);
509
Alexandre Belloni0058f082016-05-28 00:54:08 +0200510 if (atmel_use_pdc_tx(port))
Chip Coldwella6670612008-02-08 04:21:06 -0800511 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200512 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Alexandre Belloni0058f082016-05-28 00:54:08 +0200513
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100514 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200515 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100516}
517
518/*
519 * start receiving - port is in process of being opened.
520 */
521static void atmel_start_rx(struct uart_port *port)
522{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200523 /* reset status and receiver */
524 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100525
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200526 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200527
Elen Song64e22eb2013-07-22 16:30:24 +0800528 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100529 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200530 atmel_uart_writel(port, ATMEL_US_IER,
531 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
532 port->read_status_mask);
533 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100534 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200535 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100536 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000537}
538
539/*
540 * Stop receiving - port is in process of being closed.
541 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200542static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000543{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200544 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200545
Elen Song64e22eb2013-07-22 16:30:24 +0800546 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800547 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200548 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
549 atmel_uart_writel(port, ATMEL_US_IDR,
550 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
551 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100552 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200553 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100554 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000555}
556
557/*
558 * Enable modem status interrupts
559 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200560static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000561{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200562 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
563 uint32_t ier = 0;
564
565 /*
566 * Interrupt should not be enabled twice
567 */
568 if (atmel_port->ms_irq_enabled)
569 return;
570
571 atmel_port->ms_irq_enabled = true;
572
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200573 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200574 ier |= ATMEL_US_CTSIC;
575
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200576 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200577 ier |= ATMEL_US_DSRIC;
578
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200579 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200580 ier |= ATMEL_US_RIIC;
581
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200582 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200583 ier |= ATMEL_US_DCDIC;
584
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200585 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200586
587 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000588}
589
590/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200591 * Disable modem status interrupts
592 */
593static void atmel_disable_ms(struct uart_port *port)
594{
595 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
596 uint32_t idr = 0;
597
598 /*
599 * Interrupt should not be disabled twice
600 */
601 if (!atmel_port->ms_irq_enabled)
602 return;
603
604 atmel_port->ms_irq_enabled = false;
605
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200606 mctrl_gpio_disable_ms(atmel_port->gpios);
607
608 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200609 idr |= ATMEL_US_CTSIC;
610
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200611 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200612 idr |= ATMEL_US_DSRIC;
613
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200614 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200615 idr |= ATMEL_US_RIIC;
616
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200617 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200618 idr |= ATMEL_US_DCDIC;
619
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200620 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200621}
622
623/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000624 * Control the transmission of a break signal
625 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200626static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000627{
628 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200629 /* start break */
630 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000631 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200632 /* stop break */
633 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000634}
635
636/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800637 * Stores the incoming character in the ring buffer
638 */
639static void
640atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
641 unsigned int ch)
642{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800643 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800644 struct circ_buf *ring = &atmel_port->rx_ring;
645 struct atmel_uart_char *c;
646
647 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
648 /* Buffer overflow, ignore char */
649 return;
650
651 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
652 c->status = status;
653 c->ch = ch;
654
655 /* Make sure the character is stored before we update head. */
656 smp_wmb();
657
658 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
659}
660
661/*
Chip Coldwella6670612008-02-08 04:21:06 -0800662 * Deal with parity, framing and overrun errors.
663 */
664static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
665{
666 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200667 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800668
669 if (status & ATMEL_US_RXBRK) {
670 /* ignore side-effect */
671 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
672 port->icount.brk++;
673 }
674 if (status & ATMEL_US_PARE)
675 port->icount.parity++;
676 if (status & ATMEL_US_FRAME)
677 port->icount.frame++;
678 if (status & ATMEL_US_OVRE)
679 port->icount.overrun++;
680}
681
682/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000683 * Characters received (called from interrupt handler)
684 */
David Howells7d12e782006-10-05 14:55:46 +0100685static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000686{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800687 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800688 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000689
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200690 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200691 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200692 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000693
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000694 /*
695 * note that the error handling code is
696 * out of the main execution path
697 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700698 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
699 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
700 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800701
Remy Bohmerb843aa22008-02-08 04:21:01 -0800702 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200703 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800704
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700705 if (status & ATMEL_US_RXBRK
706 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700707 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200708 atmel_uart_writel(port, ATMEL_US_IER,
709 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700710 } else {
711 /*
712 * This is either the end-of-break
713 * condition or we've received at
714 * least one character without RXBRK
715 * being set. In both cases, the next
716 * RXBRK will indicate start-of-break.
717 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200718 atmel_uart_writel(port, ATMEL_US_IDR,
719 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700720 status &= ~ATMEL_US_RXBRK;
721 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100722 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000723 }
724
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800725 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200726 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000727 }
728
Nicolas Ferre98f20822016-06-26 09:44:49 +0200729 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000730}
731
732/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800733 * Transmit characters (called from tasklet with TXRDY interrupt
734 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000735 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200736static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000737{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700738 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100739 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000740
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200741 if (port->x_char &&
742 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200743 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000744 port->icount.tx++;
745 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000746 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800747 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000748 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000749
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200750 while (atmel_uart_readl(port, ATMEL_US_CSR) &
751 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200752 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000753 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
754 port->icount.tx++;
755 if (uart_circ_empty(xmit))
756 break;
757 }
758
759 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
760 uart_write_wakeup(port);
761
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800762 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100763 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200764 atmel_uart_writel(port, ATMEL_US_IER,
765 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000766}
767
Elen Song08f738b2013-07-22 16:30:26 +0800768static void atmel_complete_tx_dma(void *arg)
769{
770 struct atmel_uart_port *atmel_port = arg;
771 struct uart_port *port = &atmel_port->uart;
772 struct circ_buf *xmit = &port->state->xmit;
773 struct dma_chan *chan = atmel_port->chan_tx;
774 unsigned long flags;
775
776 spin_lock_irqsave(&port->lock, flags);
777
778 if (chan)
779 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200780 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800781 xmit->tail &= UART_XMIT_SIZE - 1;
782
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200783 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800784
785 spin_lock_irq(&atmel_port->lock_tx);
786 async_tx_ack(atmel_port->desc_tx);
787 atmel_port->cookie_tx = -EINVAL;
788 atmel_port->desc_tx = NULL;
789 spin_unlock_irq(&atmel_port->lock_tx);
790
791 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
792 uart_write_wakeup(port);
793
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100794 /*
795 * xmit is a circular buffer so, if we have just send data from
796 * xmit->tail to the end of xmit->buf, now we have to transmit the
797 * remaining data from the beginning of xmit->buf to xmit->head.
798 */
Elen Song08f738b2013-07-22 16:30:26 +0800799 if (!uart_circ_empty(xmit))
Nicolas Ferre98f20822016-06-26 09:44:49 +0200800 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Elen Song08f738b2013-07-22 16:30:26 +0800801
802 spin_unlock_irqrestore(&port->lock, flags);
803}
804
805static void atmel_release_tx_dma(struct uart_port *port)
806{
807 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
808 struct dma_chan *chan = atmel_port->chan_tx;
809
810 if (chan) {
811 dmaengine_terminate_all(chan);
812 dma_release_channel(chan);
813 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200814 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800815 }
816
817 atmel_port->desc_tx = NULL;
818 atmel_port->chan_tx = NULL;
819 atmel_port->cookie_tx = -EINVAL;
820}
821
822/*
823 * Called from tasklet with TXRDY interrupt is disabled.
824 */
825static void atmel_tx_dma(struct uart_port *port)
826{
827 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
828 struct circ_buf *xmit = &port->state->xmit;
829 struct dma_chan *chan = atmel_port->chan_tx;
830 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200831 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
832 unsigned int tx_len, part1_len, part2_len, sg_len;
833 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800834
835 /* Make sure we have an idle channel */
836 if (atmel_port->desc_tx != NULL)
837 return;
838
839 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
840 /*
841 * DMA is idle now.
842 * Port xmit buffer is already mapped,
843 * and it is one page... Just adjust
844 * offsets and lengths. Since it is a circular buffer,
845 * we have to transmit till the end, and then the rest.
846 * Take the port lock to get a
847 * consistent xmit buffer state.
848 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200849 tx_len = CIRC_CNT_TO_END(xmit->head,
850 xmit->tail,
851 UART_XMIT_SIZE);
852
853 if (atmel_port->fifo_size) {
854 /* multi data mode */
855 part1_len = (tx_len & ~0x3); /* DWORD access */
856 part2_len = (tx_len & 0x3); /* BYTE access */
857 } else {
858 /* single data (legacy) mode */
859 part1_len = 0;
860 part2_len = tx_len; /* BYTE access only */
861 }
862
863 sg_init_table(sgl, 2);
864 sg_len = 0;
865 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
866 if (part1_len) {
867 sg = &sgl[sg_len++];
868 sg_dma_address(sg) = phys_addr;
869 sg_dma_len(sg) = part1_len;
870
871 phys_addr += part1_len;
872 }
873
874 if (part2_len) {
875 sg = &sgl[sg_len++];
876 sg_dma_address(sg) = phys_addr;
877 sg_dma_len(sg) = part2_len;
878 }
879
880 /*
881 * save tx_len so atmel_complete_tx_dma() will increase
882 * xmit->tail correctly
883 */
884 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800885
886 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200887 sgl,
888 sg_len,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100889 DMA_MEM_TO_DEV,
890 DMA_PREP_INTERRUPT |
891 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800892 if (!desc) {
893 dev_err(port->dev, "Failed to send via dma!\n");
894 return;
895 }
896
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200897 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800898
899 atmel_port->desc_tx = desc;
900 desc->callback = atmel_complete_tx_dma;
901 desc->callback_param = atmel_port;
902 atmel_port->cookie_tx = dmaengine_submit(desc);
903
904 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100905 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800906 /* DMA done, stop TX, start RX for RS485 */
907 atmel_start_rx(port);
908 }
909 }
910
911 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
912 uart_write_wakeup(port);
913}
914
915static int atmel_prepare_tx_dma(struct uart_port *port)
916{
917 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
918 dma_cap_mask_t mask;
919 struct dma_slave_config config;
920 int ret, nent;
921
922 dma_cap_zero(mask);
923 dma_cap_set(DMA_SLAVE, mask);
924
925 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
926 if (atmel_port->chan_tx == NULL)
927 goto chan_err;
928 dev_info(port->dev, "using %s for tx DMA transfers\n",
929 dma_chan_name(atmel_port->chan_tx));
930
931 spin_lock_init(&atmel_port->lock_tx);
932 sg_init_table(&atmel_port->sg_tx, 1);
933 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800934 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800935 sg_set_page(&atmel_port->sg_tx,
936 virt_to_page(port->state->xmit.buf),
937 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200938 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800939 nent = dma_map_sg(port->dev,
940 &atmel_port->sg_tx,
941 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200942 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800943
944 if (!nent) {
945 dev_dbg(port->dev, "need to release resource of dma\n");
946 goto chan_err;
947 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200948 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800949 sg_dma_len(&atmel_port->sg_tx),
950 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200951 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800952 }
953
954 /* Configure the slave DMA */
955 memset(&config, 0, sizeof(config));
956 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200957 config.dst_addr_width = (atmel_port->fifo_size) ?
958 DMA_SLAVE_BUSWIDTH_4_BYTES :
959 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800960 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200961 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800962
Maxime Ripard5483c102014-10-22 17:43:16 +0200963 ret = dmaengine_slave_config(atmel_port->chan_tx,
964 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800965 if (ret) {
966 dev_err(port->dev, "DMA tx slave configuration failed\n");
967 goto chan_err;
968 }
969
970 return 0;
971
972chan_err:
973 dev_err(port->dev, "TX channel not available, switch to pio\n");
974 atmel_port->use_dma_tx = 0;
975 if (atmel_port->chan_tx)
976 atmel_release_tx_dma(port);
977 return -EINVAL;
978}
979
Elen Song34df42f2013-07-22 16:30:27 +0800980static void atmel_complete_rx_dma(void *arg)
981{
982 struct uart_port *port = arg;
983 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
984
Nicolas Ferre98f20822016-06-26 09:44:49 +0200985 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +0800986}
987
988static void atmel_release_rx_dma(struct uart_port *port)
989{
990 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
991 struct dma_chan *chan = atmel_port->chan_rx;
992
993 if (chan) {
994 dmaengine_terminate_all(chan);
995 dma_release_channel(chan);
996 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200997 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800998 }
999
1000 atmel_port->desc_rx = NULL;
1001 atmel_port->chan_rx = NULL;
1002 atmel_port->cookie_rx = -EINVAL;
1003}
1004
1005static void atmel_rx_from_dma(struct uart_port *port)
1006{
1007 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001008 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +08001009 struct circ_buf *ring = &atmel_port->rx_ring;
1010 struct dma_chan *chan = atmel_port->chan_rx;
1011 struct dma_tx_state state;
1012 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001013 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001014
1015
1016 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001017 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001018 dmastat = dmaengine_tx_status(chan,
1019 atmel_port->cookie_rx,
1020 &state);
1021 /* Restart a new tasklet if DMA status is error */
1022 if (dmastat == DMA_ERROR) {
1023 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001024 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001025 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001026 return;
1027 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001028
1029 /* CPU claims ownership of RX DMA buffer */
1030 dma_sync_sg_for_cpu(port->dev,
1031 &atmel_port->sg_rx,
1032 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001033 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001034
1035 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001036 * ring->head points to the end of data already written by the DMA.
1037 * ring->tail points to the beginning of data to be read by the
1038 * framework.
1039 * The current transfer size should not be larger than the dma buffer
1040 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001041 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001042 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1043 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1044 /*
1045 * At this point ring->head may point to the first byte right after the
1046 * last byte of the dma buffer:
1047 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1048 *
1049 * However ring->tail must always points inside the dma buffer:
1050 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1051 *
1052 * Since we use a ring buffer, we have to handle the case
1053 * where head is lower than tail. In such a case, we first read from
1054 * tail to the end of the buffer then reset tail.
1055 */
1056 if (ring->head < ring->tail) {
1057 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001058
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001059 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1060 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001061 port->icount.rx += count;
1062 }
1063
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001064 /* Finally we read data from tail to head */
1065 if (ring->tail < ring->head) {
1066 count = ring->head - ring->tail;
1067
1068 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1069 /* Wrap ring->head if needed */
1070 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1071 ring->head = 0;
1072 ring->tail = ring->head;
1073 port->icount.rx += count;
1074 }
1075
1076 /* USART retreives ownership of RX DMA buffer */
1077 dma_sync_sg_for_device(port->dev,
1078 &atmel_port->sg_rx,
1079 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001080 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001081
1082 /*
1083 * Drop the lock here since it might end up calling
1084 * uart_start(), which takes the lock.
1085 */
1086 spin_unlock(&port->lock);
1087 tty_flip_buffer_push(tport);
1088 spin_lock(&port->lock);
1089
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001090 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001091}
1092
1093static int atmel_prepare_rx_dma(struct uart_port *port)
1094{
1095 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1096 struct dma_async_tx_descriptor *desc;
1097 dma_cap_mask_t mask;
1098 struct dma_slave_config config;
1099 struct circ_buf *ring;
1100 int ret, nent;
1101
1102 ring = &atmel_port->rx_ring;
1103
1104 dma_cap_zero(mask);
1105 dma_cap_set(DMA_CYCLIC, mask);
1106
1107 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1108 if (atmel_port->chan_rx == NULL)
1109 goto chan_err;
1110 dev_info(port->dev, "using %s for rx DMA transfers\n",
1111 dma_chan_name(atmel_port->chan_rx));
1112
1113 spin_lock_init(&atmel_port->lock_rx);
1114 sg_init_table(&atmel_port->sg_rx, 1);
1115 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001116 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001117 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001118 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001119 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001120 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001121 nent = dma_map_sg(port->dev,
1122 &atmel_port->sg_rx,
1123 1,
1124 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001125
1126 if (!nent) {
1127 dev_dbg(port->dev, "need to release resource of dma\n");
1128 goto chan_err;
1129 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001130 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001131 sg_dma_len(&atmel_port->sg_rx),
1132 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001133 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001134 }
1135
1136 /* Configure the slave DMA */
1137 memset(&config, 0, sizeof(config));
1138 config.direction = DMA_DEV_TO_MEM;
1139 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1140 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001141 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001142
Maxime Ripard5483c102014-10-22 17:43:16 +02001143 ret = dmaengine_slave_config(atmel_port->chan_rx,
1144 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001145 if (ret) {
1146 dev_err(port->dev, "DMA rx slave configuration failed\n");
1147 goto chan_err;
1148 }
1149 /*
1150 * Prepare a cyclic dma transfer, assign 2 descriptors,
1151 * each one is half ring buffer size
1152 */
1153 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001154 sg_dma_address(&atmel_port->sg_rx),
1155 sg_dma_len(&atmel_port->sg_rx),
1156 sg_dma_len(&atmel_port->sg_rx)/2,
1157 DMA_DEV_TO_MEM,
1158 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001159 desc->callback = atmel_complete_rx_dma;
1160 desc->callback_param = port;
1161 atmel_port->desc_rx = desc;
1162 atmel_port->cookie_rx = dmaengine_submit(desc);
1163
1164 return 0;
1165
1166chan_err:
1167 dev_err(port->dev, "RX channel not available, switch to pio\n");
1168 atmel_port->use_dma_rx = 0;
1169 if (atmel_port->chan_rx)
1170 atmel_release_rx_dma(port);
1171 return -EINVAL;
1172}
1173
Elen Song2e68c222013-07-22 16:30:30 +08001174static void atmel_uart_timer_callback(unsigned long data)
1175{
1176 struct uart_port *port = (void *)data;
1177 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1178
Nicolas Ferre98f20822016-06-26 09:44:49 +02001179 if (!atomic_read(&atmel_port->tasklet_shutdown)) {
1180 tasklet_schedule(&atmel_port->tasklet_rx);
1181 mod_timer(&atmel_port->uart_timer,
1182 jiffies + uart_poll_timeout(port));
1183 }
Elen Song2e68c222013-07-22 16:30:30 +08001184}
1185
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001186/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001187 * receive interrupt handler.
1188 */
1189static void
1190atmel_handle_receive(struct uart_port *port, unsigned int pending)
1191{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001192 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001193
Elen Song64e22eb2013-07-22 16:30:24 +08001194 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001195 /*
1196 * PDC receive. Just schedule the tasklet and let it
1197 * figure out the details.
1198 *
1199 * TODO: We're not handling error flags correctly at
1200 * the moment.
1201 */
1202 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001203 atmel_uart_writel(port, ATMEL_US_IDR,
1204 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Nicolas Ferre98f20822016-06-26 09:44:49 +02001205 atmel_tasklet_schedule(atmel_port,
1206 &atmel_port->tasklet_rx);
Chip Coldwella6670612008-02-08 04:21:06 -08001207 }
1208
1209 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1210 ATMEL_US_FRAME | ATMEL_US_PARE))
1211 atmel_pdc_rxerr(port, pending);
1212 }
1213
Elen Song34df42f2013-07-22 16:30:27 +08001214 if (atmel_use_dma_rx(port)) {
1215 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001216 atmel_uart_writel(port, ATMEL_US_IDR,
1217 ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001218 atmel_tasklet_schedule(atmel_port,
1219 &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001220 }
1221 }
1222
Remy Bohmerb843aa22008-02-08 04:21:01 -08001223 /* Interrupt receive */
1224 if (pending & ATMEL_US_RXRDY)
1225 atmel_rx_chars(port);
1226 else if (pending & ATMEL_US_RXBRK) {
1227 /*
1228 * End of break detected. If it came along with a
1229 * character, atmel_rx_chars will handle it.
1230 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001231 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1232 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001233 atmel_port->break_active = 0;
1234 }
1235}
1236
1237/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001238 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001239 */
1240static void
1241atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1242{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001243 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001244
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001245 if (pending & atmel_port->tx_done_mask) {
1246 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001247 atmel_uart_writel(port, ATMEL_US_IDR,
1248 atmel_port->tx_done_mask);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001249 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001250 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001251}
1252
1253/*
1254 * status flags interrupt handler.
1255 */
1256static void
1257atmel_handle_status(struct uart_port *port, unsigned int pending,
1258 unsigned int status)
1259{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001260 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Nicolas Ferre92052182016-06-17 12:05:46 +02001261 unsigned int status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001262
Remy Bohmerb843aa22008-02-08 04:21:01 -08001263 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001264 | ATMEL_US_CTSIC)) {
Nicolas Ferre92052182016-06-17 12:05:46 +02001265 status_change = status ^ atmel_port->irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +08001266 atmel_port->irq_status_prev = status;
Nicolas Ferre92052182016-06-17 12:05:46 +02001267
1268 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1269 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1270 /* TODO: All reads to CSR will clear these interrupts! */
1271 if (status_change & ATMEL_US_RI)
1272 port->icount.rng++;
1273 if (status_change & ATMEL_US_DSR)
1274 port->icount.dsr++;
1275 if (status_change & ATMEL_US_DCD)
1276 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1277 if (status_change & ATMEL_US_CTS)
1278 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1279
1280 wake_up_interruptible(&port->state->port.delta_msr_wait);
1281 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001282 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001283}
1284
1285/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001286 * Interrupt handler
1287 */
David Howells7d12e782006-10-05 14:55:46 +01001288static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001289{
1290 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001291 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001292 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001293
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001294 spin_lock(&atmel_port->lock_suspended);
1295
Chip Coldwella6670612008-02-08 04:21:06 -08001296 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001297 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001298 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001299 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001300 if (!pending)
1301 break;
1302
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001303 if (atmel_port->suspended) {
1304 atmel_port->pending |= pending;
1305 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001306 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001307 pm_system_wakeup();
1308 break;
1309 }
1310
Remy Bohmerb843aa22008-02-08 04:21:01 -08001311 atmel_handle_receive(port, pending);
1312 atmel_handle_status(port, pending, status);
1313 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001314 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001315
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001316 spin_unlock(&atmel_port->lock_suspended);
1317
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001318 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001319}
1320
Elen Songa930e522013-07-22 16:30:25 +08001321static void atmel_release_tx_pdc(struct uart_port *port)
1322{
1323 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1324 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1325
1326 dma_unmap_single(port->dev,
1327 pdc->dma_addr,
1328 pdc->dma_size,
1329 DMA_TO_DEVICE);
1330}
1331
Chip Coldwella6670612008-02-08 04:21:06 -08001332/*
1333 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1334 */
Elen Song64e22eb2013-07-22 16:30:24 +08001335static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001336{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001337 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001338 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001339 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1340 int count;
1341
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001342 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001343 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001344 return;
1345
Chip Coldwella6670612008-02-08 04:21:06 -08001346 xmit->tail += pdc->ofs;
1347 xmit->tail &= UART_XMIT_SIZE - 1;
1348
1349 port->icount.tx += pdc->ofs;
1350 pdc->ofs = 0;
1351
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001352 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001353
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001354 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001355 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001356
Itai Levi1f140812009-01-15 13:50:43 -08001357 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001358 dma_sync_single_for_device(port->dev,
1359 pdc->dma_addr,
1360 pdc->dma_size,
1361 DMA_TO_DEVICE);
1362
1363 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1364 pdc->ofs = count;
1365
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001366 atmel_uart_writel(port, ATMEL_PDC_TPR,
1367 pdc->dma_addr + xmit->tail);
1368 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001369 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001370 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001371 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001372 atmel_uart_writel(port, ATMEL_US_IER,
1373 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001374 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001375 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1376 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001377 /* DMA done, stop TX, start RX for RS485 */
1378 atmel_start_rx(port);
1379 }
Chip Coldwella6670612008-02-08 04:21:06 -08001380 }
1381
1382 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1383 uart_write_wakeup(port);
1384}
1385
Elen Songa930e522013-07-22 16:30:25 +08001386static int atmel_prepare_tx_pdc(struct uart_port *port)
1387{
1388 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1389 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1390 struct circ_buf *xmit = &port->state->xmit;
1391
1392 pdc->buf = xmit->buf;
1393 pdc->dma_addr = dma_map_single(port->dev,
1394 pdc->buf,
1395 UART_XMIT_SIZE,
1396 DMA_TO_DEVICE);
1397 pdc->dma_size = UART_XMIT_SIZE;
1398 pdc->ofs = 0;
1399
1400 return 0;
1401}
1402
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001403static void atmel_rx_from_ring(struct uart_port *port)
1404{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001405 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001406 struct circ_buf *ring = &atmel_port->rx_ring;
1407 unsigned int flg;
1408 unsigned int status;
1409
1410 while (ring->head != ring->tail) {
1411 struct atmel_uart_char c;
1412
1413 /* Make sure c is loaded after head. */
1414 smp_rmb();
1415
1416 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1417
1418 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1419
1420 port->icount.rx++;
1421 status = c.status;
1422 flg = TTY_NORMAL;
1423
1424 /*
1425 * note that the error handling code is
1426 * out of the main execution path
1427 */
1428 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1429 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1430 if (status & ATMEL_US_RXBRK) {
1431 /* ignore side-effect */
1432 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1433
1434 port->icount.brk++;
1435 if (uart_handle_break(port))
1436 continue;
1437 }
1438 if (status & ATMEL_US_PARE)
1439 port->icount.parity++;
1440 if (status & ATMEL_US_FRAME)
1441 port->icount.frame++;
1442 if (status & ATMEL_US_OVRE)
1443 port->icount.overrun++;
1444
1445 status &= port->read_status_mask;
1446
1447 if (status & ATMEL_US_RXBRK)
1448 flg = TTY_BREAK;
1449 else if (status & ATMEL_US_PARE)
1450 flg = TTY_PARITY;
1451 else if (status & ATMEL_US_FRAME)
1452 flg = TTY_FRAME;
1453 }
1454
1455
1456 if (uart_handle_sysrq_char(port, c.ch))
1457 continue;
1458
1459 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1460 }
1461
1462 /*
1463 * Drop the lock here since it might end up calling
1464 * uart_start(), which takes the lock.
1465 */
1466 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001467 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001468 spin_lock(&port->lock);
1469}
1470
Elen Songa930e522013-07-22 16:30:25 +08001471static void atmel_release_rx_pdc(struct uart_port *port)
1472{
1473 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1474 int i;
1475
1476 for (i = 0; i < 2; i++) {
1477 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1478
1479 dma_unmap_single(port->dev,
1480 pdc->dma_addr,
1481 pdc->dma_size,
1482 DMA_FROM_DEVICE);
1483 kfree(pdc->buf);
1484 }
1485}
1486
Elen Song64e22eb2013-07-22 16:30:24 +08001487static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001488{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001489 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001490 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001491 struct atmel_dma_buffer *pdc;
1492 int rx_idx = atmel_port->pdc_rx_idx;
1493 unsigned int head;
1494 unsigned int tail;
1495 unsigned int count;
1496
1497 do {
1498 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001499 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001500
1501 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001502 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001503 tail = pdc->ofs;
1504
1505 /* If the PDC has switched buffers, RPR won't contain
1506 * any address within the current buffer. Since head
1507 * is unsigned, we just need a one-way comparison to
1508 * find out.
1509 *
1510 * In this case, we just need to consume the entire
1511 * buffer and resubmit it for DMA. This will clear the
1512 * ENDRX bit as well, so that we can safely re-enable
1513 * all interrupts below.
1514 */
1515 head = min(head, pdc->dma_size);
1516
1517 if (likely(head != tail)) {
1518 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1519 pdc->dma_size, DMA_FROM_DEVICE);
1520
1521 /*
1522 * head will only wrap around when we recycle
1523 * the DMA buffer, and when that happens, we
1524 * explicitly set tail to 0. So head will
1525 * always be greater than tail.
1526 */
1527 count = head - tail;
1528
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001529 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1530 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001531
1532 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1533 pdc->dma_size, DMA_FROM_DEVICE);
1534
1535 port->icount.rx += count;
1536 pdc->ofs = head;
1537 }
1538
1539 /*
1540 * If the current buffer is full, we need to check if
1541 * the next one contains any additional data.
1542 */
1543 if (head >= pdc->dma_size) {
1544 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001545 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1546 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001547
1548 rx_idx = !rx_idx;
1549 atmel_port->pdc_rx_idx = rx_idx;
1550 }
1551 } while (head >= pdc->dma_size);
1552
1553 /*
1554 * Drop the lock here since it might end up calling
1555 * uart_start(), which takes the lock.
1556 */
1557 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001558 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001559 spin_lock(&port->lock);
1560
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001561 atmel_uart_writel(port, ATMEL_US_IER,
1562 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001563}
1564
Elen Songa930e522013-07-22 16:30:25 +08001565static int atmel_prepare_rx_pdc(struct uart_port *port)
1566{
1567 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1568 int i;
1569
1570 for (i = 0; i < 2; i++) {
1571 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1572
1573 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1574 if (pdc->buf == NULL) {
1575 if (i != 0) {
1576 dma_unmap_single(port->dev,
1577 atmel_port->pdc_rx[0].dma_addr,
1578 PDC_BUFFER_SIZE,
1579 DMA_FROM_DEVICE);
1580 kfree(atmel_port->pdc_rx[0].buf);
1581 }
1582 atmel_port->use_pdc_rx = 0;
1583 return -ENOMEM;
1584 }
1585 pdc->dma_addr = dma_map_single(port->dev,
1586 pdc->buf,
1587 PDC_BUFFER_SIZE,
1588 DMA_FROM_DEVICE);
1589 pdc->dma_size = PDC_BUFFER_SIZE;
1590 pdc->ofs = 0;
1591 }
1592
1593 atmel_port->pdc_rx_idx = 0;
1594
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001595 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1596 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001597
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001598 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1599 atmel_port->pdc_rx[1].dma_addr);
1600 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001601
1602 return 0;
1603}
1604
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001605/*
1606 * tasklet handling tty stuff outside the interrupt handler.
1607 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001608static void atmel_tasklet_rx_func(unsigned long data)
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001609{
1610 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001611 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001612
1613 /* The interrupt handler does not take the lock */
1614 spin_lock(&port->lock);
Elen Songa930e522013-07-22 16:30:25 +08001615 atmel_port->schedule_rx(port);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001616 spin_unlock(&port->lock);
1617}
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001618
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001619static void atmel_tasklet_tx_func(unsigned long data)
1620{
1621 struct uart_port *port = (struct uart_port *)data;
1622 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1623
1624 /* The interrupt handler does not take the lock */
1625 spin_lock(&port->lock);
1626 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001627 spin_unlock(&port->lock);
1628}
1629
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001630static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001631 struct platform_device *pdev)
1632{
1633 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001634 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001635
1636 if (np) {
1637 /* DMA/PDC usage specification */
Julia Lawall490d5ce2016-08-05 10:56:45 +02001638 if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1639 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001640 atmel_port->use_dma_rx = true;
1641 atmel_port->use_pdc_rx = false;
1642 } else {
1643 atmel_port->use_dma_rx = false;
1644 atmel_port->use_pdc_rx = true;
1645 }
1646 } else {
1647 atmel_port->use_dma_rx = false;
1648 atmel_port->use_pdc_rx = false;
1649 }
1650
Julia Lawall490d5ce2016-08-05 10:56:45 +02001651 if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1652 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001653 atmel_port->use_dma_tx = true;
1654 atmel_port->use_pdc_tx = false;
1655 } else {
1656 atmel_port->use_dma_tx = false;
1657 atmel_port->use_pdc_tx = true;
1658 }
1659 } else {
1660 atmel_port->use_dma_tx = false;
1661 atmel_port->use_pdc_tx = false;
1662 }
1663
1664 } else {
1665 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1666 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1667 atmel_port->use_dma_rx = false;
1668 atmel_port->use_dma_tx = false;
1669 }
1670
Elen Song33d64c42013-07-22 16:30:28 +08001671}
1672
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001673static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001674 struct platform_device *pdev)
1675{
1676 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001677 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001678
1679 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001680 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001681 u32 rs485_delay[2];
1682 /* rs485 properties */
1683 if (of_property_read_u32_array(np, "rs485-rts-delay",
1684 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001685 rs485conf->delay_rts_before_send = rs485_delay[0];
1686 rs485conf->delay_rts_after_send = rs485_delay[1];
1687 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001688 }
Elen Song33d64c42013-07-22 16:30:28 +08001689
1690 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1691 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1692
1693 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1694 NULL))
1695 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001696 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001697 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001698 }
1699
1700}
1701
Elen Songa930e522013-07-22 16:30:25 +08001702static void atmel_set_ops(struct uart_port *port)
1703{
1704 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1705
Elen Song34df42f2013-07-22 16:30:27 +08001706 if (atmel_use_dma_rx(port)) {
1707 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1708 atmel_port->schedule_rx = &atmel_rx_from_dma;
1709 atmel_port->release_rx = &atmel_release_rx_dma;
1710 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001711 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1712 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1713 atmel_port->release_rx = &atmel_release_rx_pdc;
1714 } else {
1715 atmel_port->prepare_rx = NULL;
1716 atmel_port->schedule_rx = &atmel_rx_from_ring;
1717 atmel_port->release_rx = NULL;
1718 }
1719
Elen Song08f738b2013-07-22 16:30:26 +08001720 if (atmel_use_dma_tx(port)) {
1721 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1722 atmel_port->schedule_tx = &atmel_tx_dma;
1723 atmel_port->release_tx = &atmel_release_tx_dma;
1724 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001725 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1726 atmel_port->schedule_tx = &atmel_tx_pdc;
1727 atmel_port->release_tx = &atmel_release_tx_pdc;
1728 } else {
1729 atmel_port->prepare_tx = NULL;
1730 atmel_port->schedule_tx = &atmel_tx_chars;
1731 atmel_port->release_tx = NULL;
1732 }
1733}
1734
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001735/*
Elen Song055560b2013-07-22 16:30:29 +08001736 * Get ip name usart or uart
1737 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001738static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001739{
1740 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001741 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001742 u32 version;
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001743 u32 usart, dbgu_uart, new_uart;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001744 /* ASCII decoding for IP version */
1745 usart = 0x55534152; /* USAR(T) */
1746 dbgu_uart = 0x44424755; /* DBGU */
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001747 new_uart = 0x55415254; /* UART */
Elen Song055560b2013-07-22 16:30:29 +08001748
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001749 /*
1750 * Only USART devices from at91sam9260 SOC implement fractional
1751 * baudrate.
1752 */
1753 atmel_port->has_frac_baudrate = false;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001754 atmel_port->has_hw_timer = false;
Elen Song055560b2013-07-22 16:30:29 +08001755
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001756 if (name == new_uart) {
1757 dev_dbg(port->dev, "Uart with hw timer");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001758 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001759 atmel_port->rtor = ATMEL_UA_RTOR;
1760 } else if (name == usart) {
1761 dev_dbg(port->dev, "Usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001762 atmel_port->has_frac_baudrate = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001763 atmel_port->has_hw_timer = true;
1764 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001765 } else if (name == dbgu_uart) {
1766 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
Elen Song055560b2013-07-22 16:30:29 +08001767 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001768 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001769 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001770 switch (version) {
1771 case 0x302:
1772 case 0x10213:
1773 dev_dbg(port->dev, "This version is usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001774 atmel_port->has_frac_baudrate = true;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001775 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001776 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001777 break;
1778 case 0x203:
1779 case 0x10202:
1780 dev_dbg(port->dev, "This version is uart\n");
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001781 break;
1782 default:
1783 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1784 }
Elen Song055560b2013-07-22 16:30:29 +08001785 }
Elen Song055560b2013-07-22 16:30:29 +08001786}
1787
1788/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001789 * Perform initialization and enable port for reception
1790 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001791static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001792{
Elen Song33d64c42013-07-22 16:30:28 +08001793 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001794 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001795 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001796 int retval;
1797
1798 /*
1799 * Ensure that no interrupts are enabled otherwise when
1800 * request_irq() is called we could get stuck trying to
1801 * handle an unexpected interrupt
1802 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001803 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001804 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001805
1806 /*
1807 * Allocate the IRQ
1808 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001809 retval = request_irq(port->irq, atmel_interrupt,
1810 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001811 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001812 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001813 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001814 return retval;
1815 }
1816
Nicolas Ferre98f20822016-06-26 09:44:49 +02001817 atomic_set(&atmel_port->tasklet_shutdown, 0);
1818 tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
1819 (unsigned long)port);
1820 tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
1821 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08001822
Richard Genoudab5e4e42014-05-13 20:20:45 +02001823 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001824 * Initialize DMA (if necessary)
1825 */
Elen Song33d64c42013-07-22 16:30:28 +08001826 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001827 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001828
Elen Songa930e522013-07-22 16:30:25 +08001829 if (atmel_port->prepare_rx) {
1830 retval = atmel_port->prepare_rx(port);
1831 if (retval < 0)
1832 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001833 }
1834
Elen Songa930e522013-07-22 16:30:25 +08001835 if (atmel_port->prepare_tx) {
1836 retval = atmel_port->prepare_tx(port);
1837 if (retval < 0)
1838 atmel_set_ops(port);
1839 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001840
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001841 /*
1842 * Enable FIFO when available
1843 */
1844 if (atmel_port->fifo_size) {
1845 unsigned int txrdym = ATMEL_US_ONE_DATA;
1846 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1847 unsigned int fmr;
1848
1849 atmel_uart_writel(port, ATMEL_US_CR,
1850 ATMEL_US_FIFOEN |
1851 ATMEL_US_RXFCLR |
1852 ATMEL_US_TXFLCLR);
1853
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001854 if (atmel_use_dma_tx(port))
1855 txrdym = ATMEL_US_FOUR_DATA;
1856
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001857 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1858 if (atmel_port->rts_high &&
1859 atmel_port->rts_low)
1860 fmr |= ATMEL_US_FRTSC |
1861 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1862 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1863
1864 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1865 }
1866
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001867 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001868 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001869
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001870 /*
1871 * Finally, enable the serial port
1872 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001873 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001874 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001875 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001876
Marek Roszko8bc661b2014-01-10 10:33:11 +01001877 setup_timer(&atmel_port->uart_timer,
1878 atmel_uart_timer_callback,
1879 (unsigned long)port);
1880
Elen Song64e22eb2013-07-22 16:30:24 +08001881 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001882 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001883 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001884 mod_timer(&atmel_port->uart_timer,
1885 jiffies + uart_poll_timeout(port));
1886 /* set USART timeout */
1887 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001888 atmel_uart_writel(port, atmel_port->rtor,
1889 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001890 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001891
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001892 atmel_uart_writel(port, ATMEL_US_IER,
1893 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001894 }
Chip Coldwella6670612008-02-08 04:21:06 -08001895 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001896 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001897 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001898 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001899 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001900 mod_timer(&atmel_port->uart_timer,
1901 jiffies + uart_poll_timeout(port));
1902 /* set USART timeout */
1903 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001904 atmel_uart_writel(port, atmel_port->rtor,
1905 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001906 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001907
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001908 atmel_uart_writel(port, ATMEL_US_IER,
1909 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001910 }
Chip Coldwella6670612008-02-08 04:21:06 -08001911 } else {
1912 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001913 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001914 }
Andrew Victorafefc412006-06-19 19:53:19 +01001915
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001916 return 0;
1917}
1918
1919/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001920 * Flush any TX data submitted for DMA. Called when the TX circular
1921 * buffer is reset.
1922 */
1923static void atmel_flush_buffer(struct uart_port *port)
1924{
1925 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1926
1927 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001928 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001929 atmel_port->pdc_tx.ofs = 0;
1930 }
1931}
1932
1933/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001934 * Disable the port
1935 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001936static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001937{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001938 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001939
Nicolas Ferre98f20822016-06-26 09:44:49 +02001940 /* Disable interrupts at device level */
1941 atmel_uart_writel(port, ATMEL_US_IDR, -1);
1942
1943 /* Prevent spurious interrupts from scheduling the tasklet */
1944 atomic_inc(&atmel_port->tasklet_shutdown);
1945
Chip Coldwella6670612008-02-08 04:21:06 -08001946 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001947 * Prevent any tasklets being scheduled during
1948 * cleanup
1949 */
1950 del_timer_sync(&atmel_port->uart_timer);
1951
Nicolas Ferre98f20822016-06-26 09:44:49 +02001952 /* Make sure that no interrupt is on the fly */
1953 synchronize_irq(port->irq);
1954
Marek Roszko8bc661b2014-01-10 10:33:11 +01001955 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001956 * Clear out any scheduled tasklets before
1957 * we destroy the buffers
1958 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001959 tasklet_kill(&atmel_port->tasklet_rx);
1960 tasklet_kill(&atmel_port->tasklet_tx);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001961
1962 /*
1963 * Ensure everything is stopped and
Nicolas Ferre98f20822016-06-26 09:44:49 +02001964 * disable port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001965 */
1966 atmel_stop_rx(port);
1967 atmel_stop_tx(port);
1968
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001969 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001970
Chip Coldwella6670612008-02-08 04:21:06 -08001971 /*
1972 * Shut-down the DMA.
1973 */
Elen Songa930e522013-07-22 16:30:25 +08001974 if (atmel_port->release_rx)
1975 atmel_port->release_rx(port);
1976 if (atmel_port->release_tx)
1977 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001978
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001979 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001980 * Reset ring buffer pointers
1981 */
1982 atmel_port->rx_ring.head = 0;
1983 atmel_port->rx_ring.tail = 0;
1984
1985 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001986 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001987 */
1988 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001989
1990 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001991
Peter Hurley479e9b92014-10-16 16:54:18 -04001992 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001993}
1994
1995/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001996 * Power / Clock management.
1997 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001998static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1999 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002000{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002001 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002002
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002003 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002004 case 0:
2005 /*
2006 * Enable the peripheral clock for this serial port.
2007 * This is called on uart_open() or a resume event.
2008 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002009 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07002010
2011 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002012 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002013 break;
2014 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07002015 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002016 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2017 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07002018
Remy Bohmerb843aa22008-02-08 04:21:01 -08002019 /*
2020 * Disable the peripheral clock for this serial port.
2021 * This is called on uart_close() or a suspend event.
2022 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002023 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002024 break;
2025 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01002026 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002027 }
2028}
2029
2030/*
2031 * Change the port parameters
2032 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002033static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2034 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002035{
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002036 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002037 unsigned long flags;
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002038 unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002039
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002040 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002041 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002042
2043 /* reset the mode, clock divisor, parity, stop bits and data size */
2044 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2045 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002046
Remy Bohmerb843aa22008-02-08 04:21:01 -08002047 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002048
2049 /* byte size */
2050 switch (termios->c_cflag & CSIZE) {
2051 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002052 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002053 break;
2054 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002055 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002056 break;
2057 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002058 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002059 break;
2060 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002061 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002062 break;
2063 }
2064
2065 /* stop bits */
2066 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002067 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002068
2069 /* parity */
2070 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002071 /* Mark or Space parity */
2072 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002073 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002074 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002075 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002076 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002077 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002078 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002079 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002080 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002081 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002082 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002083
2084 spin_lock_irqsave(&port->lock, flags);
2085
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002086 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002087 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002088 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002089 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002090 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002091
Elen Song64e22eb2013-07-22 16:30:24 +08002092 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002093 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002094 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002095
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002096 /*
2097 * Characters to ignore
2098 */
2099 port->ignore_status_mask = 0;
2100 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002101 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002102 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002103 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002104 /*
2105 * If we're ignoring parity and break indicators,
2106 * ignore overruns too (for real raw support).
2107 */
2108 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002109 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002110 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002111 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002112
2113 /* update the per-port timeout */
2114 uart_update_timeout(port, termios->c_cflag, baud);
2115
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002116 /*
2117 * save/disable interrupts. The tty layer will ensure that the
2118 * transmitter is empty if requested by the caller, so there's
2119 * no need to wait for it here.
2120 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002121 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2122 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002123
2124 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002125 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002126
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002127 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002128 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002129 atmel_uart_writel(port, ATMEL_US_TTGR,
2130 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002131 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002132 } else if (termios->c_cflag & CRTSCTS) {
2133 /* RS232 with hardware handshake (RTS/CTS) */
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002134 if (atmel_use_dma_rx(port) && !atmel_use_fifo(port)) {
2135 dev_info(port->dev, "not enabling hardware flow control because DMA is used");
2136 termios->c_cflag &= ~CRTSCTS;
2137 } else {
2138 mode |= ATMEL_US_USMODE_HWHS;
2139 }
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002140 } else {
2141 /* RS232 without hadware handshake */
2142 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002143 }
2144
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002145 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002146 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002147
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002148 /*
2149 * when switching the mode, set the RTS line state according to the
2150 * new mode, otherwise keep the former state
2151 */
2152 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2153 unsigned int rts_state;
2154
2155 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2156 /* let the hardware control the RTS line */
2157 rts_state = ATMEL_US_RTSDIS;
2158 } else {
2159 /* force RTS line to low level */
2160 rts_state = ATMEL_US_RTSEN;
2161 }
2162
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002163 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002164 }
2165
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002166 /*
2167 * Set the baud rate:
2168 * Fractional baudrate allows to setup output frequency more
2169 * accurately. This feature is enabled only when using normal mode.
2170 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
2171 * Currently, OVER is always set to 0 so we get
2172 * baudrate = selected clock (16 * (CD + FP / 8))
2173 */
2174 if (atmel_port->has_frac_baudrate &&
2175 (mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_NORMAL) {
2176 div = DIV_ROUND_CLOSEST(port->uartclk, baud);
2177 cd = div / 16;
2178 fp = DIV_ROUND_CLOSEST(div % 16, 2);
2179 } else {
2180 cd = uart_get_divisor(port, baud);
2181 }
2182
2183 if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */
2184 cd /= 8;
2185 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2186 }
2187 quot = cd | fp << ATMEL_US_FP_OFFSET;
2188
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002189 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2190 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2191 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002192
2193 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002194 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002195
2196 /* CTS flow-control and modem-status interrupts */
2197 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002198 atmel_enable_ms(port);
2199 else
2200 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002201
2202 spin_unlock_irqrestore(&port->lock, flags);
2203}
2204
Peter Hurley732a84a2014-11-05 13:11:43 -05002205static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002206{
Peter Hurley732a84a2014-11-05 13:11:43 -05002207 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002208 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002209 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002210 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002211 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002212 } else {
2213 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002214 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2215 spin_lock_irq(&port->lock);
2216 atmel_disable_ms(port);
2217 spin_unlock_irq(&port->lock);
2218 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002219 }
2220}
2221
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002222/*
2223 * Return string describing the specified port
2224 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002225static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002226{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002227 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002228}
2229
2230/*
2231 * Release the memory region(s) being used by 'port'.
2232 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002233static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002234{
Andrew Victorafefc412006-06-19 19:53:19 +01002235 struct platform_device *pdev = to_platform_device(port->dev);
2236 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2237
2238 release_mem_region(port->mapbase, size);
2239
2240 if (port->flags & UPF_IOREMAP) {
2241 iounmap(port->membase);
2242 port->membase = NULL;
2243 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002244}
2245
2246/*
2247 * Request the memory region(s) being used by 'port'.
2248 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002249static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002250{
Andrew Victorafefc412006-06-19 19:53:19 +01002251 struct platform_device *pdev = to_platform_device(port->dev);
2252 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002253
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002254 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002255 return -EBUSY;
2256
2257 if (port->flags & UPF_IOREMAP) {
2258 port->membase = ioremap(port->mapbase, size);
2259 if (port->membase == NULL) {
2260 release_mem_region(port->mapbase, size);
2261 return -ENOMEM;
2262 }
2263 }
2264
2265 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002266}
2267
2268/*
2269 * Configure/autoconfigure the port.
2270 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002271static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002272{
2273 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002274 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002275 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002276 }
2277}
2278
2279/*
2280 * Verify the new serial_struct (for TIOCSSERIAL).
2281 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002282static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002283{
2284 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002285 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002286 ret = -EINVAL;
2287 if (port->irq != ser->irq)
2288 ret = -EINVAL;
2289 if (ser->io_type != SERIAL_IO_MEM)
2290 ret = -EINVAL;
2291 if (port->uartclk / 16 != ser->baud_base)
2292 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002293 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002294 ret = -EINVAL;
2295 if (port->iobase != ser->port)
2296 ret = -EINVAL;
2297 if (ser->hub6 != 0)
2298 ret = -EINVAL;
2299 return ret;
2300}
2301
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002302#ifdef CONFIG_CONSOLE_POLL
2303static int atmel_poll_get_char(struct uart_port *port)
2304{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002305 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002306 cpu_relax();
2307
Cyrille Pitchena6499432015-07-30 16:33:38 +02002308 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002309}
2310
2311static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2312{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002313 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002314 cpu_relax();
2315
Cyrille Pitchena6499432015-07-30 16:33:38 +02002316 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002317}
2318#endif
2319
Julia Lawall5c7dcdb2016-09-01 19:51:31 +02002320static const struct uart_ops atmel_pops = {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002321 .tx_empty = atmel_tx_empty,
2322 .set_mctrl = atmel_set_mctrl,
2323 .get_mctrl = atmel_get_mctrl,
2324 .stop_tx = atmel_stop_tx,
2325 .start_tx = atmel_start_tx,
2326 .stop_rx = atmel_stop_rx,
2327 .enable_ms = atmel_enable_ms,
2328 .break_ctl = atmel_break_ctl,
2329 .startup = atmel_startup,
2330 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002331 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002332 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002333 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002334 .type = atmel_type,
2335 .release_port = atmel_release_port,
2336 .request_port = atmel_request_port,
2337 .config_port = atmel_config_port,
2338 .verify_port = atmel_verify_port,
2339 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002340#ifdef CONFIG_CONSOLE_POLL
2341 .poll_get_char = atmel_poll_get_char,
2342 .poll_put_char = atmel_poll_put_char,
2343#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002344};
2345
Andrew Victorafefc412006-06-19 19:53:19 +01002346/*
2347 * Configure the port from the platform device resource info.
2348 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002349static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002350 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002351{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002352 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002353 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002354 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002355
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002356 atmel_init_property(atmel_port, pdev);
2357 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002358
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002359 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002360
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002361 port->iotype = UPIO_MEM;
2362 port->flags = UPF_BOOT_AUTOCONF;
2363 port->ops = &atmel_pops;
2364 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002365 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002366 port->mapbase = pdev->resource[0].start;
2367 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002368 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002369
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002370 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2371
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002372 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002373 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002374 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002375 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002376 port->flags |= UPF_IOREMAP;
2377 port->membase = NULL;
2378 }
2379
Remy Bohmerb843aa22008-02-08 04:21:01 -08002380 /* for console, the clock could already be configured */
2381 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002382 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002383 if (IS_ERR(atmel_port->clk)) {
2384 ret = PTR_ERR(atmel_port->clk);
2385 atmel_port->clk = NULL;
2386 return ret;
2387 }
2388 ret = clk_prepare_enable(atmel_port->clk);
2389 if (ret) {
2390 clk_put(atmel_port->clk);
2391 atmel_port->clk = NULL;
2392 return ret;
2393 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002394 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002395 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002396 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002397 }
Chip Coldwella6670612008-02-08 04:21:06 -08002398
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002399 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002400 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002401 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002402 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002403 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002404 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2405 } else {
2406 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2407 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002408
2409 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002410}
2411
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002412struct platform_device *atmel_default_console_device; /* the serial console device */
2413
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002414#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002415static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002416{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002417 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002418 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002419 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002420}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002421
2422/*
2423 * Interrupts are disabled on entering
2424 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002425static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002426{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002427 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002428 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002429 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002430 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002431
2432 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002433 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002434 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002435 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2436 atmel_uart_writel(port, ATMEL_US_IDR,
2437 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002438
Marc Pignat39d4c922008-04-02 13:04:42 -07002439 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002440 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2441 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002442
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002443 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002444
2445 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002446 * Finally, wait for transmitter to become empty
2447 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002448 */
2449 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002450 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002451 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002452
2453 /* Restore PDC transmit status */
2454 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002455 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002456
Remy Bohmerb843aa22008-02-08 04:21:01 -08002457 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002458 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002459}
2460
2461/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002462 * If the port was already initialised (eg, by a boot loader),
2463 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002464 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002465static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2466 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002467{
2468 unsigned int mr, quot;
2469
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002470 /*
2471 * If the baud rate generator isn't running, the port wasn't
2472 * initialized by the boot loader.
2473 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002474 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002475 if (!quot)
2476 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002477
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002478 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002479 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002480 *bits = 8;
2481 else
2482 *bits = 7;
2483
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002484 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002485 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002486 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002487 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002488 *parity = 'o';
2489
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002490 /*
2491 * The serial core only rounds down when matching this to a
2492 * supported baud rate. Make sure we don't end up slightly
2493 * lower than one of those, as it would make us fall through
2494 * to a much lower baud rate than we really want.
2495 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002496 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002497}
2498
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002499static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002500{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002501 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002502 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002503 int baud = 115200;
2504 int bits = 8;
2505 int parity = 'n';
2506 int flow = 'n';
2507
Remy Bohmerb843aa22008-02-08 04:21:01 -08002508 if (port->membase == NULL) {
2509 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002510 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002511 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002512
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002513 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2514 if (ret)
2515 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002516
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002517 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2518 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2519 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002520
2521 if (options)
2522 uart_parse_options(options, &baud, &parity, &bits, &flow);
2523 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002524 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002525
2526 return uart_set_options(port, co, baud, parity, bits, flow);
2527}
2528
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002529static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002530
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002531static struct console atmel_console = {
2532 .name = ATMEL_DEVICENAME,
2533 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002534 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002535 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002536 .flags = CON_PRINTBUFFER,
2537 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002538 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002539};
2540
David Brownell06a7f052008-11-06 12:53:40 -08002541#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002542
Andrew Victorafefc412006-06-19 19:53:19 +01002543/*
2544 * Early console initialization (before VM subsystem initialized).
2545 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002546static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002547{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002548 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002549 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002550 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002551 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002552 int id = pdata->num;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002553 struct atmel_uart_port *atmel_port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002554
Jaeden Amerob78cd162016-01-26 12:34:49 +01002555 atmel_port->backup_imr = 0;
2556 atmel_port->uart.line = id;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002557
2558 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002559 ret = atmel_init_port(atmel_port, atmel_default_console_device);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002560 if (ret)
2561 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002562 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002563 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002564
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002565 return 0;
2566}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002567
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002568console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002569
Andrew Victorafefc412006-06-19 19:53:19 +01002570/*
2571 * Late console initialization.
2572 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002573static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002574{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002575 if (atmel_default_console_device
2576 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002577 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002578
2579 return 0;
2580}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002581
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002582core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002583
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002584static inline bool atmel_is_console_port(struct uart_port *port)
2585{
2586 return port->cons && port->cons->index == port->line;
2587}
2588
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002589#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002590#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002591
2592static inline bool atmel_is_console_port(struct uart_port *port)
2593{
2594 return false;
2595}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002596#endif
2597
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002598static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002599 .owner = THIS_MODULE,
2600 .driver_name = "atmel_serial",
2601 .dev_name = ATMEL_DEVICENAME,
2602 .major = SERIAL_ATMEL_MAJOR,
2603 .minor = MINOR_START,
2604 .nr = ATMEL_MAX_UART,
2605 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002606};
2607
Andrew Victorafefc412006-06-19 19:53:19 +01002608#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002609static bool atmel_serial_clk_will_stop(void)
2610{
2611#ifdef CONFIG_ARCH_AT91
2612 return at91_suspend_entering_slow_clock();
2613#else
2614 return false;
2615#endif
2616}
2617
Remy Bohmerb843aa22008-02-08 04:21:01 -08002618static int atmel_serial_suspend(struct platform_device *pdev,
2619 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002620{
Andrew Victorafefc412006-06-19 19:53:19 +01002621 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002622 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002623
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002624 if (atmel_is_console_port(port) && console_suspend_enabled) {
2625 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002626 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2627 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002628 cpu_relax();
2629 }
2630
Anti Sullinf05596d2008-09-22 13:57:54 -07002631 /* we can not wake up if we're running on slow clock */
2632 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002633 if (atmel_serial_clk_will_stop()) {
2634 unsigned long flags;
2635
2636 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2637 atmel_port->suspended = true;
2638 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002639 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002640 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002641
2642 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002643
2644 return 0;
2645}
2646
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002647static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002648{
2649 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002650 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002651 unsigned long flags;
2652
2653 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2654 if (atmel_port->pending) {
2655 atmel_handle_receive(port, atmel_port->pending);
2656 atmel_handle_status(port, atmel_port->pending,
2657 atmel_port->pending_status);
2658 atmel_handle_transmit(port, atmel_port->pending);
2659 atmel_port->pending = 0;
2660 }
2661 atmel_port->suspended = false;
2662 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002663
Anti Sullinf05596d2008-09-22 13:57:54 -07002664 uart_resume_port(&atmel_uart, port);
2665 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002666
2667 return 0;
2668}
2669#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002670#define atmel_serial_suspend NULL
2671#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002672#endif
2673
Jaeden Amerob78cd162016-01-26 12:34:49 +01002674static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002675 struct platform_device *pdev)
2676{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002677 atmel_port->fifo_size = 0;
2678 atmel_port->rts_low = 0;
2679 atmel_port->rts_high = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002680
2681 if (of_property_read_u32(pdev->dev.of_node,
2682 "atmel,fifo-size",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002683 &atmel_port->fifo_size))
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002684 return;
2685
Jaeden Amerob78cd162016-01-26 12:34:49 +01002686 if (!atmel_port->fifo_size)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002687 return;
2688
Jaeden Amerob78cd162016-01-26 12:34:49 +01002689 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2690 atmel_port->fifo_size = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002691 dev_err(&pdev->dev, "Invalid FIFO size\n");
2692 return;
2693 }
2694
2695 /*
2696 * 0 <= rts_low <= rts_high <= fifo_size
2697 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2698 * to flush their internal TX FIFO, commonly up to 16 data, before
2699 * actually stopping to send new data. So we try to set the RTS High
2700 * Threshold to a reasonably high value respecting this 16 data
2701 * empirical rule when possible.
2702 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002703 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2704 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2705 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2,
2706 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002707
2708 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002709 atmel_port->fifo_size);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002710 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002711 atmel_port->rts_high);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002712 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002713 atmel_port->rts_low);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002714}
2715
Bill Pemberton9671f092012-11-19 13:21:50 -05002716static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002717{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002718 struct atmel_uart_port *atmel_port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002719 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002720 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002721 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002722 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002723 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002724
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002725 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002726
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002727 if (np)
2728 ret = of_alias_get_id(np, "serial");
2729 else
2730 if (pdata)
2731 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002732
2733 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002734 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002735 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002736 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002737
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002738 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002739 ret = -ENODEV;
2740 goto err;
2741 }
2742
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002743 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002744 /* port already in use */
2745 ret = -EBUSY;
2746 goto err;
2747 }
2748
Jaeden Amerob78cd162016-01-26 12:34:49 +01002749 atmel_port = &atmel_ports[ret];
2750 atmel_port->backup_imr = 0;
2751 atmel_port->uart.line = ret;
2752 atmel_serial_probe_fifos(atmel_port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002753
Nicolas Ferre98f20822016-06-26 09:44:49 +02002754 atomic_set(&atmel_port->tasklet_shutdown, 0);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002755 spin_lock_init(&atmel_port->lock_suspended);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002756
Jaeden Amerob78cd162016-01-26 12:34:49 +01002757 ret = atmel_init_port(atmel_port, pdev);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002758 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002759 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002760
Jaeden Amerob78cd162016-01-26 12:34:49 +01002761 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2762 if (IS_ERR(atmel_port->gpios)) {
2763 ret = PTR_ERR(atmel_port->gpios);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002764 goto err_clear_bit;
2765 }
2766
Jaeden Amerob78cd162016-01-26 12:34:49 +01002767 if (!atmel_use_pdc_rx(&atmel_port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002768 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002769 data = kmalloc(sizeof(struct atmel_uart_char)
2770 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002771 if (!data)
2772 goto err_alloc_ring;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002773 atmel_port->rx_ring.buf = data;
Chip Coldwella6670612008-02-08 04:21:06 -08002774 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002775
Jaeden Amerob78cd162016-01-26 12:34:49 +01002776 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002777
Jaeden Amerob78cd162016-01-26 12:34:49 +01002778 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002779 if (ret)
2780 goto err_add_port;
2781
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002782#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Jaeden Amerob78cd162016-01-26 12:34:49 +01002783 if (atmel_is_console_port(&atmel_port->uart)
David Brownell06a7f052008-11-06 12:53:40 -08002784 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2785 /*
2786 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002787 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002788 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002789 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002790 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002791#endif
David Brownell06a7f052008-11-06 12:53:40 -08002792
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002793 device_init_wakeup(&pdev->dev, 1);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002794 platform_set_drvdata(pdev, atmel_port);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002795
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002796 /*
2797 * The peripheral clock has been disabled by atmel_init_port():
2798 * enable it before accessing I/O registers
2799 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002800 clk_prepare_enable(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002801
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002802 if (rs485_enabled) {
Jaeden Amerob78cd162016-01-26 12:34:49 +01002803 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002804 ATMEL_US_USMODE_NORMAL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002805 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2806 ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002807 }
2808
Elen Song055560b2013-07-22 16:30:29 +08002809 /*
2810 * Get port name of usart or uart
2811 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002812 atmel_get_ip_name(&atmel_port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002813
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002814 /*
2815 * The peripheral clock can now safely be disabled till the port
2816 * is used
2817 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002818 clk_disable_unprepare(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002819
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002820 return 0;
2821
2822err_add_port:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002823 kfree(atmel_port->rx_ring.buf);
2824 atmel_port->rx_ring.buf = NULL;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002825err_alloc_ring:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002826 if (!atmel_is_console_port(&atmel_port->uart)) {
2827 clk_put(atmel_port->clk);
2828 atmel_port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002829 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002830err_clear_bit:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002831 clear_bit(atmel_port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002832err:
Andrew Victorafefc412006-06-19 19:53:19 +01002833 return ret;
2834}
2835
Romain Izardf4a8ab042016-02-26 11:15:04 +01002836/*
2837 * Even if the driver is not modular, it makes sense to be able to
2838 * unbind a device: there can be many bound devices, and there are
2839 * situations where dynamic binding and unbinding can be useful.
2840 *
2841 * For example, a connected device can require a specific firmware update
2842 * protocol that needs bitbanging on IO lines, but use the regular serial
2843 * port in the normal case.
2844 */
2845static int atmel_serial_remove(struct platform_device *pdev)
2846{
2847 struct uart_port *port = platform_get_drvdata(pdev);
2848 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2849 int ret = 0;
2850
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002851 tasklet_kill(&atmel_port->tasklet_rx);
2852 tasklet_kill(&atmel_port->tasklet_tx);
Romain Izardf4a8ab042016-02-26 11:15:04 +01002853
2854 device_init_wakeup(&pdev->dev, 0);
2855
2856 ret = uart_remove_one_port(&atmel_uart, port);
2857
2858 kfree(atmel_port->rx_ring.buf);
2859
2860 /* "port" is allocated statically, so we shouldn't free it */
2861
2862 clear_bit(port->line, atmel_ports_in_use);
2863
2864 clk_put(atmel_port->clk);
2865 atmel_port->clk = NULL;
2866
2867 return ret;
2868}
2869
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002870static struct platform_driver atmel_serial_driver = {
2871 .probe = atmel_serial_probe,
Romain Izardf4a8ab042016-02-26 11:15:04 +01002872 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002873 .suspend = atmel_serial_suspend,
2874 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002875 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002876 .name = "atmel_usart",
2877 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002878 },
2879};
2880
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002881static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002882{
2883 int ret;
2884
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002885 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002886 if (ret)
2887 return ret;
2888
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002889 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002890 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002891 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002892
2893 return ret;
2894}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002895device_initcall(atmel_serial_init);