blob: 8d2cec0f03c2575c7a204338a7824c3e4f3f74cb [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
Alexandre Belloni6a5f0e22017-02-03 23:53:16 +0100178 struct {
179 u32 cr;
180 u32 mr;
181 u32 imr;
182 u32 brgr;
183 u32 rtor;
184 u32 ttgr;
185 u32 fmr;
186 u32 fimr;
187 } cache;
188
Elen Songa930e522013-07-22 16:30:25 +0800189 int (*prepare_rx)(struct uart_port *port);
190 int (*prepare_tx)(struct uart_port *port);
191 void (*schedule_rx)(struct uart_port *port);
192 void (*schedule_tx)(struct uart_port *port);
193 void (*release_rx)(struct uart_port *port);
194 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100195};
196
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200197static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100198static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100199
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000200#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200201static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000202#endif
203
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200204#if defined(CONFIG_OF)
205static const struct of_device_id atmel_serial_dt_ids[] = {
206 { .compatible = "atmel,at91rm9200-usart" },
207 { .compatible = "atmel,at91sam9260-usart" },
208 { /* sentinel */ }
209};
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200210#endif
211
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800212static inline struct atmel_uart_port *
213to_atmel_uart_port(struct uart_port *uart)
214{
215 return container_of(uart, struct atmel_uart_port, uart);
216}
217
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200218static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
219{
220 return __raw_readl(port->membase + reg);
221}
222
223static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
224{
225 __raw_writel(value, port->membase + reg);
226}
227
Cyrille Pitchena6499432015-07-30 16:33:38 +0200228#ifdef CONFIG_AVR32
229
230/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
231static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200232{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200233 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200234}
235
Cyrille Pitchena6499432015-07-30 16:33:38 +0200236static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200237{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200238 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200239}
240
Cyrille Pitchena6499432015-07-30 16:33:38 +0200241#else
242
243static inline u8 atmel_uart_read_char(struct uart_port *port)
244{
245 return __raw_readb(port->membase + ATMEL_US_RHR);
246}
247
248static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
249{
250 __raw_writeb(value, port->membase + ATMEL_US_THR);
251}
252
253#endif
254
Chip Coldwella6670612008-02-08 04:21:06 -0800255#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800256static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800257{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800258 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800259
Elen Song64e22eb2013-07-22 16:30:24 +0800260 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800261}
262
Elen Song64e22eb2013-07-22 16:30:24 +0800263static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800264{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800265 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800266
Elen Song64e22eb2013-07-22 16:30:24 +0800267 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800268}
269#else
Elen Song64e22eb2013-07-22 16:30:24 +0800270static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800271{
272 return false;
273}
274
Elen Song64e22eb2013-07-22 16:30:24 +0800275static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800276{
277 return false;
278}
279#endif
280
Elen Song08f738b2013-07-22 16:30:26 +0800281static bool atmel_use_dma_tx(struct uart_port *port)
282{
283 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
284
285 return atmel_port->use_dma_tx;
286}
287
Elen Song34df42f2013-07-22 16:30:27 +0800288static bool atmel_use_dma_rx(struct uart_port *port)
289{
290 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
291
292 return atmel_port->use_dma_rx;
293}
294
Alexandre Belloni5be605a2016-04-12 14:51:40 +0200295static bool atmel_use_fifo(struct uart_port *port)
296{
297 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
298
299 return atmel_port->fifo_size;
300}
301
Nicolas Ferre98f20822016-06-26 09:44:49 +0200302static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
303 struct tasklet_struct *t)
304{
305 if (!atomic_read(&atmel_port->tasklet_shutdown))
306 tasklet_schedule(t);
307}
308
Richard Genoude0b0baa2014-05-13 20:20:44 +0200309static unsigned int atmel_get_lines_status(struct uart_port *port)
310{
311 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
312 unsigned int status, ret = 0;
313
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200314 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200315
316 mctrl_gpio_get(atmel_port->gpios, &ret);
317
318 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
319 UART_GPIO_CTS))) {
320 if (ret & TIOCM_CTS)
321 status &= ~ATMEL_US_CTS;
322 else
323 status |= ATMEL_US_CTS;
324 }
325
326 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
327 UART_GPIO_DSR))) {
328 if (ret & TIOCM_DSR)
329 status &= ~ATMEL_US_DSR;
330 else
331 status |= ATMEL_US_DSR;
332 }
333
334 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
335 UART_GPIO_RI))) {
336 if (ret & TIOCM_RI)
337 status &= ~ATMEL_US_RI;
338 else
339 status |= ATMEL_US_RI;
340 }
341
342 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
343 UART_GPIO_DCD))) {
344 if (ret & TIOCM_CD)
345 status &= ~ATMEL_US_DCD;
346 else
347 status |= ATMEL_US_DCD;
348 }
349
350 return status;
351}
352
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100353/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100354static int atmel_config_rs485(struct uart_port *port,
355 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100356{
357 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
358 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100359
360 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200361 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100362
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200363 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100364
365 /* Resetting serial mode to RS232 (0x0) */
366 mode &= ~ATMEL_US_USMODE;
367
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100368 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100369
370 if (rs485conf->flags & SER_RS485_ENABLED) {
371 dev_dbg(port->dev, "Setting UART to RS485\n");
372 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200373 atmel_uart_writel(port, ATMEL_US_TTGR,
374 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100375 mode |= ATMEL_US_USMODE_RS485;
376 } else {
377 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800378 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100379 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
380 ATMEL_US_TXBUFE;
381 else
382 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
383 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200384 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100385
386 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200387 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100388
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100389 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100390}
391
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000392/*
393 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
394 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200395static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000396{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200397 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
398 TIOCSER_TEMT :
399 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000400}
401
402/*
403 * Set state of the modem control output lines
404 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200405static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000406{
407 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200408 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100409 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100410 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000411
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100412 /* override mode to RS485 if needed, otherwise keep the current mode */
413 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200414 atmel_uart_writel(port, ATMEL_US_TTGR,
415 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100416 mode &= ~ATMEL_US_USMODE;
417 mode |= ATMEL_US_USMODE_RS485;
418 }
419
420 /* set the RTS line state according to the mode */
421 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
422 /* force RTS line to high level */
423 rts_paused = ATMEL_US_RTSEN;
424
425 /* give the control of the RTS line back to the hardware */
426 rts_ready = ATMEL_US_RTSDIS;
427 } else {
428 /* force RTS line to high level */
429 rts_paused = ATMEL_US_RTSDIS;
430
431 /* force RTS line to low level */
432 rts_ready = ATMEL_US_RTSEN;
433 }
434
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000435 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100436 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100438 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000439
440 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200441 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000442 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200443 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000444
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200445 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100446
Richard Genoude0b0baa2014-05-13 20:20:44 +0200447 mctrl_gpio_set(atmel_port->gpios, mctrl);
448
Andrew Victorafefc412006-06-19 19:53:19 +0100449 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100450 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100451 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200452 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100453 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200454 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100455
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200456 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457}
458
459/*
460 * Get state of the modem control input lines
461 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200462static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000463{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200464 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
465 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000466
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200467 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000468
469 /*
470 * The control signals are active low.
471 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200472 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000473 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200474 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000475 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200476 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000477 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200478 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000479 ret |= TIOCM_RI;
480
Richard Genoude0b0baa2014-05-13 20:20:44 +0200481 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000482}
483
484/*
485 * Stop transmitting.
486 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200487static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000488{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100489 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
490
Elen Song64e22eb2013-07-22 16:30:24 +0800491 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800492 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200493 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100494 }
Richard Genoud89d82322016-12-13 17:27:56 +0100495
496 /*
497 * Disable the transmitter.
498 * This is mandatory when DMA is used, otherwise the DMA buffer
499 * is fully transmitted.
500 */
501 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
502
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100503 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200504 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100505
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_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000509}
510
511/*
512 * Start transmitting.
513 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200514static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000515{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100516 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
517
Alexandre Belloni0058f082016-05-28 00:54:08 +0200518 if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
519 & ATMEL_PDC_TXTEN))
520 /* The transmitter is already running. Yes, we
521 really need this.*/
522 return;
Chip Coldwella6670612008-02-08 04:21:06 -0800523
Alexandre Belloni0058f082016-05-28 00:54:08 +0200524 if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100525 if ((port->rs485.flags & SER_RS485_ENABLED) &&
526 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100527 atmel_stop_rx(port);
528
Alexandre Belloni0058f082016-05-28 00:54:08 +0200529 if (atmel_use_pdc_tx(port))
Chip Coldwella6670612008-02-08 04:21:06 -0800530 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200531 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Alexandre Belloni0058f082016-05-28 00:54:08 +0200532
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100533 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200534 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Richard Genoud89d82322016-12-13 17:27:56 +0100535
536 /* re-enable the transmitter */
537 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100538}
539
540/*
541 * start receiving - port is in process of being opened.
542 */
543static void atmel_start_rx(struct uart_port *port)
544{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200545 /* reset status and receiver */
546 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100547
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200548 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200549
Elen Song64e22eb2013-07-22 16:30:24 +0800550 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100551 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200552 atmel_uart_writel(port, ATMEL_US_IER,
553 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
554 port->read_status_mask);
555 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100556 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200557 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100558 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000559}
560
561/*
562 * Stop receiving - port is in process of being closed.
563 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200564static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000565{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200566 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200567
Elen Song64e22eb2013-07-22 16:30:24 +0800568 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800569 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200570 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
571 atmel_uart_writel(port, ATMEL_US_IDR,
572 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
573 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100574 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200575 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100576 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000577}
578
579/*
580 * Enable modem status interrupts
581 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200582static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000583{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200584 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
585 uint32_t ier = 0;
586
587 /*
588 * Interrupt should not be enabled twice
589 */
590 if (atmel_port->ms_irq_enabled)
591 return;
592
593 atmel_port->ms_irq_enabled = true;
594
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200595 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200596 ier |= ATMEL_US_CTSIC;
597
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200598 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200599 ier |= ATMEL_US_DSRIC;
600
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200601 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200602 ier |= ATMEL_US_RIIC;
603
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200604 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200605 ier |= ATMEL_US_DCDIC;
606
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200607 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200608
609 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000610}
611
612/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200613 * Disable modem status interrupts
614 */
615static void atmel_disable_ms(struct uart_port *port)
616{
617 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
618 uint32_t idr = 0;
619
620 /*
621 * Interrupt should not be disabled twice
622 */
623 if (!atmel_port->ms_irq_enabled)
624 return;
625
626 atmel_port->ms_irq_enabled = false;
627
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200628 mctrl_gpio_disable_ms(atmel_port->gpios);
629
630 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200631 idr |= ATMEL_US_CTSIC;
632
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200633 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200634 idr |= ATMEL_US_DSRIC;
635
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200636 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200637 idr |= ATMEL_US_RIIC;
638
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200639 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200640 idr |= ATMEL_US_DCDIC;
641
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200642 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200643}
644
645/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000646 * Control the transmission of a break signal
647 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200648static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000649{
650 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200651 /* start break */
652 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000653 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200654 /* stop break */
655 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000656}
657
658/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800659 * Stores the incoming character in the ring buffer
660 */
661static void
662atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
663 unsigned int ch)
664{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800665 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800666 struct circ_buf *ring = &atmel_port->rx_ring;
667 struct atmel_uart_char *c;
668
669 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
670 /* Buffer overflow, ignore char */
671 return;
672
673 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
674 c->status = status;
675 c->ch = ch;
676
677 /* Make sure the character is stored before we update head. */
678 smp_wmb();
679
680 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
681}
682
683/*
Chip Coldwella6670612008-02-08 04:21:06 -0800684 * Deal with parity, framing and overrun errors.
685 */
686static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
687{
688 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200689 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800690
691 if (status & ATMEL_US_RXBRK) {
692 /* ignore side-effect */
693 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
694 port->icount.brk++;
695 }
696 if (status & ATMEL_US_PARE)
697 port->icount.parity++;
698 if (status & ATMEL_US_FRAME)
699 port->icount.frame++;
700 if (status & ATMEL_US_OVRE)
701 port->icount.overrun++;
702}
703
704/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000705 * Characters received (called from interrupt handler)
706 */
David Howells7d12e782006-10-05 14:55:46 +0100707static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000708{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800709 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800710 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000711
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200712 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200713 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200714 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000715
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000716 /*
717 * note that the error handling code is
718 * out of the main execution path
719 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700720 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
721 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
722 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800723
Remy Bohmerb843aa22008-02-08 04:21:01 -0800724 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200725 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800726
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700727 if (status & ATMEL_US_RXBRK
728 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700729 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200730 atmel_uart_writel(port, ATMEL_US_IER,
731 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700732 } else {
733 /*
734 * This is either the end-of-break
735 * condition or we've received at
736 * least one character without RXBRK
737 * being set. In both cases, the next
738 * RXBRK will indicate start-of-break.
739 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200740 atmel_uart_writel(port, ATMEL_US_IDR,
741 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700742 status &= ~ATMEL_US_RXBRK;
743 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100744 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000745 }
746
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800747 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200748 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000749 }
750
Nicolas Ferre98f20822016-06-26 09:44:49 +0200751 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000752}
753
754/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800755 * Transmit characters (called from tasklet with TXRDY interrupt
756 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000757 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200758static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000759{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700760 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100761 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000762
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200763 if (port->x_char &&
764 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200765 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000766 port->icount.tx++;
767 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000768 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800769 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000770 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000771
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200772 while (atmel_uart_readl(port, ATMEL_US_CSR) &
773 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200774 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000775 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
776 port->icount.tx++;
777 if (uart_circ_empty(xmit))
778 break;
779 }
780
781 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
782 uart_write_wakeup(port);
783
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800784 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100785 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200786 atmel_uart_writel(port, ATMEL_US_IER,
787 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000788}
789
Elen Song08f738b2013-07-22 16:30:26 +0800790static void atmel_complete_tx_dma(void *arg)
791{
792 struct atmel_uart_port *atmel_port = arg;
793 struct uart_port *port = &atmel_port->uart;
794 struct circ_buf *xmit = &port->state->xmit;
795 struct dma_chan *chan = atmel_port->chan_tx;
796 unsigned long flags;
797
798 spin_lock_irqsave(&port->lock, flags);
799
800 if (chan)
801 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200802 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800803 xmit->tail &= UART_XMIT_SIZE - 1;
804
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200805 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800806
807 spin_lock_irq(&atmel_port->lock_tx);
808 async_tx_ack(atmel_port->desc_tx);
809 atmel_port->cookie_tx = -EINVAL;
810 atmel_port->desc_tx = NULL;
811 spin_unlock_irq(&atmel_port->lock_tx);
812
813 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
814 uart_write_wakeup(port);
815
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100816 /*
817 * xmit is a circular buffer so, if we have just send data from
818 * xmit->tail to the end of xmit->buf, now we have to transmit the
819 * remaining data from the beginning of xmit->buf to xmit->head.
820 */
Elen Song08f738b2013-07-22 16:30:26 +0800821 if (!uart_circ_empty(xmit))
Nicolas Ferre98f20822016-06-26 09:44:49 +0200822 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Richard Genoudb389f172016-12-06 13:05:33 +0100823 else if ((port->rs485.flags & SER_RS485_ENABLED) &&
824 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
825 /* DMA done, stop TX, start RX for RS485 */
826 atmel_start_rx(port);
827 }
Elen Song08f738b2013-07-22 16:30:26 +0800828
829 spin_unlock_irqrestore(&port->lock, flags);
830}
831
832static void atmel_release_tx_dma(struct uart_port *port)
833{
834 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
835 struct dma_chan *chan = atmel_port->chan_tx;
836
837 if (chan) {
838 dmaengine_terminate_all(chan);
839 dma_release_channel(chan);
840 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200841 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800842 }
843
844 atmel_port->desc_tx = NULL;
845 atmel_port->chan_tx = NULL;
846 atmel_port->cookie_tx = -EINVAL;
847}
848
849/*
850 * Called from tasklet with TXRDY interrupt is disabled.
851 */
852static void atmel_tx_dma(struct uart_port *port)
853{
854 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
855 struct circ_buf *xmit = &port->state->xmit;
856 struct dma_chan *chan = atmel_port->chan_tx;
857 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200858 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
859 unsigned int tx_len, part1_len, part2_len, sg_len;
860 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800861
862 /* Make sure we have an idle channel */
863 if (atmel_port->desc_tx != NULL)
864 return;
865
866 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
867 /*
868 * DMA is idle now.
869 * Port xmit buffer is already mapped,
870 * and it is one page... Just adjust
871 * offsets and lengths. Since it is a circular buffer,
872 * we have to transmit till the end, and then the rest.
873 * Take the port lock to get a
874 * consistent xmit buffer state.
875 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200876 tx_len = CIRC_CNT_TO_END(xmit->head,
877 xmit->tail,
878 UART_XMIT_SIZE);
879
880 if (atmel_port->fifo_size) {
881 /* multi data mode */
882 part1_len = (tx_len & ~0x3); /* DWORD access */
883 part2_len = (tx_len & 0x3); /* BYTE access */
884 } else {
885 /* single data (legacy) mode */
886 part1_len = 0;
887 part2_len = tx_len; /* BYTE access only */
888 }
889
890 sg_init_table(sgl, 2);
891 sg_len = 0;
892 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
893 if (part1_len) {
894 sg = &sgl[sg_len++];
895 sg_dma_address(sg) = phys_addr;
896 sg_dma_len(sg) = part1_len;
897
898 phys_addr += part1_len;
899 }
900
901 if (part2_len) {
902 sg = &sgl[sg_len++];
903 sg_dma_address(sg) = phys_addr;
904 sg_dma_len(sg) = part2_len;
905 }
906
907 /*
908 * save tx_len so atmel_complete_tx_dma() will increase
909 * xmit->tail correctly
910 */
911 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800912
913 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200914 sgl,
915 sg_len,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100916 DMA_MEM_TO_DEV,
917 DMA_PREP_INTERRUPT |
918 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800919 if (!desc) {
920 dev_err(port->dev, "Failed to send via dma!\n");
921 return;
922 }
923
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200924 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800925
926 atmel_port->desc_tx = desc;
927 desc->callback = atmel_complete_tx_dma;
928 desc->callback_param = atmel_port;
929 atmel_port->cookie_tx = dmaengine_submit(desc);
Elen Song08f738b2013-07-22 16:30:26 +0800930 }
931
932 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
933 uart_write_wakeup(port);
934}
935
936static int atmel_prepare_tx_dma(struct uart_port *port)
937{
938 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
939 dma_cap_mask_t mask;
940 struct dma_slave_config config;
941 int ret, nent;
942
943 dma_cap_zero(mask);
944 dma_cap_set(DMA_SLAVE, mask);
945
946 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
947 if (atmel_port->chan_tx == NULL)
948 goto chan_err;
949 dev_info(port->dev, "using %s for tx DMA transfers\n",
950 dma_chan_name(atmel_port->chan_tx));
951
952 spin_lock_init(&atmel_port->lock_tx);
953 sg_init_table(&atmel_port->sg_tx, 1);
954 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800955 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800956 sg_set_page(&atmel_port->sg_tx,
957 virt_to_page(port->state->xmit.buf),
958 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200959 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800960 nent = dma_map_sg(port->dev,
961 &atmel_port->sg_tx,
962 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200963 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800964
965 if (!nent) {
966 dev_dbg(port->dev, "need to release resource of dma\n");
967 goto chan_err;
968 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200969 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800970 sg_dma_len(&atmel_port->sg_tx),
971 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200972 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800973 }
974
975 /* Configure the slave DMA */
976 memset(&config, 0, sizeof(config));
977 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200978 config.dst_addr_width = (atmel_port->fifo_size) ?
979 DMA_SLAVE_BUSWIDTH_4_BYTES :
980 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800981 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200982 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800983
Maxime Ripard5483c102014-10-22 17:43:16 +0200984 ret = dmaengine_slave_config(atmel_port->chan_tx,
985 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800986 if (ret) {
987 dev_err(port->dev, "DMA tx slave configuration failed\n");
988 goto chan_err;
989 }
990
991 return 0;
992
993chan_err:
994 dev_err(port->dev, "TX channel not available, switch to pio\n");
995 atmel_port->use_dma_tx = 0;
996 if (atmel_port->chan_tx)
997 atmel_release_tx_dma(port);
998 return -EINVAL;
999}
1000
Elen Song34df42f2013-07-22 16:30:27 +08001001static void atmel_complete_rx_dma(void *arg)
1002{
1003 struct uart_port *port = arg;
1004 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1005
Nicolas Ferre98f20822016-06-26 09:44:49 +02001006 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001007}
1008
1009static void atmel_release_rx_dma(struct uart_port *port)
1010{
1011 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1012 struct dma_chan *chan = atmel_port->chan_rx;
1013
1014 if (chan) {
1015 dmaengine_terminate_all(chan);
1016 dma_release_channel(chan);
1017 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +02001018 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001019 }
1020
1021 atmel_port->desc_rx = NULL;
1022 atmel_port->chan_rx = NULL;
1023 atmel_port->cookie_rx = -EINVAL;
1024}
1025
1026static void atmel_rx_from_dma(struct uart_port *port)
1027{
1028 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001029 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +08001030 struct circ_buf *ring = &atmel_port->rx_ring;
1031 struct dma_chan *chan = atmel_port->chan_rx;
1032 struct dma_tx_state state;
1033 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001034 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001035
1036
1037 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001038 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001039 dmastat = dmaengine_tx_status(chan,
1040 atmel_port->cookie_rx,
1041 &state);
1042 /* Restart a new tasklet if DMA status is error */
1043 if (dmastat == DMA_ERROR) {
1044 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001045 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001046 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001047 return;
1048 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001049
1050 /* CPU claims ownership of RX DMA buffer */
1051 dma_sync_sg_for_cpu(port->dev,
1052 &atmel_port->sg_rx,
1053 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001054 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001055
1056 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001057 * ring->head points to the end of data already written by the DMA.
1058 * ring->tail points to the beginning of data to be read by the
1059 * framework.
1060 * The current transfer size should not be larger than the dma buffer
1061 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001062 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001063 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1064 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1065 /*
1066 * At this point ring->head may point to the first byte right after the
1067 * last byte of the dma buffer:
1068 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1069 *
1070 * However ring->tail must always points inside the dma buffer:
1071 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1072 *
1073 * Since we use a ring buffer, we have to handle the case
1074 * where head is lower than tail. In such a case, we first read from
1075 * tail to the end of the buffer then reset tail.
1076 */
1077 if (ring->head < ring->tail) {
1078 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001079
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001080 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1081 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001082 port->icount.rx += count;
1083 }
1084
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001085 /* Finally we read data from tail to head */
1086 if (ring->tail < ring->head) {
1087 count = ring->head - ring->tail;
1088
1089 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1090 /* Wrap ring->head if needed */
1091 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1092 ring->head = 0;
1093 ring->tail = ring->head;
1094 port->icount.rx += count;
1095 }
1096
1097 /* USART retreives ownership of RX DMA buffer */
1098 dma_sync_sg_for_device(port->dev,
1099 &atmel_port->sg_rx,
1100 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001101 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001102
1103 /*
1104 * Drop the lock here since it might end up calling
1105 * uart_start(), which takes the lock.
1106 */
1107 spin_unlock(&port->lock);
1108 tty_flip_buffer_push(tport);
1109 spin_lock(&port->lock);
1110
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001111 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001112}
1113
1114static int atmel_prepare_rx_dma(struct uart_port *port)
1115{
1116 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1117 struct dma_async_tx_descriptor *desc;
1118 dma_cap_mask_t mask;
1119 struct dma_slave_config config;
1120 struct circ_buf *ring;
1121 int ret, nent;
1122
1123 ring = &atmel_port->rx_ring;
1124
1125 dma_cap_zero(mask);
1126 dma_cap_set(DMA_CYCLIC, mask);
1127
1128 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1129 if (atmel_port->chan_rx == NULL)
1130 goto chan_err;
1131 dev_info(port->dev, "using %s for rx DMA transfers\n",
1132 dma_chan_name(atmel_port->chan_rx));
1133
1134 spin_lock_init(&atmel_port->lock_rx);
1135 sg_init_table(&atmel_port->sg_rx, 1);
1136 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001137 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001138 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001139 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001140 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001141 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001142 nent = dma_map_sg(port->dev,
1143 &atmel_port->sg_rx,
1144 1,
1145 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001146
1147 if (!nent) {
1148 dev_dbg(port->dev, "need to release resource of dma\n");
1149 goto chan_err;
1150 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001151 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001152 sg_dma_len(&atmel_port->sg_rx),
1153 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001154 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001155 }
1156
1157 /* Configure the slave DMA */
1158 memset(&config, 0, sizeof(config));
1159 config.direction = DMA_DEV_TO_MEM;
1160 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1161 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001162 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001163
Maxime Ripard5483c102014-10-22 17:43:16 +02001164 ret = dmaengine_slave_config(atmel_port->chan_rx,
1165 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001166 if (ret) {
1167 dev_err(port->dev, "DMA rx slave configuration failed\n");
1168 goto chan_err;
1169 }
1170 /*
1171 * Prepare a cyclic dma transfer, assign 2 descriptors,
1172 * each one is half ring buffer size
1173 */
1174 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001175 sg_dma_address(&atmel_port->sg_rx),
1176 sg_dma_len(&atmel_port->sg_rx),
1177 sg_dma_len(&atmel_port->sg_rx)/2,
1178 DMA_DEV_TO_MEM,
1179 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001180 desc->callback = atmel_complete_rx_dma;
1181 desc->callback_param = port;
1182 atmel_port->desc_rx = desc;
1183 atmel_port->cookie_rx = dmaengine_submit(desc);
1184
1185 return 0;
1186
1187chan_err:
1188 dev_err(port->dev, "RX channel not available, switch to pio\n");
1189 atmel_port->use_dma_rx = 0;
1190 if (atmel_port->chan_rx)
1191 atmel_release_rx_dma(port);
1192 return -EINVAL;
1193}
1194
Elen Song2e68c222013-07-22 16:30:30 +08001195static void atmel_uart_timer_callback(unsigned long data)
1196{
1197 struct uart_port *port = (void *)data;
1198 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1199
Nicolas Ferre98f20822016-06-26 09:44:49 +02001200 if (!atomic_read(&atmel_port->tasklet_shutdown)) {
1201 tasklet_schedule(&atmel_port->tasklet_rx);
1202 mod_timer(&atmel_port->uart_timer,
1203 jiffies + uart_poll_timeout(port));
1204 }
Elen Song2e68c222013-07-22 16:30:30 +08001205}
1206
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001207/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001208 * receive interrupt handler.
1209 */
1210static void
1211atmel_handle_receive(struct uart_port *port, unsigned int pending)
1212{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001213 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001214
Elen Song64e22eb2013-07-22 16:30:24 +08001215 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001216 /*
1217 * PDC receive. Just schedule the tasklet and let it
1218 * figure out the details.
1219 *
1220 * TODO: We're not handling error flags correctly at
1221 * the moment.
1222 */
1223 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001224 atmel_uart_writel(port, ATMEL_US_IDR,
1225 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Nicolas Ferre98f20822016-06-26 09:44:49 +02001226 atmel_tasklet_schedule(atmel_port,
1227 &atmel_port->tasklet_rx);
Chip Coldwella6670612008-02-08 04:21:06 -08001228 }
1229
1230 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1231 ATMEL_US_FRAME | ATMEL_US_PARE))
1232 atmel_pdc_rxerr(port, pending);
1233 }
1234
Elen Song34df42f2013-07-22 16:30:27 +08001235 if (atmel_use_dma_rx(port)) {
1236 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001237 atmel_uart_writel(port, ATMEL_US_IDR,
1238 ATMEL_US_TIMEOUT);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001239 atmel_tasklet_schedule(atmel_port,
1240 &atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001241 }
1242 }
1243
Remy Bohmerb843aa22008-02-08 04:21:01 -08001244 /* Interrupt receive */
1245 if (pending & ATMEL_US_RXRDY)
1246 atmel_rx_chars(port);
1247 else if (pending & ATMEL_US_RXBRK) {
1248 /*
1249 * End of break detected. If it came along with a
1250 * character, atmel_rx_chars will handle it.
1251 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001252 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1253 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001254 atmel_port->break_active = 0;
1255 }
1256}
1257
1258/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001259 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001260 */
1261static void
1262atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1263{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001264 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001265
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001266 if (pending & atmel_port->tx_done_mask) {
1267 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001268 atmel_uart_writel(port, ATMEL_US_IDR,
1269 atmel_port->tx_done_mask);
Nicolas Ferre98f20822016-06-26 09:44:49 +02001270 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001271 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001272}
1273
1274/*
1275 * status flags interrupt handler.
1276 */
1277static void
1278atmel_handle_status(struct uart_port *port, unsigned int pending,
1279 unsigned int status)
1280{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001281 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Nicolas Ferre92052182016-06-17 12:05:46 +02001282 unsigned int status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001283
Remy Bohmerb843aa22008-02-08 04:21:01 -08001284 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001285 | ATMEL_US_CTSIC)) {
Nicolas Ferre92052182016-06-17 12:05:46 +02001286 status_change = status ^ atmel_port->irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +08001287 atmel_port->irq_status_prev = status;
Nicolas Ferre92052182016-06-17 12:05:46 +02001288
1289 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1290 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1291 /* TODO: All reads to CSR will clear these interrupts! */
1292 if (status_change & ATMEL_US_RI)
1293 port->icount.rng++;
1294 if (status_change & ATMEL_US_DSR)
1295 port->icount.dsr++;
1296 if (status_change & ATMEL_US_DCD)
1297 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1298 if (status_change & ATMEL_US_CTS)
1299 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1300
1301 wake_up_interruptible(&port->state->port.delta_msr_wait);
1302 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001303 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001304}
1305
1306/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001307 * Interrupt handler
1308 */
David Howells7d12e782006-10-05 14:55:46 +01001309static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001310{
1311 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001312 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001313 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001314
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001315 spin_lock(&atmel_port->lock_suspended);
1316
Chip Coldwella6670612008-02-08 04:21:06 -08001317 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001318 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001319 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001320 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001321 if (!pending)
1322 break;
1323
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001324 if (atmel_port->suspended) {
1325 atmel_port->pending |= pending;
1326 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001327 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001328 pm_system_wakeup();
1329 break;
1330 }
1331
Remy Bohmerb843aa22008-02-08 04:21:01 -08001332 atmel_handle_receive(port, pending);
1333 atmel_handle_status(port, pending, status);
1334 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001335 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001336
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001337 spin_unlock(&atmel_port->lock_suspended);
1338
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001339 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001340}
1341
Elen Songa930e522013-07-22 16:30:25 +08001342static void atmel_release_tx_pdc(struct uart_port *port)
1343{
1344 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1345 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1346
1347 dma_unmap_single(port->dev,
1348 pdc->dma_addr,
1349 pdc->dma_size,
1350 DMA_TO_DEVICE);
1351}
1352
Chip Coldwella6670612008-02-08 04:21:06 -08001353/*
1354 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1355 */
Elen Song64e22eb2013-07-22 16:30:24 +08001356static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001357{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001358 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001359 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001360 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1361 int count;
1362
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001363 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001364 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001365 return;
1366
Chip Coldwella6670612008-02-08 04:21:06 -08001367 xmit->tail += pdc->ofs;
1368 xmit->tail &= UART_XMIT_SIZE - 1;
1369
1370 port->icount.tx += pdc->ofs;
1371 pdc->ofs = 0;
1372
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001373 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001374
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001375 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001376 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001377
Itai Levi1f140812009-01-15 13:50:43 -08001378 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001379 dma_sync_single_for_device(port->dev,
1380 pdc->dma_addr,
1381 pdc->dma_size,
1382 DMA_TO_DEVICE);
1383
1384 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1385 pdc->ofs = count;
1386
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001387 atmel_uart_writel(port, ATMEL_PDC_TPR,
1388 pdc->dma_addr + xmit->tail);
1389 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001390 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001391 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001392 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001393 atmel_uart_writel(port, ATMEL_US_IER,
1394 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001395 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001396 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1397 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001398 /* DMA done, stop TX, start RX for RS485 */
1399 atmel_start_rx(port);
1400 }
Chip Coldwella6670612008-02-08 04:21:06 -08001401 }
1402
1403 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1404 uart_write_wakeup(port);
1405}
1406
Elen Songa930e522013-07-22 16:30:25 +08001407static int atmel_prepare_tx_pdc(struct uart_port *port)
1408{
1409 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1410 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1411 struct circ_buf *xmit = &port->state->xmit;
1412
1413 pdc->buf = xmit->buf;
1414 pdc->dma_addr = dma_map_single(port->dev,
1415 pdc->buf,
1416 UART_XMIT_SIZE,
1417 DMA_TO_DEVICE);
1418 pdc->dma_size = UART_XMIT_SIZE;
1419 pdc->ofs = 0;
1420
1421 return 0;
1422}
1423
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001424static void atmel_rx_from_ring(struct uart_port *port)
1425{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001426 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001427 struct circ_buf *ring = &atmel_port->rx_ring;
1428 unsigned int flg;
1429 unsigned int status;
1430
1431 while (ring->head != ring->tail) {
1432 struct atmel_uart_char c;
1433
1434 /* Make sure c is loaded after head. */
1435 smp_rmb();
1436
1437 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1438
1439 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1440
1441 port->icount.rx++;
1442 status = c.status;
1443 flg = TTY_NORMAL;
1444
1445 /*
1446 * note that the error handling code is
1447 * out of the main execution path
1448 */
1449 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1450 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1451 if (status & ATMEL_US_RXBRK) {
1452 /* ignore side-effect */
1453 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1454
1455 port->icount.brk++;
1456 if (uart_handle_break(port))
1457 continue;
1458 }
1459 if (status & ATMEL_US_PARE)
1460 port->icount.parity++;
1461 if (status & ATMEL_US_FRAME)
1462 port->icount.frame++;
1463 if (status & ATMEL_US_OVRE)
1464 port->icount.overrun++;
1465
1466 status &= port->read_status_mask;
1467
1468 if (status & ATMEL_US_RXBRK)
1469 flg = TTY_BREAK;
1470 else if (status & ATMEL_US_PARE)
1471 flg = TTY_PARITY;
1472 else if (status & ATMEL_US_FRAME)
1473 flg = TTY_FRAME;
1474 }
1475
1476
1477 if (uart_handle_sysrq_char(port, c.ch))
1478 continue;
1479
1480 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1481 }
1482
1483 /*
1484 * Drop the lock here since it might end up calling
1485 * uart_start(), which takes the lock.
1486 */
1487 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001488 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001489 spin_lock(&port->lock);
1490}
1491
Elen Songa930e522013-07-22 16:30:25 +08001492static void atmel_release_rx_pdc(struct uart_port *port)
1493{
1494 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1495 int i;
1496
1497 for (i = 0; i < 2; i++) {
1498 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1499
1500 dma_unmap_single(port->dev,
1501 pdc->dma_addr,
1502 pdc->dma_size,
1503 DMA_FROM_DEVICE);
1504 kfree(pdc->buf);
1505 }
1506}
1507
Elen Song64e22eb2013-07-22 16:30:24 +08001508static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001509{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001510 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001511 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001512 struct atmel_dma_buffer *pdc;
1513 int rx_idx = atmel_port->pdc_rx_idx;
1514 unsigned int head;
1515 unsigned int tail;
1516 unsigned int count;
1517
1518 do {
1519 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001520 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001521
1522 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001523 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001524 tail = pdc->ofs;
1525
1526 /* If the PDC has switched buffers, RPR won't contain
1527 * any address within the current buffer. Since head
1528 * is unsigned, we just need a one-way comparison to
1529 * find out.
1530 *
1531 * In this case, we just need to consume the entire
1532 * buffer and resubmit it for DMA. This will clear the
1533 * ENDRX bit as well, so that we can safely re-enable
1534 * all interrupts below.
1535 */
1536 head = min(head, pdc->dma_size);
1537
1538 if (likely(head != tail)) {
1539 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1540 pdc->dma_size, DMA_FROM_DEVICE);
1541
1542 /*
1543 * head will only wrap around when we recycle
1544 * the DMA buffer, and when that happens, we
1545 * explicitly set tail to 0. So head will
1546 * always be greater than tail.
1547 */
1548 count = head - tail;
1549
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001550 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1551 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001552
1553 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1554 pdc->dma_size, DMA_FROM_DEVICE);
1555
1556 port->icount.rx += count;
1557 pdc->ofs = head;
1558 }
1559
1560 /*
1561 * If the current buffer is full, we need to check if
1562 * the next one contains any additional data.
1563 */
1564 if (head >= pdc->dma_size) {
1565 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001566 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1567 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001568
1569 rx_idx = !rx_idx;
1570 atmel_port->pdc_rx_idx = rx_idx;
1571 }
1572 } while (head >= pdc->dma_size);
1573
1574 /*
1575 * Drop the lock here since it might end up calling
1576 * uart_start(), which takes the lock.
1577 */
1578 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001579 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001580 spin_lock(&port->lock);
1581
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001582 atmel_uart_writel(port, ATMEL_US_IER,
1583 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001584}
1585
Elen Songa930e522013-07-22 16:30:25 +08001586static int atmel_prepare_rx_pdc(struct uart_port *port)
1587{
1588 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1589 int i;
1590
1591 for (i = 0; i < 2; i++) {
1592 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1593
1594 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1595 if (pdc->buf == NULL) {
1596 if (i != 0) {
1597 dma_unmap_single(port->dev,
1598 atmel_port->pdc_rx[0].dma_addr,
1599 PDC_BUFFER_SIZE,
1600 DMA_FROM_DEVICE);
1601 kfree(atmel_port->pdc_rx[0].buf);
1602 }
1603 atmel_port->use_pdc_rx = 0;
1604 return -ENOMEM;
1605 }
1606 pdc->dma_addr = dma_map_single(port->dev,
1607 pdc->buf,
1608 PDC_BUFFER_SIZE,
1609 DMA_FROM_DEVICE);
1610 pdc->dma_size = PDC_BUFFER_SIZE;
1611 pdc->ofs = 0;
1612 }
1613
1614 atmel_port->pdc_rx_idx = 0;
1615
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001616 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1617 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001618
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001619 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1620 atmel_port->pdc_rx[1].dma_addr);
1621 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001622
1623 return 0;
1624}
1625
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001626/*
1627 * tasklet handling tty stuff outside the interrupt handler.
1628 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001629static void atmel_tasklet_rx_func(unsigned long data)
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001630{
1631 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001632 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001633
1634 /* The interrupt handler does not take the lock */
1635 spin_lock(&port->lock);
Elen Songa930e522013-07-22 16:30:25 +08001636 atmel_port->schedule_rx(port);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001637 spin_unlock(&port->lock);
1638}
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001639
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001640static void atmel_tasklet_tx_func(unsigned long data)
1641{
1642 struct uart_port *port = (struct uart_port *)data;
1643 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1644
1645 /* The interrupt handler does not take the lock */
1646 spin_lock(&port->lock);
1647 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001648 spin_unlock(&port->lock);
1649}
1650
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001651static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001652 struct platform_device *pdev)
1653{
1654 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001655 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001656
1657 if (np) {
1658 /* DMA/PDC usage specification */
Julia Lawall490d5ce2016-08-05 10:56:45 +02001659 if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1660 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001661 atmel_port->use_dma_rx = true;
1662 atmel_port->use_pdc_rx = false;
1663 } else {
1664 atmel_port->use_dma_rx = false;
1665 atmel_port->use_pdc_rx = true;
1666 }
1667 } else {
1668 atmel_port->use_dma_rx = false;
1669 atmel_port->use_pdc_rx = false;
1670 }
1671
Julia Lawall490d5ce2016-08-05 10:56:45 +02001672 if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1673 if (of_property_read_bool(np, "dmas")) {
Elen Song33d64c42013-07-22 16:30:28 +08001674 atmel_port->use_dma_tx = true;
1675 atmel_port->use_pdc_tx = false;
1676 } else {
1677 atmel_port->use_dma_tx = false;
1678 atmel_port->use_pdc_tx = true;
1679 }
1680 } else {
1681 atmel_port->use_dma_tx = false;
1682 atmel_port->use_pdc_tx = false;
1683 }
1684
1685 } else {
1686 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1687 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1688 atmel_port->use_dma_rx = false;
1689 atmel_port->use_dma_tx = false;
1690 }
1691
Elen Song33d64c42013-07-22 16:30:28 +08001692}
1693
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001694static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001695 struct platform_device *pdev)
1696{
1697 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001698 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001699
1700 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001701 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001702 u32 rs485_delay[2];
1703 /* rs485 properties */
1704 if (of_property_read_u32_array(np, "rs485-rts-delay",
1705 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001706 rs485conf->delay_rts_before_send = rs485_delay[0];
1707 rs485conf->delay_rts_after_send = rs485_delay[1];
1708 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001709 }
Elen Song33d64c42013-07-22 16:30:28 +08001710
1711 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1712 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1713
1714 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1715 NULL))
1716 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001717 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001718 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001719 }
1720
1721}
1722
Elen Songa930e522013-07-22 16:30:25 +08001723static void atmel_set_ops(struct uart_port *port)
1724{
1725 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1726
Elen Song34df42f2013-07-22 16:30:27 +08001727 if (atmel_use_dma_rx(port)) {
1728 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1729 atmel_port->schedule_rx = &atmel_rx_from_dma;
1730 atmel_port->release_rx = &atmel_release_rx_dma;
1731 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001732 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1733 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1734 atmel_port->release_rx = &atmel_release_rx_pdc;
1735 } else {
1736 atmel_port->prepare_rx = NULL;
1737 atmel_port->schedule_rx = &atmel_rx_from_ring;
1738 atmel_port->release_rx = NULL;
1739 }
1740
Elen Song08f738b2013-07-22 16:30:26 +08001741 if (atmel_use_dma_tx(port)) {
1742 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1743 atmel_port->schedule_tx = &atmel_tx_dma;
1744 atmel_port->release_tx = &atmel_release_tx_dma;
1745 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001746 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1747 atmel_port->schedule_tx = &atmel_tx_pdc;
1748 atmel_port->release_tx = &atmel_release_tx_pdc;
1749 } else {
1750 atmel_port->prepare_tx = NULL;
1751 atmel_port->schedule_tx = &atmel_tx_chars;
1752 atmel_port->release_tx = NULL;
1753 }
1754}
1755
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001756/*
Elen Song055560b2013-07-22 16:30:29 +08001757 * Get ip name usart or uart
1758 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001759static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001760{
1761 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001762 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001763 u32 version;
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001764 u32 usart, dbgu_uart, new_uart;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001765 /* ASCII decoding for IP version */
1766 usart = 0x55534152; /* USAR(T) */
1767 dbgu_uart = 0x44424755; /* DBGU */
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001768 new_uart = 0x55415254; /* UART */
Elen Song055560b2013-07-22 16:30:29 +08001769
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001770 /*
1771 * Only USART devices from at91sam9260 SOC implement fractional
1772 * baudrate.
1773 */
1774 atmel_port->has_frac_baudrate = false;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001775 atmel_port->has_hw_timer = false;
Elen Song055560b2013-07-22 16:30:29 +08001776
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001777 if (name == new_uart) {
1778 dev_dbg(port->dev, "Uart with hw timer");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001779 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001780 atmel_port->rtor = ATMEL_UA_RTOR;
1781 } else if (name == usart) {
1782 dev_dbg(port->dev, "Usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001783 atmel_port->has_frac_baudrate = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001784 atmel_port->has_hw_timer = true;
1785 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001786 } else if (name == dbgu_uart) {
1787 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
Elen Song055560b2013-07-22 16:30:29 +08001788 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001789 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001790 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001791 switch (version) {
1792 case 0x302:
1793 case 0x10213:
1794 dev_dbg(port->dev, "This version is usart\n");
Ludovic Desroches5bf56352016-08-25 15:47:56 +02001795 atmel_port->has_frac_baudrate = true;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001796 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001797 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001798 break;
1799 case 0x203:
1800 case 0x10202:
1801 dev_dbg(port->dev, "This version is uart\n");
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001802 break;
1803 default:
1804 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1805 }
Elen Song055560b2013-07-22 16:30:29 +08001806 }
Elen Song055560b2013-07-22 16:30:29 +08001807}
1808
1809/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001810 * Perform initialization and enable port for reception
1811 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001812static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001813{
Elen Song33d64c42013-07-22 16:30:28 +08001814 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001815 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001816 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001817 int retval;
1818
1819 /*
1820 * Ensure that no interrupts are enabled otherwise when
1821 * request_irq() is called we could get stuck trying to
1822 * handle an unexpected interrupt
1823 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001824 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001825 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001826
1827 /*
1828 * Allocate the IRQ
1829 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001830 retval = request_irq(port->irq, atmel_interrupt,
1831 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001832 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001833 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001834 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001835 return retval;
1836 }
1837
Nicolas Ferre98f20822016-06-26 09:44:49 +02001838 atomic_set(&atmel_port->tasklet_shutdown, 0);
1839 tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
1840 (unsigned long)port);
1841 tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
1842 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08001843
Richard Genoudab5e4e42014-05-13 20:20:45 +02001844 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001845 * Initialize DMA (if necessary)
1846 */
Elen Song33d64c42013-07-22 16:30:28 +08001847 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001848 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001849
Elen Songa930e522013-07-22 16:30:25 +08001850 if (atmel_port->prepare_rx) {
1851 retval = atmel_port->prepare_rx(port);
1852 if (retval < 0)
1853 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001854 }
1855
Elen Songa930e522013-07-22 16:30:25 +08001856 if (atmel_port->prepare_tx) {
1857 retval = atmel_port->prepare_tx(port);
1858 if (retval < 0)
1859 atmel_set_ops(port);
1860 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001861
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001862 /*
1863 * Enable FIFO when available
1864 */
1865 if (atmel_port->fifo_size) {
1866 unsigned int txrdym = ATMEL_US_ONE_DATA;
1867 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1868 unsigned int fmr;
1869
1870 atmel_uart_writel(port, ATMEL_US_CR,
1871 ATMEL_US_FIFOEN |
1872 ATMEL_US_RXFCLR |
1873 ATMEL_US_TXFLCLR);
1874
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001875 if (atmel_use_dma_tx(port))
1876 txrdym = ATMEL_US_FOUR_DATA;
1877
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001878 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1879 if (atmel_port->rts_high &&
1880 atmel_port->rts_low)
1881 fmr |= ATMEL_US_FRTSC |
1882 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1883 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1884
1885 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1886 }
1887
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001888 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001889 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001890
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001891 /*
1892 * Finally, enable the serial port
1893 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001894 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001895 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001896 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001897
Marek Roszko8bc661b2014-01-10 10:33:11 +01001898 setup_timer(&atmel_port->uart_timer,
1899 atmel_uart_timer_callback,
1900 (unsigned long)port);
1901
Elen Song64e22eb2013-07-22 16:30:24 +08001902 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001903 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001904 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001905 mod_timer(&atmel_port->uart_timer,
1906 jiffies + uart_poll_timeout(port));
1907 /* set USART timeout */
1908 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001909 atmel_uart_writel(port, atmel_port->rtor,
1910 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001911 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001912
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001913 atmel_uart_writel(port, ATMEL_US_IER,
1914 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001915 }
Chip Coldwella6670612008-02-08 04:21:06 -08001916 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001917 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001918 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001919 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001920 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001921 mod_timer(&atmel_port->uart_timer,
1922 jiffies + uart_poll_timeout(port));
1923 /* set USART timeout */
1924 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001925 atmel_uart_writel(port, atmel_port->rtor,
1926 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001927 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001928
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001929 atmel_uart_writel(port, ATMEL_US_IER,
1930 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001931 }
Chip Coldwella6670612008-02-08 04:21:06 -08001932 } else {
1933 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001934 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001935 }
Andrew Victorafefc412006-06-19 19:53:19 +01001936
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001937 return 0;
1938}
1939
1940/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001941 * Flush any TX data submitted for DMA. Called when the TX circular
1942 * buffer is reset.
1943 */
1944static void atmel_flush_buffer(struct uart_port *port)
1945{
1946 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1947
1948 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001949 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001950 atmel_port->pdc_tx.ofs = 0;
1951 }
1952}
1953
1954/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001955 * Disable the port
1956 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001957static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001958{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001959 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001960
Richard Genoud0ae9fde2016-09-12 15:34:41 +02001961 /* Disable modem control lines interrupts */
1962 atmel_disable_ms(port);
1963
Nicolas Ferre98f20822016-06-26 09:44:49 +02001964 /* Disable interrupts at device level */
1965 atmel_uart_writel(port, ATMEL_US_IDR, -1);
1966
1967 /* Prevent spurious interrupts from scheduling the tasklet */
1968 atomic_inc(&atmel_port->tasklet_shutdown);
1969
Chip Coldwella6670612008-02-08 04:21:06 -08001970 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001971 * Prevent any tasklets being scheduled during
1972 * cleanup
1973 */
1974 del_timer_sync(&atmel_port->uart_timer);
1975
Nicolas Ferre98f20822016-06-26 09:44:49 +02001976 /* Make sure that no interrupt is on the fly */
1977 synchronize_irq(port->irq);
1978
Marek Roszko8bc661b2014-01-10 10:33:11 +01001979 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001980 * Clear out any scheduled tasklets before
1981 * we destroy the buffers
1982 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001983 tasklet_kill(&atmel_port->tasklet_rx);
1984 tasklet_kill(&atmel_port->tasklet_tx);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001985
1986 /*
1987 * Ensure everything is stopped and
Nicolas Ferre98f20822016-06-26 09:44:49 +02001988 * disable port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001989 */
1990 atmel_stop_rx(port);
1991 atmel_stop_tx(port);
1992
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001993 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001994
Chip Coldwella6670612008-02-08 04:21:06 -08001995 /*
1996 * Shut-down the DMA.
1997 */
Elen Songa930e522013-07-22 16:30:25 +08001998 if (atmel_port->release_rx)
1999 atmel_port->release_rx(port);
2000 if (atmel_port->release_tx)
2001 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08002002
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002003 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01002004 * Reset ring buffer pointers
2005 */
2006 atmel_port->rx_ring.head = 0;
2007 atmel_port->rx_ring.tail = 0;
2008
2009 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02002010 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002011 */
2012 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02002013
Peter Hurley479e9b92014-10-16 16:54:18 -04002014 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002015}
2016
2017/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002018 * Power / Clock management.
2019 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002020static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2021 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002022{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002023 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002024
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002025 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002026 case 0:
2027 /*
2028 * Enable the peripheral clock for this serial port.
2029 * This is called on uart_open() or a resume event.
2030 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002031 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07002032
2033 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002034 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002035 break;
2036 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07002037 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002038 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2039 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07002040
Remy Bohmerb843aa22008-02-08 04:21:01 -08002041 /*
2042 * Disable the peripheral clock for this serial port.
2043 * This is called on uart_close() or a suspend event.
2044 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002045 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002046 break;
2047 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01002048 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002049 }
2050}
2051
2052/*
2053 * Change the port parameters
2054 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002055static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2056 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002057{
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002058 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002059 unsigned long flags;
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002060 unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002061
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002062 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002063 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002064
2065 /* reset the mode, clock divisor, parity, stop bits and data size */
2066 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2067 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002068
Remy Bohmerb843aa22008-02-08 04:21:01 -08002069 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002070
2071 /* byte size */
2072 switch (termios->c_cflag & CSIZE) {
2073 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002074 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002075 break;
2076 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002077 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002078 break;
2079 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002080 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002081 break;
2082 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002083 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002084 break;
2085 }
2086
2087 /* stop bits */
2088 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002089 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002090
2091 /* parity */
2092 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002093 /* Mark or Space parity */
2094 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002095 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002096 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002097 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002098 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002099 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002100 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002101 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002102 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002103 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002104 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002105
2106 spin_lock_irqsave(&port->lock, flags);
2107
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002108 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002109 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002110 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002111 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002112 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002113
Elen Song64e22eb2013-07-22 16:30:24 +08002114 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002115 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002116 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002117
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002118 /*
2119 * Characters to ignore
2120 */
2121 port->ignore_status_mask = 0;
2122 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002123 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002124 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002125 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002126 /*
2127 * If we're ignoring parity and break indicators,
2128 * ignore overruns too (for real raw support).
2129 */
2130 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002131 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002132 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002133 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002134
2135 /* update the per-port timeout */
2136 uart_update_timeout(port, termios->c_cflag, baud);
2137
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002138 /*
2139 * save/disable interrupts. The tty layer will ensure that the
2140 * transmitter is empty if requested by the caller, so there's
2141 * no need to wait for it here.
2142 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002143 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2144 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002145
2146 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002147 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002148
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002149 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002150 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002151 atmel_uart_writel(port, ATMEL_US_TTGR,
2152 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002153 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002154 } else if (termios->c_cflag & CRTSCTS) {
2155 /* RS232 with hardware handshake (RTS/CTS) */
Richard Genoud9bcffe72016-10-27 18:04:06 +02002156 if (atmel_use_fifo(port) &&
2157 !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
2158 /*
2159 * with ATMEL_US_USMODE_HWHS set, the controller will
2160 * be able to drive the RTS pin high/low when the RX
2161 * FIFO is above RXFTHRES/below RXFTHRES2.
2162 * It will also disable the transmitter when the CTS
2163 * pin is high.
2164 * This mode is not activated if CTS pin is a GPIO
2165 * because in this case, the transmitter is always
2166 * disabled (there must be an internal pull-up
2167 * responsible for this behaviour).
2168 * If the RTS pin is a GPIO, the controller won't be
2169 * able to drive it according to the FIFO thresholds,
2170 * but it will be handled by the driver.
2171 */
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002172 mode |= ATMEL_US_USMODE_HWHS;
Richard Genoud9bcffe72016-10-27 18:04:06 +02002173 } else {
2174 /*
2175 * For platforms without FIFO, the flow control is
2176 * handled by the driver.
2177 */
2178 mode |= ATMEL_US_USMODE_NORMAL;
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002179 }
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002180 } else {
2181 /* RS232 without hadware handshake */
2182 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002183 }
2184
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002185 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002186 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002187
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002188 /*
2189 * when switching the mode, set the RTS line state according to the
2190 * new mode, otherwise keep the former state
2191 */
2192 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2193 unsigned int rts_state;
2194
2195 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2196 /* let the hardware control the RTS line */
2197 rts_state = ATMEL_US_RTSDIS;
2198 } else {
2199 /* force RTS line to low level */
2200 rts_state = ATMEL_US_RTSEN;
2201 }
2202
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002203 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002204 }
2205
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002206 /*
2207 * Set the baud rate:
2208 * Fractional baudrate allows to setup output frequency more
2209 * accurately. This feature is enabled only when using normal mode.
2210 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
2211 * Currently, OVER is always set to 0 so we get
Alexey Starikovskiy36131cd2016-09-21 12:44:14 +02002212 * baudrate = selected clock / (16 * (CD + FP / 8))
2213 * then
2214 * 8 CD + FP = selected clock / (2 * baudrate)
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002215 */
2216 if (atmel_port->has_frac_baudrate &&
2217 (mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_NORMAL) {
Alexey Starikovskiy36131cd2016-09-21 12:44:14 +02002218 div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
2219 cd = div >> 3;
2220 fp = div & ATMEL_US_FP_MASK;
Ludovic Desroches5bf56352016-08-25 15:47:56 +02002221 } else {
2222 cd = uart_get_divisor(port, baud);
2223 }
2224
2225 if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */
2226 cd /= 8;
2227 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2228 }
2229 quot = cd | fp << ATMEL_US_FP_OFFSET;
2230
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002231 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2232 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2233 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002234
2235 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002236 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002237
2238 /* CTS flow-control and modem-status interrupts */
2239 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002240 atmel_enable_ms(port);
2241 else
2242 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002243
2244 spin_unlock_irqrestore(&port->lock, flags);
2245}
2246
Peter Hurley732a84a2014-11-05 13:11:43 -05002247static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002248{
Peter Hurley732a84a2014-11-05 13:11:43 -05002249 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002250 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002251 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002252 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002253 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002254 } else {
2255 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002256 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2257 spin_lock_irq(&port->lock);
2258 atmel_disable_ms(port);
2259 spin_unlock_irq(&port->lock);
2260 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002261 }
2262}
2263
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002264/*
2265 * Return string describing the specified port
2266 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002267static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002268{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002269 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002270}
2271
2272/*
2273 * Release the memory region(s) being used by 'port'.
2274 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002275static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002276{
Andrew Victorafefc412006-06-19 19:53:19 +01002277 struct platform_device *pdev = to_platform_device(port->dev);
2278 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2279
2280 release_mem_region(port->mapbase, size);
2281
2282 if (port->flags & UPF_IOREMAP) {
2283 iounmap(port->membase);
2284 port->membase = NULL;
2285 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002286}
2287
2288/*
2289 * Request the memory region(s) being used by 'port'.
2290 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002291static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002292{
Andrew Victorafefc412006-06-19 19:53:19 +01002293 struct platform_device *pdev = to_platform_device(port->dev);
2294 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002295
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002296 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002297 return -EBUSY;
2298
2299 if (port->flags & UPF_IOREMAP) {
2300 port->membase = ioremap(port->mapbase, size);
2301 if (port->membase == NULL) {
2302 release_mem_region(port->mapbase, size);
2303 return -ENOMEM;
2304 }
2305 }
2306
2307 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002308}
2309
2310/*
2311 * Configure/autoconfigure the port.
2312 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002313static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002314{
2315 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002316 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002317 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002318 }
2319}
2320
2321/*
2322 * Verify the new serial_struct (for TIOCSSERIAL).
2323 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002324static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002325{
2326 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002327 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002328 ret = -EINVAL;
2329 if (port->irq != ser->irq)
2330 ret = -EINVAL;
2331 if (ser->io_type != SERIAL_IO_MEM)
2332 ret = -EINVAL;
2333 if (port->uartclk / 16 != ser->baud_base)
2334 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002335 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002336 ret = -EINVAL;
2337 if (port->iobase != ser->port)
2338 ret = -EINVAL;
2339 if (ser->hub6 != 0)
2340 ret = -EINVAL;
2341 return ret;
2342}
2343
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002344#ifdef CONFIG_CONSOLE_POLL
2345static int atmel_poll_get_char(struct uart_port *port)
2346{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002347 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002348 cpu_relax();
2349
Cyrille Pitchena6499432015-07-30 16:33:38 +02002350 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002351}
2352
2353static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2354{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002355 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002356 cpu_relax();
2357
Cyrille Pitchena6499432015-07-30 16:33:38 +02002358 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002359}
2360#endif
2361
Julia Lawall5c7dcdb2016-09-01 19:51:31 +02002362static const struct uart_ops atmel_pops = {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002363 .tx_empty = atmel_tx_empty,
2364 .set_mctrl = atmel_set_mctrl,
2365 .get_mctrl = atmel_get_mctrl,
2366 .stop_tx = atmel_stop_tx,
2367 .start_tx = atmel_start_tx,
2368 .stop_rx = atmel_stop_rx,
2369 .enable_ms = atmel_enable_ms,
2370 .break_ctl = atmel_break_ctl,
2371 .startup = atmel_startup,
2372 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002373 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002374 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002375 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002376 .type = atmel_type,
2377 .release_port = atmel_release_port,
2378 .request_port = atmel_request_port,
2379 .config_port = atmel_config_port,
2380 .verify_port = atmel_verify_port,
2381 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002382#ifdef CONFIG_CONSOLE_POLL
2383 .poll_get_char = atmel_poll_get_char,
2384 .poll_put_char = atmel_poll_put_char,
2385#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002386};
2387
Andrew Victorafefc412006-06-19 19:53:19 +01002388/*
2389 * Configure the port from the platform device resource info.
2390 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002391static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002392 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002393{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002394 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002395 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002396 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002397
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002398 atmel_init_property(atmel_port, pdev);
2399 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002400
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002401 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002402
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002403 port->iotype = UPIO_MEM;
2404 port->flags = UPF_BOOT_AUTOCONF;
2405 port->ops = &atmel_pops;
2406 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002407 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002408 port->mapbase = pdev->resource[0].start;
2409 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002410 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002411
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002412 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2413
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002414 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002415 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002416 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002417 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002418 port->flags |= UPF_IOREMAP;
2419 port->membase = NULL;
2420 }
2421
Remy Bohmerb843aa22008-02-08 04:21:01 -08002422 /* for console, the clock could already be configured */
2423 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002424 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002425 if (IS_ERR(atmel_port->clk)) {
2426 ret = PTR_ERR(atmel_port->clk);
2427 atmel_port->clk = NULL;
2428 return ret;
2429 }
2430 ret = clk_prepare_enable(atmel_port->clk);
2431 if (ret) {
2432 clk_put(atmel_port->clk);
2433 atmel_port->clk = NULL;
2434 return ret;
2435 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002436 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002437 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002438 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002439 }
Chip Coldwella6670612008-02-08 04:21:06 -08002440
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002441 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002442 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002443 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002444 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002445 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002446 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2447 } else {
2448 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2449 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002450
2451 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002452}
2453
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002454struct platform_device *atmel_default_console_device; /* the serial console device */
2455
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002456#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002457static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002458{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002459 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002460 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002461 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002462}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002463
2464/*
2465 * Interrupts are disabled on entering
2466 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002467static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002468{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002469 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002470 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002471 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002472 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002473
2474 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002475 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002476 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002477 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2478 atmel_uart_writel(port, ATMEL_US_IDR,
2479 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002480
Marc Pignat39d4c922008-04-02 13:04:42 -07002481 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002482 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2483 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002484
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002485 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002486
2487 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002488 * Finally, wait for transmitter to become empty
2489 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002490 */
2491 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002492 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002493 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002494
2495 /* Restore PDC transmit status */
2496 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002497 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002498
Remy Bohmerb843aa22008-02-08 04:21:01 -08002499 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002500 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002501}
2502
2503/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002504 * If the port was already initialised (eg, by a boot loader),
2505 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002506 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002507static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2508 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002509{
2510 unsigned int mr, quot;
2511
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002512 /*
2513 * If the baud rate generator isn't running, the port wasn't
2514 * initialized by the boot loader.
2515 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002516 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002517 if (!quot)
2518 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002519
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002520 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002521 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002522 *bits = 8;
2523 else
2524 *bits = 7;
2525
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002526 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002527 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002528 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002529 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002530 *parity = 'o';
2531
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002532 /*
2533 * The serial core only rounds down when matching this to a
2534 * supported baud rate. Make sure we don't end up slightly
2535 * lower than one of those, as it would make us fall through
2536 * to a much lower baud rate than we really want.
2537 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002538 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002539}
2540
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002541static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002542{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002543 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002544 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002545 int baud = 115200;
2546 int bits = 8;
2547 int parity = 'n';
2548 int flow = 'n';
2549
Remy Bohmerb843aa22008-02-08 04:21:01 -08002550 if (port->membase == NULL) {
2551 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002552 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002553 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002554
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002555 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2556 if (ret)
2557 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002558
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002559 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2560 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2561 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002562
2563 if (options)
2564 uart_parse_options(options, &baud, &parity, &bits, &flow);
2565 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002566 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002567
2568 return uart_set_options(port, co, baud, parity, bits, flow);
2569}
2570
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002571static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002572
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002573static struct console atmel_console = {
2574 .name = ATMEL_DEVICENAME,
2575 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002576 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002577 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002578 .flags = CON_PRINTBUFFER,
2579 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002580 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002581};
2582
David Brownell06a7f052008-11-06 12:53:40 -08002583#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002584
Andrew Victorafefc412006-06-19 19:53:19 +01002585/*
2586 * Early console initialization (before VM subsystem initialized).
2587 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002588static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002589{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002590 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002591 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002592 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002593 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002594 int id = pdata->num;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002595 struct atmel_uart_port *atmel_port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002596
Jaeden Amerob78cd162016-01-26 12:34:49 +01002597 atmel_port->backup_imr = 0;
2598 atmel_port->uart.line = id;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002599
2600 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002601 ret = atmel_init_port(atmel_port, atmel_default_console_device);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002602 if (ret)
2603 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002604 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002605 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002606
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002607 return 0;
2608}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002609
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002610console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002611
Andrew Victorafefc412006-06-19 19:53:19 +01002612/*
2613 * Late console initialization.
2614 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002615static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002616{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002617 if (atmel_default_console_device
2618 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002619 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002620
2621 return 0;
2622}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002623
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002624core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002625
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002626static inline bool atmel_is_console_port(struct uart_port *port)
2627{
2628 return port->cons && port->cons->index == port->line;
2629}
2630
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002631#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002632#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002633
2634static inline bool atmel_is_console_port(struct uart_port *port)
2635{
2636 return false;
2637}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002638#endif
2639
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002640static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002641 .owner = THIS_MODULE,
2642 .driver_name = "atmel_serial",
2643 .dev_name = ATMEL_DEVICENAME,
2644 .major = SERIAL_ATMEL_MAJOR,
2645 .minor = MINOR_START,
2646 .nr = ATMEL_MAX_UART,
2647 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002648};
2649
Andrew Victorafefc412006-06-19 19:53:19 +01002650#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002651static bool atmel_serial_clk_will_stop(void)
2652{
2653#ifdef CONFIG_ARCH_AT91
2654 return at91_suspend_entering_slow_clock();
2655#else
2656 return false;
2657#endif
2658}
2659
Remy Bohmerb843aa22008-02-08 04:21:01 -08002660static int atmel_serial_suspend(struct platform_device *pdev,
2661 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002662{
Andrew Victorafefc412006-06-19 19:53:19 +01002663 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002664 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002665
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002666 if (atmel_is_console_port(port) && console_suspend_enabled) {
2667 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002668 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2669 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002670 cpu_relax();
2671 }
2672
Alexandre Belloni6a5f0e22017-02-03 23:53:16 +01002673 if (atmel_is_console_port(port) && !console_suspend_enabled) {
2674 /* Cache register values as we won't get a full shutdown/startup
2675 * cycle
2676 */
2677 atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
2678 atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
2679 atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
2680 atmel_port->cache.rtor = atmel_uart_readl(port,
2681 atmel_port->rtor);
2682 atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
2683 atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
2684 atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
2685 }
2686
Anti Sullinf05596d2008-09-22 13:57:54 -07002687 /* we can not wake up if we're running on slow clock */
2688 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002689 if (atmel_serial_clk_will_stop()) {
2690 unsigned long flags;
2691
2692 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2693 atmel_port->suspended = true;
2694 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002695 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002696 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002697
2698 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002699
2700 return 0;
2701}
2702
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002703static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002704{
2705 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002706 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002707 unsigned long flags;
2708
Alexandre Belloni6a5f0e22017-02-03 23:53:16 +01002709 if (atmel_is_console_port(port) && !console_suspend_enabled) {
2710 atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
2711 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
2712 atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
2713 atmel_uart_writel(port, atmel_port->rtor,
2714 atmel_port->cache.rtor);
2715 atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
2716
2717 if (atmel_port->fifo_size) {
2718 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
2719 ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
2720 atmel_uart_writel(port, ATMEL_US_FMR,
2721 atmel_port->cache.fmr);
2722 atmel_uart_writel(port, ATMEL_US_FIER,
2723 atmel_port->cache.fimr);
2724 }
2725 atmel_start_rx(port);
2726 }
2727
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002728 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2729 if (atmel_port->pending) {
2730 atmel_handle_receive(port, atmel_port->pending);
2731 atmel_handle_status(port, atmel_port->pending,
2732 atmel_port->pending_status);
2733 atmel_handle_transmit(port, atmel_port->pending);
2734 atmel_port->pending = 0;
2735 }
2736 atmel_port->suspended = false;
2737 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002738
Anti Sullinf05596d2008-09-22 13:57:54 -07002739 uart_resume_port(&atmel_uart, port);
2740 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002741
2742 return 0;
2743}
2744#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002745#define atmel_serial_suspend NULL
2746#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002747#endif
2748
Jaeden Amerob78cd162016-01-26 12:34:49 +01002749static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002750 struct platform_device *pdev)
2751{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002752 atmel_port->fifo_size = 0;
2753 atmel_port->rts_low = 0;
2754 atmel_port->rts_high = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002755
2756 if (of_property_read_u32(pdev->dev.of_node,
2757 "atmel,fifo-size",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002758 &atmel_port->fifo_size))
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002759 return;
2760
Jaeden Amerob78cd162016-01-26 12:34:49 +01002761 if (!atmel_port->fifo_size)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002762 return;
2763
Jaeden Amerob78cd162016-01-26 12:34:49 +01002764 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2765 atmel_port->fifo_size = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002766 dev_err(&pdev->dev, "Invalid FIFO size\n");
2767 return;
2768 }
2769
2770 /*
2771 * 0 <= rts_low <= rts_high <= fifo_size
2772 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2773 * to flush their internal TX FIFO, commonly up to 16 data, before
2774 * actually stopping to send new data. So we try to set the RTS High
2775 * Threshold to a reasonably high value respecting this 16 data
2776 * empirical rule when possible.
2777 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002778 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2779 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2780 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2,
2781 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002782
2783 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002784 atmel_port->fifo_size);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002785 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002786 atmel_port->rts_high);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002787 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002788 atmel_port->rts_low);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002789}
2790
Bill Pemberton9671f092012-11-19 13:21:50 -05002791static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002792{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002793 struct atmel_uart_port *atmel_port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002794 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002795 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002796 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002797 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002798 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002799
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002800 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002801
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002802 if (np)
2803 ret = of_alias_get_id(np, "serial");
2804 else
2805 if (pdata)
2806 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002807
2808 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002809 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002810 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002811 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002812
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002813 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002814 ret = -ENODEV;
2815 goto err;
2816 }
2817
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002818 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002819 /* port already in use */
2820 ret = -EBUSY;
2821 goto err;
2822 }
2823
Jaeden Amerob78cd162016-01-26 12:34:49 +01002824 atmel_port = &atmel_ports[ret];
2825 atmel_port->backup_imr = 0;
2826 atmel_port->uart.line = ret;
2827 atmel_serial_probe_fifos(atmel_port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002828
Nicolas Ferre98f20822016-06-26 09:44:49 +02002829 atomic_set(&atmel_port->tasklet_shutdown, 0);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002830 spin_lock_init(&atmel_port->lock_suspended);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002831
Jaeden Amerob78cd162016-01-26 12:34:49 +01002832 ret = atmel_init_port(atmel_port, pdev);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002833 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002834 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002835
Jaeden Amerob78cd162016-01-26 12:34:49 +01002836 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2837 if (IS_ERR(atmel_port->gpios)) {
2838 ret = PTR_ERR(atmel_port->gpios);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002839 goto err_clear_bit;
2840 }
2841
Jaeden Amerob78cd162016-01-26 12:34:49 +01002842 if (!atmel_use_pdc_rx(&atmel_port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002843 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002844 data = kmalloc(sizeof(struct atmel_uart_char)
2845 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002846 if (!data)
2847 goto err_alloc_ring;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002848 atmel_port->rx_ring.buf = data;
Chip Coldwella6670612008-02-08 04:21:06 -08002849 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002850
Jaeden Amerob78cd162016-01-26 12:34:49 +01002851 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002852
Jaeden Amerob78cd162016-01-26 12:34:49 +01002853 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002854 if (ret)
2855 goto err_add_port;
2856
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002857#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Jaeden Amerob78cd162016-01-26 12:34:49 +01002858 if (atmel_is_console_port(&atmel_port->uart)
David Brownell06a7f052008-11-06 12:53:40 -08002859 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2860 /*
2861 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002862 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002863 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002864 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002865 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002866#endif
David Brownell06a7f052008-11-06 12:53:40 -08002867
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002868 device_init_wakeup(&pdev->dev, 1);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002869 platform_set_drvdata(pdev, atmel_port);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002870
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002871 /*
2872 * The peripheral clock has been disabled by atmel_init_port():
2873 * enable it before accessing I/O registers
2874 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002875 clk_prepare_enable(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002876
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002877 if (rs485_enabled) {
Jaeden Amerob78cd162016-01-26 12:34:49 +01002878 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002879 ATMEL_US_USMODE_NORMAL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002880 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2881 ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002882 }
2883
Elen Song055560b2013-07-22 16:30:29 +08002884 /*
2885 * Get port name of usart or uart
2886 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002887 atmel_get_ip_name(&atmel_port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002888
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002889 /*
2890 * The peripheral clock can now safely be disabled till the port
2891 * is used
2892 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002893 clk_disable_unprepare(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002894
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002895 return 0;
2896
2897err_add_port:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002898 kfree(atmel_port->rx_ring.buf);
2899 atmel_port->rx_ring.buf = NULL;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002900err_alloc_ring:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002901 if (!atmel_is_console_port(&atmel_port->uart)) {
2902 clk_put(atmel_port->clk);
2903 atmel_port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002904 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002905err_clear_bit:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002906 clear_bit(atmel_port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002907err:
Andrew Victorafefc412006-06-19 19:53:19 +01002908 return ret;
2909}
2910
Romain Izardf4a8ab042016-02-26 11:15:04 +01002911/*
2912 * Even if the driver is not modular, it makes sense to be able to
2913 * unbind a device: there can be many bound devices, and there are
2914 * situations where dynamic binding and unbinding can be useful.
2915 *
2916 * For example, a connected device can require a specific firmware update
2917 * protocol that needs bitbanging on IO lines, but use the regular serial
2918 * port in the normal case.
2919 */
2920static int atmel_serial_remove(struct platform_device *pdev)
2921{
2922 struct uart_port *port = platform_get_drvdata(pdev);
2923 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2924 int ret = 0;
2925
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002926 tasklet_kill(&atmel_port->tasklet_rx);
2927 tasklet_kill(&atmel_port->tasklet_tx);
Romain Izardf4a8ab042016-02-26 11:15:04 +01002928
2929 device_init_wakeup(&pdev->dev, 0);
2930
2931 ret = uart_remove_one_port(&atmel_uart, port);
2932
2933 kfree(atmel_port->rx_ring.buf);
2934
2935 /* "port" is allocated statically, so we shouldn't free it */
2936
2937 clear_bit(port->line, atmel_ports_in_use);
2938
2939 clk_put(atmel_port->clk);
2940 atmel_port->clk = NULL;
2941
2942 return ret;
2943}
2944
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002945static struct platform_driver atmel_serial_driver = {
2946 .probe = atmel_serial_probe,
Romain Izardf4a8ab042016-02-26 11:15:04 +01002947 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002948 .suspend = atmel_serial_suspend,
2949 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002950 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002951 .name = "atmel_usart",
2952 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002953 },
2954};
2955
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002956static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002957{
2958 int ret;
2959
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002960 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002961 if (ret)
2962 return ret;
2963
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002964 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002965 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002966 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002967
2968 return ret;
2969}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002970device_initcall(atmel_serial_init);