blob: 5ca5cf3e9359cf17f9a3aaebbff028ecada3a910 [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/*
115 * We wrap our port structure around the generic uart_port.
116 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200117struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100118 struct uart_port uart; /* uart */
119 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700120 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
121 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700122 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800123
Elen Song34df42f2013-07-22 16:30:27 +0800124 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800125 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800126 short pdc_rx_idx; /* current PDC RX buffer */
127 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
128
Elen Song08f738b2013-07-22 16:30:26 +0800129 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800130 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800131 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
132
Elen Song08f738b2013-07-22 16:30:26 +0800133 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800134 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800135 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800136 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800137 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800138 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800139 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800140 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800141 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800142 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800143 struct tasklet_struct tasklet;
144 unsigned int irq_status;
145 unsigned int irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +0800146 unsigned int status_change;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200147 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800148
149 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100150
Richard Genoude0b0baa2014-05-13 20:20:44 +0200151 struct mctrl_gpios *gpios;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200152 int gpio_irq[UART_GPIO_MAX];
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100153 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200154 u32 fifo_size;
155 u32 rts_high;
156 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200157 bool ms_irq_enabled;
Elen Song055560b2013-07-22 16:30:29 +0800158 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800159 struct timer_list uart_timer; /* uart timer */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100160
161 bool suspended;
162 unsigned int pending;
163 unsigned int pending_status;
164 spinlock_t lock_suspended;
165
Elen Songa930e522013-07-22 16:30:25 +0800166 int (*prepare_rx)(struct uart_port *port);
167 int (*prepare_tx)(struct uart_port *port);
168 void (*schedule_rx)(struct uart_port *port);
169 void (*schedule_tx)(struct uart_port *port);
170 void (*release_rx)(struct uart_port *port);
171 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100172};
173
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200174static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100175static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100176
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000177#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200178static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000179#endif
180
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200181#if defined(CONFIG_OF)
182static const struct of_device_id atmel_serial_dt_ids[] = {
183 { .compatible = "atmel,at91rm9200-usart" },
184 { .compatible = "atmel,at91sam9260-usart" },
185 { /* sentinel */ }
186};
187
188MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
189#endif
190
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800191static inline struct atmel_uart_port *
192to_atmel_uart_port(struct uart_port *uart)
193{
194 return container_of(uart, struct atmel_uart_port, uart);
195}
196
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200197static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
198{
199 return __raw_readl(port->membase + reg);
200}
201
202static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
203{
204 __raw_writel(value, port->membase + reg);
205}
206
Cyrille Pitchena6499432015-07-30 16:33:38 +0200207#ifdef CONFIG_AVR32
208
209/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
210static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200211{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200212 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200213}
214
Cyrille Pitchena6499432015-07-30 16:33:38 +0200215static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200216{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200217 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200218}
219
Cyrille Pitchena6499432015-07-30 16:33:38 +0200220#else
221
222static inline u8 atmel_uart_read_char(struct uart_port *port)
223{
224 return __raw_readb(port->membase + ATMEL_US_RHR);
225}
226
227static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
228{
229 __raw_writeb(value, port->membase + ATMEL_US_THR);
230}
231
232#endif
233
Chip Coldwella6670612008-02-08 04:21:06 -0800234#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800235static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800236{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800237 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800238
Elen Song64e22eb2013-07-22 16:30:24 +0800239 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800240}
241
Elen Song64e22eb2013-07-22 16:30:24 +0800242static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800243{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800244 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800245
Elen Song64e22eb2013-07-22 16:30:24 +0800246 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800247}
248#else
Elen Song64e22eb2013-07-22 16:30:24 +0800249static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800250{
251 return false;
252}
253
Elen Song64e22eb2013-07-22 16:30:24 +0800254static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800255{
256 return false;
257}
258#endif
259
Elen Song08f738b2013-07-22 16:30:26 +0800260static bool atmel_use_dma_tx(struct uart_port *port)
261{
262 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
263
264 return atmel_port->use_dma_tx;
265}
266
Elen Song34df42f2013-07-22 16:30:27 +0800267static bool atmel_use_dma_rx(struct uart_port *port)
268{
269 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
270
271 return atmel_port->use_dma_rx;
272}
273
Richard Genoude0b0baa2014-05-13 20:20:44 +0200274static unsigned int atmel_get_lines_status(struct uart_port *port)
275{
276 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
277 unsigned int status, ret = 0;
278
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200279 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200280
281 mctrl_gpio_get(atmel_port->gpios, &ret);
282
283 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
284 UART_GPIO_CTS))) {
285 if (ret & TIOCM_CTS)
286 status &= ~ATMEL_US_CTS;
287 else
288 status |= ATMEL_US_CTS;
289 }
290
291 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
292 UART_GPIO_DSR))) {
293 if (ret & TIOCM_DSR)
294 status &= ~ATMEL_US_DSR;
295 else
296 status |= ATMEL_US_DSR;
297 }
298
299 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
300 UART_GPIO_RI))) {
301 if (ret & TIOCM_RI)
302 status &= ~ATMEL_US_RI;
303 else
304 status |= ATMEL_US_RI;
305 }
306
307 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
308 UART_GPIO_DCD))) {
309 if (ret & TIOCM_CD)
310 status &= ~ATMEL_US_DCD;
311 else
312 status |= ATMEL_US_DCD;
313 }
314
315 return status;
316}
317
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100318/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100319static int atmel_config_rs485(struct uart_port *port,
320 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100321{
322 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
323 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100324
325 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200326 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100327
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200328 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100329
330 /* Resetting serial mode to RS232 (0x0) */
331 mode &= ~ATMEL_US_USMODE;
332
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100333 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100334
335 if (rs485conf->flags & SER_RS485_ENABLED) {
336 dev_dbg(port->dev, "Setting UART to RS485\n");
337 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200338 atmel_uart_writel(port, ATMEL_US_TTGR,
339 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100340 mode |= ATMEL_US_USMODE_RS485;
341 } else {
342 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800343 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100344 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
345 ATMEL_US_TXBUFE;
346 else
347 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
348 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200349 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100350
351 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200352 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100353
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100354 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100355}
356
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000357/*
358 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
359 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200360static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000361{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200362 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
363 TIOCSER_TEMT :
364 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000365}
366
367/*
368 * Set state of the modem control output lines
369 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200370static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000371{
372 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200373 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100374 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100375 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000376
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100377 /* override mode to RS485 if needed, otherwise keep the current mode */
378 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200379 atmel_uart_writel(port, ATMEL_US_TTGR,
380 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100381 mode &= ~ATMEL_US_USMODE;
382 mode |= ATMEL_US_USMODE_RS485;
383 }
384
385 /* set the RTS line state according to the mode */
386 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
387 /* force RTS line to high level */
388 rts_paused = ATMEL_US_RTSEN;
389
390 /* give the control of the RTS line back to the hardware */
391 rts_ready = ATMEL_US_RTSDIS;
392 } else {
393 /* force RTS line to high level */
394 rts_paused = ATMEL_US_RTSDIS;
395
396 /* force RTS line to low level */
397 rts_ready = ATMEL_US_RTSEN;
398 }
399
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000400 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100401 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000402 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100403 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000404
405 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200406 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000407 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200408 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000409
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200410 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100411
Richard Genoude0b0baa2014-05-13 20:20:44 +0200412 mctrl_gpio_set(atmel_port->gpios, mctrl);
413
Andrew Victorafefc412006-06-19 19:53:19 +0100414 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100415 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100416 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200417 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100418 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200419 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100420
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200421 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000422}
423
424/*
425 * Get state of the modem control input lines
426 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200427static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000428{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200429 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
430 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000431
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200432 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000433
434 /*
435 * The control signals are active low.
436 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200437 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000438 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200439 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000440 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200441 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000442 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200443 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000444 ret |= TIOCM_RI;
445
Richard Genoude0b0baa2014-05-13 20:20:44 +0200446 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000447}
448
449/*
450 * Stop transmitting.
451 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200452static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000453{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100454 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
455
Elen Song64e22eb2013-07-22 16:30:24 +0800456 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800457 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200458 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100459 }
460 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200461 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100462
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100463 if ((port->rs485.flags & SER_RS485_ENABLED) &&
464 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100465 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000466}
467
468/*
469 * Start transmitting.
470 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200471static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000472{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100473 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
474
Elen Song64e22eb2013-07-22 16:30:24 +0800475 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200476 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800477 /* The transmitter is already running. Yes, we
478 really need this.*/
479 return;
480
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100481 if ((port->rs485.flags & SER_RS485_ENABLED) &&
482 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100483 atmel_stop_rx(port);
484
Chip Coldwella6670612008-02-08 04:21:06 -0800485 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200486 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100487 }
488 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200489 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100490}
491
492/*
493 * start receiving - port is in process of being opened.
494 */
495static void atmel_start_rx(struct uart_port *port)
496{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200497 /* reset status and receiver */
498 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100499
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200500 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200501
Elen Song64e22eb2013-07-22 16:30:24 +0800502 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100503 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200504 atmel_uart_writel(port, ATMEL_US_IER,
505 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
506 port->read_status_mask);
507 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100508 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200509 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100510 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000511}
512
513/*
514 * Stop receiving - port is in process of being closed.
515 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200516static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000517{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200518 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200519
Elen Song64e22eb2013-07-22 16:30:24 +0800520 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800521 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200522 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
523 atmel_uart_writel(port, ATMEL_US_IDR,
524 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
525 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100526 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200527 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100528 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000529}
530
531/*
532 * Enable modem status interrupts
533 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200534static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000535{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200536 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
537 uint32_t ier = 0;
538
539 /*
540 * Interrupt should not be enabled twice
541 */
542 if (atmel_port->ms_irq_enabled)
543 return;
544
545 atmel_port->ms_irq_enabled = true;
546
547 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
548 enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
549 else
550 ier |= ATMEL_US_CTSIC;
551
552 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
553 enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
554 else
555 ier |= ATMEL_US_DSRIC;
556
557 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
558 enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
559 else
560 ier |= ATMEL_US_RIIC;
561
562 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
563 enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
564 else
565 ier |= ATMEL_US_DCDIC;
566
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200567 atmel_uart_writel(port, ATMEL_US_IER, ier);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000568}
569
570/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200571 * Disable modem status interrupts
572 */
573static void atmel_disable_ms(struct uart_port *port)
574{
575 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
576 uint32_t idr = 0;
577
578 /*
579 * Interrupt should not be disabled twice
580 */
581 if (!atmel_port->ms_irq_enabled)
582 return;
583
584 atmel_port->ms_irq_enabled = false;
585
586 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
587 disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
588 else
589 idr |= ATMEL_US_CTSIC;
590
591 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
592 disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
593 else
594 idr |= ATMEL_US_DSRIC;
595
596 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
597 disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
598 else
599 idr |= ATMEL_US_RIIC;
600
601 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
602 disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
603 else
604 idr |= ATMEL_US_DCDIC;
605
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200606 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200607}
608
609/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000610 * Control the transmission of a break signal
611 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200612static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000613{
614 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200615 /* start break */
616 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000617 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200618 /* stop break */
619 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000620}
621
622/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800623 * Stores the incoming character in the ring buffer
624 */
625static void
626atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
627 unsigned int ch)
628{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800629 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800630 struct circ_buf *ring = &atmel_port->rx_ring;
631 struct atmel_uart_char *c;
632
633 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
634 /* Buffer overflow, ignore char */
635 return;
636
637 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
638 c->status = status;
639 c->ch = ch;
640
641 /* Make sure the character is stored before we update head. */
642 smp_wmb();
643
644 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
645}
646
647/*
Chip Coldwella6670612008-02-08 04:21:06 -0800648 * Deal with parity, framing and overrun errors.
649 */
650static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
651{
652 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200653 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800654
655 if (status & ATMEL_US_RXBRK) {
656 /* ignore side-effect */
657 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
658 port->icount.brk++;
659 }
660 if (status & ATMEL_US_PARE)
661 port->icount.parity++;
662 if (status & ATMEL_US_FRAME)
663 port->icount.frame++;
664 if (status & ATMEL_US_OVRE)
665 port->icount.overrun++;
666}
667
668/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000669 * Characters received (called from interrupt handler)
670 */
David Howells7d12e782006-10-05 14:55:46 +0100671static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800673 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800674 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000675
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200676 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200677 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200678 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000679
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000680 /*
681 * note that the error handling code is
682 * out of the main execution path
683 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700684 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
685 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
686 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800687
Remy Bohmerb843aa22008-02-08 04:21:01 -0800688 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200689 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800690
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700691 if (status & ATMEL_US_RXBRK
692 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700693 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200694 atmel_uart_writel(port, ATMEL_US_IER,
695 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700696 } else {
697 /*
698 * This is either the end-of-break
699 * condition or we've received at
700 * least one character without RXBRK
701 * being set. In both cases, the next
702 * RXBRK will indicate start-of-break.
703 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200704 atmel_uart_writel(port, ATMEL_US_IDR,
705 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700706 status &= ~ATMEL_US_RXBRK;
707 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100708 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000709 }
710
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800711 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200712 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000713 }
714
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800715 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000716}
717
718/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800719 * Transmit characters (called from tasklet with TXRDY interrupt
720 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000721 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200722static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000723{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700724 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100725 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000726
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200727 if (port->x_char &&
728 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200729 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000730 port->icount.tx++;
731 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000732 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800733 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000734 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000735
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200736 while (atmel_uart_readl(port, ATMEL_US_CSR) &
737 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200738 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000739 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
740 port->icount.tx++;
741 if (uart_circ_empty(xmit))
742 break;
743 }
744
745 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
746 uart_write_wakeup(port);
747
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800748 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100749 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200750 atmel_uart_writel(port, ATMEL_US_IER,
751 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000752}
753
Elen Song08f738b2013-07-22 16:30:26 +0800754static void atmel_complete_tx_dma(void *arg)
755{
756 struct atmel_uart_port *atmel_port = arg;
757 struct uart_port *port = &atmel_port->uart;
758 struct circ_buf *xmit = &port->state->xmit;
759 struct dma_chan *chan = atmel_port->chan_tx;
760 unsigned long flags;
761
762 spin_lock_irqsave(&port->lock, flags);
763
764 if (chan)
765 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200766 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800767 xmit->tail &= UART_XMIT_SIZE - 1;
768
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200769 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800770
771 spin_lock_irq(&atmel_port->lock_tx);
772 async_tx_ack(atmel_port->desc_tx);
773 atmel_port->cookie_tx = -EINVAL;
774 atmel_port->desc_tx = NULL;
775 spin_unlock_irq(&atmel_port->lock_tx);
776
777 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
778 uart_write_wakeup(port);
779
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100780 /*
781 * xmit is a circular buffer so, if we have just send data from
782 * xmit->tail to the end of xmit->buf, now we have to transmit the
783 * remaining data from the beginning of xmit->buf to xmit->head.
784 */
Elen Song08f738b2013-07-22 16:30:26 +0800785 if (!uart_circ_empty(xmit))
786 tasklet_schedule(&atmel_port->tasklet);
787
788 spin_unlock_irqrestore(&port->lock, flags);
789}
790
791static void atmel_release_tx_dma(struct uart_port *port)
792{
793 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
794 struct dma_chan *chan = atmel_port->chan_tx;
795
796 if (chan) {
797 dmaengine_terminate_all(chan);
798 dma_release_channel(chan);
799 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200800 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800801 }
802
803 atmel_port->desc_tx = NULL;
804 atmel_port->chan_tx = NULL;
805 atmel_port->cookie_tx = -EINVAL;
806}
807
808/*
809 * Called from tasklet with TXRDY interrupt is disabled.
810 */
811static void atmel_tx_dma(struct uart_port *port)
812{
813 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
814 struct circ_buf *xmit = &port->state->xmit;
815 struct dma_chan *chan = atmel_port->chan_tx;
816 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200817 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
818 unsigned int tx_len, part1_len, part2_len, sg_len;
819 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800820
821 /* Make sure we have an idle channel */
822 if (atmel_port->desc_tx != NULL)
823 return;
824
825 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
826 /*
827 * DMA is idle now.
828 * Port xmit buffer is already mapped,
829 * and it is one page... Just adjust
830 * offsets and lengths. Since it is a circular buffer,
831 * we have to transmit till the end, and then the rest.
832 * Take the port lock to get a
833 * consistent xmit buffer state.
834 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200835 tx_len = CIRC_CNT_TO_END(xmit->head,
836 xmit->tail,
837 UART_XMIT_SIZE);
838
839 if (atmel_port->fifo_size) {
840 /* multi data mode */
841 part1_len = (tx_len & ~0x3); /* DWORD access */
842 part2_len = (tx_len & 0x3); /* BYTE access */
843 } else {
844 /* single data (legacy) mode */
845 part1_len = 0;
846 part2_len = tx_len; /* BYTE access only */
847 }
848
849 sg_init_table(sgl, 2);
850 sg_len = 0;
851 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
852 if (part1_len) {
853 sg = &sgl[sg_len++];
854 sg_dma_address(sg) = phys_addr;
855 sg_dma_len(sg) = part1_len;
856
857 phys_addr += part1_len;
858 }
859
860 if (part2_len) {
861 sg = &sgl[sg_len++];
862 sg_dma_address(sg) = phys_addr;
863 sg_dma_len(sg) = part2_len;
864 }
865
866 /*
867 * save tx_len so atmel_complete_tx_dma() will increase
868 * xmit->tail correctly
869 */
870 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800871
872 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200873 sgl,
874 sg_len,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +0100875 DMA_MEM_TO_DEV,
876 DMA_PREP_INTERRUPT |
877 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800878 if (!desc) {
879 dev_err(port->dev, "Failed to send via dma!\n");
880 return;
881 }
882
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200883 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800884
885 atmel_port->desc_tx = desc;
886 desc->callback = atmel_complete_tx_dma;
887 desc->callback_param = atmel_port;
888 atmel_port->cookie_tx = dmaengine_submit(desc);
889
890 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100891 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800892 /* DMA done, stop TX, start RX for RS485 */
893 atmel_start_rx(port);
894 }
895 }
896
897 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
898 uart_write_wakeup(port);
899}
900
901static int atmel_prepare_tx_dma(struct uart_port *port)
902{
903 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
904 dma_cap_mask_t mask;
905 struct dma_slave_config config;
906 int ret, nent;
907
908 dma_cap_zero(mask);
909 dma_cap_set(DMA_SLAVE, mask);
910
911 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
912 if (atmel_port->chan_tx == NULL)
913 goto chan_err;
914 dev_info(port->dev, "using %s for tx DMA transfers\n",
915 dma_chan_name(atmel_port->chan_tx));
916
917 spin_lock_init(&atmel_port->lock_tx);
918 sg_init_table(&atmel_port->sg_tx, 1);
919 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800920 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800921 sg_set_page(&atmel_port->sg_tx,
922 virt_to_page(port->state->xmit.buf),
923 UART_XMIT_SIZE,
924 (int)port->state->xmit.buf & ~PAGE_MASK);
925 nent = dma_map_sg(port->dev,
926 &atmel_port->sg_tx,
927 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200928 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800929
930 if (!nent) {
931 dev_dbg(port->dev, "need to release resource of dma\n");
932 goto chan_err;
933 } else {
934 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
935 sg_dma_len(&atmel_port->sg_tx),
936 port->state->xmit.buf,
937 sg_dma_address(&atmel_port->sg_tx));
938 }
939
940 /* Configure the slave DMA */
941 memset(&config, 0, sizeof(config));
942 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200943 config.dst_addr_width = (atmel_port->fifo_size) ?
944 DMA_SLAVE_BUSWIDTH_4_BYTES :
945 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800946 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200947 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800948
Maxime Ripard5483c102014-10-22 17:43:16 +0200949 ret = dmaengine_slave_config(atmel_port->chan_tx,
950 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800951 if (ret) {
952 dev_err(port->dev, "DMA tx slave configuration failed\n");
953 goto chan_err;
954 }
955
956 return 0;
957
958chan_err:
959 dev_err(port->dev, "TX channel not available, switch to pio\n");
960 atmel_port->use_dma_tx = 0;
961 if (atmel_port->chan_tx)
962 atmel_release_tx_dma(port);
963 return -EINVAL;
964}
965
Elen Song34df42f2013-07-22 16:30:27 +0800966static void atmel_complete_rx_dma(void *arg)
967{
968 struct uart_port *port = arg;
969 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
970
971 tasklet_schedule(&atmel_port->tasklet);
972}
973
974static void atmel_release_rx_dma(struct uart_port *port)
975{
976 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
977 struct dma_chan *chan = atmel_port->chan_rx;
978
979 if (chan) {
980 dmaengine_terminate_all(chan);
981 dma_release_channel(chan);
982 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200983 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800984 }
985
986 atmel_port->desc_rx = NULL;
987 atmel_port->chan_rx = NULL;
988 atmel_port->cookie_rx = -EINVAL;
989}
990
991static void atmel_rx_from_dma(struct uart_port *port)
992{
993 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200994 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +0800995 struct circ_buf *ring = &atmel_port->rx_ring;
996 struct dma_chan *chan = atmel_port->chan_rx;
997 struct dma_tx_state state;
998 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +0200999 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001000
1001
1002 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001003 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001004 dmastat = dmaengine_tx_status(chan,
1005 atmel_port->cookie_rx,
1006 &state);
1007 /* Restart a new tasklet if DMA status is error */
1008 if (dmastat == DMA_ERROR) {
1009 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001010 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001011 tasklet_schedule(&atmel_port->tasklet);
1012 return;
1013 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001014
1015 /* CPU claims ownership of RX DMA buffer */
1016 dma_sync_sg_for_cpu(port->dev,
1017 &atmel_port->sg_rx,
1018 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001019 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001020
1021 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001022 * ring->head points to the end of data already written by the DMA.
1023 * ring->tail points to the beginning of data to be read by the
1024 * framework.
1025 * The current transfer size should not be larger than the dma buffer
1026 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001027 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001028 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1029 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1030 /*
1031 * At this point ring->head may point to the first byte right after the
1032 * last byte of the dma buffer:
1033 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1034 *
1035 * However ring->tail must always points inside the dma buffer:
1036 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1037 *
1038 * Since we use a ring buffer, we have to handle the case
1039 * where head is lower than tail. In such a case, we first read from
1040 * tail to the end of the buffer then reset tail.
1041 */
1042 if (ring->head < ring->tail) {
1043 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001044
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001045 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1046 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001047 port->icount.rx += count;
1048 }
1049
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001050 /* Finally we read data from tail to head */
1051 if (ring->tail < ring->head) {
1052 count = ring->head - ring->tail;
1053
1054 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1055 /* Wrap ring->head if needed */
1056 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1057 ring->head = 0;
1058 ring->tail = ring->head;
1059 port->icount.rx += count;
1060 }
1061
1062 /* USART retreives ownership of RX DMA buffer */
1063 dma_sync_sg_for_device(port->dev,
1064 &atmel_port->sg_rx,
1065 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001066 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001067
1068 /*
1069 * Drop the lock here since it might end up calling
1070 * uart_start(), which takes the lock.
1071 */
1072 spin_unlock(&port->lock);
1073 tty_flip_buffer_push(tport);
1074 spin_lock(&port->lock);
1075
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001076 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001077}
1078
1079static int atmel_prepare_rx_dma(struct uart_port *port)
1080{
1081 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1082 struct dma_async_tx_descriptor *desc;
1083 dma_cap_mask_t mask;
1084 struct dma_slave_config config;
1085 struct circ_buf *ring;
1086 int ret, nent;
1087
1088 ring = &atmel_port->rx_ring;
1089
1090 dma_cap_zero(mask);
1091 dma_cap_set(DMA_CYCLIC, mask);
1092
1093 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1094 if (atmel_port->chan_rx == NULL)
1095 goto chan_err;
1096 dev_info(port->dev, "using %s for rx DMA transfers\n",
1097 dma_chan_name(atmel_port->chan_rx));
1098
1099 spin_lock_init(&atmel_port->lock_rx);
1100 sg_init_table(&atmel_port->sg_rx, 1);
1101 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001102 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001103 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001104 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001105 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001106 (int)ring->buf & ~PAGE_MASK);
1107 nent = dma_map_sg(port->dev,
1108 &atmel_port->sg_rx,
1109 1,
1110 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001111
1112 if (!nent) {
1113 dev_dbg(port->dev, "need to release resource of dma\n");
1114 goto chan_err;
1115 } else {
1116 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
1117 sg_dma_len(&atmel_port->sg_rx),
1118 ring->buf,
1119 sg_dma_address(&atmel_port->sg_rx));
1120 }
1121
1122 /* Configure the slave DMA */
1123 memset(&config, 0, sizeof(config));
1124 config.direction = DMA_DEV_TO_MEM;
1125 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1126 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001127 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001128
Maxime Ripard5483c102014-10-22 17:43:16 +02001129 ret = dmaengine_slave_config(atmel_port->chan_rx,
1130 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001131 if (ret) {
1132 dev_err(port->dev, "DMA rx slave configuration failed\n");
1133 goto chan_err;
1134 }
1135 /*
1136 * Prepare a cyclic dma transfer, assign 2 descriptors,
1137 * each one is half ring buffer size
1138 */
1139 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc22014-12-09 14:31:36 +01001140 sg_dma_address(&atmel_port->sg_rx),
1141 sg_dma_len(&atmel_port->sg_rx),
1142 sg_dma_len(&atmel_port->sg_rx)/2,
1143 DMA_DEV_TO_MEM,
1144 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001145 desc->callback = atmel_complete_rx_dma;
1146 desc->callback_param = port;
1147 atmel_port->desc_rx = desc;
1148 atmel_port->cookie_rx = dmaengine_submit(desc);
1149
1150 return 0;
1151
1152chan_err:
1153 dev_err(port->dev, "RX channel not available, switch to pio\n");
1154 atmel_port->use_dma_rx = 0;
1155 if (atmel_port->chan_rx)
1156 atmel_release_rx_dma(port);
1157 return -EINVAL;
1158}
1159
Elen Song2e68c222013-07-22 16:30:30 +08001160static void atmel_uart_timer_callback(unsigned long data)
1161{
1162 struct uart_port *port = (void *)data;
1163 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1164
1165 tasklet_schedule(&atmel_port->tasklet);
1166 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1167}
1168
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001169/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001170 * receive interrupt handler.
1171 */
1172static void
1173atmel_handle_receive(struct uart_port *port, unsigned int pending)
1174{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001175 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001176
Elen Song64e22eb2013-07-22 16:30:24 +08001177 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001178 /*
1179 * PDC receive. Just schedule the tasklet and let it
1180 * figure out the details.
1181 *
1182 * TODO: We're not handling error flags correctly at
1183 * the moment.
1184 */
1185 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001186 atmel_uart_writel(port, ATMEL_US_IDR,
1187 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Chip Coldwella6670612008-02-08 04:21:06 -08001188 tasklet_schedule(&atmel_port->tasklet);
1189 }
1190
1191 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1192 ATMEL_US_FRAME | ATMEL_US_PARE))
1193 atmel_pdc_rxerr(port, pending);
1194 }
1195
Elen Song34df42f2013-07-22 16:30:27 +08001196 if (atmel_use_dma_rx(port)) {
1197 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001198 atmel_uart_writel(port, ATMEL_US_IDR,
1199 ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001200 tasklet_schedule(&atmel_port->tasklet);
1201 }
1202 }
1203
Remy Bohmerb843aa22008-02-08 04:21:01 -08001204 /* Interrupt receive */
1205 if (pending & ATMEL_US_RXRDY)
1206 atmel_rx_chars(port);
1207 else if (pending & ATMEL_US_RXBRK) {
1208 /*
1209 * End of break detected. If it came along with a
1210 * character, atmel_rx_chars will handle it.
1211 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001212 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1213 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001214 atmel_port->break_active = 0;
1215 }
1216}
1217
1218/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001219 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001220 */
1221static void
1222atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1223{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001224 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001225
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001226 if (pending & atmel_port->tx_done_mask) {
1227 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001228 atmel_uart_writel(port, ATMEL_US_IDR,
1229 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001230 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001231 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001232}
1233
1234/*
1235 * status flags interrupt handler.
1236 */
1237static void
1238atmel_handle_status(struct uart_port *port, unsigned int pending,
1239 unsigned int status)
1240{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001241 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001242
Remy Bohmerb843aa22008-02-08 04:21:01 -08001243 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001244 | ATMEL_US_CTSIC)) {
1245 atmel_port->irq_status = status;
Leilei Zhaod033e822015-04-09 10:48:15 +08001246 atmel_port->status_change = atmel_port->irq_status ^
1247 atmel_port->irq_status_prev;
1248 atmel_port->irq_status_prev = status;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001249 tasklet_schedule(&atmel_port->tasklet);
1250 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001251}
1252
1253/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001254 * Interrupt handler
1255 */
David Howells7d12e782006-10-05 14:55:46 +01001256static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001257{
1258 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001259 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001260 unsigned int status, pending, mask, pass_counter = 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001261 bool gpio_handled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001262
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001263 spin_lock(&atmel_port->lock_suspended);
1264
Chip Coldwella6670612008-02-08 04:21:06 -08001265 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001266 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001267 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001268 pending = status & mask;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001269 if (!gpio_handled) {
1270 /*
1271 * Dealing with GPIO interrupt
1272 */
1273 if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1274 pending |= ATMEL_US_CTSIC;
1275
1276 if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1277 pending |= ATMEL_US_DSRIC;
1278
1279 if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1280 pending |= ATMEL_US_RIIC;
1281
1282 if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1283 pending |= ATMEL_US_DCDIC;
1284
1285 gpio_handled = true;
1286 }
Chip Coldwella6670612008-02-08 04:21:06 -08001287 if (!pending)
1288 break;
1289
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001290 if (atmel_port->suspended) {
1291 atmel_port->pending |= pending;
1292 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001293 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001294 pm_system_wakeup();
1295 break;
1296 }
1297
Remy Bohmerb843aa22008-02-08 04:21:01 -08001298 atmel_handle_receive(port, pending);
1299 atmel_handle_status(port, pending, status);
1300 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001301 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001302
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001303 spin_unlock(&atmel_port->lock_suspended);
1304
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001305 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001306}
1307
Elen Songa930e522013-07-22 16:30:25 +08001308static void atmel_release_tx_pdc(struct uart_port *port)
1309{
1310 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1311 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1312
1313 dma_unmap_single(port->dev,
1314 pdc->dma_addr,
1315 pdc->dma_size,
1316 DMA_TO_DEVICE);
1317}
1318
Chip Coldwella6670612008-02-08 04:21:06 -08001319/*
1320 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1321 */
Elen Song64e22eb2013-07-22 16:30:24 +08001322static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001323{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001324 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001325 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001326 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1327 int count;
1328
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001329 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001330 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001331 return;
1332
Chip Coldwella6670612008-02-08 04:21:06 -08001333 xmit->tail += pdc->ofs;
1334 xmit->tail &= UART_XMIT_SIZE - 1;
1335
1336 port->icount.tx += pdc->ofs;
1337 pdc->ofs = 0;
1338
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001339 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001340
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001341 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001342 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001343
Itai Levi1f140812009-01-15 13:50:43 -08001344 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001345 dma_sync_single_for_device(port->dev,
1346 pdc->dma_addr,
1347 pdc->dma_size,
1348 DMA_TO_DEVICE);
1349
1350 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1351 pdc->ofs = count;
1352
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001353 atmel_uart_writel(port, ATMEL_PDC_TPR,
1354 pdc->dma_addr + xmit->tail);
1355 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001356 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001357 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001358 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001359 atmel_uart_writel(port, ATMEL_US_IER,
1360 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001361 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001362 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1363 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001364 /* DMA done, stop TX, start RX for RS485 */
1365 atmel_start_rx(port);
1366 }
Chip Coldwella6670612008-02-08 04:21:06 -08001367 }
1368
1369 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1370 uart_write_wakeup(port);
1371}
1372
Elen Songa930e522013-07-22 16:30:25 +08001373static int atmel_prepare_tx_pdc(struct uart_port *port)
1374{
1375 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1376 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1377 struct circ_buf *xmit = &port->state->xmit;
1378
1379 pdc->buf = xmit->buf;
1380 pdc->dma_addr = dma_map_single(port->dev,
1381 pdc->buf,
1382 UART_XMIT_SIZE,
1383 DMA_TO_DEVICE);
1384 pdc->dma_size = UART_XMIT_SIZE;
1385 pdc->ofs = 0;
1386
1387 return 0;
1388}
1389
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001390static void atmel_rx_from_ring(struct uart_port *port)
1391{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001392 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001393 struct circ_buf *ring = &atmel_port->rx_ring;
1394 unsigned int flg;
1395 unsigned int status;
1396
1397 while (ring->head != ring->tail) {
1398 struct atmel_uart_char c;
1399
1400 /* Make sure c is loaded after head. */
1401 smp_rmb();
1402
1403 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1404
1405 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1406
1407 port->icount.rx++;
1408 status = c.status;
1409 flg = TTY_NORMAL;
1410
1411 /*
1412 * note that the error handling code is
1413 * out of the main execution path
1414 */
1415 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1416 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1417 if (status & ATMEL_US_RXBRK) {
1418 /* ignore side-effect */
1419 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1420
1421 port->icount.brk++;
1422 if (uart_handle_break(port))
1423 continue;
1424 }
1425 if (status & ATMEL_US_PARE)
1426 port->icount.parity++;
1427 if (status & ATMEL_US_FRAME)
1428 port->icount.frame++;
1429 if (status & ATMEL_US_OVRE)
1430 port->icount.overrun++;
1431
1432 status &= port->read_status_mask;
1433
1434 if (status & ATMEL_US_RXBRK)
1435 flg = TTY_BREAK;
1436 else if (status & ATMEL_US_PARE)
1437 flg = TTY_PARITY;
1438 else if (status & ATMEL_US_FRAME)
1439 flg = TTY_FRAME;
1440 }
1441
1442
1443 if (uart_handle_sysrq_char(port, c.ch))
1444 continue;
1445
1446 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1447 }
1448
1449 /*
1450 * Drop the lock here since it might end up calling
1451 * uart_start(), which takes the lock.
1452 */
1453 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001454 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001455 spin_lock(&port->lock);
1456}
1457
Elen Songa930e522013-07-22 16:30:25 +08001458static void atmel_release_rx_pdc(struct uart_port *port)
1459{
1460 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1461 int i;
1462
1463 for (i = 0; i < 2; i++) {
1464 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1465
1466 dma_unmap_single(port->dev,
1467 pdc->dma_addr,
1468 pdc->dma_size,
1469 DMA_FROM_DEVICE);
1470 kfree(pdc->buf);
1471 }
1472}
1473
Elen Song64e22eb2013-07-22 16:30:24 +08001474static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001475{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001476 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001477 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001478 struct atmel_dma_buffer *pdc;
1479 int rx_idx = atmel_port->pdc_rx_idx;
1480 unsigned int head;
1481 unsigned int tail;
1482 unsigned int count;
1483
1484 do {
1485 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001486 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001487
1488 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001489 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001490 tail = pdc->ofs;
1491
1492 /* If the PDC has switched buffers, RPR won't contain
1493 * any address within the current buffer. Since head
1494 * is unsigned, we just need a one-way comparison to
1495 * find out.
1496 *
1497 * In this case, we just need to consume the entire
1498 * buffer and resubmit it for DMA. This will clear the
1499 * ENDRX bit as well, so that we can safely re-enable
1500 * all interrupts below.
1501 */
1502 head = min(head, pdc->dma_size);
1503
1504 if (likely(head != tail)) {
1505 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1506 pdc->dma_size, DMA_FROM_DEVICE);
1507
1508 /*
1509 * head will only wrap around when we recycle
1510 * the DMA buffer, and when that happens, we
1511 * explicitly set tail to 0. So head will
1512 * always be greater than tail.
1513 */
1514 count = head - tail;
1515
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001516 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1517 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001518
1519 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1520 pdc->dma_size, DMA_FROM_DEVICE);
1521
1522 port->icount.rx += count;
1523 pdc->ofs = head;
1524 }
1525
1526 /*
1527 * If the current buffer is full, we need to check if
1528 * the next one contains any additional data.
1529 */
1530 if (head >= pdc->dma_size) {
1531 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001532 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1533 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001534
1535 rx_idx = !rx_idx;
1536 atmel_port->pdc_rx_idx = rx_idx;
1537 }
1538 } while (head >= pdc->dma_size);
1539
1540 /*
1541 * Drop the lock here since it might end up calling
1542 * uart_start(), which takes the lock.
1543 */
1544 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001545 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001546 spin_lock(&port->lock);
1547
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001548 atmel_uart_writel(port, ATMEL_US_IER,
1549 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001550}
1551
Elen Songa930e522013-07-22 16:30:25 +08001552static int atmel_prepare_rx_pdc(struct uart_port *port)
1553{
1554 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1555 int i;
1556
1557 for (i = 0; i < 2; i++) {
1558 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1559
1560 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1561 if (pdc->buf == NULL) {
1562 if (i != 0) {
1563 dma_unmap_single(port->dev,
1564 atmel_port->pdc_rx[0].dma_addr,
1565 PDC_BUFFER_SIZE,
1566 DMA_FROM_DEVICE);
1567 kfree(atmel_port->pdc_rx[0].buf);
1568 }
1569 atmel_port->use_pdc_rx = 0;
1570 return -ENOMEM;
1571 }
1572 pdc->dma_addr = dma_map_single(port->dev,
1573 pdc->buf,
1574 PDC_BUFFER_SIZE,
1575 DMA_FROM_DEVICE);
1576 pdc->dma_size = PDC_BUFFER_SIZE;
1577 pdc->ofs = 0;
1578 }
1579
1580 atmel_port->pdc_rx_idx = 0;
1581
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001582 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1583 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001584
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001585 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1586 atmel_port->pdc_rx[1].dma_addr);
1587 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001588
1589 return 0;
1590}
1591
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001592/*
1593 * tasklet handling tty stuff outside the interrupt handler.
1594 */
1595static void atmel_tasklet_func(unsigned long data)
1596{
1597 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001598 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Leilei Zhaod033e822015-04-09 10:48:15 +08001599 unsigned int status = atmel_port->irq_status;
1600 unsigned int status_change = atmel_port->status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001601
1602 /* The interrupt handler does not take the lock */
1603 spin_lock(&port->lock);
1604
Elen Songa930e522013-07-22 16:30:25 +08001605 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001606
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001607 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1608 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1609 /* TODO: All reads to CSR will clear these interrupts! */
1610 if (status_change & ATMEL_US_RI)
1611 port->icount.rng++;
1612 if (status_change & ATMEL_US_DSR)
1613 port->icount.dsr++;
1614 if (status_change & ATMEL_US_DCD)
1615 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1616 if (status_change & ATMEL_US_CTS)
1617 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1618
Alan Coxbdc04e32009-09-19 13:13:31 -07001619 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001620
Leilei Zhaod033e822015-04-09 10:48:15 +08001621 atmel_port->status_change = 0;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001622 }
1623
Elen Songa930e522013-07-22 16:30:25 +08001624 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001625
1626 spin_unlock(&port->lock);
1627}
1628
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001629static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001630 struct platform_device *pdev)
1631{
1632 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001633 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001634
1635 if (np) {
1636 /* DMA/PDC usage specification */
1637 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1638 if (of_get_property(np, "dmas", NULL)) {
1639 atmel_port->use_dma_rx = true;
1640 atmel_port->use_pdc_rx = false;
1641 } else {
1642 atmel_port->use_dma_rx = false;
1643 atmel_port->use_pdc_rx = true;
1644 }
1645 } else {
1646 atmel_port->use_dma_rx = false;
1647 atmel_port->use_pdc_rx = false;
1648 }
1649
1650 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1651 if (of_get_property(np, "dmas", NULL)) {
1652 atmel_port->use_dma_tx = true;
1653 atmel_port->use_pdc_tx = false;
1654 } else {
1655 atmel_port->use_dma_tx = false;
1656 atmel_port->use_pdc_tx = true;
1657 }
1658 } else {
1659 atmel_port->use_dma_tx = false;
1660 atmel_port->use_pdc_tx = false;
1661 }
1662
1663 } else {
1664 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1665 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1666 atmel_port->use_dma_rx = false;
1667 atmel_port->use_dma_tx = false;
1668 }
1669
Elen Song33d64c42013-07-22 16:30:28 +08001670}
1671
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001672static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001673 struct platform_device *pdev)
1674{
1675 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001676 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001677
1678 if (np) {
1679 u32 rs485_delay[2];
1680 /* rs485 properties */
1681 if (of_property_read_u32_array(np, "rs485-rts-delay",
1682 rs485_delay, 2) == 0) {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001683 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001684
1685 rs485conf->delay_rts_before_send = rs485_delay[0];
1686 rs485conf->delay_rts_after_send = rs485_delay[1];
1687 rs485conf->flags = 0;
1688
1689 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1690 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1691
1692 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1693 NULL))
1694 rs485conf->flags |= SER_RS485_ENABLED;
1695 }
1696 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001697 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001698 }
1699
1700}
1701
Elen Songa930e522013-07-22 16:30:25 +08001702static void atmel_set_ops(struct uart_port *port)
1703{
1704 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1705
Elen Song34df42f2013-07-22 16:30:27 +08001706 if (atmel_use_dma_rx(port)) {
1707 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1708 atmel_port->schedule_rx = &atmel_rx_from_dma;
1709 atmel_port->release_rx = &atmel_release_rx_dma;
1710 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001711 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1712 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1713 atmel_port->release_rx = &atmel_release_rx_pdc;
1714 } else {
1715 atmel_port->prepare_rx = NULL;
1716 atmel_port->schedule_rx = &atmel_rx_from_ring;
1717 atmel_port->release_rx = NULL;
1718 }
1719
Elen Song08f738b2013-07-22 16:30:26 +08001720 if (atmel_use_dma_tx(port)) {
1721 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1722 atmel_port->schedule_tx = &atmel_tx_dma;
1723 atmel_port->release_tx = &atmel_release_tx_dma;
1724 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001725 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1726 atmel_port->schedule_tx = &atmel_tx_pdc;
1727 atmel_port->release_tx = &atmel_release_tx_pdc;
1728 } else {
1729 atmel_port->prepare_tx = NULL;
1730 atmel_port->schedule_tx = &atmel_tx_chars;
1731 atmel_port->release_tx = NULL;
1732 }
1733}
1734
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001735/*
Elen Song055560b2013-07-22 16:30:29 +08001736 * Get ip name usart or uart
1737 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001738static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001739{
1740 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001741 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001742 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001743 int usart, uart;
1744 /* usart and uart ascii */
1745 usart = 0x55534152;
1746 uart = 0x44424755;
1747
1748 atmel_port->is_usart = false;
1749
1750 if (name == usart) {
1751 dev_dbg(port->dev, "This is usart\n");
1752 atmel_port->is_usart = true;
1753 } else if (name == uart) {
1754 dev_dbg(port->dev, "This is uart\n");
1755 atmel_port->is_usart = false;
1756 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001757 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001758 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001759 switch (version) {
1760 case 0x302:
1761 case 0x10213:
1762 dev_dbg(port->dev, "This version is usart\n");
1763 atmel_port->is_usart = true;
1764 break;
1765 case 0x203:
1766 case 0x10202:
1767 dev_dbg(port->dev, "This version is uart\n");
1768 atmel_port->is_usart = false;
1769 break;
1770 default:
1771 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1772 }
Elen Song055560b2013-07-22 16:30:29 +08001773 }
Elen Song055560b2013-07-22 16:30:29 +08001774}
1775
Richard Genoudab5e4e42014-05-13 20:20:45 +02001776static void atmel_free_gpio_irq(struct uart_port *port)
1777{
1778 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1779 enum mctrl_gpio_idx i;
1780
1781 for (i = 0; i < UART_GPIO_MAX; i++)
1782 if (atmel_port->gpio_irq[i] >= 0)
1783 free_irq(atmel_port->gpio_irq[i], port);
1784}
1785
1786static int atmel_request_gpio_irq(struct uart_port *port)
1787{
1788 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1789 int *irq = atmel_port->gpio_irq;
1790 enum mctrl_gpio_idx i;
1791 int err = 0;
1792
1793 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1794 if (irq[i] < 0)
1795 continue;
1796
1797 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1798 err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1799 "atmel_serial", port);
1800 if (err)
1801 dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1802 irq[i]);
1803 }
1804
1805 /*
1806 * If something went wrong, rollback.
1807 */
1808 while (err && (--i >= 0))
1809 if (irq[i] >= 0)
1810 free_irq(irq[i], port);
1811
1812 return err;
1813}
1814
Elen Song055560b2013-07-22 16:30:29 +08001815/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001816 * Perform initialization and enable port for reception
1817 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001818static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001819{
Elen Song33d64c42013-07-22 16:30:28 +08001820 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001821 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001822 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001823 int retval;
1824
1825 /*
1826 * Ensure that no interrupts are enabled otherwise when
1827 * request_irq() is called we could get stuck trying to
1828 * handle an unexpected interrupt
1829 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001830 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001831 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001832
1833 /*
1834 * Allocate the IRQ
1835 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001836 retval = request_irq(port->irq, atmel_interrupt,
1837 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001838 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001839 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001840 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001841 return retval;
1842 }
1843
1844 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001845 * Get the GPIO lines IRQ
1846 */
1847 retval = atmel_request_gpio_irq(port);
1848 if (retval)
1849 goto free_irq;
1850
Leilei Zhao1e125782015-02-27 16:07:18 +08001851 tasklet_enable(&atmel_port->tasklet);
1852
Richard Genoudab5e4e42014-05-13 20:20:45 +02001853 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001854 * Initialize DMA (if necessary)
1855 */
Elen Song33d64c42013-07-22 16:30:28 +08001856 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001857 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001858
Elen Songa930e522013-07-22 16:30:25 +08001859 if (atmel_port->prepare_rx) {
1860 retval = atmel_port->prepare_rx(port);
1861 if (retval < 0)
1862 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001863 }
1864
Elen Songa930e522013-07-22 16:30:25 +08001865 if (atmel_port->prepare_tx) {
1866 retval = atmel_port->prepare_tx(port);
1867 if (retval < 0)
1868 atmel_set_ops(port);
1869 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001870
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001871 /*
1872 * Enable FIFO when available
1873 */
1874 if (atmel_port->fifo_size) {
1875 unsigned int txrdym = ATMEL_US_ONE_DATA;
1876 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1877 unsigned int fmr;
1878
1879 atmel_uart_writel(port, ATMEL_US_CR,
1880 ATMEL_US_FIFOEN |
1881 ATMEL_US_RXFCLR |
1882 ATMEL_US_TXFLCLR);
1883
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001884 if (atmel_use_dma_tx(port))
1885 txrdym = ATMEL_US_FOUR_DATA;
1886
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001887 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1888 if (atmel_port->rts_high &&
1889 atmel_port->rts_low)
1890 fmr |= ATMEL_US_FRTSC |
1891 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1892 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1893
1894 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1895 }
1896
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001897 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001898 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001899 atmel_port->irq_status = atmel_port->irq_status_prev;
1900
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001901 /*
1902 * Finally, enable the serial port
1903 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001904 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001905 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001906 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001907
Marek Roszko8bc661b2014-01-10 10:33:11 +01001908 setup_timer(&atmel_port->uart_timer,
1909 atmel_uart_timer_callback,
1910 (unsigned long)port);
1911
Elen Song64e22eb2013-07-22 16:30:24 +08001912 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001913 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001914 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001915 mod_timer(&atmel_port->uart_timer,
1916 jiffies + uart_poll_timeout(port));
1917 /* set USART timeout */
1918 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001919 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1920 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001921
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001922 atmel_uart_writel(port, ATMEL_US_IER,
1923 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001924 }
Chip Coldwella6670612008-02-08 04:21:06 -08001925 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001926 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001927 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001928 /* set UART timeout */
1929 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001930 mod_timer(&atmel_port->uart_timer,
1931 jiffies + uart_poll_timeout(port));
1932 /* set USART timeout */
1933 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001934 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1935 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001936
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001937 atmel_uart_writel(port, ATMEL_US_IER,
1938 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001939 }
Chip Coldwella6670612008-02-08 04:21:06 -08001940 } else {
1941 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001942 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001943 }
Andrew Victorafefc412006-06-19 19:53:19 +01001944
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001945 return 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001946
1947free_irq:
1948 free_irq(port->irq, port);
1949
1950 return retval;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951}
1952
1953/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001954 * Flush any TX data submitted for DMA. Called when the TX circular
1955 * buffer is reset.
1956 */
1957static void atmel_flush_buffer(struct uart_port *port)
1958{
1959 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1960
1961 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001962 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001963 atmel_port->pdc_tx.ofs = 0;
1964 }
1965}
1966
1967/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001968 * Disable the port
1969 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001970static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001971{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001972 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001973
Chip Coldwella6670612008-02-08 04:21:06 -08001974 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001975 * Prevent any tasklets being scheduled during
1976 * cleanup
1977 */
1978 del_timer_sync(&atmel_port->uart_timer);
1979
1980 /*
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001981 * Clear out any scheduled tasklets before
1982 * we destroy the buffers
1983 */
Leilei Zhao1e125782015-02-27 16:07:18 +08001984 tasklet_disable(&atmel_port->tasklet);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001985 tasklet_kill(&atmel_port->tasklet);
1986
1987 /*
1988 * Ensure everything is stopped and
1989 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001990 */
1991 atmel_stop_rx(port);
1992 atmel_stop_tx(port);
1993
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001994 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1995 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c72014-01-07 11:45:06 +01001996
1997
Chip Coldwella6670612008-02-08 04:21:06 -08001998 /*
1999 * Shut-down the DMA.
2000 */
Elen Songa930e522013-07-22 16:30:25 +08002001 if (atmel_port->release_rx)
2002 atmel_port->release_rx(port);
2003 if (atmel_port->release_tx)
2004 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08002005
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002006 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01002007 * Reset ring buffer pointers
2008 */
2009 atmel_port->rx_ring.head = 0;
2010 atmel_port->rx_ring.tail = 0;
2011
2012 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02002013 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002014 */
2015 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02002016 atmel_free_gpio_irq(port);
2017
2018 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002019
Peter Hurley479e9b92014-10-16 16:54:18 -04002020 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002021}
2022
2023/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002024 * Power / Clock management.
2025 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002026static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2027 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002028{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002029 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002030
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002031 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002032 case 0:
2033 /*
2034 * Enable the peripheral clock for this serial port.
2035 * This is called on uart_open() or a resume event.
2036 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002037 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07002038
2039 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002040 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002041 break;
2042 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07002043 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002044 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2045 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07002046
Remy Bohmerb843aa22008-02-08 04:21:01 -08002047 /*
2048 * Disable the peripheral clock for this serial port.
2049 * This is called on uart_close() or a suspend event.
2050 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002051 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002052 break;
2053 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01002054 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002055 }
2056}
2057
2058/*
2059 * Change the port parameters
2060 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002061static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2062 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002063{
2064 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002065 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002066
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002067 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002068 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002069
2070 /* reset the mode, clock divisor, parity, stop bits and data size */
2071 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2072 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002073
Remy Bohmerb843aa22008-02-08 04:21:01 -08002074 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002075 quot = uart_get_divisor(port, baud);
2076
Remy Bohmerb843aa22008-02-08 04:21:01 -08002077 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01002078 quot /= 8;
2079 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2080 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002081
2082 /* byte size */
2083 switch (termios->c_cflag & CSIZE) {
2084 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002085 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002086 break;
2087 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002088 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002089 break;
2090 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002091 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002092 break;
2093 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002094 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002095 break;
2096 }
2097
2098 /* stop bits */
2099 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002100 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002101
2102 /* parity */
2103 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002104 /* Mark or Space parity */
2105 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002106 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002107 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002108 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002109 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002110 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002111 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002112 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002113 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002114 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002115 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002116
2117 spin_lock_irqsave(&port->lock, flags);
2118
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002119 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002120 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002121 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002122 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002123 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002124
Elen Song64e22eb2013-07-22 16:30:24 +08002125 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002126 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002127 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002128
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002129 /*
2130 * Characters to ignore
2131 */
2132 port->ignore_status_mask = 0;
2133 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002134 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002135 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002136 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002137 /*
2138 * If we're ignoring parity and break indicators,
2139 * ignore overruns too (for real raw support).
2140 */
2141 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002142 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002143 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002144 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002145
2146 /* update the per-port timeout */
2147 uart_update_timeout(port, termios->c_cflag, baud);
2148
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002149 /*
2150 * save/disable interrupts. The tty layer will ensure that the
2151 * transmitter is empty if requested by the caller, so there's
2152 * no need to wait for it here.
2153 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002154 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2155 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002156
2157 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002158 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002159
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002160 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002161 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002162 atmel_uart_writel(port, ATMEL_US_TTGR,
2163 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002164 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002165 } else if (termios->c_cflag & CRTSCTS) {
2166 /* RS232 with hardware handshake (RTS/CTS) */
2167 mode |= ATMEL_US_USMODE_HWHS;
2168 } else {
2169 /* RS232 without hadware handshake */
2170 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002171 }
2172
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002173 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002174 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002175
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002176 /*
2177 * when switching the mode, set the RTS line state according to the
2178 * new mode, otherwise keep the former state
2179 */
2180 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2181 unsigned int rts_state;
2182
2183 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2184 /* let the hardware control the RTS line */
2185 rts_state = ATMEL_US_RTSDIS;
2186 } else {
2187 /* force RTS line to low level */
2188 rts_state = ATMEL_US_RTSEN;
2189 }
2190
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002191 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002192 }
2193
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002194 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002195 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2196 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2197 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002198
2199 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002200 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002201
2202 /* CTS flow-control and modem-status interrupts */
2203 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002204 atmel_enable_ms(port);
2205 else
2206 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002207
2208 spin_unlock_irqrestore(&port->lock, flags);
2209}
2210
Peter Hurley732a84a2014-11-05 13:11:43 -05002211static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002212{
Peter Hurley732a84a2014-11-05 13:11:43 -05002213 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002214 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002215 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002216 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002217 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002218 } else {
2219 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002220 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2221 spin_lock_irq(&port->lock);
2222 atmel_disable_ms(port);
2223 spin_unlock_irq(&port->lock);
2224 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002225 }
2226}
2227
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002228/*
2229 * Return string describing the specified port
2230 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002231static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002232{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002233 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002234}
2235
2236/*
2237 * Release the memory region(s) being used by 'port'.
2238 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002239static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002240{
Andrew Victorafefc412006-06-19 19:53:19 +01002241 struct platform_device *pdev = to_platform_device(port->dev);
2242 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2243
2244 release_mem_region(port->mapbase, size);
2245
2246 if (port->flags & UPF_IOREMAP) {
2247 iounmap(port->membase);
2248 port->membase = NULL;
2249 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002250}
2251
2252/*
2253 * Request the memory region(s) being used by 'port'.
2254 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002255static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002256{
Andrew Victorafefc412006-06-19 19:53:19 +01002257 struct platform_device *pdev = to_platform_device(port->dev);
2258 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002259
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002260 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002261 return -EBUSY;
2262
2263 if (port->flags & UPF_IOREMAP) {
2264 port->membase = ioremap(port->mapbase, size);
2265 if (port->membase == NULL) {
2266 release_mem_region(port->mapbase, size);
2267 return -ENOMEM;
2268 }
2269 }
2270
2271 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002272}
2273
2274/*
2275 * Configure/autoconfigure the port.
2276 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002277static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002278{
2279 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002280 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002281 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002282 }
2283}
2284
2285/*
2286 * Verify the new serial_struct (for TIOCSSERIAL).
2287 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002288static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002289{
2290 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002291 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002292 ret = -EINVAL;
2293 if (port->irq != ser->irq)
2294 ret = -EINVAL;
2295 if (ser->io_type != SERIAL_IO_MEM)
2296 ret = -EINVAL;
2297 if (port->uartclk / 16 != ser->baud_base)
2298 ret = -EINVAL;
2299 if ((void *)port->mapbase != ser->iomem_base)
2300 ret = -EINVAL;
2301 if (port->iobase != ser->port)
2302 ret = -EINVAL;
2303 if (ser->hub6 != 0)
2304 ret = -EINVAL;
2305 return ret;
2306}
2307
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002308#ifdef CONFIG_CONSOLE_POLL
2309static int atmel_poll_get_char(struct uart_port *port)
2310{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002311 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002312 cpu_relax();
2313
Cyrille Pitchena6499432015-07-30 16:33:38 +02002314 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002315}
2316
2317static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2318{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002319 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002320 cpu_relax();
2321
Cyrille Pitchena6499432015-07-30 16:33:38 +02002322 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002323}
2324#endif
2325
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002326static struct uart_ops atmel_pops = {
2327 .tx_empty = atmel_tx_empty,
2328 .set_mctrl = atmel_set_mctrl,
2329 .get_mctrl = atmel_get_mctrl,
2330 .stop_tx = atmel_stop_tx,
2331 .start_tx = atmel_start_tx,
2332 .stop_rx = atmel_stop_rx,
2333 .enable_ms = atmel_enable_ms,
2334 .break_ctl = atmel_break_ctl,
2335 .startup = atmel_startup,
2336 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002337 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002338 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002339 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002340 .type = atmel_type,
2341 .release_port = atmel_release_port,
2342 .request_port = atmel_request_port,
2343 .config_port = atmel_config_port,
2344 .verify_port = atmel_verify_port,
2345 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002346#ifdef CONFIG_CONSOLE_POLL
2347 .poll_get_char = atmel_poll_get_char,
2348 .poll_put_char = atmel_poll_put_char,
2349#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002350};
2351
Andrew Victorafefc412006-06-19 19:53:19 +01002352/*
2353 * Configure the port from the platform device resource info.
2354 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002355static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002356 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002357{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002358 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002359 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002360 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002361
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002362 atmel_init_property(atmel_port, pdev);
2363 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002364
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002365 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002366
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002367 port->iotype = UPIO_MEM;
2368 port->flags = UPF_BOOT_AUTOCONF;
2369 port->ops = &atmel_pops;
2370 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002371 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002372 port->mapbase = pdev->resource[0].start;
2373 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002374 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002375
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002376 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2377 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08002378 tasklet_disable(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002379
2380 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2381
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002382 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002383 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002384 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002385 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002386 port->flags |= UPF_IOREMAP;
2387 port->membase = NULL;
2388 }
2389
Remy Bohmerb843aa22008-02-08 04:21:01 -08002390 /* for console, the clock could already be configured */
2391 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002392 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002393 if (IS_ERR(atmel_port->clk)) {
2394 ret = PTR_ERR(atmel_port->clk);
2395 atmel_port->clk = NULL;
2396 return ret;
2397 }
2398 ret = clk_prepare_enable(atmel_port->clk);
2399 if (ret) {
2400 clk_put(atmel_port->clk);
2401 atmel_port->clk = NULL;
2402 return ret;
2403 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002404 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002405 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002406 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002407 }
Chip Coldwella6670612008-02-08 04:21:06 -08002408
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002409 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002410 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002411 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002412 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002413 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002414 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2415 } else {
2416 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2417 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002418
2419 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002420}
2421
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002422struct platform_device *atmel_default_console_device; /* the serial console device */
2423
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002424#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002425static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002426{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002427 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002428 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002429 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002430}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002431
2432/*
2433 * Interrupts are disabled on entering
2434 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002435static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002436{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002437 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002438 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002439 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002440 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002441
2442 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002443 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002444 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002445 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2446 atmel_uart_writel(port, ATMEL_US_IDR,
2447 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002448
Marc Pignat39d4c922008-04-02 13:04:42 -07002449 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002450 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2451 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002452
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002453 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002454
2455 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002456 * Finally, wait for transmitter to become empty
2457 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002458 */
2459 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002460 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002461 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002462
2463 /* Restore PDC transmit status */
2464 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002465 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002466
Remy Bohmerb843aa22008-02-08 04:21:01 -08002467 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002468 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002469}
2470
2471/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002472 * If the port was already initialised (eg, by a boot loader),
2473 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002474 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002475static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2476 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002477{
2478 unsigned int mr, quot;
2479
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002480 /*
2481 * If the baud rate generator isn't running, the port wasn't
2482 * initialized by the boot loader.
2483 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002484 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002485 if (!quot)
2486 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002487
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002488 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002489 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002490 *bits = 8;
2491 else
2492 *bits = 7;
2493
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002494 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002495 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002496 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002497 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002498 *parity = 'o';
2499
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002500 /*
2501 * The serial core only rounds down when matching this to a
2502 * supported baud rate. Make sure we don't end up slightly
2503 * lower than one of those, as it would make us fall through
2504 * to a much lower baud rate than we really want.
2505 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002506 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002507}
2508
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002509static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002510{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002511 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002512 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002513 int baud = 115200;
2514 int bits = 8;
2515 int parity = 'n';
2516 int flow = 'n';
2517
Remy Bohmerb843aa22008-02-08 04:21:01 -08002518 if (port->membase == NULL) {
2519 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002520 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002521 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002522
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002523 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2524 if (ret)
2525 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002526
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002527 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2528 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2529 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002530
2531 if (options)
2532 uart_parse_options(options, &baud, &parity, &bits, &flow);
2533 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002534 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002535
2536 return uart_set_options(port, co, baud, parity, bits, flow);
2537}
2538
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002539static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002540
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002541static struct console atmel_console = {
2542 .name = ATMEL_DEVICENAME,
2543 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002544 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002545 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002546 .flags = CON_PRINTBUFFER,
2547 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002548 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002549};
2550
David Brownell06a7f052008-11-06 12:53:40 -08002551#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002552
Andrew Victorafefc412006-06-19 19:53:19 +01002553/*
2554 * Early console initialization (before VM subsystem initialized).
2555 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002556static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002557{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002558 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002559 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002560 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002561 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002562 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002563 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002564
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002565 port->backup_imr = 0;
2566 port->uart.line = id;
2567
2568 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002569 ret = atmel_init_port(port, atmel_default_console_device);
2570 if (ret)
2571 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002572 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002573 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002574
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002575 return 0;
2576}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002577
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002578console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002579
Andrew Victorafefc412006-06-19 19:53:19 +01002580/*
2581 * Late console initialization.
2582 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002583static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002584{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002585 if (atmel_default_console_device
2586 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002587 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002588
2589 return 0;
2590}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002591
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002592core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002593
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002594static inline bool atmel_is_console_port(struct uart_port *port)
2595{
2596 return port->cons && port->cons->index == port->line;
2597}
2598
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002599#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002600#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002601
2602static inline bool atmel_is_console_port(struct uart_port *port)
2603{
2604 return false;
2605}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002606#endif
2607
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002608static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002609 .owner = THIS_MODULE,
2610 .driver_name = "atmel_serial",
2611 .dev_name = ATMEL_DEVICENAME,
2612 .major = SERIAL_ATMEL_MAJOR,
2613 .minor = MINOR_START,
2614 .nr = ATMEL_MAX_UART,
2615 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002616};
2617
Andrew Victorafefc412006-06-19 19:53:19 +01002618#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002619static bool atmel_serial_clk_will_stop(void)
2620{
2621#ifdef CONFIG_ARCH_AT91
2622 return at91_suspend_entering_slow_clock();
2623#else
2624 return false;
2625#endif
2626}
2627
Remy Bohmerb843aa22008-02-08 04:21:01 -08002628static int atmel_serial_suspend(struct platform_device *pdev,
2629 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002630{
Andrew Victorafefc412006-06-19 19:53:19 +01002631 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002632 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002633
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002634 if (atmel_is_console_port(port) && console_suspend_enabled) {
2635 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002636 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2637 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002638 cpu_relax();
2639 }
2640
Anti Sullinf05596d2008-09-22 13:57:54 -07002641 /* we can not wake up if we're running on slow clock */
2642 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002643 if (atmel_serial_clk_will_stop()) {
2644 unsigned long flags;
2645
2646 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2647 atmel_port->suspended = true;
2648 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002649 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002650 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002651
2652 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002653
2654 return 0;
2655}
2656
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002657static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002658{
2659 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002660 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002661 unsigned long flags;
2662
2663 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2664 if (atmel_port->pending) {
2665 atmel_handle_receive(port, atmel_port->pending);
2666 atmel_handle_status(port, atmel_port->pending,
2667 atmel_port->pending_status);
2668 atmel_handle_transmit(port, atmel_port->pending);
2669 atmel_port->pending = 0;
2670 }
2671 atmel_port->suspended = false;
2672 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002673
Anti Sullinf05596d2008-09-22 13:57:54 -07002674 uart_resume_port(&atmel_uart, port);
2675 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002676
2677 return 0;
2678}
2679#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002680#define atmel_serial_suspend NULL
2681#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002682#endif
2683
Richard Genoude0b0baa2014-05-13 20:20:44 +02002684static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2685{
Richard Genoudab5e4e42014-05-13 20:20:45 +02002686 enum mctrl_gpio_idx i;
2687 struct gpio_desc *gpiod;
2688
Richard Genoude0b0baa2014-05-13 20:20:44 +02002689 p->gpios = mctrl_gpio_init(dev, 0);
Uwe Kleine-König722ccf42015-02-12 15:24:38 +01002690 if (IS_ERR(p->gpios))
2691 return PTR_ERR(p->gpios);
Richard Genoude0b0baa2014-05-13 20:20:44 +02002692
Richard Genoudab5e4e42014-05-13 20:20:45 +02002693 for (i = 0; i < UART_GPIO_MAX; i++) {
2694 gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2695 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2696 p->gpio_irq[i] = gpiod_to_irq(gpiod);
2697 else
2698 p->gpio_irq[i] = -EINVAL;
2699 }
2700
Richard Genoude0b0baa2014-05-13 20:20:44 +02002701 return 0;
2702}
2703
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002704static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2705 struct platform_device *pdev)
2706{
2707 port->fifo_size = 0;
2708 port->rts_low = 0;
2709 port->rts_high = 0;
2710
2711 if (of_property_read_u32(pdev->dev.of_node,
2712 "atmel,fifo-size",
2713 &port->fifo_size))
2714 return;
2715
2716 if (!port->fifo_size)
2717 return;
2718
2719 if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2720 port->fifo_size = 0;
2721 dev_err(&pdev->dev, "Invalid FIFO size\n");
2722 return;
2723 }
2724
2725 /*
2726 * 0 <= rts_low <= rts_high <= fifo_size
2727 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2728 * to flush their internal TX FIFO, commonly up to 16 data, before
2729 * actually stopping to send new data. So we try to set the RTS High
2730 * Threshold to a reasonably high value respecting this 16 data
2731 * empirical rule when possible.
2732 */
2733 port->rts_high = max_t(int, port->fifo_size >> 1,
2734 port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2735 port->rts_low = max_t(int, port->fifo_size >> 2,
2736 port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2737
2738 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2739 port->fifo_size);
2740 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2741 port->rts_high);
2742 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
2743 port->rts_low);
2744}
2745
Bill Pemberton9671f092012-11-19 13:21:50 -05002746static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002747{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002748 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002749 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002750 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002751 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002752 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002753 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002754
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002755 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002756
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002757 if (np)
2758 ret = of_alias_get_id(np, "serial");
2759 else
2760 if (pdata)
2761 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002762
2763 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002764 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002765 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002766 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002767
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002768 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002769 ret = -ENODEV;
2770 goto err;
2771 }
2772
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002773 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002774 /* port already in use */
2775 ret = -EBUSY;
2776 goto err;
2777 }
2778
2779 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002780 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002781 port->uart.line = ret;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002782 atmel_serial_probe_fifos(port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002783
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002784 spin_lock_init(&port->lock_suspended);
2785
Richard Genoude0b0baa2014-05-13 20:20:44 +02002786 ret = atmel_init_gpios(port, &pdev->dev);
Uwe Kleine-König722ccf42015-02-12 15:24:38 +01002787 if (ret < 0) {
2788 dev_err(&pdev->dev, "Failed to initialize GPIOs.");
2789 goto err;
2790 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002791
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002792 ret = atmel_init_port(port, pdev);
2793 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002794 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002795
Elen Song64e22eb2013-07-22 16:30:24 +08002796 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002797 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002798 data = kmalloc(sizeof(struct atmel_uart_char)
2799 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002800 if (!data)
2801 goto err_alloc_ring;
2802 port->rx_ring.buf = data;
2803 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002804
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002805 rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2806
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002807 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002808 if (ret)
2809 goto err_add_port;
2810
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002811#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002812 if (atmel_is_console_port(&port->uart)
2813 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2814 /*
2815 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002816 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002817 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002818 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002819 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002820#endif
David Brownell06a7f052008-11-06 12:53:40 -08002821
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002822 device_init_wakeup(&pdev->dev, 1);
2823 platform_set_drvdata(pdev, port);
2824
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002825 /*
2826 * The peripheral clock has been disabled by atmel_init_port():
2827 * enable it before accessing I/O registers
2828 */
2829 clk_prepare_enable(port->clk);
2830
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002831 if (rs485_enabled) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002832 atmel_uart_writel(&port->uart, ATMEL_US_MR,
2833 ATMEL_US_USMODE_NORMAL);
2834 atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002835 }
2836
Elen Song055560b2013-07-22 16:30:29 +08002837 /*
2838 * Get port name of usart or uart
2839 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002840 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002841
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002842 /*
2843 * The peripheral clock can now safely be disabled till the port
2844 * is used
2845 */
2846 clk_disable_unprepare(port->clk);
2847
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002848 return 0;
2849
2850err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002851 kfree(port->rx_ring.buf);
2852 port->rx_ring.buf = NULL;
2853err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002854 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002855 clk_put(port->clk);
2856 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002857 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002858err_clear_bit:
2859 clear_bit(port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002860err:
Andrew Victorafefc412006-06-19 19:53:19 +01002861 return ret;
2862}
2863
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002864static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002865{
2866 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002867 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002868 int ret = 0;
2869
Marek Roszkof50c995f2014-01-07 11:45:07 +01002870 tasklet_kill(&atmel_port->tasklet);
2871
Andrew Victorafefc412006-06-19 19:53:19 +01002872 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002873
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002874 ret = uart_remove_one_port(&atmel_uart, port);
2875
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002876 kfree(atmel_port->rx_ring.buf);
2877
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002878 /* "port" is allocated statically, so we shouldn't free it */
2879
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002880 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002881
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002882 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002883
2884 return ret;
2885}
2886
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002887static struct platform_driver atmel_serial_driver = {
2888 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002889 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002890 .suspend = atmel_serial_suspend,
2891 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002892 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002893 .name = "atmel_usart",
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002894 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002895 },
2896};
2897
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002898static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002899{
2900 int ret;
2901
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002902 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002903 if (ret)
2904 return ret;
2905
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002906 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002907 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002908 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002909
2910 return ret;
2911}
2912
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002913static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002914{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002915 platform_driver_unregister(&atmel_serial_driver);
2916 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002917}
2918
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002919module_init(atmel_serial_init);
2920module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002921
2922MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002923MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002924MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002925MODULE_ALIAS("platform:atmel_usart");