blob: 002f7f25472c8f6d6b58d003938fae52ba40680f [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/module.h>
26#include <linux/tty.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/serial.h>
Andrew Victorafefc412006-06-19 19:53:19 +010031#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000032#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010035#include <linux/platform_device.h>
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +020036#include <linux/of.h>
37#include <linux/of_device.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010038#include <linux/of_gpio.h>
Chip Coldwella6670612008-02-08 04:21:06 -080039#include <linux/dma-mapping.h>
Vinod Koul6b997ba2014-10-16 12:59:06 +053040#include <linux/dmaengine.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010041#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010042#include <linux/atmel_serial.h>
Claudio Scordinoe8faff72010-05-03 13:31:28 +010043#include <linux/uaccess.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080044#include <linux/platform_data/atmel.h>
Elen Song2e68c222013-07-22 16:30:30 +080045#include <linux/timer.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010046#include <linux/gpio.h>
Richard Genoude0b0baa2014-05-13 20:20:44 +020047#include <linux/gpio/consumer.h>
48#include <linux/err.h>
Richard Genoudab5e4e42014-05-13 20:20:45 +020049#include <linux/irq.h>
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +010050#include <linux/suspend.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000051
52#include <asm/io.h>
Peter Huewef7512e72010-06-29 19:35:39 +020053#include <asm/ioctls.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000054
Chip Coldwella6670612008-02-08 04:21:06 -080055#define PDC_BUFFER_SIZE 512
56/* Revisit: We should calculate this based on the actual port settings */
57#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
58
Cyrille Pitchenb5199d42015-07-02 15:18:12 +020059/* The minium number of data FIFOs should be able to contain */
60#define ATMEL_MIN_FIFO_SIZE 8
61/*
62 * These two offsets are substracted from the RX FIFO size to define the RTS
63 * high and low thresholds
64 */
65#define ATMEL_RTS_HIGH_OFFSET 16
66#define ATMEL_RTS_LOW_OFFSET 20
67
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020068#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000069#define SUPPORT_SYSRQ
70#endif
71
72#include <linux/serial_core.h>
73
Richard Genoude0b0baa2014-05-13 20:20:44 +020074#include "serial_mctrl_gpio.h"
75
Claudio Scordinoe8faff72010-05-03 13:31:28 +010076static void atmel_start_rx(struct uart_port *port);
77static void atmel_stop_rx(struct uart_port *port);
78
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020079#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000080
81/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
82 * should coexist with the 8250 driver, such as if we have an external 16C550
83 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020084#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000085#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020086#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000087
88#else
89
90/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
91 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020092#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000093#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020094#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000095
96#endif
97
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020098#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000099
Chip Coldwella6670612008-02-08 04:21:06 -0800100struct atmel_dma_buffer {
101 unsigned char *buf;
102 dma_addr_t dma_addr;
103 unsigned int dma_size;
104 unsigned int ofs;
105};
106
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800107struct atmel_uart_char {
108 u16 status;
109 u16 ch;
110};
111
112#define ATMEL_SERIAL_RINGSIZE 1024
113
Andrew Victorafefc412006-06-19 19:53:19 +0100114/*
Alexandre Belloni9af92fb2015-09-10 11:29:03 +0200115 * at91: 6 USARTs and one DBGU port (SAM9260)
116 * avr32: 4
117 */
118#define ATMEL_MAX_UART 7
119
120/*
Andrew Victorafefc412006-06-19 19:53:19 +0100121 * We wrap our port structure around the generic uart_port.
122 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200123struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100124 struct uart_port uart; /* uart */
125 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700126 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
127 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700128 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800129
Elen Song34df42f2013-07-22 16:30:27 +0800130 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800131 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800132 short pdc_rx_idx; /* current PDC RX buffer */
133 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
134
Elen Song08f738b2013-07-22 16:30:26 +0800135 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800136 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800137 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
138
Elen Song08f738b2013-07-22 16:30:26 +0800139 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800140 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800141 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800142 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800143 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800144 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800145 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800146 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800147 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800148 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800149 struct tasklet_struct tasklet;
150 unsigned int irq_status;
151 unsigned int irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +0800152 unsigned int status_change;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200153 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800154
155 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100156
Richard Genoude0b0baa2014-05-13 20:20:44 +0200157 struct mctrl_gpios *gpios;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100158 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200159 u32 fifo_size;
160 u32 rts_high;
161 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200162 bool ms_irq_enabled;
Elen Song055560b2013-07-22 16:30:29 +0800163 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800164 struct timer_list uart_timer; /* uart timer */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100165
166 bool suspended;
167 unsigned int pending;
168 unsigned int pending_status;
169 spinlock_t lock_suspended;
170
Elen Songa930e522013-07-22 16:30:25 +0800171 int (*prepare_rx)(struct uart_port *port);
172 int (*prepare_tx)(struct uart_port *port);
173 void (*schedule_rx)(struct uart_port *port);
174 void (*schedule_tx)(struct uart_port *port);
175 void (*release_rx)(struct uart_port *port);
176 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100177};
178
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200179static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100180static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100181
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000182#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200183static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000184#endif
185
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200186#if defined(CONFIG_OF)
187static const struct of_device_id atmel_serial_dt_ids[] = {
188 { .compatible = "atmel,at91rm9200-usart" },
189 { .compatible = "atmel,at91sam9260-usart" },
190 { /* sentinel */ }
191};
192
193MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
194#endif
195
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800196static inline struct atmel_uart_port *
197to_atmel_uart_port(struct uart_port *uart)
198{
199 return container_of(uart, struct atmel_uart_port, uart);
200}
201
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200202static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
203{
204 return __raw_readl(port->membase + reg);
205}
206
207static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
208{
209 __raw_writel(value, port->membase + reg);
210}
211
Cyrille Pitchena6499432015-07-30 16:33:38 +0200212#ifdef CONFIG_AVR32
213
214/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
215static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200216{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200217 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200218}
219
Cyrille Pitchena6499432015-07-30 16:33:38 +0200220static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200221{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200222 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200223}
224
Cyrille Pitchena6499432015-07-30 16:33:38 +0200225#else
226
227static inline u8 atmel_uart_read_char(struct uart_port *port)
228{
229 return __raw_readb(port->membase + ATMEL_US_RHR);
230}
231
232static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
233{
234 __raw_writeb(value, port->membase + ATMEL_US_THR);
235}
236
237#endif
238
Chip Coldwella6670612008-02-08 04:21:06 -0800239#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800240static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800241{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800242 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800243
Elen Song64e22eb2013-07-22 16:30:24 +0800244 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800245}
246
Elen Song64e22eb2013-07-22 16:30:24 +0800247static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800248{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800249 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800250
Elen Song64e22eb2013-07-22 16:30:24 +0800251 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800252}
253#else
Elen Song64e22eb2013-07-22 16:30:24 +0800254static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800255{
256 return false;
257}
258
Elen Song64e22eb2013-07-22 16:30:24 +0800259static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800260{
261 return false;
262}
263#endif
264
Elen Song08f738b2013-07-22 16:30:26 +0800265static bool atmel_use_dma_tx(struct uart_port *port)
266{
267 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
268
269 return atmel_port->use_dma_tx;
270}
271
Elen Song34df42f2013-07-22 16:30:27 +0800272static bool atmel_use_dma_rx(struct uart_port *port)
273{
274 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
275
276 return atmel_port->use_dma_rx;
277}
278
Richard Genoude0b0baa2014-05-13 20:20:44 +0200279static unsigned int atmel_get_lines_status(struct uart_port *port)
280{
281 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
282 unsigned int status, ret = 0;
283
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200284 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200285
286 mctrl_gpio_get(atmel_port->gpios, &ret);
287
288 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
289 UART_GPIO_CTS))) {
290 if (ret & TIOCM_CTS)
291 status &= ~ATMEL_US_CTS;
292 else
293 status |= ATMEL_US_CTS;
294 }
295
296 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
297 UART_GPIO_DSR))) {
298 if (ret & TIOCM_DSR)
299 status &= ~ATMEL_US_DSR;
300 else
301 status |= ATMEL_US_DSR;
302 }
303
304 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
305 UART_GPIO_RI))) {
306 if (ret & TIOCM_RI)
307 status &= ~ATMEL_US_RI;
308 else
309 status |= ATMEL_US_RI;
310 }
311
312 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
313 UART_GPIO_DCD))) {
314 if (ret & TIOCM_CD)
315 status &= ~ATMEL_US_DCD;
316 else
317 status |= ATMEL_US_DCD;
318 }
319
320 return status;
321}
322
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100323/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100324static int atmel_config_rs485(struct uart_port *port,
325 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100326{
327 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
328 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100329
330 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200331 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100332
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200333 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100334
335 /* Resetting serial mode to RS232 (0x0) */
336 mode &= ~ATMEL_US_USMODE;
337
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100338 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100339
340 if (rs485conf->flags & SER_RS485_ENABLED) {
341 dev_dbg(port->dev, "Setting UART to RS485\n");
342 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200343 atmel_uart_writel(port, ATMEL_US_TTGR,
344 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100345 mode |= ATMEL_US_USMODE_RS485;
346 } else {
347 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800348 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100349 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
350 ATMEL_US_TXBUFE;
351 else
352 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
353 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200354 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100355
356 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200357 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100358
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100359 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100360}
361
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000362/*
363 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
364 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200365static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000366{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200367 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
368 TIOCSER_TEMT :
369 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000370}
371
372/*
373 * Set state of the modem control output lines
374 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200375static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000376{
377 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200378 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100379 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100380 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000381
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100382 /* override mode to RS485 if needed, otherwise keep the current mode */
383 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200384 atmel_uart_writel(port, ATMEL_US_TTGR,
385 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100386 mode &= ~ATMEL_US_USMODE;
387 mode |= ATMEL_US_USMODE_RS485;
388 }
389
390 /* set the RTS line state according to the mode */
391 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
392 /* force RTS line to high level */
393 rts_paused = ATMEL_US_RTSEN;
394
395 /* give the control of the RTS line back to the hardware */
396 rts_ready = ATMEL_US_RTSDIS;
397 } else {
398 /* force RTS line to high level */
399 rts_paused = ATMEL_US_RTSDIS;
400
401 /* force RTS line to low level */
402 rts_ready = ATMEL_US_RTSEN;
403 }
404
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000405 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100406 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000407 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100408 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000409
410 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200411 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000412 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200413 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000414
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200415 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100416
Richard Genoude0b0baa2014-05-13 20:20:44 +0200417 mctrl_gpio_set(atmel_port->gpios, mctrl);
418
Andrew Victorafefc412006-06-19 19:53:19 +0100419 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100420 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100421 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200422 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100423 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200424 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100425
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200426 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000427}
428
429/*
430 * Get state of the modem control input lines
431 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200432static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000433{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200434 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
435 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000436
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200437 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000438
439 /*
440 * The control signals are active low.
441 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200442 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000443 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200444 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000445 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200446 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000447 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200448 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000449 ret |= TIOCM_RI;
450
Richard Genoude0b0baa2014-05-13 20:20:44 +0200451 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000452}
453
454/*
455 * Stop transmitting.
456 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200457static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000458{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100459 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
460
Elen Song64e22eb2013-07-22 16:30:24 +0800461 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800462 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200463 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100464 }
465 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200466 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100467
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100468 if ((port->rs485.flags & SER_RS485_ENABLED) &&
469 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100470 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000471}
472
473/*
474 * Start transmitting.
475 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200476static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000477{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100478 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
479
Elen Song64e22eb2013-07-22 16:30:24 +0800480 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200481 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800482 /* The transmitter is already running. Yes, we
483 really need this.*/
484 return;
485
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100486 if ((port->rs485.flags & SER_RS485_ENABLED) &&
487 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100488 atmel_stop_rx(port);
489
Chip Coldwella6670612008-02-08 04:21:06 -0800490 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200491 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100492 }
493 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200494 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100495}
496
497/*
498 * start receiving - port is in process of being opened.
499 */
500static void atmel_start_rx(struct uart_port *port)
501{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200502 /* reset status and receiver */
503 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100504
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200505 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200506
Elen Song64e22eb2013-07-22 16:30:24 +0800507 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100508 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200509 atmel_uart_writel(port, ATMEL_US_IER,
510 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
511 port->read_status_mask);
512 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100513 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200514 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100515 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000516}
517
518/*
519 * Stop receiving - port is in process of being closed.
520 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200521static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000522{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200523 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200524
Elen Song64e22eb2013-07-22 16:30:24 +0800525 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800526 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200527 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
528 atmel_uart_writel(port, ATMEL_US_IDR,
529 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
530 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100531 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200532 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100533 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000534}
535
536/*
537 * Enable modem status interrupts
538 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200539static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000540{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200541 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
542 uint32_t ier = 0;
543
544 /*
545 * Interrupt should not be enabled twice
546 */
547 if (atmel_port->ms_irq_enabled)
548 return;
549
550 atmel_port->ms_irq_enabled = true;
551
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200552 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200553 ier |= ATMEL_US_CTSIC;
554
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200555 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200556 ier |= ATMEL_US_DSRIC;
557
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200558 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200559 ier |= ATMEL_US_RIIC;
560
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200561 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoudab5e4e42014-05-13 20:20:45 +0200562 ier |= ATMEL_US_DCDIC;
563
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200564 atmel_uart_writel(port, ATMEL_US_IER, ier);
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200565
566 mctrl_gpio_enable_ms(atmel_port->gpios);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000567}
568
569/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200570 * Disable modem status interrupts
571 */
572static void atmel_disable_ms(struct uart_port *port)
573{
574 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
575 uint32_t idr = 0;
576
577 /*
578 * Interrupt should not be disabled twice
579 */
580 if (!atmel_port->ms_irq_enabled)
581 return;
582
583 atmel_port->ms_irq_enabled = false;
584
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200585 mctrl_gpio_disable_ms(atmel_port->gpios);
586
587 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
Richard Genoud35b675b2014-09-03 18:09:26 +0200588 idr |= ATMEL_US_CTSIC;
589
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200590 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
Richard Genoud35b675b2014-09-03 18:09:26 +0200591 idr |= ATMEL_US_DSRIC;
592
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200593 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
Richard Genoud35b675b2014-09-03 18:09:26 +0200594 idr |= ATMEL_US_RIIC;
595
Uwe Kleine-König18dfef92015-10-18 21:34:45 +0200596 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
Richard Genoud35b675b2014-09-03 18:09:26 +0200597 idr |= ATMEL_US_DCDIC;
598
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200599 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200600}
601
602/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000603 * Control the transmission of a break signal
604 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200605static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000606{
607 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200608 /* start break */
609 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000610 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200611 /* stop break */
612 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000613}
614
615/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800616 * Stores the incoming character in the ring buffer
617 */
618static void
619atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
620 unsigned int ch)
621{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800622 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800623 struct circ_buf *ring = &atmel_port->rx_ring;
624 struct atmel_uart_char *c;
625
626 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
627 /* Buffer overflow, ignore char */
628 return;
629
630 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
631 c->status = status;
632 c->ch = ch;
633
634 /* Make sure the character is stored before we update head. */
635 smp_wmb();
636
637 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
638}
639
640/*
Chip Coldwella6670612008-02-08 04:21:06 -0800641 * Deal with parity, framing and overrun errors.
642 */
643static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
644{
645 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200646 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800647
648 if (status & ATMEL_US_RXBRK) {
649 /* ignore side-effect */
650 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
651 port->icount.brk++;
652 }
653 if (status & ATMEL_US_PARE)
654 port->icount.parity++;
655 if (status & ATMEL_US_FRAME)
656 port->icount.frame++;
657 if (status & ATMEL_US_OVRE)
658 port->icount.overrun++;
659}
660
661/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000662 * Characters received (called from interrupt handler)
663 */
David Howells7d12e782006-10-05 14:55:46 +0100664static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000665{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800666 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800667 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000668
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200669 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200670 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200671 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000673 /*
674 * note that the error handling code is
675 * out of the main execution path
676 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700677 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
678 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
679 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800680
Remy Bohmerb843aa22008-02-08 04:21:01 -0800681 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200682 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800683
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700684 if (status & ATMEL_US_RXBRK
685 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700686 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200687 atmel_uart_writel(port, ATMEL_US_IER,
688 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700689 } else {
690 /*
691 * This is either the end-of-break
692 * condition or we've received at
693 * least one character without RXBRK
694 * being set. In both cases, the next
695 * RXBRK will indicate start-of-break.
696 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200697 atmel_uart_writel(port, ATMEL_US_IDR,
698 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700699 status &= ~ATMEL_US_RXBRK;
700 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100701 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000702 }
703
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800704 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200705 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000706 }
707
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800708 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000709}
710
711/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800712 * Transmit characters (called from tasklet with TXRDY interrupt
713 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000714 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200715static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000716{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700717 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100718 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000719
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200720 if (port->x_char &&
721 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200722 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000723 port->icount.tx++;
724 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000725 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800726 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000727 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000728
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200729 while (atmel_uart_readl(port, ATMEL_US_CSR) &
730 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200731 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000732 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
733 port->icount.tx++;
734 if (uart_circ_empty(xmit))
735 break;
736 }
737
738 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
739 uart_write_wakeup(port);
740
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800741 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100742 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200743 atmel_uart_writel(port, ATMEL_US_IER,
744 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000745}
746
Elen Song08f738b2013-07-22 16:30:26 +0800747static void atmel_complete_tx_dma(void *arg)
748{
749 struct atmel_uart_port *atmel_port = arg;
750 struct uart_port *port = &atmel_port->uart;
751 struct circ_buf *xmit = &port->state->xmit;
752 struct dma_chan *chan = atmel_port->chan_tx;
753 unsigned long flags;
754
755 spin_lock_irqsave(&port->lock, flags);
756
757 if (chan)
758 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200759 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800760 xmit->tail &= UART_XMIT_SIZE - 1;
761
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200762 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800763
764 spin_lock_irq(&atmel_port->lock_tx);
765 async_tx_ack(atmel_port->desc_tx);
766 atmel_port->cookie_tx = -EINVAL;
767 atmel_port->desc_tx = NULL;
768 spin_unlock_irq(&atmel_port->lock_tx);
769
770 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
771 uart_write_wakeup(port);
772
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100773 /*
774 * xmit is a circular buffer so, if we have just send data from
775 * xmit->tail to the end of xmit->buf, now we have to transmit the
776 * remaining data from the beginning of xmit->buf to xmit->head.
777 */
Elen Song08f738b2013-07-22 16:30:26 +0800778 if (!uart_circ_empty(xmit))
779 tasklet_schedule(&atmel_port->tasklet);
780
781 spin_unlock_irqrestore(&port->lock, flags);
782}
783
784static void atmel_release_tx_dma(struct uart_port *port)
785{
786 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
787 struct dma_chan *chan = atmel_port->chan_tx;
788
789 if (chan) {
790 dmaengine_terminate_all(chan);
791 dma_release_channel(chan);
792 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200793 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800794 }
795
796 atmel_port->desc_tx = NULL;
797 atmel_port->chan_tx = NULL;
798 atmel_port->cookie_tx = -EINVAL;
799}
800
801/*
802 * Called from tasklet with TXRDY interrupt is disabled.
803 */
804static void atmel_tx_dma(struct uart_port *port)
805{
806 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
807 struct circ_buf *xmit = &port->state->xmit;
808 struct dma_chan *chan = atmel_port->chan_tx;
809 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200810 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
811 unsigned int tx_len, part1_len, part2_len, sg_len;
812 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800813
814 /* Make sure we have an idle channel */
815 if (atmel_port->desc_tx != NULL)
816 return;
817
818 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
819 /*
820 * DMA is idle now.
821 * Port xmit buffer is already mapped,
822 * and it is one page... Just adjust
823 * offsets and lengths. Since it is a circular buffer,
824 * we have to transmit till the end, and then the rest.
825 * Take the port lock to get a
826 * consistent xmit buffer state.
827 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200828 tx_len = CIRC_CNT_TO_END(xmit->head,
829 xmit->tail,
830 UART_XMIT_SIZE);
831
832 if (atmel_port->fifo_size) {
833 /* multi data mode */
834 part1_len = (tx_len & ~0x3); /* DWORD access */
835 part2_len = (tx_len & 0x3); /* BYTE access */
836 } else {
837 /* single data (legacy) mode */
838 part1_len = 0;
839 part2_len = tx_len; /* BYTE access only */
840 }
841
842 sg_init_table(sgl, 2);
843 sg_len = 0;
844 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
845 if (part1_len) {
846 sg = &sgl[sg_len++];
847 sg_dma_address(sg) = phys_addr;
848 sg_dma_len(sg) = part1_len;
849
850 phys_addr += part1_len;
851 }
852
853 if (part2_len) {
854 sg = &sgl[sg_len++];
855 sg_dma_address(sg) = phys_addr;
856 sg_dma_len(sg) = part2_len;
857 }
858
859 /*
860 * save tx_len so atmel_complete_tx_dma() will increase
861 * xmit->tail correctly
862 */
863 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800864
865 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200866 sgl,
867 sg_len,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100868 DMA_MEM_TO_DEV,
869 DMA_PREP_INTERRUPT |
870 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800871 if (!desc) {
872 dev_err(port->dev, "Failed to send via dma!\n");
873 return;
874 }
875
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200876 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800877
878 atmel_port->desc_tx = desc;
879 desc->callback = atmel_complete_tx_dma;
880 desc->callback_param = atmel_port;
881 atmel_port->cookie_tx = dmaengine_submit(desc);
882
883 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100884 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800885 /* DMA done, stop TX, start RX for RS485 */
886 atmel_start_rx(port);
887 }
888 }
889
890 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
891 uart_write_wakeup(port);
892}
893
894static int atmel_prepare_tx_dma(struct uart_port *port)
895{
896 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
897 dma_cap_mask_t mask;
898 struct dma_slave_config config;
899 int ret, nent;
900
901 dma_cap_zero(mask);
902 dma_cap_set(DMA_SLAVE, mask);
903
904 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
905 if (atmel_port->chan_tx == NULL)
906 goto chan_err;
907 dev_info(port->dev, "using %s for tx DMA transfers\n",
908 dma_chan_name(atmel_port->chan_tx));
909
910 spin_lock_init(&atmel_port->lock_tx);
911 sg_init_table(&atmel_port->sg_tx, 1);
912 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800913 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800914 sg_set_page(&atmel_port->sg_tx,
915 virt_to_page(port->state->xmit.buf),
916 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200917 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800918 nent = dma_map_sg(port->dev,
919 &atmel_port->sg_tx,
920 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200921 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800922
923 if (!nent) {
924 dev_dbg(port->dev, "need to release resource of dma\n");
925 goto chan_err;
926 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200927 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800928 sg_dma_len(&atmel_port->sg_tx),
929 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200930 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800931 }
932
933 /* Configure the slave DMA */
934 memset(&config, 0, sizeof(config));
935 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200936 config.dst_addr_width = (atmel_port->fifo_size) ?
937 DMA_SLAVE_BUSWIDTH_4_BYTES :
938 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800939 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200940 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800941
Maxime Ripard5483c102014-10-22 17:43:16 +0200942 ret = dmaengine_slave_config(atmel_port->chan_tx,
943 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800944 if (ret) {
945 dev_err(port->dev, "DMA tx slave configuration failed\n");
946 goto chan_err;
947 }
948
949 return 0;
950
951chan_err:
952 dev_err(port->dev, "TX channel not available, switch to pio\n");
953 atmel_port->use_dma_tx = 0;
954 if (atmel_port->chan_tx)
955 atmel_release_tx_dma(port);
956 return -EINVAL;
957}
958
Elen Song34df42f2013-07-22 16:30:27 +0800959static void atmel_complete_rx_dma(void *arg)
960{
961 struct uart_port *port = arg;
962 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
963
964 tasklet_schedule(&atmel_port->tasklet);
965}
966
967static void atmel_release_rx_dma(struct uart_port *port)
968{
969 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
970 struct dma_chan *chan = atmel_port->chan_rx;
971
972 if (chan) {
973 dmaengine_terminate_all(chan);
974 dma_release_channel(chan);
975 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200976 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800977 }
978
979 atmel_port->desc_rx = NULL;
980 atmel_port->chan_rx = NULL;
981 atmel_port->cookie_rx = -EINVAL;
982}
983
984static void atmel_rx_from_dma(struct uart_port *port)
985{
986 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200987 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800988 struct circ_buf *ring = &atmel_port->rx_ring;
989 struct dma_chan *chan = atmel_port->chan_rx;
990 struct dma_tx_state state;
991 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200992 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +0800993
994
995 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200996 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +0800997 dmastat = dmaengine_tx_status(chan,
998 atmel_port->cookie_rx,
999 &state);
1000 /* Restart a new tasklet if DMA status is error */
1001 if (dmastat == DMA_ERROR) {
1002 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001003 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001004 tasklet_schedule(&atmel_port->tasklet);
1005 return;
1006 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001007
1008 /* CPU claims ownership of RX DMA buffer */
1009 dma_sync_sg_for_cpu(port->dev,
1010 &atmel_port->sg_rx,
1011 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001012 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001013
1014 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001015 * ring->head points to the end of data already written by the DMA.
1016 * ring->tail points to the beginning of data to be read by the
1017 * framework.
1018 * The current transfer size should not be larger than the dma buffer
1019 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001020 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001021 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1022 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1023 /*
1024 * At this point ring->head may point to the first byte right after the
1025 * last byte of the dma buffer:
1026 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1027 *
1028 * However ring->tail must always points inside the dma buffer:
1029 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1030 *
1031 * Since we use a ring buffer, we have to handle the case
1032 * where head is lower than tail. In such a case, we first read from
1033 * tail to the end of the buffer then reset tail.
1034 */
1035 if (ring->head < ring->tail) {
1036 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001037
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001038 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1039 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001040 port->icount.rx += count;
1041 }
1042
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001043 /* Finally we read data from tail to head */
1044 if (ring->tail < ring->head) {
1045 count = ring->head - ring->tail;
1046
1047 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1048 /* Wrap ring->head if needed */
1049 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1050 ring->head = 0;
1051 ring->tail = ring->head;
1052 port->icount.rx += count;
1053 }
1054
1055 /* USART retreives ownership of RX DMA buffer */
1056 dma_sync_sg_for_device(port->dev,
1057 &atmel_port->sg_rx,
1058 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001059 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001060
1061 /*
1062 * Drop the lock here since it might end up calling
1063 * uart_start(), which takes the lock.
1064 */
1065 spin_unlock(&port->lock);
1066 tty_flip_buffer_push(tport);
1067 spin_lock(&port->lock);
1068
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001069 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001070}
1071
1072static int atmel_prepare_rx_dma(struct uart_port *port)
1073{
1074 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1075 struct dma_async_tx_descriptor *desc;
1076 dma_cap_mask_t mask;
1077 struct dma_slave_config config;
1078 struct circ_buf *ring;
1079 int ret, nent;
1080
1081 ring = &atmel_port->rx_ring;
1082
1083 dma_cap_zero(mask);
1084 dma_cap_set(DMA_CYCLIC, mask);
1085
1086 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1087 if (atmel_port->chan_rx == NULL)
1088 goto chan_err;
1089 dev_info(port->dev, "using %s for rx DMA transfers\n",
1090 dma_chan_name(atmel_port->chan_rx));
1091
1092 spin_lock_init(&atmel_port->lock_rx);
1093 sg_init_table(&atmel_port->sg_rx, 1);
1094 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001095 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001096 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001097 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001098 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001099 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001100 nent = dma_map_sg(port->dev,
1101 &atmel_port->sg_rx,
1102 1,
1103 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001104
1105 if (!nent) {
1106 dev_dbg(port->dev, "need to release resource of dma\n");
1107 goto chan_err;
1108 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001109 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001110 sg_dma_len(&atmel_port->sg_rx),
1111 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001112 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001113 }
1114
1115 /* Configure the slave DMA */
1116 memset(&config, 0, sizeof(config));
1117 config.direction = DMA_DEV_TO_MEM;
1118 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1119 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001120 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001121
Maxime Ripard5483c102014-10-22 17:43:16 +02001122 ret = dmaengine_slave_config(atmel_port->chan_rx,
1123 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001124 if (ret) {
1125 dev_err(port->dev, "DMA rx slave configuration failed\n");
1126 goto chan_err;
1127 }
1128 /*
1129 * Prepare a cyclic dma transfer, assign 2 descriptors,
1130 * each one is half ring buffer size
1131 */
1132 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001133 sg_dma_address(&atmel_port->sg_rx),
1134 sg_dma_len(&atmel_port->sg_rx),
1135 sg_dma_len(&atmel_port->sg_rx)/2,
1136 DMA_DEV_TO_MEM,
1137 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001138 desc->callback = atmel_complete_rx_dma;
1139 desc->callback_param = port;
1140 atmel_port->desc_rx = desc;
1141 atmel_port->cookie_rx = dmaengine_submit(desc);
1142
1143 return 0;
1144
1145chan_err:
1146 dev_err(port->dev, "RX channel not available, switch to pio\n");
1147 atmel_port->use_dma_rx = 0;
1148 if (atmel_port->chan_rx)
1149 atmel_release_rx_dma(port);
1150 return -EINVAL;
1151}
1152
Elen Song2e68c222013-07-22 16:30:30 +08001153static void atmel_uart_timer_callback(unsigned long data)
1154{
1155 struct uart_port *port = (void *)data;
1156 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1157
1158 tasklet_schedule(&atmel_port->tasklet);
1159 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1160}
1161
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001162/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001163 * receive interrupt handler.
1164 */
1165static void
1166atmel_handle_receive(struct uart_port *port, unsigned int pending)
1167{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001168 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001169
Elen Song64e22eb2013-07-22 16:30:24 +08001170 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001171 /*
1172 * PDC receive. Just schedule the tasklet and let it
1173 * figure out the details.
1174 *
1175 * TODO: We're not handling error flags correctly at
1176 * the moment.
1177 */
1178 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001179 atmel_uart_writel(port, ATMEL_US_IDR,
1180 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Chip Coldwella6670612008-02-08 04:21:06 -08001181 tasklet_schedule(&atmel_port->tasklet);
1182 }
1183
1184 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1185 ATMEL_US_FRAME | ATMEL_US_PARE))
1186 atmel_pdc_rxerr(port, pending);
1187 }
1188
Elen Song34df42f2013-07-22 16:30:27 +08001189 if (atmel_use_dma_rx(port)) {
1190 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001191 atmel_uart_writel(port, ATMEL_US_IDR,
1192 ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001193 tasklet_schedule(&atmel_port->tasklet);
1194 }
1195 }
1196
Remy Bohmerb843aa22008-02-08 04:21:01 -08001197 /* Interrupt receive */
1198 if (pending & ATMEL_US_RXRDY)
1199 atmel_rx_chars(port);
1200 else if (pending & ATMEL_US_RXBRK) {
1201 /*
1202 * End of break detected. If it came along with a
1203 * character, atmel_rx_chars will handle it.
1204 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001205 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1206 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001207 atmel_port->break_active = 0;
1208 }
1209}
1210
1211/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001212 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001213 */
1214static void
1215atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1216{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001217 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001218
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001219 if (pending & atmel_port->tx_done_mask) {
1220 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001221 atmel_uart_writel(port, ATMEL_US_IDR,
1222 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001223 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001224 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001225}
1226
1227/*
1228 * status flags interrupt handler.
1229 */
1230static void
1231atmel_handle_status(struct uart_port *port, unsigned int pending,
1232 unsigned int status)
1233{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001234 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001235
Remy Bohmerb843aa22008-02-08 04:21:01 -08001236 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001237 | ATMEL_US_CTSIC)) {
1238 atmel_port->irq_status = status;
Leilei Zhaod033e822015-04-09 10:48:15 +08001239 atmel_port->status_change = atmel_port->irq_status ^
1240 atmel_port->irq_status_prev;
1241 atmel_port->irq_status_prev = status;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001242 tasklet_schedule(&atmel_port->tasklet);
1243 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001244}
1245
1246/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001247 * Interrupt handler
1248 */
David Howells7d12e782006-10-05 14:55:46 +01001249static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001250{
1251 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001252 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001253 unsigned int status, pending, mask, pass_counter = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001254
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001255 spin_lock(&atmel_port->lock_suspended);
1256
Chip Coldwella6670612008-02-08 04:21:06 -08001257 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001258 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001259 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001260 pending = status & mask;
Chip Coldwella6670612008-02-08 04:21:06 -08001261 if (!pending)
1262 break;
1263
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001264 if (atmel_port->suspended) {
1265 atmel_port->pending |= pending;
1266 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001267 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001268 pm_system_wakeup();
1269 break;
1270 }
1271
Remy Bohmerb843aa22008-02-08 04:21:01 -08001272 atmel_handle_receive(port, pending);
1273 atmel_handle_status(port, pending, status);
1274 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001275 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001276
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001277 spin_unlock(&atmel_port->lock_suspended);
1278
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001279 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001280}
1281
Elen Songa930e522013-07-22 16:30:25 +08001282static void atmel_release_tx_pdc(struct uart_port *port)
1283{
1284 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1285 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1286
1287 dma_unmap_single(port->dev,
1288 pdc->dma_addr,
1289 pdc->dma_size,
1290 DMA_TO_DEVICE);
1291}
1292
Chip Coldwella6670612008-02-08 04:21:06 -08001293/*
1294 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1295 */
Elen Song64e22eb2013-07-22 16:30:24 +08001296static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001297{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001298 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001299 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001300 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1301 int count;
1302
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001303 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001304 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001305 return;
1306
Chip Coldwella6670612008-02-08 04:21:06 -08001307 xmit->tail += pdc->ofs;
1308 xmit->tail &= UART_XMIT_SIZE - 1;
1309
1310 port->icount.tx += pdc->ofs;
1311 pdc->ofs = 0;
1312
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001313 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001314
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001315 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001316 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001317
Itai Levi1f140812009-01-15 13:50:43 -08001318 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001319 dma_sync_single_for_device(port->dev,
1320 pdc->dma_addr,
1321 pdc->dma_size,
1322 DMA_TO_DEVICE);
1323
1324 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1325 pdc->ofs = count;
1326
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001327 atmel_uart_writel(port, ATMEL_PDC_TPR,
1328 pdc->dma_addr + xmit->tail);
1329 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001330 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001331 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001332 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001333 atmel_uart_writel(port, ATMEL_US_IER,
1334 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001335 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001336 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1337 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001338 /* DMA done, stop TX, start RX for RS485 */
1339 atmel_start_rx(port);
1340 }
Chip Coldwella6670612008-02-08 04:21:06 -08001341 }
1342
1343 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1344 uart_write_wakeup(port);
1345}
1346
Elen Songa930e522013-07-22 16:30:25 +08001347static int atmel_prepare_tx_pdc(struct uart_port *port)
1348{
1349 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1350 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1351 struct circ_buf *xmit = &port->state->xmit;
1352
1353 pdc->buf = xmit->buf;
1354 pdc->dma_addr = dma_map_single(port->dev,
1355 pdc->buf,
1356 UART_XMIT_SIZE,
1357 DMA_TO_DEVICE);
1358 pdc->dma_size = UART_XMIT_SIZE;
1359 pdc->ofs = 0;
1360
1361 return 0;
1362}
1363
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001364static void atmel_rx_from_ring(struct uart_port *port)
1365{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001366 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001367 struct circ_buf *ring = &atmel_port->rx_ring;
1368 unsigned int flg;
1369 unsigned int status;
1370
1371 while (ring->head != ring->tail) {
1372 struct atmel_uart_char c;
1373
1374 /* Make sure c is loaded after head. */
1375 smp_rmb();
1376
1377 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1378
1379 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1380
1381 port->icount.rx++;
1382 status = c.status;
1383 flg = TTY_NORMAL;
1384
1385 /*
1386 * note that the error handling code is
1387 * out of the main execution path
1388 */
1389 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1390 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1391 if (status & ATMEL_US_RXBRK) {
1392 /* ignore side-effect */
1393 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1394
1395 port->icount.brk++;
1396 if (uart_handle_break(port))
1397 continue;
1398 }
1399 if (status & ATMEL_US_PARE)
1400 port->icount.parity++;
1401 if (status & ATMEL_US_FRAME)
1402 port->icount.frame++;
1403 if (status & ATMEL_US_OVRE)
1404 port->icount.overrun++;
1405
1406 status &= port->read_status_mask;
1407
1408 if (status & ATMEL_US_RXBRK)
1409 flg = TTY_BREAK;
1410 else if (status & ATMEL_US_PARE)
1411 flg = TTY_PARITY;
1412 else if (status & ATMEL_US_FRAME)
1413 flg = TTY_FRAME;
1414 }
1415
1416
1417 if (uart_handle_sysrq_char(port, c.ch))
1418 continue;
1419
1420 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1421 }
1422
1423 /*
1424 * Drop the lock here since it might end up calling
1425 * uart_start(), which takes the lock.
1426 */
1427 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001428 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001429 spin_lock(&port->lock);
1430}
1431
Elen Songa930e522013-07-22 16:30:25 +08001432static void atmel_release_rx_pdc(struct uart_port *port)
1433{
1434 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1435 int i;
1436
1437 for (i = 0; i < 2; i++) {
1438 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1439
1440 dma_unmap_single(port->dev,
1441 pdc->dma_addr,
1442 pdc->dma_size,
1443 DMA_FROM_DEVICE);
1444 kfree(pdc->buf);
1445 }
1446}
1447
Elen Song64e22eb2013-07-22 16:30:24 +08001448static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001449{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001450 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001451 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001452 struct atmel_dma_buffer *pdc;
1453 int rx_idx = atmel_port->pdc_rx_idx;
1454 unsigned int head;
1455 unsigned int tail;
1456 unsigned int count;
1457
1458 do {
1459 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001460 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001461
1462 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001463 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001464 tail = pdc->ofs;
1465
1466 /* If the PDC has switched buffers, RPR won't contain
1467 * any address within the current buffer. Since head
1468 * is unsigned, we just need a one-way comparison to
1469 * find out.
1470 *
1471 * In this case, we just need to consume the entire
1472 * buffer and resubmit it for DMA. This will clear the
1473 * ENDRX bit as well, so that we can safely re-enable
1474 * all interrupts below.
1475 */
1476 head = min(head, pdc->dma_size);
1477
1478 if (likely(head != tail)) {
1479 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1480 pdc->dma_size, DMA_FROM_DEVICE);
1481
1482 /*
1483 * head will only wrap around when we recycle
1484 * the DMA buffer, and when that happens, we
1485 * explicitly set tail to 0. So head will
1486 * always be greater than tail.
1487 */
1488 count = head - tail;
1489
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001490 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1491 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001492
1493 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1494 pdc->dma_size, DMA_FROM_DEVICE);
1495
1496 port->icount.rx += count;
1497 pdc->ofs = head;
1498 }
1499
1500 /*
1501 * If the current buffer is full, we need to check if
1502 * the next one contains any additional data.
1503 */
1504 if (head >= pdc->dma_size) {
1505 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001506 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1507 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001508
1509 rx_idx = !rx_idx;
1510 atmel_port->pdc_rx_idx = rx_idx;
1511 }
1512 } while (head >= pdc->dma_size);
1513
1514 /*
1515 * Drop the lock here since it might end up calling
1516 * uart_start(), which takes the lock.
1517 */
1518 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001519 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001520 spin_lock(&port->lock);
1521
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001522 atmel_uart_writel(port, ATMEL_US_IER,
1523 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001524}
1525
Elen Songa930e522013-07-22 16:30:25 +08001526static int atmel_prepare_rx_pdc(struct uart_port *port)
1527{
1528 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1529 int i;
1530
1531 for (i = 0; i < 2; i++) {
1532 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1533
1534 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1535 if (pdc->buf == NULL) {
1536 if (i != 0) {
1537 dma_unmap_single(port->dev,
1538 atmel_port->pdc_rx[0].dma_addr,
1539 PDC_BUFFER_SIZE,
1540 DMA_FROM_DEVICE);
1541 kfree(atmel_port->pdc_rx[0].buf);
1542 }
1543 atmel_port->use_pdc_rx = 0;
1544 return -ENOMEM;
1545 }
1546 pdc->dma_addr = dma_map_single(port->dev,
1547 pdc->buf,
1548 PDC_BUFFER_SIZE,
1549 DMA_FROM_DEVICE);
1550 pdc->dma_size = PDC_BUFFER_SIZE;
1551 pdc->ofs = 0;
1552 }
1553
1554 atmel_port->pdc_rx_idx = 0;
1555
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001556 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1557 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001558
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001559 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1560 atmel_port->pdc_rx[1].dma_addr);
1561 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001562
1563 return 0;
1564}
1565
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001566/*
1567 * tasklet handling tty stuff outside the interrupt handler.
1568 */
1569static void atmel_tasklet_func(unsigned long data)
1570{
1571 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001572 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Leilei Zhaod033e822015-04-09 10:48:15 +08001573 unsigned int status = atmel_port->irq_status;
1574 unsigned int status_change = atmel_port->status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001575
1576 /* The interrupt handler does not take the lock */
1577 spin_lock(&port->lock);
1578
Elen Songa930e522013-07-22 16:30:25 +08001579 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001580
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001581 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1582 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1583 /* TODO: All reads to CSR will clear these interrupts! */
1584 if (status_change & ATMEL_US_RI)
1585 port->icount.rng++;
1586 if (status_change & ATMEL_US_DSR)
1587 port->icount.dsr++;
1588 if (status_change & ATMEL_US_DCD)
1589 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1590 if (status_change & ATMEL_US_CTS)
1591 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1592
Alan Coxbdc04e32009-09-19 13:13:31 -07001593 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001594
Leilei Zhaod033e822015-04-09 10:48:15 +08001595 atmel_port->status_change = 0;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001596 }
1597
Elen Songa930e522013-07-22 16:30:25 +08001598 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001599
1600 spin_unlock(&port->lock);
1601}
1602
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001603static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001604 struct platform_device *pdev)
1605{
1606 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001607 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001608
1609 if (np) {
1610 /* DMA/PDC usage specification */
1611 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1612 if (of_get_property(np, "dmas", NULL)) {
1613 atmel_port->use_dma_rx = true;
1614 atmel_port->use_pdc_rx = false;
1615 } else {
1616 atmel_port->use_dma_rx = false;
1617 atmel_port->use_pdc_rx = true;
1618 }
1619 } else {
1620 atmel_port->use_dma_rx = false;
1621 atmel_port->use_pdc_rx = false;
1622 }
1623
1624 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1625 if (of_get_property(np, "dmas", NULL)) {
1626 atmel_port->use_dma_tx = true;
1627 atmel_port->use_pdc_tx = false;
1628 } else {
1629 atmel_port->use_dma_tx = false;
1630 atmel_port->use_pdc_tx = true;
1631 }
1632 } else {
1633 atmel_port->use_dma_tx = false;
1634 atmel_port->use_pdc_tx = false;
1635 }
1636
1637 } else {
1638 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1639 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1640 atmel_port->use_dma_rx = false;
1641 atmel_port->use_dma_tx = false;
1642 }
1643
Elen Song33d64c42013-07-22 16:30:28 +08001644}
1645
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001646static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001647 struct platform_device *pdev)
1648{
1649 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001650 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001651
1652 if (np) {
Jiri Slaby77bdec62015-10-11 15:22:44 +02001653 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001654 u32 rs485_delay[2];
1655 /* rs485 properties */
1656 if (of_property_read_u32_array(np, "rs485-rts-delay",
1657 rs485_delay, 2) == 0) {
Elen Song33d64c42013-07-22 16:30:28 +08001658 rs485conf->delay_rts_before_send = rs485_delay[0];
1659 rs485conf->delay_rts_after_send = rs485_delay[1];
1660 rs485conf->flags = 0;
Jiri Slaby77bdec62015-10-11 15:22:44 +02001661 }
Elen Song33d64c42013-07-22 16:30:28 +08001662
1663 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1664 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1665
1666 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1667 NULL))
1668 rs485conf->flags |= SER_RS485_ENABLED;
Elen Song33d64c42013-07-22 16:30:28 +08001669 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001670 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001671 }
1672
1673}
1674
Elen Songa930e522013-07-22 16:30:25 +08001675static void atmel_set_ops(struct uart_port *port)
1676{
1677 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1678
Elen Song34df42f2013-07-22 16:30:27 +08001679 if (atmel_use_dma_rx(port)) {
1680 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1681 atmel_port->schedule_rx = &atmel_rx_from_dma;
1682 atmel_port->release_rx = &atmel_release_rx_dma;
1683 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001684 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1685 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1686 atmel_port->release_rx = &atmel_release_rx_pdc;
1687 } else {
1688 atmel_port->prepare_rx = NULL;
1689 atmel_port->schedule_rx = &atmel_rx_from_ring;
1690 atmel_port->release_rx = NULL;
1691 }
1692
Elen Song08f738b2013-07-22 16:30:26 +08001693 if (atmel_use_dma_tx(port)) {
1694 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1695 atmel_port->schedule_tx = &atmel_tx_dma;
1696 atmel_port->release_tx = &atmel_release_tx_dma;
1697 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001698 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1699 atmel_port->schedule_tx = &atmel_tx_pdc;
1700 atmel_port->release_tx = &atmel_release_tx_pdc;
1701 } else {
1702 atmel_port->prepare_tx = NULL;
1703 atmel_port->schedule_tx = &atmel_tx_chars;
1704 atmel_port->release_tx = NULL;
1705 }
1706}
1707
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001708/*
Elen Song055560b2013-07-22 16:30:29 +08001709 * Get ip name usart or uart
1710 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001711static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001712{
1713 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001714 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001715 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001716 int usart, uart;
1717 /* usart and uart ascii */
1718 usart = 0x55534152;
1719 uart = 0x44424755;
1720
1721 atmel_port->is_usart = false;
1722
1723 if (name == usart) {
1724 dev_dbg(port->dev, "This is usart\n");
1725 atmel_port->is_usart = true;
1726 } else if (name == uart) {
1727 dev_dbg(port->dev, "This is uart\n");
1728 atmel_port->is_usart = false;
1729 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001730 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001731 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001732 switch (version) {
1733 case 0x302:
1734 case 0x10213:
1735 dev_dbg(port->dev, "This version is usart\n");
1736 atmel_port->is_usart = true;
1737 break;
1738 case 0x203:
1739 case 0x10202:
1740 dev_dbg(port->dev, "This version is uart\n");
1741 atmel_port->is_usart = false;
1742 break;
1743 default:
1744 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1745 }
Elen Song055560b2013-07-22 16:30:29 +08001746 }
Elen Song055560b2013-07-22 16:30:29 +08001747}
1748
1749/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001750 * Perform initialization and enable port for reception
1751 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001752static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001753{
Elen Song33d64c42013-07-22 16:30:28 +08001754 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001755 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001756 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001757 int retval;
1758
1759 /*
1760 * Ensure that no interrupts are enabled otherwise when
1761 * request_irq() is called we could get stuck trying to
1762 * handle an unexpected interrupt
1763 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001764 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001765 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001766
1767 /*
1768 * Allocate the IRQ
1769 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001770 retval = request_irq(port->irq, atmel_interrupt,
1771 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001772 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001773 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001774 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001775 return retval;
1776 }
1777
Leilei Zhao1e125782015-02-27 16:07:18 +08001778 tasklet_enable(&atmel_port->tasklet);
1779
Richard Genoudab5e4e42014-05-13 20:20:45 +02001780 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001781 * Initialize DMA (if necessary)
1782 */
Elen Song33d64c42013-07-22 16:30:28 +08001783 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001784 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001785
Elen Songa930e522013-07-22 16:30:25 +08001786 if (atmel_port->prepare_rx) {
1787 retval = atmel_port->prepare_rx(port);
1788 if (retval < 0)
1789 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001790 }
1791
Elen Songa930e522013-07-22 16:30:25 +08001792 if (atmel_port->prepare_tx) {
1793 retval = atmel_port->prepare_tx(port);
1794 if (retval < 0)
1795 atmel_set_ops(port);
1796 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001797
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001798 /*
1799 * Enable FIFO when available
1800 */
1801 if (atmel_port->fifo_size) {
1802 unsigned int txrdym = ATMEL_US_ONE_DATA;
1803 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1804 unsigned int fmr;
1805
1806 atmel_uart_writel(port, ATMEL_US_CR,
1807 ATMEL_US_FIFOEN |
1808 ATMEL_US_RXFCLR |
1809 ATMEL_US_TXFLCLR);
1810
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001811 if (atmel_use_dma_tx(port))
1812 txrdym = ATMEL_US_FOUR_DATA;
1813
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001814 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1815 if (atmel_port->rts_high &&
1816 atmel_port->rts_low)
1817 fmr |= ATMEL_US_FRTSC |
1818 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1819 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1820
1821 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1822 }
1823
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001824 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001825 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001826 atmel_port->irq_status = atmel_port->irq_status_prev;
1827
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001828 /*
1829 * Finally, enable the serial port
1830 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001831 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001832 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001833 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001834
Marek Roszko8bc661b2014-01-10 10:33:11 +01001835 setup_timer(&atmel_port->uart_timer,
1836 atmel_uart_timer_callback,
1837 (unsigned long)port);
1838
Elen Song64e22eb2013-07-22 16:30:24 +08001839 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001840 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001841 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001842 mod_timer(&atmel_port->uart_timer,
1843 jiffies + uart_poll_timeout(port));
1844 /* set USART timeout */
1845 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001846 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1847 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001848
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001849 atmel_uart_writel(port, ATMEL_US_IER,
1850 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001851 }
Chip Coldwella6670612008-02-08 04:21:06 -08001852 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001853 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001854 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001855 /* set UART timeout */
1856 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001857 mod_timer(&atmel_port->uart_timer,
1858 jiffies + uart_poll_timeout(port));
1859 /* set USART timeout */
1860 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001861 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1862 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001863
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001864 atmel_uart_writel(port, ATMEL_US_IER,
1865 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001866 }
Chip Coldwella6670612008-02-08 04:21:06 -08001867 } else {
1868 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001869 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001870 }
Andrew Victorafefc412006-06-19 19:53:19 +01001871
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001872 return 0;
1873}
1874
1875/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001876 * Flush any TX data submitted for DMA. Called when the TX circular
1877 * buffer is reset.
1878 */
1879static void atmel_flush_buffer(struct uart_port *port)
1880{
1881 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1882
1883 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001884 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001885 atmel_port->pdc_tx.ofs = 0;
1886 }
1887}
1888
1889/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001890 * Disable the port
1891 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001892static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001893{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001894 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001895
Chip Coldwella6670612008-02-08 04:21:06 -08001896 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001897 * Prevent any tasklets being scheduled during
1898 * cleanup
1899 */
1900 del_timer_sync(&atmel_port->uart_timer);
1901
1902 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001903 * Clear out any scheduled tasklets before
1904 * we destroy the buffers
1905 */
Leilei Zhao1e125782015-02-27 16:07:18 +08001906 tasklet_disable(&atmel_port->tasklet);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001907 tasklet_kill(&atmel_port->tasklet);
1908
1909 /*
1910 * Ensure everything is stopped and
1911 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001912 */
1913 atmel_stop_rx(port);
1914 atmel_stop_tx(port);
1915
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001916 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1917 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001918
1919
Chip Coldwella6670612008-02-08 04:21:06 -08001920 /*
1921 * Shut-down the DMA.
1922 */
Elen Songa930e522013-07-22 16:30:25 +08001923 if (atmel_port->release_rx)
1924 atmel_port->release_rx(port);
1925 if (atmel_port->release_tx)
1926 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001927
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001928 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01001929 * Reset ring buffer pointers
1930 */
1931 atmel_port->rx_ring.head = 0;
1932 atmel_port->rx_ring.tail = 0;
1933
1934 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001935 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001936 */
1937 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001938
1939 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001940
Peter Hurley479e9b92014-10-16 16:54:18 -04001941 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001942}
1943
1944/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001945 * Power / Clock management.
1946 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001947static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1948 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001949{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001950 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001951
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001952 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001953 case 0:
1954 /*
1955 * Enable the peripheral clock for this serial port.
1956 * This is called on uart_open() or a resume event.
1957 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001958 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07001959
1960 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001961 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001962 break;
1963 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07001964 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001965 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
1966 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07001967
Remy Bohmerb843aa22008-02-08 04:21:01 -08001968 /*
1969 * Disable the peripheral clock for this serial port.
1970 * This is called on uart_close() or a suspend event.
1971 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02001972 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001973 break;
1974 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01001975 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001976 }
1977}
1978
1979/*
1980 * Change the port parameters
1981 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001982static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1983 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001984{
1985 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001986 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001987
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001988 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001989 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01001990
1991 /* reset the mode, clock divisor, parity, stop bits and data size */
1992 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
1993 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01001994
Remy Bohmerb843aa22008-02-08 04:21:01 -08001995 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001996 quot = uart_get_divisor(port, baud);
1997
Remy Bohmerb843aa22008-02-08 04:21:01 -08001998 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001999 quot /= 8;
2000 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2001 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002002
2003 /* byte size */
2004 switch (termios->c_cflag & CSIZE) {
2005 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002006 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002007 break;
2008 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002009 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002010 break;
2011 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002012 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002013 break;
2014 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002015 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002016 break;
2017 }
2018
2019 /* stop bits */
2020 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002021 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002022
2023 /* parity */
2024 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002025 /* Mark or Space parity */
2026 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002027 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002028 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002029 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002030 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002031 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002032 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002033 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002034 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002035 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002036 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002037
2038 spin_lock_irqsave(&port->lock, flags);
2039
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002040 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002041 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002042 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002043 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002044 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002045
Elen Song64e22eb2013-07-22 16:30:24 +08002046 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002047 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002048 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002049
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002050 /*
2051 * Characters to ignore
2052 */
2053 port->ignore_status_mask = 0;
2054 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002055 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002056 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002057 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002058 /*
2059 * If we're ignoring parity and break indicators,
2060 * ignore overruns too (for real raw support).
2061 */
2062 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002063 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002064 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002065 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002066
2067 /* update the per-port timeout */
2068 uart_update_timeout(port, termios->c_cflag, baud);
2069
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002070 /*
2071 * save/disable interrupts. The tty layer will ensure that the
2072 * transmitter is empty if requested by the caller, so there's
2073 * no need to wait for it here.
2074 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002075 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2076 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002077
2078 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002079 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002080
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002081 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002082 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002083 atmel_uart_writel(port, ATMEL_US_TTGR,
2084 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002085 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002086 } else if (termios->c_cflag & CRTSCTS) {
2087 /* RS232 with hardware handshake (RTS/CTS) */
2088 mode |= ATMEL_US_USMODE_HWHS;
2089 } else {
2090 /* RS232 without hadware handshake */
2091 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002092 }
2093
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002094 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002095 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002096
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002097 /*
2098 * when switching the mode, set the RTS line state according to the
2099 * new mode, otherwise keep the former state
2100 */
2101 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2102 unsigned int rts_state;
2103
2104 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2105 /* let the hardware control the RTS line */
2106 rts_state = ATMEL_US_RTSDIS;
2107 } else {
2108 /* force RTS line to low level */
2109 rts_state = ATMEL_US_RTSEN;
2110 }
2111
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002112 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002113 }
2114
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002115 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002116 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2117 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2118 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002119
2120 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002121 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002122
2123 /* CTS flow-control and modem-status interrupts */
2124 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002125 atmel_enable_ms(port);
2126 else
2127 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002128
2129 spin_unlock_irqrestore(&port->lock, flags);
2130}
2131
Peter Hurley732a84a2014-11-05 13:11:43 -05002132static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002133{
Peter Hurley732a84a2014-11-05 13:11:43 -05002134 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002135 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002136 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002137 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002138 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002139 } else {
2140 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002141 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2142 spin_lock_irq(&port->lock);
2143 atmel_disable_ms(port);
2144 spin_unlock_irq(&port->lock);
2145 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002146 }
2147}
2148
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002149/*
2150 * Return string describing the specified port
2151 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002152static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002153{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002154 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002155}
2156
2157/*
2158 * Release the memory region(s) being used by 'port'.
2159 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002160static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002161{
Andrew Victorafefc412006-06-19 19:53:19 +01002162 struct platform_device *pdev = to_platform_device(port->dev);
2163 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2164
2165 release_mem_region(port->mapbase, size);
2166
2167 if (port->flags & UPF_IOREMAP) {
2168 iounmap(port->membase);
2169 port->membase = NULL;
2170 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002171}
2172
2173/*
2174 * Request the memory region(s) being used by 'port'.
2175 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002176static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002177{
Andrew Victorafefc412006-06-19 19:53:19 +01002178 struct platform_device *pdev = to_platform_device(port->dev);
2179 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002180
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002181 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002182 return -EBUSY;
2183
2184 if (port->flags & UPF_IOREMAP) {
2185 port->membase = ioremap(port->mapbase, size);
2186 if (port->membase == NULL) {
2187 release_mem_region(port->mapbase, size);
2188 return -ENOMEM;
2189 }
2190 }
2191
2192 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002193}
2194
2195/*
2196 * Configure/autoconfigure the port.
2197 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002198static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002199{
2200 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002201 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002202 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002203 }
2204}
2205
2206/*
2207 * Verify the new serial_struct (for TIOCSSERIAL).
2208 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002209static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002210{
2211 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002212 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002213 ret = -EINVAL;
2214 if (port->irq != ser->irq)
2215 ret = -EINVAL;
2216 if (ser->io_type != SERIAL_IO_MEM)
2217 ret = -EINVAL;
2218 if (port->uartclk / 16 != ser->baud_base)
2219 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002220 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002221 ret = -EINVAL;
2222 if (port->iobase != ser->port)
2223 ret = -EINVAL;
2224 if (ser->hub6 != 0)
2225 ret = -EINVAL;
2226 return ret;
2227}
2228
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002229#ifdef CONFIG_CONSOLE_POLL
2230static int atmel_poll_get_char(struct uart_port *port)
2231{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002232 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002233 cpu_relax();
2234
Cyrille Pitchena6499432015-07-30 16:33:38 +02002235 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002236}
2237
2238static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2239{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002240 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002241 cpu_relax();
2242
Cyrille Pitchena6499432015-07-30 16:33:38 +02002243 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002244}
2245#endif
2246
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002247static struct uart_ops atmel_pops = {
2248 .tx_empty = atmel_tx_empty,
2249 .set_mctrl = atmel_set_mctrl,
2250 .get_mctrl = atmel_get_mctrl,
2251 .stop_tx = atmel_stop_tx,
2252 .start_tx = atmel_start_tx,
2253 .stop_rx = atmel_stop_rx,
2254 .enable_ms = atmel_enable_ms,
2255 .break_ctl = atmel_break_ctl,
2256 .startup = atmel_startup,
2257 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002258 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002259 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002260 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002261 .type = atmel_type,
2262 .release_port = atmel_release_port,
2263 .request_port = atmel_request_port,
2264 .config_port = atmel_config_port,
2265 .verify_port = atmel_verify_port,
2266 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002267#ifdef CONFIG_CONSOLE_POLL
2268 .poll_get_char = atmel_poll_get_char,
2269 .poll_put_char = atmel_poll_put_char,
2270#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002271};
2272
Andrew Victorafefc412006-06-19 19:53:19 +01002273/*
2274 * Configure the port from the platform device resource info.
2275 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002276static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002277 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002278{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002279 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002280 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002281 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002282
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002283 atmel_init_property(atmel_port, pdev);
2284 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002285
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002286 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002287
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002288 port->iotype = UPIO_MEM;
2289 port->flags = UPF_BOOT_AUTOCONF;
2290 port->ops = &atmel_pops;
2291 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002292 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002293 port->mapbase = pdev->resource[0].start;
2294 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002295 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002296
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002297 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2298 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08002299 tasklet_disable(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002300
2301 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2302
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002303 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002304 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002305 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002306 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002307 port->flags |= UPF_IOREMAP;
2308 port->membase = NULL;
2309 }
2310
Remy Bohmerb843aa22008-02-08 04:21:01 -08002311 /* for console, the clock could already be configured */
2312 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002313 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002314 if (IS_ERR(atmel_port->clk)) {
2315 ret = PTR_ERR(atmel_port->clk);
2316 atmel_port->clk = NULL;
2317 return ret;
2318 }
2319 ret = clk_prepare_enable(atmel_port->clk);
2320 if (ret) {
2321 clk_put(atmel_port->clk);
2322 atmel_port->clk = NULL;
2323 return ret;
2324 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002325 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002326 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002327 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002328 }
Chip Coldwella6670612008-02-08 04:21:06 -08002329
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002330 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002331 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002332 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002333 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002334 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002335 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2336 } else {
2337 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2338 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002339
2340 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002341}
2342
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002343struct platform_device *atmel_default_console_device; /* the serial console device */
2344
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002345#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002346static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002347{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002348 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002349 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002350 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002351}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002352
2353/*
2354 * Interrupts are disabled on entering
2355 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002356static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002357{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002358 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002359 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002360 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002361 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002362
2363 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002364 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002365 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002366 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2367 atmel_uart_writel(port, ATMEL_US_IDR,
2368 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002369
Marc Pignat39d4c922008-04-02 13:04:42 -07002370 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002371 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2372 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002373
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002374 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002375
2376 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002377 * Finally, wait for transmitter to become empty
2378 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002379 */
2380 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002381 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002382 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002383
2384 /* Restore PDC transmit status */
2385 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002386 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002387
Remy Bohmerb843aa22008-02-08 04:21:01 -08002388 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002389 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002390}
2391
2392/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002393 * If the port was already initialised (eg, by a boot loader),
2394 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002395 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002396static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2397 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002398{
2399 unsigned int mr, quot;
2400
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002401 /*
2402 * If the baud rate generator isn't running, the port wasn't
2403 * initialized by the boot loader.
2404 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002405 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002406 if (!quot)
2407 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002408
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002409 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002410 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002411 *bits = 8;
2412 else
2413 *bits = 7;
2414
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002415 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002416 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002417 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002418 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002419 *parity = 'o';
2420
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002421 /*
2422 * The serial core only rounds down when matching this to a
2423 * supported baud rate. Make sure we don't end up slightly
2424 * lower than one of those, as it would make us fall through
2425 * to a much lower baud rate than we really want.
2426 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002427 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002428}
2429
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002430static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002431{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002432 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002433 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002434 int baud = 115200;
2435 int bits = 8;
2436 int parity = 'n';
2437 int flow = 'n';
2438
Remy Bohmerb843aa22008-02-08 04:21:01 -08002439 if (port->membase == NULL) {
2440 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002441 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002442 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002443
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002444 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2445 if (ret)
2446 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002447
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002448 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2449 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2450 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002451
2452 if (options)
2453 uart_parse_options(options, &baud, &parity, &bits, &flow);
2454 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002455 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002456
2457 return uart_set_options(port, co, baud, parity, bits, flow);
2458}
2459
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002460static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002461
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002462static struct console atmel_console = {
2463 .name = ATMEL_DEVICENAME,
2464 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002465 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002466 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002467 .flags = CON_PRINTBUFFER,
2468 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002469 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002470};
2471
David Brownell06a7f052008-11-06 12:53:40 -08002472#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002473
Andrew Victorafefc412006-06-19 19:53:19 +01002474/*
2475 * Early console initialization (before VM subsystem initialized).
2476 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002477static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002478{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002479 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002480 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002481 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002482 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002483 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002484 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002485
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002486 port->backup_imr = 0;
2487 port->uart.line = id;
2488
2489 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002490 ret = atmel_init_port(port, atmel_default_console_device);
2491 if (ret)
2492 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002493 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002494 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002495
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002496 return 0;
2497}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002498
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002499console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002500
Andrew Victorafefc412006-06-19 19:53:19 +01002501/*
2502 * Late console initialization.
2503 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002504static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002505{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002506 if (atmel_default_console_device
2507 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002508 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002509
2510 return 0;
2511}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002512
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002513core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002514
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002515static inline bool atmel_is_console_port(struct uart_port *port)
2516{
2517 return port->cons && port->cons->index == port->line;
2518}
2519
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002520#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002521#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002522
2523static inline bool atmel_is_console_port(struct uart_port *port)
2524{
2525 return false;
2526}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002527#endif
2528
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002529static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002530 .owner = THIS_MODULE,
2531 .driver_name = "atmel_serial",
2532 .dev_name = ATMEL_DEVICENAME,
2533 .major = SERIAL_ATMEL_MAJOR,
2534 .minor = MINOR_START,
2535 .nr = ATMEL_MAX_UART,
2536 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002537};
2538
Andrew Victorafefc412006-06-19 19:53:19 +01002539#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002540static bool atmel_serial_clk_will_stop(void)
2541{
2542#ifdef CONFIG_ARCH_AT91
2543 return at91_suspend_entering_slow_clock();
2544#else
2545 return false;
2546#endif
2547}
2548
Remy Bohmerb843aa22008-02-08 04:21:01 -08002549static int atmel_serial_suspend(struct platform_device *pdev,
2550 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002551{
Andrew Victorafefc412006-06-19 19:53:19 +01002552 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002553 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002554
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002555 if (atmel_is_console_port(port) && console_suspend_enabled) {
2556 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002557 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2558 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002559 cpu_relax();
2560 }
2561
Anti Sullinf05596d2008-09-22 13:57:54 -07002562 /* we can not wake up if we're running on slow clock */
2563 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002564 if (atmel_serial_clk_will_stop()) {
2565 unsigned long flags;
2566
2567 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2568 atmel_port->suspended = true;
2569 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002570 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002571 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002572
2573 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002574
2575 return 0;
2576}
2577
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002578static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002579{
2580 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);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002582 unsigned long flags;
2583
2584 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2585 if (atmel_port->pending) {
2586 atmel_handle_receive(port, atmel_port->pending);
2587 atmel_handle_status(port, atmel_port->pending,
2588 atmel_port->pending_status);
2589 atmel_handle_transmit(port, atmel_port->pending);
2590 atmel_port->pending = 0;
2591 }
2592 atmel_port->suspended = false;
2593 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002594
Anti Sullinf05596d2008-09-22 13:57:54 -07002595 uart_resume_port(&atmel_uart, port);
2596 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002597
2598 return 0;
2599}
2600#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002601#define atmel_serial_suspend NULL
2602#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002603#endif
2604
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002605static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2606 struct platform_device *pdev)
2607{
2608 port->fifo_size = 0;
2609 port->rts_low = 0;
2610 port->rts_high = 0;
2611
2612 if (of_property_read_u32(pdev->dev.of_node,
2613 "atmel,fifo-size",
2614 &port->fifo_size))
2615 return;
2616
2617 if (!port->fifo_size)
2618 return;
2619
2620 if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2621 port->fifo_size = 0;
2622 dev_err(&pdev->dev, "Invalid FIFO size\n");
2623 return;
2624 }
2625
2626 /*
2627 * 0 <= rts_low <= rts_high <= fifo_size
2628 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2629 * to flush their internal TX FIFO, commonly up to 16 data, before
2630 * actually stopping to send new data. So we try to set the RTS High
2631 * Threshold to a reasonably high value respecting this 16 data
2632 * empirical rule when possible.
2633 */
2634 port->rts_high = max_t(int, port->fifo_size >> 1,
2635 port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2636 port->rts_low = max_t(int, port->fifo_size >> 2,
2637 port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2638
2639 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2640 port->fifo_size);
2641 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2642 port->rts_high);
2643 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
2644 port->rts_low);
2645}
2646
Bill Pemberton9671f092012-11-19 13:21:50 -05002647static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002648{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002649 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002650 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002651 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002652 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002653 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002654 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002655
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002656 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002657
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002658 if (np)
2659 ret = of_alias_get_id(np, "serial");
2660 else
2661 if (pdata)
2662 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002663
2664 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002665 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002666 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002667 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002668
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002669 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002670 ret = -ENODEV;
2671 goto err;
2672 }
2673
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002674 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002675 /* port already in use */
2676 ret = -EBUSY;
2677 goto err;
2678 }
2679
2680 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002681 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002682 port->uart.line = ret;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002683 atmel_serial_probe_fifos(port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002684
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002685 spin_lock_init(&port->lock_suspended);
2686
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002687 ret = atmel_init_port(port, pdev);
2688 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002689 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002690
Uwe Kleine-König18dfef92015-10-18 21:34:45 +02002691 port->gpios = mctrl_gpio_init(&port->uart, 0);
2692 if (IS_ERR(port->gpios)) {
2693 ret = PTR_ERR(port->gpios);
2694 goto err_clear_bit;
2695 }
2696
Elen Song64e22eb2013-07-22 16:30:24 +08002697 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002698 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002699 data = kmalloc(sizeof(struct atmel_uart_char)
2700 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002701 if (!data)
2702 goto err_alloc_ring;
2703 port->rx_ring.buf = data;
2704 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002705
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002706 rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2707
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002708 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002709 if (ret)
2710 goto err_add_port;
2711
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002712#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002713 if (atmel_is_console_port(&port->uart)
2714 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2715 /*
2716 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002717 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002718 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002719 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002720 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002721#endif
David Brownell06a7f052008-11-06 12:53:40 -08002722
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002723 device_init_wakeup(&pdev->dev, 1);
2724 platform_set_drvdata(pdev, port);
2725
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002726 /*
2727 * The peripheral clock has been disabled by atmel_init_port():
2728 * enable it before accessing I/O registers
2729 */
2730 clk_prepare_enable(port->clk);
2731
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002732 if (rs485_enabled) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002733 atmel_uart_writel(&port->uart, ATMEL_US_MR,
2734 ATMEL_US_USMODE_NORMAL);
2735 atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002736 }
2737
Elen Song055560b2013-07-22 16:30:29 +08002738 /*
2739 * Get port name of usart or uart
2740 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002741 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002742
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002743 /*
2744 * The peripheral clock can now safely be disabled till the port
2745 * is used
2746 */
2747 clk_disable_unprepare(port->clk);
2748
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002749 return 0;
2750
2751err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002752 kfree(port->rx_ring.buf);
2753 port->rx_ring.buf = NULL;
2754err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002755 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002756 clk_put(port->clk);
2757 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002758 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002759err_clear_bit:
2760 clear_bit(port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002761err:
Andrew Victorafefc412006-06-19 19:53:19 +01002762 return ret;
2763}
2764
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002765static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002766{
2767 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002768 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002769 int ret = 0;
2770
Marek Roszkof50c995f2014-01-07 11:45:07 +01002771 tasklet_kill(&atmel_port->tasklet);
2772
Andrew Victorafefc412006-06-19 19:53:19 +01002773 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002774
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002775 ret = uart_remove_one_port(&atmel_uart, port);
2776
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002777 kfree(atmel_port->rx_ring.buf);
2778
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002779 /* "port" is allocated statically, so we shouldn't free it */
2780
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002781 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002782
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002783 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002784
2785 return ret;
2786}
2787
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002788static struct platform_driver atmel_serial_driver = {
2789 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002790 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002791 .suspend = atmel_serial_suspend,
2792 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002793 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002794 .name = "atmel_usart",
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002795 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002796 },
2797};
2798
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002799static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002800{
2801 int ret;
2802
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002803 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002804 if (ret)
2805 return ret;
2806
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002807 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002808 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002809 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002810
2811 return ret;
2812}
2813
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002814static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002815{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002816 platform_driver_unregister(&atmel_serial_driver);
2817 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002818}
2819
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002820module_init(atmel_serial_init);
2821module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002822
2823MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002824MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002825MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002826MODULE_ALIAS("platform:atmel_usart");