blob: 857016367a7a9d2d8d4eb924454a9f2b6ae4ac91 [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;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800156 unsigned int irq_status_prev;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200157 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800158
159 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100160
Richard Genoude0b0baa2014-05-13 20:20:44 +0200161 struct mctrl_gpios *gpios;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100162 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200163 u32 fifo_size;
164 u32 rts_high;
165 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200166 bool ms_irq_enabled;
Ludovic Desroches2958cce2016-02-22 15:18:55 +0100167 u32 rtor; /* address of receiver timeout register if it exists */
Nicolas Ferre4b769372016-01-26 11:26:14 +0100168 bool has_hw_timer;
169 struct timer_list uart_timer;
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100170
171 bool suspended;
172 unsigned int pending;
173 unsigned int pending_status;
174 spinlock_t lock_suspended;
175
Elen Songa930e522013-07-22 16:30:25 +0800176 int (*prepare_rx)(struct uart_port *port);
177 int (*prepare_tx)(struct uart_port *port);
178 void (*schedule_rx)(struct uart_port *port);
179 void (*schedule_tx)(struct uart_port *port);
180 void (*release_rx)(struct uart_port *port);
181 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100182};
183
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200184static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100185static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100186
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000187#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200188static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000189#endif
190
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200191#if defined(CONFIG_OF)
192static const struct of_device_id atmel_serial_dt_ids[] = {
193 { .compatible = "atmel,at91rm9200-usart" },
194 { .compatible = "atmel,at91sam9260-usart" },
195 { /* sentinel */ }
196};
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200197#endif
198
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800199static inline struct atmel_uart_port *
200to_atmel_uart_port(struct uart_port *uart)
201{
202 return container_of(uart, struct atmel_uart_port, uart);
203}
204
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200205static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
206{
207 return __raw_readl(port->membase + reg);
208}
209
210static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
211{
212 __raw_writel(value, port->membase + reg);
213}
214
Cyrille Pitchena6499432015-07-30 16:33:38 +0200215#ifdef CONFIG_AVR32
216
217/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
218static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200219{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200220 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200221}
222
Cyrille Pitchena6499432015-07-30 16:33:38 +0200223static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200224{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200225 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200226}
227
Cyrille Pitchena6499432015-07-30 16:33:38 +0200228#else
229
230static inline u8 atmel_uart_read_char(struct uart_port *port)
231{
232 return __raw_readb(port->membase + ATMEL_US_RHR);
233}
234
235static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
236{
237 __raw_writeb(value, port->membase + ATMEL_US_THR);
238}
239
240#endif
241
Chip Coldwella6670612008-02-08 04:21:06 -0800242#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800243static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800244{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800245 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800246
Elen Song64e22eb2013-07-22 16:30:24 +0800247 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800248}
249
Elen Song64e22eb2013-07-22 16:30:24 +0800250static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800251{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800252 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800253
Elen Song64e22eb2013-07-22 16:30:24 +0800254 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800255}
256#else
Elen Song64e22eb2013-07-22 16:30:24 +0800257static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800258{
259 return false;
260}
261
Elen Song64e22eb2013-07-22 16:30:24 +0800262static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800263{
264 return false;
265}
266#endif
267
Elen Song08f738b2013-07-22 16:30:26 +0800268static bool atmel_use_dma_tx(struct uart_port *port)
269{
270 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
271
272 return atmel_port->use_dma_tx;
273}
274
Elen Song34df42f2013-07-22 16:30:27 +0800275static bool atmel_use_dma_rx(struct uart_port *port)
276{
277 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
278
279 return atmel_port->use_dma_rx;
280}
281
Alexandre Belloni5be605a2016-04-12 14:51:40 +0200282static bool atmel_use_fifo(struct uart_port *port)
283{
284 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
285
286 return atmel_port->fifo_size;
287}
288
Richard Genoude0b0baa2014-05-13 20:20:44 +0200289static unsigned int atmel_get_lines_status(struct uart_port *port)
290{
291 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
292 unsigned int status, ret = 0;
293
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200294 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200295
296 mctrl_gpio_get(atmel_port->gpios, &ret);
297
298 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
299 UART_GPIO_CTS))) {
300 if (ret & TIOCM_CTS)
301 status &= ~ATMEL_US_CTS;
302 else
303 status |= ATMEL_US_CTS;
304 }
305
306 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
307 UART_GPIO_DSR))) {
308 if (ret & TIOCM_DSR)
309 status &= ~ATMEL_US_DSR;
310 else
311 status |= ATMEL_US_DSR;
312 }
313
314 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
315 UART_GPIO_RI))) {
316 if (ret & TIOCM_RI)
317 status &= ~ATMEL_US_RI;
318 else
319 status |= ATMEL_US_RI;
320 }
321
322 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
323 UART_GPIO_DCD))) {
324 if (ret & TIOCM_CD)
325 status &= ~ATMEL_US_DCD;
326 else
327 status |= ATMEL_US_DCD;
328 }
329
330 return status;
331}
332
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100333/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100334static int atmel_config_rs485(struct uart_port *port,
335 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100336{
337 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
338 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100339
340 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200341 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100342
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200343 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100344
345 /* Resetting serial mode to RS232 (0x0) */
346 mode &= ~ATMEL_US_USMODE;
347
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100348 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100349
350 if (rs485conf->flags & SER_RS485_ENABLED) {
351 dev_dbg(port->dev, "Setting UART to RS485\n");
352 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200353 atmel_uart_writel(port, ATMEL_US_TTGR,
354 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100355 mode |= ATMEL_US_USMODE_RS485;
356 } else {
357 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800358 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100359 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
360 ATMEL_US_TXBUFE;
361 else
362 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
363 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200364 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100365
366 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200367 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100368
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100369 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100370}
371
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000372/*
373 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
374 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200375static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000376{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200377 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
378 TIOCSER_TEMT :
379 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000380}
381
382/*
383 * Set state of the modem control output lines
384 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200385static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000386{
387 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200388 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100389 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100390 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000391
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100392 /* override mode to RS485 if needed, otherwise keep the current mode */
393 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200394 atmel_uart_writel(port, ATMEL_US_TTGR,
395 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100396 mode &= ~ATMEL_US_USMODE;
397 mode |= ATMEL_US_USMODE_RS485;
398 }
399
400 /* set the RTS line state according to the mode */
401 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
402 /* force RTS line to high level */
403 rts_paused = ATMEL_US_RTSEN;
404
405 /* give the control of the RTS line back to the hardware */
406 rts_ready = ATMEL_US_RTSDIS;
407 } else {
408 /* force RTS line to high level */
409 rts_paused = ATMEL_US_RTSDIS;
410
411 /* force RTS line to low level */
412 rts_ready = ATMEL_US_RTSEN;
413 }
414
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000415 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100416 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000417 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100418 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000419
420 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200421 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000422 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200423 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000424
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200425 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100426
Richard Genoude0b0baa2014-05-13 20:20:44 +0200427 mctrl_gpio_set(atmel_port->gpios, mctrl);
428
Andrew Victorafefc412006-06-19 19:53:19 +0100429 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100430 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100431 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200432 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100433 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200434 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100435
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200436 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437}
438
439/*
440 * Get state of the modem control input lines
441 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200442static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000443{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200444 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
445 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000446
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200447 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000448
449 /*
450 * The control signals are active low.
451 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200452 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000453 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200454 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000455 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200456 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000457 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200458 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000459 ret |= TIOCM_RI;
460
Richard Genoude0b0baa2014-05-13 20:20:44 +0200461 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000462}
463
464/*
465 * Stop transmitting.
466 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200467static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000468{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100469 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
470
Elen Song64e22eb2013-07-22 16:30:24 +0800471 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800472 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200473 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100474 }
475 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200476 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100477
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100478 if ((port->rs485.flags & SER_RS485_ENABLED) &&
479 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100480 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000481}
482
483/*
484 * Start transmitting.
485 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200486static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000487{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100488 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
489
Elen Song64e22eb2013-07-22 16:30:24 +0800490 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200491 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800492 /* The transmitter is already running. Yes, we
493 really need this.*/
494 return;
495
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100496 if ((port->rs485.flags & SER_RS485_ENABLED) &&
497 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100498 atmel_stop_rx(port);
499
Chip Coldwella6670612008-02-08 04:21:06 -0800500 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200501 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100502 }
503 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200504 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100505}
506
507/*
508 * start receiving - port is in process of being opened.
509 */
510static void atmel_start_rx(struct uart_port *port)
511{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200512 /* reset status and receiver */
513 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100514
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200515 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200516
Elen Song64e22eb2013-07-22 16:30:24 +0800517 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100518 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200519 atmel_uart_writel(port, ATMEL_US_IER,
520 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
521 port->read_status_mask);
522 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100523 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200524 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100525 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000526}
527
528/*
529 * Stop receiving - port is in process of being closed.
530 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200531static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000532{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200533 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200534
Elen Song64e22eb2013-07-22 16:30:24 +0800535 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800536 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200537 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
538 atmel_uart_writel(port, ATMEL_US_IDR,
539 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
540 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100541 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200542 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100543 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000544}
545
546/*
547 * Enable modem status interrupts
548 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200549static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000550{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200551 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
552 uint32_t ier = 0;
553
554 /*
555 * Interrupt should not be enabled twice
556 */
557 if (atmel_port->ms_irq_enabled)
558 return;
559
560 atmel_port->ms_irq_enabled = true;
561
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200562 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200563 ier |= ATMEL_US_CTSIC;
564
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200565 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200566 ier |= ATMEL_US_DSRIC;
567
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200568 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200569 ier |= ATMEL_US_RIIC;
570
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200571 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200572 ier |= ATMEL_US_DCDIC;
573
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200574 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200575
576 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000577}
578
579/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200580 * Disable modem status interrupts
581 */
582static void atmel_disable_ms(struct uart_port *port)
583{
584 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
585 uint32_t idr = 0;
586
587 /*
588 * Interrupt should not be disabled twice
589 */
590 if (!atmel_port->ms_irq_enabled)
591 return;
592
593 atmel_port->ms_irq_enabled = false;
594
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200595 mctrl_gpio_disable_ms(atmel_port->gpios);
596
597 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200598 idr |= ATMEL_US_CTSIC;
599
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200600 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200601 idr |= ATMEL_US_DSRIC;
602
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200603 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200604 idr |= ATMEL_US_RIIC;
605
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200606 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200607 idr |= ATMEL_US_DCDIC;
608
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200609 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200610}
611
612/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000613 * Control the transmission of a break signal
614 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200615static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000616{
617 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200618 /* start break */
619 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000620 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200621 /* stop break */
622 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000623}
624
625/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800626 * Stores the incoming character in the ring buffer
627 */
628static void
629atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
630 unsigned int ch)
631{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800632 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800633 struct circ_buf *ring = &atmel_port->rx_ring;
634 struct atmel_uart_char *c;
635
636 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
637 /* Buffer overflow, ignore char */
638 return;
639
640 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
641 c->status = status;
642 c->ch = ch;
643
644 /* Make sure the character is stored before we update head. */
645 smp_wmb();
646
647 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
648}
649
650/*
Chip Coldwella6670612008-02-08 04:21:06 -0800651 * Deal with parity, framing and overrun errors.
652 */
653static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
654{
655 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200656 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800657
658 if (status & ATMEL_US_RXBRK) {
659 /* ignore side-effect */
660 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
661 port->icount.brk++;
662 }
663 if (status & ATMEL_US_PARE)
664 port->icount.parity++;
665 if (status & ATMEL_US_FRAME)
666 port->icount.frame++;
667 if (status & ATMEL_US_OVRE)
668 port->icount.overrun++;
669}
670
671/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672 * Characters received (called from interrupt handler)
673 */
David Howells7d12e782006-10-05 14:55:46 +0100674static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000675{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800676 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800677 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000678
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200679 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200680 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200681 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000682
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000683 /*
684 * note that the error handling code is
685 * out of the main execution path
686 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700687 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
688 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
689 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800690
Remy Bohmerb843aa22008-02-08 04:21:01 -0800691 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200692 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800693
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700694 if (status & ATMEL_US_RXBRK
695 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700696 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200697 atmel_uart_writel(port, ATMEL_US_IER,
698 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700699 } else {
700 /*
701 * This is either the end-of-break
702 * condition or we've received at
703 * least one character without RXBRK
704 * being set. In both cases, the next
705 * RXBRK will indicate start-of-break.
706 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200707 atmel_uart_writel(port, ATMEL_US_IDR,
708 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700709 status &= ~ATMEL_US_RXBRK;
710 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100711 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000712 }
713
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800714 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200715 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000716 }
717
Nicolas Ferre00e8e6582016-06-17 12:05:47 +0200718 tasklet_schedule(&atmel_port->tasklet_rx);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000719}
720
721/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800722 * Transmit characters (called from tasklet with TXRDY interrupt
723 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000724 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200725static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000726{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700727 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100728 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000729
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200730 if (port->x_char &&
731 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200732 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000733 port->icount.tx++;
734 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000735 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800736 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000737 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000738
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200739 while (atmel_uart_readl(port, ATMEL_US_CSR) &
740 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200741 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000742 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
743 port->icount.tx++;
744 if (uart_circ_empty(xmit))
745 break;
746 }
747
748 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
749 uart_write_wakeup(port);
750
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800751 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100752 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200753 atmel_uart_writel(port, ATMEL_US_IER,
754 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000755}
756
Elen Song08f738b2013-07-22 16:30:26 +0800757static void atmel_complete_tx_dma(void *arg)
758{
759 struct atmel_uart_port *atmel_port = arg;
760 struct uart_port *port = &atmel_port->uart;
761 struct circ_buf *xmit = &port->state->xmit;
762 struct dma_chan *chan = atmel_port->chan_tx;
763 unsigned long flags;
764
765 spin_lock_irqsave(&port->lock, flags);
766
767 if (chan)
768 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200769 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800770 xmit->tail &= UART_XMIT_SIZE - 1;
771
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200772 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800773
774 spin_lock_irq(&atmel_port->lock_tx);
775 async_tx_ack(atmel_port->desc_tx);
776 atmel_port->cookie_tx = -EINVAL;
777 atmel_port->desc_tx = NULL;
778 spin_unlock_irq(&atmel_port->lock_tx);
779
780 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
781 uart_write_wakeup(port);
782
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100783 /*
784 * xmit is a circular buffer so, if we have just send data from
785 * xmit->tail to the end of xmit->buf, now we have to transmit the
786 * remaining data from the beginning of xmit->buf to xmit->head.
787 */
Elen Song08f738b2013-07-22 16:30:26 +0800788 if (!uart_circ_empty(xmit))
Nicolas Ferre00e8e6582016-06-17 12:05:47 +0200789 tasklet_schedule(&atmel_port->tasklet_tx);
Elen Song08f738b2013-07-22 16:30:26 +0800790
791 spin_unlock_irqrestore(&port->lock, flags);
792}
793
794static void atmel_release_tx_dma(struct uart_port *port)
795{
796 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
797 struct dma_chan *chan = atmel_port->chan_tx;
798
799 if (chan) {
800 dmaengine_terminate_all(chan);
801 dma_release_channel(chan);
802 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200803 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800804 }
805
806 atmel_port->desc_tx = NULL;
807 atmel_port->chan_tx = NULL;
808 atmel_port->cookie_tx = -EINVAL;
809}
810
811/*
812 * Called from tasklet with TXRDY interrupt is disabled.
813 */
814static void atmel_tx_dma(struct uart_port *port)
815{
816 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
817 struct circ_buf *xmit = &port->state->xmit;
818 struct dma_chan *chan = atmel_port->chan_tx;
819 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200820 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
821 unsigned int tx_len, part1_len, part2_len, sg_len;
822 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800823
824 /* Make sure we have an idle channel */
825 if (atmel_port->desc_tx != NULL)
826 return;
827
828 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
829 /*
830 * DMA is idle now.
831 * Port xmit buffer is already mapped,
832 * and it is one page... Just adjust
833 * offsets and lengths. Since it is a circular buffer,
834 * we have to transmit till the end, and then the rest.
835 * Take the port lock to get a
836 * consistent xmit buffer state.
837 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200838 tx_len = CIRC_CNT_TO_END(xmit->head,
839 xmit->tail,
840 UART_XMIT_SIZE);
841
842 if (atmel_port->fifo_size) {
843 /* multi data mode */
844 part1_len = (tx_len & ~0x3); /* DWORD access */
845 part2_len = (tx_len & 0x3); /* BYTE access */
846 } else {
847 /* single data (legacy) mode */
848 part1_len = 0;
849 part2_len = tx_len; /* BYTE access only */
850 }
851
852 sg_init_table(sgl, 2);
853 sg_len = 0;
854 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
855 if (part1_len) {
856 sg = &sgl[sg_len++];
857 sg_dma_address(sg) = phys_addr;
858 sg_dma_len(sg) = part1_len;
859
860 phys_addr += part1_len;
861 }
862
863 if (part2_len) {
864 sg = &sgl[sg_len++];
865 sg_dma_address(sg) = phys_addr;
866 sg_dma_len(sg) = part2_len;
867 }
868
869 /*
870 * save tx_len so atmel_complete_tx_dma() will increase
871 * xmit->tail correctly
872 */
873 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800874
875 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200876 sgl,
877 sg_len,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100878 DMA_MEM_TO_DEV,
879 DMA_PREP_INTERRUPT |
880 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800881 if (!desc) {
882 dev_err(port->dev, "Failed to send via dma!\n");
883 return;
884 }
885
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200886 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800887
888 atmel_port->desc_tx = desc;
889 desc->callback = atmel_complete_tx_dma;
890 desc->callback_param = atmel_port;
891 atmel_port->cookie_tx = dmaengine_submit(desc);
892
893 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100894 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800895 /* DMA done, stop TX, start RX for RS485 */
896 atmel_start_rx(port);
897 }
898 }
899
900 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
901 uart_write_wakeup(port);
902}
903
904static int atmel_prepare_tx_dma(struct uart_port *port)
905{
906 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
907 dma_cap_mask_t mask;
908 struct dma_slave_config config;
909 int ret, nent;
910
911 dma_cap_zero(mask);
912 dma_cap_set(DMA_SLAVE, mask);
913
914 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
915 if (atmel_port->chan_tx == NULL)
916 goto chan_err;
917 dev_info(port->dev, "using %s for tx DMA transfers\n",
918 dma_chan_name(atmel_port->chan_tx));
919
920 spin_lock_init(&atmel_port->lock_tx);
921 sg_init_table(&atmel_port->sg_tx, 1);
922 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800923 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800924 sg_set_page(&atmel_port->sg_tx,
925 virt_to_page(port->state->xmit.buf),
926 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200927 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800928 nent = dma_map_sg(port->dev,
929 &atmel_port->sg_tx,
930 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200931 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800932
933 if (!nent) {
934 dev_dbg(port->dev, "need to release resource of dma\n");
935 goto chan_err;
936 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200937 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800938 sg_dma_len(&atmel_port->sg_tx),
939 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200940 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800941 }
942
943 /* Configure the slave DMA */
944 memset(&config, 0, sizeof(config));
945 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200946 config.dst_addr_width = (atmel_port->fifo_size) ?
947 DMA_SLAVE_BUSWIDTH_4_BYTES :
948 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800949 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200950 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800951
Maxime Ripard5483c102014-10-22 17:43:16 +0200952 ret = dmaengine_slave_config(atmel_port->chan_tx,
953 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800954 if (ret) {
955 dev_err(port->dev, "DMA tx slave configuration failed\n");
956 goto chan_err;
957 }
958
959 return 0;
960
961chan_err:
962 dev_err(port->dev, "TX channel not available, switch to pio\n");
963 atmel_port->use_dma_tx = 0;
964 if (atmel_port->chan_tx)
965 atmel_release_tx_dma(port);
966 return -EINVAL;
967}
968
Elen Song34df42f2013-07-22 16:30:27 +0800969static void atmel_complete_rx_dma(void *arg)
970{
971 struct uart_port *port = arg;
972 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
973
Nicolas Ferre00e8e6582016-06-17 12:05:47 +0200974 tasklet_schedule(&atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +0800975}
976
977static void atmel_release_rx_dma(struct uart_port *port)
978{
979 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
980 struct dma_chan *chan = atmel_port->chan_rx;
981
982 if (chan) {
983 dmaengine_terminate_all(chan);
984 dma_release_channel(chan);
985 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200986 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800987 }
988
989 atmel_port->desc_rx = NULL;
990 atmel_port->chan_rx = NULL;
991 atmel_port->cookie_rx = -EINVAL;
992}
993
994static void atmel_rx_from_dma(struct uart_port *port)
995{
996 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200997 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800998 struct circ_buf *ring = &atmel_port->rx_ring;
999 struct dma_chan *chan = atmel_port->chan_rx;
1000 struct dma_tx_state state;
1001 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001002 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001003
1004
1005 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001006 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001007 dmastat = dmaengine_tx_status(chan,
1008 atmel_port->cookie_rx,
1009 &state);
1010 /* Restart a new tasklet if DMA status is error */
1011 if (dmastat == DMA_ERROR) {
1012 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001013 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001014 tasklet_schedule(&atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001015 return;
1016 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001017
1018 /* CPU claims ownership of RX DMA buffer */
1019 dma_sync_sg_for_cpu(port->dev,
1020 &atmel_port->sg_rx,
1021 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001022 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001023
1024 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001025 * ring->head points to the end of data already written by the DMA.
1026 * ring->tail points to the beginning of data to be read by the
1027 * framework.
1028 * The current transfer size should not be larger than the dma buffer
1029 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001030 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001031 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1032 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1033 /*
1034 * At this point ring->head may point to the first byte right after the
1035 * last byte of the dma buffer:
1036 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1037 *
1038 * However ring->tail must always points inside the dma buffer:
1039 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1040 *
1041 * Since we use a ring buffer, we have to handle the case
1042 * where head is lower than tail. In such a case, we first read from
1043 * tail to the end of the buffer then reset tail.
1044 */
1045 if (ring->head < ring->tail) {
1046 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001047
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001048 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1049 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001050 port->icount.rx += count;
1051 }
1052
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001053 /* Finally we read data from tail to head */
1054 if (ring->tail < ring->head) {
1055 count = ring->head - ring->tail;
1056
1057 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1058 /* Wrap ring->head if needed */
1059 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1060 ring->head = 0;
1061 ring->tail = ring->head;
1062 port->icount.rx += count;
1063 }
1064
1065 /* USART retreives ownership of RX DMA buffer */
1066 dma_sync_sg_for_device(port->dev,
1067 &atmel_port->sg_rx,
1068 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001069 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001070
1071 /*
1072 * Drop the lock here since it might end up calling
1073 * uart_start(), which takes the lock.
1074 */
1075 spin_unlock(&port->lock);
1076 tty_flip_buffer_push(tport);
1077 spin_lock(&port->lock);
1078
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001079 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001080}
1081
1082static int atmel_prepare_rx_dma(struct uart_port *port)
1083{
1084 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1085 struct dma_async_tx_descriptor *desc;
1086 dma_cap_mask_t mask;
1087 struct dma_slave_config config;
1088 struct circ_buf *ring;
1089 int ret, nent;
1090
1091 ring = &atmel_port->rx_ring;
1092
1093 dma_cap_zero(mask);
1094 dma_cap_set(DMA_CYCLIC, mask);
1095
1096 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1097 if (atmel_port->chan_rx == NULL)
1098 goto chan_err;
1099 dev_info(port->dev, "using %s for rx DMA transfers\n",
1100 dma_chan_name(atmel_port->chan_rx));
1101
1102 spin_lock_init(&atmel_port->lock_rx);
1103 sg_init_table(&atmel_port->sg_rx, 1);
1104 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001105 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001106 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001107 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001108 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001109 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001110 nent = dma_map_sg(port->dev,
1111 &atmel_port->sg_rx,
1112 1,
1113 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001114
1115 if (!nent) {
1116 dev_dbg(port->dev, "need to release resource of dma\n");
1117 goto chan_err;
1118 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001119 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001120 sg_dma_len(&atmel_port->sg_rx),
1121 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001122 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001123 }
1124
1125 /* Configure the slave DMA */
1126 memset(&config, 0, sizeof(config));
1127 config.direction = DMA_DEV_TO_MEM;
1128 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1129 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001130 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001131
Maxime Ripard5483c102014-10-22 17:43:16 +02001132 ret = dmaengine_slave_config(atmel_port->chan_rx,
1133 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001134 if (ret) {
1135 dev_err(port->dev, "DMA rx slave configuration failed\n");
1136 goto chan_err;
1137 }
1138 /*
1139 * Prepare a cyclic dma transfer, assign 2 descriptors,
1140 * each one is half ring buffer size
1141 */
1142 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001143 sg_dma_address(&atmel_port->sg_rx),
1144 sg_dma_len(&atmel_port->sg_rx),
1145 sg_dma_len(&atmel_port->sg_rx)/2,
1146 DMA_DEV_TO_MEM,
1147 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001148 desc->callback = atmel_complete_rx_dma;
1149 desc->callback_param = port;
1150 atmel_port->desc_rx = desc;
1151 atmel_port->cookie_rx = dmaengine_submit(desc);
1152
1153 return 0;
1154
1155chan_err:
1156 dev_err(port->dev, "RX channel not available, switch to pio\n");
1157 atmel_port->use_dma_rx = 0;
1158 if (atmel_port->chan_rx)
1159 atmel_release_rx_dma(port);
1160 return -EINVAL;
1161}
1162
Elen Song2e68c222013-07-22 16:30:30 +08001163static void atmel_uart_timer_callback(unsigned long data)
1164{
1165 struct uart_port *port = (void *)data;
1166 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1167
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001168 tasklet_schedule(&atmel_port->tasklet_rx);
Elen Song2e68c222013-07-22 16:30:30 +08001169 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1170}
1171
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001172/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001173 * receive interrupt handler.
1174 */
1175static void
1176atmel_handle_receive(struct uart_port *port, unsigned int pending)
1177{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001178 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001179
Elen Song64e22eb2013-07-22 16:30:24 +08001180 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001181 /*
1182 * PDC receive. Just schedule the tasklet and let it
1183 * figure out the details.
1184 *
1185 * TODO: We're not handling error flags correctly at
1186 * the moment.
1187 */
1188 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001189 atmel_uart_writel(port, ATMEL_US_IDR,
1190 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001191 tasklet_schedule(&atmel_port->tasklet_rx);
Chip Coldwella6670612008-02-08 04:21:06 -08001192 }
1193
1194 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1195 ATMEL_US_FRAME | ATMEL_US_PARE))
1196 atmel_pdc_rxerr(port, pending);
1197 }
1198
Elen Song34df42f2013-07-22 16:30:27 +08001199 if (atmel_use_dma_rx(port)) {
1200 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001201 atmel_uart_writel(port, ATMEL_US_IDR,
1202 ATMEL_US_TIMEOUT);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001203 tasklet_schedule(&atmel_port->tasklet_rx);
Elen Song34df42f2013-07-22 16:30:27 +08001204 }
1205 }
1206
Remy Bohmerb843aa22008-02-08 04:21:01 -08001207 /* Interrupt receive */
1208 if (pending & ATMEL_US_RXRDY)
1209 atmel_rx_chars(port);
1210 else if (pending & ATMEL_US_RXBRK) {
1211 /*
1212 * End of break detected. If it came along with a
1213 * character, atmel_rx_chars will handle it.
1214 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001215 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1216 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001217 atmel_port->break_active = 0;
1218 }
1219}
1220
1221/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001222 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001223 */
1224static void
1225atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1226{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001227 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001228
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001229 if (pending & atmel_port->tx_done_mask) {
1230 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001231 atmel_uart_writel(port, ATMEL_US_IDR,
1232 atmel_port->tx_done_mask);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001233 tasklet_schedule(&atmel_port->tasklet_tx);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001234 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001235}
1236
1237/*
1238 * status flags interrupt handler.
1239 */
1240static void
1241atmel_handle_status(struct uart_port *port, unsigned int pending,
1242 unsigned int status)
1243{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001244 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Nicolas Ferre92052182016-06-17 12:05:46 +02001245 unsigned int status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001246
Remy Bohmerb843aa22008-02-08 04:21:01 -08001247 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001248 | ATMEL_US_CTSIC)) {
Nicolas Ferre92052182016-06-17 12:05:46 +02001249 status_change = status ^ atmel_port->irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +08001250 atmel_port->irq_status_prev = status;
Nicolas Ferre92052182016-06-17 12:05:46 +02001251
1252 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1253 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1254 /* TODO: All reads to CSR will clear these interrupts! */
1255 if (status_change & ATMEL_US_RI)
1256 port->icount.rng++;
1257 if (status_change & ATMEL_US_DSR)
1258 port->icount.dsr++;
1259 if (status_change & ATMEL_US_DCD)
1260 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1261 if (status_change & ATMEL_US_CTS)
1262 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1263
1264 wake_up_interruptible(&port->state->port.delta_msr_wait);
1265 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001266 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001267}
1268
1269/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001270 * Interrupt handler
1271 */
David Howells7d12e782006-10-05 14:55:46 +01001272static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001273{
1274 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001275 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001276 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001277
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001278 spin_lock(&atmel_port->lock_suspended);
1279
Chip Coldwella6670612008-02-08 04:21:06 -08001280 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001281 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001282 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001283 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001284 if (!pending)
1285 break;
1286
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001287 if (atmel_port->suspended) {
1288 atmel_port->pending |= pending;
1289 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001290 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001291 pm_system_wakeup();
1292 break;
1293 }
1294
Remy Bohmerb843aa22008-02-08 04:21:01 -08001295 atmel_handle_receive(port, pending);
1296 atmel_handle_status(port, pending, status);
1297 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001298 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001299
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001300 spin_unlock(&atmel_port->lock_suspended);
1301
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001302 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001303}
1304
Elen Songa930e522013-07-22 16:30:25 +08001305static void atmel_release_tx_pdc(struct uart_port *port)
1306{
1307 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1308 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1309
1310 dma_unmap_single(port->dev,
1311 pdc->dma_addr,
1312 pdc->dma_size,
1313 DMA_TO_DEVICE);
1314}
1315
Chip Coldwella6670612008-02-08 04:21:06 -08001316/*
1317 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1318 */
Elen Song64e22eb2013-07-22 16:30:24 +08001319static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001320{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001321 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001322 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001323 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1324 int count;
1325
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001326 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001327 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001328 return;
1329
Chip Coldwella6670612008-02-08 04:21:06 -08001330 xmit->tail += pdc->ofs;
1331 xmit->tail &= UART_XMIT_SIZE - 1;
1332
1333 port->icount.tx += pdc->ofs;
1334 pdc->ofs = 0;
1335
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001336 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001337
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001338 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001339 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001340
Itai Levi1f140812009-01-15 13:50:43 -08001341 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001342 dma_sync_single_for_device(port->dev,
1343 pdc->dma_addr,
1344 pdc->dma_size,
1345 DMA_TO_DEVICE);
1346
1347 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1348 pdc->ofs = count;
1349
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001350 atmel_uart_writel(port, ATMEL_PDC_TPR,
1351 pdc->dma_addr + xmit->tail);
1352 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001353 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001354 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001355 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001356 atmel_uart_writel(port, ATMEL_US_IER,
1357 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001358 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001359 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1360 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001361 /* DMA done, stop TX, start RX for RS485 */
1362 atmel_start_rx(port);
1363 }
Chip Coldwella6670612008-02-08 04:21:06 -08001364 }
1365
1366 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1367 uart_write_wakeup(port);
1368}
1369
Elen Songa930e522013-07-22 16:30:25 +08001370static int atmel_prepare_tx_pdc(struct uart_port *port)
1371{
1372 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1373 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1374 struct circ_buf *xmit = &port->state->xmit;
1375
1376 pdc->buf = xmit->buf;
1377 pdc->dma_addr = dma_map_single(port->dev,
1378 pdc->buf,
1379 UART_XMIT_SIZE,
1380 DMA_TO_DEVICE);
1381 pdc->dma_size = UART_XMIT_SIZE;
1382 pdc->ofs = 0;
1383
1384 return 0;
1385}
1386
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001387static void atmel_rx_from_ring(struct uart_port *port)
1388{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001389 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001390 struct circ_buf *ring = &atmel_port->rx_ring;
1391 unsigned int flg;
1392 unsigned int status;
1393
1394 while (ring->head != ring->tail) {
1395 struct atmel_uart_char c;
1396
1397 /* Make sure c is loaded after head. */
1398 smp_rmb();
1399
1400 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1401
1402 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1403
1404 port->icount.rx++;
1405 status = c.status;
1406 flg = TTY_NORMAL;
1407
1408 /*
1409 * note that the error handling code is
1410 * out of the main execution path
1411 */
1412 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1413 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1414 if (status & ATMEL_US_RXBRK) {
1415 /* ignore side-effect */
1416 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1417
1418 port->icount.brk++;
1419 if (uart_handle_break(port))
1420 continue;
1421 }
1422 if (status & ATMEL_US_PARE)
1423 port->icount.parity++;
1424 if (status & ATMEL_US_FRAME)
1425 port->icount.frame++;
1426 if (status & ATMEL_US_OVRE)
1427 port->icount.overrun++;
1428
1429 status &= port->read_status_mask;
1430
1431 if (status & ATMEL_US_RXBRK)
1432 flg = TTY_BREAK;
1433 else if (status & ATMEL_US_PARE)
1434 flg = TTY_PARITY;
1435 else if (status & ATMEL_US_FRAME)
1436 flg = TTY_FRAME;
1437 }
1438
1439
1440 if (uart_handle_sysrq_char(port, c.ch))
1441 continue;
1442
1443 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1444 }
1445
1446 /*
1447 * Drop the lock here since it might end up calling
1448 * uart_start(), which takes the lock.
1449 */
1450 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001451 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001452 spin_lock(&port->lock);
1453}
1454
Elen Songa930e522013-07-22 16:30:25 +08001455static void atmel_release_rx_pdc(struct uart_port *port)
1456{
1457 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1458 int i;
1459
1460 for (i = 0; i < 2; i++) {
1461 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1462
1463 dma_unmap_single(port->dev,
1464 pdc->dma_addr,
1465 pdc->dma_size,
1466 DMA_FROM_DEVICE);
1467 kfree(pdc->buf);
1468 }
1469}
1470
Elen Song64e22eb2013-07-22 16:30:24 +08001471static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001472{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001473 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001474 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001475 struct atmel_dma_buffer *pdc;
1476 int rx_idx = atmel_port->pdc_rx_idx;
1477 unsigned int head;
1478 unsigned int tail;
1479 unsigned int count;
1480
1481 do {
1482 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001483 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001484
1485 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001486 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001487 tail = pdc->ofs;
1488
1489 /* If the PDC has switched buffers, RPR won't contain
1490 * any address within the current buffer. Since head
1491 * is unsigned, we just need a one-way comparison to
1492 * find out.
1493 *
1494 * In this case, we just need to consume the entire
1495 * buffer and resubmit it for DMA. This will clear the
1496 * ENDRX bit as well, so that we can safely re-enable
1497 * all interrupts below.
1498 */
1499 head = min(head, pdc->dma_size);
1500
1501 if (likely(head != tail)) {
1502 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1503 pdc->dma_size, DMA_FROM_DEVICE);
1504
1505 /*
1506 * head will only wrap around when we recycle
1507 * the DMA buffer, and when that happens, we
1508 * explicitly set tail to 0. So head will
1509 * always be greater than tail.
1510 */
1511 count = head - tail;
1512
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001513 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1514 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001515
1516 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1517 pdc->dma_size, DMA_FROM_DEVICE);
1518
1519 port->icount.rx += count;
1520 pdc->ofs = head;
1521 }
1522
1523 /*
1524 * If the current buffer is full, we need to check if
1525 * the next one contains any additional data.
1526 */
1527 if (head >= pdc->dma_size) {
1528 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001529 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1530 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001531
1532 rx_idx = !rx_idx;
1533 atmel_port->pdc_rx_idx = rx_idx;
1534 }
1535 } while (head >= pdc->dma_size);
1536
1537 /*
1538 * Drop the lock here since it might end up calling
1539 * uart_start(), which takes the lock.
1540 */
1541 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001542 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001543 spin_lock(&port->lock);
1544
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001545 atmel_uart_writel(port, ATMEL_US_IER,
1546 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001547}
1548
Elen Songa930e522013-07-22 16:30:25 +08001549static int atmel_prepare_rx_pdc(struct uart_port *port)
1550{
1551 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1552 int i;
1553
1554 for (i = 0; i < 2; i++) {
1555 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1556
1557 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1558 if (pdc->buf == NULL) {
1559 if (i != 0) {
1560 dma_unmap_single(port->dev,
1561 atmel_port->pdc_rx[0].dma_addr,
1562 PDC_BUFFER_SIZE,
1563 DMA_FROM_DEVICE);
1564 kfree(atmel_port->pdc_rx[0].buf);
1565 }
1566 atmel_port->use_pdc_rx = 0;
1567 return -ENOMEM;
1568 }
1569 pdc->dma_addr = dma_map_single(port->dev,
1570 pdc->buf,
1571 PDC_BUFFER_SIZE,
1572 DMA_FROM_DEVICE);
1573 pdc->dma_size = PDC_BUFFER_SIZE;
1574 pdc->ofs = 0;
1575 }
1576
1577 atmel_port->pdc_rx_idx = 0;
1578
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001579 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1580 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001581
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001582 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1583 atmel_port->pdc_rx[1].dma_addr);
1584 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001585
1586 return 0;
1587}
1588
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001589/*
1590 * tasklet handling tty stuff outside the interrupt handler.
1591 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001592static void atmel_tasklet_rx_func(unsigned long data)
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001593{
1594 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001595 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001596
1597 /* The interrupt handler does not take the lock */
1598 spin_lock(&port->lock);
Elen Songa930e522013-07-22 16:30:25 +08001599 atmel_port->schedule_rx(port);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001600 spin_unlock(&port->lock);
1601}
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001602
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001603static void atmel_tasklet_tx_func(unsigned long data)
1604{
1605 struct uart_port *port = (struct uart_port *)data;
1606 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1607
1608 /* The interrupt handler does not take the lock */
1609 spin_lock(&port->lock);
1610 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001611 spin_unlock(&port->lock);
1612}
1613
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001614static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001615 struct platform_device *pdev)
1616{
1617 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001618 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001619
1620 if (np) {
1621 /* DMA/PDC usage specification */
1622 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1623 if (of_get_property(np, "dmas", NULL)) {
1624 atmel_port->use_dma_rx = true;
1625 atmel_port->use_pdc_rx = false;
1626 } else {
1627 atmel_port->use_dma_rx = false;
1628 atmel_port->use_pdc_rx = true;
1629 }
1630 } else {
1631 atmel_port->use_dma_rx = false;
1632 atmel_port->use_pdc_rx = false;
1633 }
1634
1635 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1636 if (of_get_property(np, "dmas", NULL)) {
1637 atmel_port->use_dma_tx = true;
1638 atmel_port->use_pdc_tx = false;
1639 } else {
1640 atmel_port->use_dma_tx = false;
1641 atmel_port->use_pdc_tx = true;
1642 }
1643 } else {
1644 atmel_port->use_dma_tx = false;
1645 atmel_port->use_pdc_tx = false;
1646 }
1647
1648 } else {
1649 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1650 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1651 atmel_port->use_dma_rx = false;
1652 atmel_port->use_dma_tx = false;
1653 }
1654
Elen Song33d64c42013-07-22 16:30:28 +08001655}
1656
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001657static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001658 struct platform_device *pdev)
1659{
1660 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001661 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001662
1663 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001664 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001665 u32 rs485_delay[2];
1666 /* rs485 properties */
1667 if (of_property_read_u32_array(np, "rs485-rts-delay",
1668 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001669 rs485conf->delay_rts_before_send = rs485_delay[0];
1670 rs485conf->delay_rts_after_send = rs485_delay[1];
1671 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001672 }
Elen Song33d64c42013-07-22 16:30:28 +08001673
1674 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1675 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1676
1677 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1678 NULL))
1679 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001680 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001681 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001682 }
1683
1684}
1685
Elen Songa930e522013-07-22 16:30:25 +08001686static void atmel_set_ops(struct uart_port *port)
1687{
1688 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1689
Elen Song34df42f2013-07-22 16:30:27 +08001690 if (atmel_use_dma_rx(port)) {
1691 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1692 atmel_port->schedule_rx = &atmel_rx_from_dma;
1693 atmel_port->release_rx = &atmel_release_rx_dma;
1694 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001695 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1696 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1697 atmel_port->release_rx = &atmel_release_rx_pdc;
1698 } else {
1699 atmel_port->prepare_rx = NULL;
1700 atmel_port->schedule_rx = &atmel_rx_from_ring;
1701 atmel_port->release_rx = NULL;
1702 }
1703
Elen Song08f738b2013-07-22 16:30:26 +08001704 if (atmel_use_dma_tx(port)) {
1705 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1706 atmel_port->schedule_tx = &atmel_tx_dma;
1707 atmel_port->release_tx = &atmel_release_tx_dma;
1708 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001709 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1710 atmel_port->schedule_tx = &atmel_tx_pdc;
1711 atmel_port->release_tx = &atmel_release_tx_pdc;
1712 } else {
1713 atmel_port->prepare_tx = NULL;
1714 atmel_port->schedule_tx = &atmel_tx_chars;
1715 atmel_port->release_tx = NULL;
1716 }
1717}
1718
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001719/*
Elen Song055560b2013-07-22 16:30:29 +08001720 * Get ip name usart or uart
1721 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001722static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001723{
1724 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001725 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001726 u32 version;
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001727 u32 usart, dbgu_uart, new_uart;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001728 /* ASCII decoding for IP version */
1729 usart = 0x55534152; /* USAR(T) */
1730 dbgu_uart = 0x44424755; /* DBGU */
Nicolas Ferre1d673fb2016-01-26 11:26:15 +01001731 new_uart = 0x55415254; /* UART */
Elen Song055560b2013-07-22 16:30:29 +08001732
Nicolas Ferre4b769372016-01-26 11:26:14 +01001733 atmel_port->has_hw_timer = false;
Elen Song055560b2013-07-22 16:30:29 +08001734
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001735 if (name == new_uart) {
1736 dev_dbg(port->dev, "Uart with hw timer");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001737 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001738 atmel_port->rtor = ATMEL_UA_RTOR;
1739 } else if (name == usart) {
1740 dev_dbg(port->dev, "Usart\n");
1741 atmel_port->has_hw_timer = true;
1742 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre4b769372016-01-26 11:26:14 +01001743 } else if (name == dbgu_uart) {
1744 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
Elen Song055560b2013-07-22 16:30:29 +08001745 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001746 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001747 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001748 switch (version) {
1749 case 0x302:
1750 case 0x10213:
1751 dev_dbg(port->dev, "This version is usart\n");
Nicolas Ferre4b769372016-01-26 11:26:14 +01001752 atmel_port->has_hw_timer = true;
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001753 atmel_port->rtor = ATMEL_US_RTOR;
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001754 break;
1755 case 0x203:
1756 case 0x10202:
1757 dev_dbg(port->dev, "This version is uart\n");
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001758 break;
1759 default:
1760 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1761 }
Elen Song055560b2013-07-22 16:30:29 +08001762 }
Elen Song055560b2013-07-22 16:30:29 +08001763}
1764
1765/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001766 * Perform initialization and enable port for reception
1767 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001768static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001769{
Elen Song33d64c42013-07-22 16:30:28 +08001770 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001771 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001772 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001773 int retval;
1774
1775 /*
1776 * Ensure that no interrupts are enabled otherwise when
1777 * request_irq() is called we could get stuck trying to
1778 * handle an unexpected interrupt
1779 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001780 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001781 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001782
1783 /*
1784 * Allocate the IRQ
1785 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001786 retval = request_irq(port->irq, atmel_interrupt,
1787 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001788 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001789 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001790 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001791 return retval;
1792 }
1793
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001794 tasklet_enable(&atmel_port->tasklet_rx);
1795 tasklet_enable(&atmel_port->tasklet_tx);
Leilei Zhao1e125782015-02-27 16:07:18 +08001796
Richard Genoudab5e4e42014-05-13 20:20:45 +02001797 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001798 * Initialize DMA (if necessary)
1799 */
Elen Song33d64c42013-07-22 16:30:28 +08001800 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001801 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001802
Elen Songa930e522013-07-22 16:30:25 +08001803 if (atmel_port->prepare_rx) {
1804 retval = atmel_port->prepare_rx(port);
1805 if (retval < 0)
1806 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001807 }
1808
Elen Songa930e522013-07-22 16:30:25 +08001809 if (atmel_port->prepare_tx) {
1810 retval = atmel_port->prepare_tx(port);
1811 if (retval < 0)
1812 atmel_set_ops(port);
1813 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001814
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001815 /*
1816 * Enable FIFO when available
1817 */
1818 if (atmel_port->fifo_size) {
1819 unsigned int txrdym = ATMEL_US_ONE_DATA;
1820 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1821 unsigned int fmr;
1822
1823 atmel_uart_writel(port, ATMEL_US_CR,
1824 ATMEL_US_FIFOEN |
1825 ATMEL_US_RXFCLR |
1826 ATMEL_US_TXFLCLR);
1827
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001828 if (atmel_use_dma_tx(port))
1829 txrdym = ATMEL_US_FOUR_DATA;
1830
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001831 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1832 if (atmel_port->rts_high &&
1833 atmel_port->rts_low)
1834 fmr |= ATMEL_US_FRTSC |
1835 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1836 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1837
1838 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1839 }
1840
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001841 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001842 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001843
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001844 /*
1845 * Finally, enable the serial port
1846 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001847 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001848 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001849 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001850
Marek Roszko8bc661b2014-01-10 10:33:11 +01001851 setup_timer(&atmel_port->uart_timer,
1852 atmel_uart_timer_callback,
1853 (unsigned long)port);
1854
Elen Song64e22eb2013-07-22 16:30:24 +08001855 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001856 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001857 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001858 mod_timer(&atmel_port->uart_timer,
1859 jiffies + uart_poll_timeout(port));
1860 /* set USART timeout */
1861 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001862 atmel_uart_writel(port, atmel_port->rtor,
1863 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001864 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001865
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001866 atmel_uart_writel(port, ATMEL_US_IER,
1867 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001868 }
Chip Coldwella6670612008-02-08 04:21:06 -08001869 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001870 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001871 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001872 /* set UART timeout */
Nicolas Ferre4b769372016-01-26 11:26:14 +01001873 if (!atmel_port->has_hw_timer) {
Elen Song2e68c222013-07-22 16:30:30 +08001874 mod_timer(&atmel_port->uart_timer,
1875 jiffies + uart_poll_timeout(port));
1876 /* set USART timeout */
1877 } else {
Ludovic Desroches2958cce2016-02-22 15:18:55 +01001878 atmel_uart_writel(port, atmel_port->rtor,
1879 PDC_RX_TIMEOUT);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001880 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001881
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001882 atmel_uart_writel(port, ATMEL_US_IER,
1883 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001884 }
Chip Coldwella6670612008-02-08 04:21:06 -08001885 } else {
1886 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001887 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001888 }
Andrew Victorafefc412006-06-19 19:53:19 +01001889
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001890 return 0;
1891}
1892
1893/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001894 * Flush any TX data submitted for DMA. Called when the TX circular
1895 * buffer is reset.
1896 */
1897static void atmel_flush_buffer(struct uart_port *port)
1898{
1899 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1900
1901 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001902 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001903 atmel_port->pdc_tx.ofs = 0;
1904 }
1905}
1906
1907/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001908 * Disable the port
1909 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001910static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001911{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001912 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001913
Chip Coldwella6670612008-02-08 04:21:06 -08001914 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001915 * Prevent any tasklets being scheduled during
1916 * cleanup
1917 */
1918 del_timer_sync(&atmel_port->uart_timer);
1919
1920 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001921 * Clear out any scheduled tasklets before
1922 * we destroy the buffers
1923 */
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02001924 tasklet_disable(&atmel_port->tasklet_rx);
1925 tasklet_disable(&atmel_port->tasklet_tx);
1926 tasklet_kill(&atmel_port->tasklet_rx);
1927 tasklet_kill(&atmel_port->tasklet_tx);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001928
1929 /*
1930 * Ensure everything is stopped and
1931 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001932 */
1933 atmel_stop_rx(port);
1934 atmel_stop_tx(port);
1935
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001936 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1937 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001938
1939
Chip Coldwella6670612008-02-08 04:21:06 -08001940 /*
1941 * Shut-down the DMA.
1942 */
Elen Songa930e522013-07-22 16:30:25 +08001943 if (atmel_port->release_rx)
1944 atmel_port->release_rx(port);
1945 if (atmel_port->release_tx)
1946 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001947
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001948 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001949 * Reset ring buffer pointers
1950 */
1951 atmel_port->rx_ring.head = 0;
1952 atmel_port->rx_ring.tail = 0;
1953
1954 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001955 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001956 */
1957 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001958
1959 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001960
Peter Hurley479e9b92014-10-16 16:54:18 -04001961 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001962}
1963
1964/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001965 * Power / Clock management.
1966 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001967static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1968 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001969{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001970 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001971
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001972 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001973 case 0:
1974 /*
1975 * Enable the peripheral clock for this serial port.
1976 * This is called on uart_open() or a resume event.
1977 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001978 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001979
1980 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001981 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001982 break;
1983 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001984 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001985 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
1986 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07001987
Remy Bohmerb843aa22008-02-08 04:21:01 -08001988 /*
1989 * Disable the peripheral clock for this serial port.
1990 * This is called on uart_close() or a suspend event.
1991 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001992 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001993 break;
1994 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001995 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001996 }
1997}
1998
1999/*
2000 * Change the port parameters
2001 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002002static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2003 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002004{
2005 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002006 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002007
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002008 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002009 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002010
2011 /* reset the mode, clock divisor, parity, stop bits and data size */
2012 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2013 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002014
Remy Bohmerb843aa22008-02-08 04:21:01 -08002015 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002016 quot = uart_get_divisor(port, baud);
2017
Remy Bohmerb843aa22008-02-08 04:21:01 -08002018 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01002019 quot /= 8;
2020 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2021 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002022
2023 /* byte size */
2024 switch (termios->c_cflag & CSIZE) {
2025 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002026 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002027 break;
2028 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002029 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002030 break;
2031 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002032 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002033 break;
2034 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002035 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002036 break;
2037 }
2038
2039 /* stop bits */
2040 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002041 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002042
2043 /* parity */
2044 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002045 /* Mark or Space parity */
2046 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002047 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002048 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002049 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002050 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002051 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002052 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002053 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002054 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002055 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002056 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002057
2058 spin_lock_irqsave(&port->lock, flags);
2059
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002060 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002061 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002062 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002063 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002064 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002065
Elen Song64e22eb2013-07-22 16:30:24 +08002066 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002067 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002068 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002069
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002070 /*
2071 * Characters to ignore
2072 */
2073 port->ignore_status_mask = 0;
2074 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002075 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002076 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002077 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002078 /*
2079 * If we're ignoring parity and break indicators,
2080 * ignore overruns too (for real raw support).
2081 */
2082 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002083 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002084 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002085 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002086
2087 /* update the per-port timeout */
2088 uart_update_timeout(port, termios->c_cflag, baud);
2089
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002090 /*
2091 * save/disable interrupts. The tty layer will ensure that the
2092 * transmitter is empty if requested by the caller, so there's
2093 * no need to wait for it here.
2094 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002095 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2096 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002097
2098 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002099 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002100
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002101 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002102 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002103 atmel_uart_writel(port, ATMEL_US_TTGR,
2104 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002105 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002106 } else if (termios->c_cflag & CRTSCTS) {
2107 /* RS232 with hardware handshake (RTS/CTS) */
Alexandre Belloni5be605a2016-04-12 14:51:40 +02002108 if (atmel_use_dma_rx(port) && !atmel_use_fifo(port)) {
2109 dev_info(port->dev, "not enabling hardware flow control because DMA is used");
2110 termios->c_cflag &= ~CRTSCTS;
2111 } else {
2112 mode |= ATMEL_US_USMODE_HWHS;
2113 }
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002114 } else {
2115 /* RS232 without hadware handshake */
2116 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002117 }
2118
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002119 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002120 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002121
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002122 /*
2123 * when switching the mode, set the RTS line state according to the
2124 * new mode, otherwise keep the former state
2125 */
2126 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2127 unsigned int rts_state;
2128
2129 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2130 /* let the hardware control the RTS line */
2131 rts_state = ATMEL_US_RTSDIS;
2132 } else {
2133 /* force RTS line to low level */
2134 rts_state = ATMEL_US_RTSEN;
2135 }
2136
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002137 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002138 }
2139
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002140 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002141 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2142 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2143 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002144
2145 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002146 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002147
2148 /* CTS flow-control and modem-status interrupts */
2149 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002150 atmel_enable_ms(port);
2151 else
2152 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002153
2154 spin_unlock_irqrestore(&port->lock, flags);
2155}
2156
Peter Hurley732a84a2014-11-05 13:11:43 -05002157static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002158{
Peter Hurley732a84a2014-11-05 13:11:43 -05002159 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002160 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002161 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002162 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002163 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002164 } else {
2165 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002166 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2167 spin_lock_irq(&port->lock);
2168 atmel_disable_ms(port);
2169 spin_unlock_irq(&port->lock);
2170 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002171 }
2172}
2173
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002174/*
2175 * Return string describing the specified port
2176 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002177static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002178{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002179 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002180}
2181
2182/*
2183 * Release the memory region(s) being used by 'port'.
2184 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002185static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002186{
Andrew Victorafefc412006-06-19 19:53:19 +01002187 struct platform_device *pdev = to_platform_device(port->dev);
2188 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2189
2190 release_mem_region(port->mapbase, size);
2191
2192 if (port->flags & UPF_IOREMAP) {
2193 iounmap(port->membase);
2194 port->membase = NULL;
2195 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002196}
2197
2198/*
2199 * Request the memory region(s) being used by 'port'.
2200 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002201static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002202{
Andrew Victorafefc412006-06-19 19:53:19 +01002203 struct platform_device *pdev = to_platform_device(port->dev);
2204 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002205
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002206 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002207 return -EBUSY;
2208
2209 if (port->flags & UPF_IOREMAP) {
2210 port->membase = ioremap(port->mapbase, size);
2211 if (port->membase == NULL) {
2212 release_mem_region(port->mapbase, size);
2213 return -ENOMEM;
2214 }
2215 }
2216
2217 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002218}
2219
2220/*
2221 * Configure/autoconfigure the port.
2222 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002223static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002224{
2225 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002226 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002227 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002228 }
2229}
2230
2231/*
2232 * Verify the new serial_struct (for TIOCSSERIAL).
2233 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002234static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002235{
2236 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002237 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002238 ret = -EINVAL;
2239 if (port->irq != ser->irq)
2240 ret = -EINVAL;
2241 if (ser->io_type != SERIAL_IO_MEM)
2242 ret = -EINVAL;
2243 if (port->uartclk / 16 != ser->baud_base)
2244 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002245 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002246 ret = -EINVAL;
2247 if (port->iobase != ser->port)
2248 ret = -EINVAL;
2249 if (ser->hub6 != 0)
2250 ret = -EINVAL;
2251 return ret;
2252}
2253
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002254#ifdef CONFIG_CONSOLE_POLL
2255static int atmel_poll_get_char(struct uart_port *port)
2256{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002257 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002258 cpu_relax();
2259
Cyrille Pitchena6499432015-07-30 16:33:38 +02002260 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002261}
2262
2263static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2264{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002265 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002266 cpu_relax();
2267
Cyrille Pitchena6499432015-07-30 16:33:38 +02002268 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002269}
2270#endif
2271
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002272static struct uart_ops atmel_pops = {
2273 .tx_empty = atmel_tx_empty,
2274 .set_mctrl = atmel_set_mctrl,
2275 .get_mctrl = atmel_get_mctrl,
2276 .stop_tx = atmel_stop_tx,
2277 .start_tx = atmel_start_tx,
2278 .stop_rx = atmel_stop_rx,
2279 .enable_ms = atmel_enable_ms,
2280 .break_ctl = atmel_break_ctl,
2281 .startup = atmel_startup,
2282 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002283 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002284 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002285 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002286 .type = atmel_type,
2287 .release_port = atmel_release_port,
2288 .request_port = atmel_request_port,
2289 .config_port = atmel_config_port,
2290 .verify_port = atmel_verify_port,
2291 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002292#ifdef CONFIG_CONSOLE_POLL
2293 .poll_get_char = atmel_poll_get_char,
2294 .poll_put_char = atmel_poll_put_char,
2295#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002296};
2297
Andrew Victorafefc412006-06-19 19:53:19 +01002298/*
2299 * Configure the port from the platform device resource info.
2300 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002301static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002302 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002303{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002304 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002305 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002306 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002307
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002308 atmel_init_property(atmel_port, pdev);
2309 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002310
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002311 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002312
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002313 port->iotype = UPIO_MEM;
2314 port->flags = UPF_BOOT_AUTOCONF;
2315 port->ops = &atmel_pops;
2316 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002317 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002318 port->mapbase = pdev->resource[0].start;
2319 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002320 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002321
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002322 tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002323 (unsigned long)port);
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002324 tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
2325 (unsigned long)port);
2326 tasklet_disable(&atmel_port->tasklet_rx);
2327 tasklet_disable(&atmel_port->tasklet_tx);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002328
2329 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2330
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002331 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002332 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002333 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002334 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002335 port->flags |= UPF_IOREMAP;
2336 port->membase = NULL;
2337 }
2338
Remy Bohmerb843aa22008-02-08 04:21:01 -08002339 /* for console, the clock could already be configured */
2340 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002341 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002342 if (IS_ERR(atmel_port->clk)) {
2343 ret = PTR_ERR(atmel_port->clk);
2344 atmel_port->clk = NULL;
2345 return ret;
2346 }
2347 ret = clk_prepare_enable(atmel_port->clk);
2348 if (ret) {
2349 clk_put(atmel_port->clk);
2350 atmel_port->clk = NULL;
2351 return ret;
2352 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002353 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002354 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002355 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002356 }
Chip Coldwella6670612008-02-08 04:21:06 -08002357
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002358 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002359 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002360 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002361 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002362 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002363 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2364 } else {
2365 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2366 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002367
2368 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002369}
2370
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002371struct platform_device *atmel_default_console_device; /* the serial console device */
2372
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002373#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002374static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002375{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002376 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002377 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002378 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002379}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002380
2381/*
2382 * Interrupts are disabled on entering
2383 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002384static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002385{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002386 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002387 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002388 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002389 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002390
2391 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002392 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002393 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002394 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2395 atmel_uart_writel(port, ATMEL_US_IDR,
2396 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002397
Marc Pignat39d4c922008-04-02 13:04:42 -07002398 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002399 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2400 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002401
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002402 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002403
2404 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002405 * Finally, wait for transmitter to become empty
2406 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002407 */
2408 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002409 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002410 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002411
2412 /* Restore PDC transmit status */
2413 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002414 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002415
Remy Bohmerb843aa22008-02-08 04:21:01 -08002416 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002417 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002418}
2419
2420/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002421 * If the port was already initialised (eg, by a boot loader),
2422 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002423 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002424static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2425 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002426{
2427 unsigned int mr, quot;
2428
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002429 /*
2430 * If the baud rate generator isn't running, the port wasn't
2431 * initialized by the boot loader.
2432 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002433 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002434 if (!quot)
2435 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002436
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002437 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002438 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002439 *bits = 8;
2440 else
2441 *bits = 7;
2442
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002443 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002444 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002445 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002446 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002447 *parity = 'o';
2448
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002449 /*
2450 * The serial core only rounds down when matching this to a
2451 * supported baud rate. Make sure we don't end up slightly
2452 * lower than one of those, as it would make us fall through
2453 * to a much lower baud rate than we really want.
2454 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002455 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002456}
2457
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002458static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002459{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002460 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002461 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002462 int baud = 115200;
2463 int bits = 8;
2464 int parity = 'n';
2465 int flow = 'n';
2466
Remy Bohmerb843aa22008-02-08 04:21:01 -08002467 if (port->membase == NULL) {
2468 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002469 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002470 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002471
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002472 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2473 if (ret)
2474 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002475
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002476 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2477 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2478 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002479
2480 if (options)
2481 uart_parse_options(options, &baud, &parity, &bits, &flow);
2482 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002483 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002484
2485 return uart_set_options(port, co, baud, parity, bits, flow);
2486}
2487
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002488static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002489
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002490static struct console atmel_console = {
2491 .name = ATMEL_DEVICENAME,
2492 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002493 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002494 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002495 .flags = CON_PRINTBUFFER,
2496 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002497 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002498};
2499
David Brownell06a7f052008-11-06 12:53:40 -08002500#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002501
Andrew Victorafefc412006-06-19 19:53:19 +01002502/*
2503 * Early console initialization (before VM subsystem initialized).
2504 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002505static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002506{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002507 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002508 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002509 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002510 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002511 int id = pdata->num;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002512 struct atmel_uart_port *atmel_port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002513
Jaeden Amerob78cd162016-01-26 12:34:49 +01002514 atmel_port->backup_imr = 0;
2515 atmel_port->uart.line = id;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002516
2517 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002518 ret = atmel_init_port(atmel_port, atmel_default_console_device);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002519 if (ret)
2520 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002521 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002522 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002523
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002524 return 0;
2525}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002526
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002527console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002528
Andrew Victorafefc412006-06-19 19:53:19 +01002529/*
2530 * Late console initialization.
2531 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002532static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002533{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002534 if (atmel_default_console_device
2535 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002536 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002537
2538 return 0;
2539}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002540
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002541core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002542
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002543static inline bool atmel_is_console_port(struct uart_port *port)
2544{
2545 return port->cons && port->cons->index == port->line;
2546}
2547
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002548#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002549#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002550
2551static inline bool atmel_is_console_port(struct uart_port *port)
2552{
2553 return false;
2554}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002555#endif
2556
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002557static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002558 .owner = THIS_MODULE,
2559 .driver_name = "atmel_serial",
2560 .dev_name = ATMEL_DEVICENAME,
2561 .major = SERIAL_ATMEL_MAJOR,
2562 .minor = MINOR_START,
2563 .nr = ATMEL_MAX_UART,
2564 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002565};
2566
Andrew Victorafefc412006-06-19 19:53:19 +01002567#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002568static bool atmel_serial_clk_will_stop(void)
2569{
2570#ifdef CONFIG_ARCH_AT91
2571 return at91_suspend_entering_slow_clock();
2572#else
2573 return false;
2574#endif
2575}
2576
Remy Bohmerb843aa22008-02-08 04:21:01 -08002577static int atmel_serial_suspend(struct platform_device *pdev,
2578 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002579{
Andrew Victorafefc412006-06-19 19:53:19 +01002580 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002581 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002582
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002583 if (atmel_is_console_port(port) && console_suspend_enabled) {
2584 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002585 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2586 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002587 cpu_relax();
2588 }
2589
Anti Sullinf05596d2008-09-22 13:57:54 -07002590 /* we can not wake up if we're running on slow clock */
2591 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002592 if (atmel_serial_clk_will_stop()) {
2593 unsigned long flags;
2594
2595 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2596 atmel_port->suspended = true;
2597 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002598 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002599 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002600
2601 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002602
2603 return 0;
2604}
2605
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002606static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002607{
2608 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002609 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002610 unsigned long flags;
2611
2612 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2613 if (atmel_port->pending) {
2614 atmel_handle_receive(port, atmel_port->pending);
2615 atmel_handle_status(port, atmel_port->pending,
2616 atmel_port->pending_status);
2617 atmel_handle_transmit(port, atmel_port->pending);
2618 atmel_port->pending = 0;
2619 }
2620 atmel_port->suspended = false;
2621 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002622
Anti Sullinf05596d2008-09-22 13:57:54 -07002623 uart_resume_port(&atmel_uart, port);
2624 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002625
2626 return 0;
2627}
2628#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002629#define atmel_serial_suspend NULL
2630#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002631#endif
2632
Jaeden Amerob78cd162016-01-26 12:34:49 +01002633static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002634 struct platform_device *pdev)
2635{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002636 atmel_port->fifo_size = 0;
2637 atmel_port->rts_low = 0;
2638 atmel_port->rts_high = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002639
2640 if (of_property_read_u32(pdev->dev.of_node,
2641 "atmel,fifo-size",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002642 &atmel_port->fifo_size))
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002643 return;
2644
Jaeden Amerob78cd162016-01-26 12:34:49 +01002645 if (!atmel_port->fifo_size)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002646 return;
2647
Jaeden Amerob78cd162016-01-26 12:34:49 +01002648 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2649 atmel_port->fifo_size = 0;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002650 dev_err(&pdev->dev, "Invalid FIFO size\n");
2651 return;
2652 }
2653
2654 /*
2655 * 0 <= rts_low <= rts_high <= fifo_size
2656 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2657 * to flush their internal TX FIFO, commonly up to 16 data, before
2658 * actually stopping to send new data. So we try to set the RTS High
2659 * Threshold to a reasonably high value respecting this 16 data
2660 * empirical rule when possible.
2661 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002662 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2663 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2664 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2,
2665 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002666
2667 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002668 atmel_port->fifo_size);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002669 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002670 atmel_port->rts_high);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002671 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
Jaeden Amerob78cd162016-01-26 12:34:49 +01002672 atmel_port->rts_low);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002673}
2674
Bill Pemberton9671f092012-11-19 13:21:50 -05002675static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002676{
Jaeden Amerob78cd162016-01-26 12:34:49 +01002677 struct atmel_uart_port *atmel_port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002678 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002679 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002680 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002681 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002682 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002683
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002684 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002685
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002686 if (np)
2687 ret = of_alias_get_id(np, "serial");
2688 else
2689 if (pdata)
2690 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002691
2692 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002693 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002694 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002695 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002696
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002697 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002698 ret = -ENODEV;
2699 goto err;
2700 }
2701
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002702 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002703 /* port already in use */
2704 ret = -EBUSY;
2705 goto err;
2706 }
2707
Jaeden Amerob78cd162016-01-26 12:34:49 +01002708 atmel_port = &atmel_ports[ret];
2709 atmel_port->backup_imr = 0;
2710 atmel_port->uart.line = ret;
2711 atmel_serial_probe_fifos(atmel_port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002712
Jaeden Amerob78cd162016-01-26 12:34:49 +01002713 spin_lock_init(&atmel_port->lock_suspended);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002714
Jaeden Amerob78cd162016-01-26 12:34:49 +01002715 ret = atmel_init_port(atmel_port, pdev);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002716 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002717 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002718
Jaeden Amerob78cd162016-01-26 12:34:49 +01002719 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2720 if (IS_ERR(atmel_port->gpios)) {
2721 ret = PTR_ERR(atmel_port->gpios);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002722 goto err_clear_bit;
2723 }
2724
Jaeden Amerob78cd162016-01-26 12:34:49 +01002725 if (!atmel_use_pdc_rx(&atmel_port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002726 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002727 data = kmalloc(sizeof(struct atmel_uart_char)
2728 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002729 if (!data)
2730 goto err_alloc_ring;
Jaeden Amerob78cd162016-01-26 12:34:49 +01002731 atmel_port->rx_ring.buf = data;
Chip Coldwella6670612008-02-08 04:21:06 -08002732 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002733
Jaeden Amerob78cd162016-01-26 12:34:49 +01002734 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002735
Jaeden Amerob78cd162016-01-26 12:34:49 +01002736 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002737 if (ret)
2738 goto err_add_port;
2739
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002740#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Jaeden Amerob78cd162016-01-26 12:34:49 +01002741 if (atmel_is_console_port(&atmel_port->uart)
David Brownell06a7f052008-11-06 12:53:40 -08002742 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2743 /*
2744 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002745 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002746 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002747 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002748 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002749#endif
David Brownell06a7f052008-11-06 12:53:40 -08002750
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002751 device_init_wakeup(&pdev->dev, 1);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002752 platform_set_drvdata(pdev, atmel_port);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002753
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002754 /*
2755 * The peripheral clock has been disabled by atmel_init_port():
2756 * enable it before accessing I/O registers
2757 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002758 clk_prepare_enable(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002759
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002760 if (rs485_enabled) {
Jaeden Amerob78cd162016-01-26 12:34:49 +01002761 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002762 ATMEL_US_USMODE_NORMAL);
Jaeden Amerob78cd162016-01-26 12:34:49 +01002763 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2764 ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002765 }
2766
Elen Song055560b2013-07-22 16:30:29 +08002767 /*
2768 * Get port name of usart or uart
2769 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002770 atmel_get_ip_name(&atmel_port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002771
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002772 /*
2773 * The peripheral clock can now safely be disabled till the port
2774 * is used
2775 */
Jaeden Amerob78cd162016-01-26 12:34:49 +01002776 clk_disable_unprepare(atmel_port->clk);
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002777
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002778 return 0;
2779
2780err_add_port:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002781 kfree(atmel_port->rx_ring.buf);
2782 atmel_port->rx_ring.buf = NULL;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002783err_alloc_ring:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002784 if (!atmel_is_console_port(&atmel_port->uart)) {
2785 clk_put(atmel_port->clk);
2786 atmel_port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002787 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002788err_clear_bit:
Jaeden Amerob78cd162016-01-26 12:34:49 +01002789 clear_bit(atmel_port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002790err:
Andrew Victorafefc412006-06-19 19:53:19 +01002791 return ret;
2792}
2793
Romain Izardf4a8ab042016-02-26 11:15:04 +01002794/*
2795 * Even if the driver is not modular, it makes sense to be able to
2796 * unbind a device: there can be many bound devices, and there are
2797 * situations where dynamic binding and unbinding can be useful.
2798 *
2799 * For example, a connected device can require a specific firmware update
2800 * protocol that needs bitbanging on IO lines, but use the regular serial
2801 * port in the normal case.
2802 */
2803static int atmel_serial_remove(struct platform_device *pdev)
2804{
2805 struct uart_port *port = platform_get_drvdata(pdev);
2806 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2807 int ret = 0;
2808
Nicolas Ferre00e8e6582016-06-17 12:05:47 +02002809 tasklet_kill(&atmel_port->tasklet_rx);
2810 tasklet_kill(&atmel_port->tasklet_tx);
Romain Izardf4a8ab042016-02-26 11:15:04 +01002811
2812 device_init_wakeup(&pdev->dev, 0);
2813
2814 ret = uart_remove_one_port(&atmel_uart, port);
2815
2816 kfree(atmel_port->rx_ring.buf);
2817
2818 /* "port" is allocated statically, so we shouldn't free it */
2819
2820 clear_bit(port->line, atmel_ports_in_use);
2821
2822 clk_put(atmel_port->clk);
2823 atmel_port->clk = NULL;
2824
2825 return ret;
2826}
2827
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002828static struct platform_driver atmel_serial_driver = {
2829 .probe = atmel_serial_probe,
Romain Izardf4a8ab042016-02-26 11:15:04 +01002830 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002831 .suspend = atmel_serial_suspend,
2832 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002833 .driver = {
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002834 .name = "atmel_usart",
2835 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002836 },
2837};
2838
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002839static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002840{
2841 int ret;
2842
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002843 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002844 if (ret)
2845 return ret;
2846
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002847 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002848 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002849 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002850
2851 return ret;
2852}
Paul Gortmakerc39dfeb2015-10-18 18:21:16 -04002853device_initcall(atmel_serial_init);