blob: 3a218482a47162d9c01f3c0822e70a3bb055598b [file] [log] [blame]
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001/*
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002 * Driver for Atmel AT91 / AT32 Serial ports
Andrew Victor1e6c9c22006-01-10 16:59:27 +00003 * Copyright (C) 2003 Rick Bronson
4 *
5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7 *
Chip Coldwella6670612008-02-08 04:21:06 -08008 * DMA support added by Chip Coldwell.
9 *
Andrew Victor1e6c9c22006-01-10 16:59:27 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
Andrew Victor1e6c9c22006-01-10 16:59:27 +000025#include <linux/module.h>
26#include <linux/tty.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/serial.h>
Andrew Victorafefc412006-06-19 19:53:19 +010031#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000032#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010035#include <linux/platform_device.h>
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +020036#include <linux/of.h>
37#include <linux/of_device.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010038#include <linux/of_gpio.h>
Chip Coldwella6670612008-02-08 04:21:06 -080039#include <linux/dma-mapping.h>
Vinod Koul6b997ba2014-10-16 12:59:06 +053040#include <linux/dmaengine.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010041#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010042#include <linux/atmel_serial.h>
Claudio Scordinoe8faff72010-05-03 13:31:28 +010043#include <linux/uaccess.h>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080044#include <linux/platform_data/atmel.h>
Elen Song2e68c222013-07-22 16:30:30 +080045#include <linux/timer.h>
Linus Walleij354e57f2013-11-07 10:25:55 +010046#include <linux/gpio.h>
Richard Genoude0b0baa2014-05-13 20:20:44 +020047#include <linux/gpio/consumer.h>
48#include <linux/err.h>
Richard Genoudab5e4e42014-05-13 20:20:45 +020049#include <linux/irq.h>
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +010050#include <linux/suspend.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000051
52#include <asm/io.h>
Peter Huewef7512e72010-06-29 19:35:39 +020053#include <asm/ioctls.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000054
Chip Coldwella6670612008-02-08 04:21:06 -080055#define PDC_BUFFER_SIZE 512
56/* Revisit: We should calculate this based on the actual port settings */
57#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
58
Cyrille Pitchenb5199d42015-07-02 15:18:12 +020059/* The minium number of data FIFOs should be able to contain */
60#define ATMEL_MIN_FIFO_SIZE 8
61/*
62 * These two offsets are substracted from the RX FIFO size to define the RTS
63 * high and low thresholds
64 */
65#define ATMEL_RTS_HIGH_OFFSET 16
66#define ATMEL_RTS_LOW_OFFSET 20
67
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020068#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000069#define SUPPORT_SYSRQ
70#endif
71
72#include <linux/serial_core.h>
73
Richard Genoude0b0baa2014-05-13 20:20:44 +020074#include "serial_mctrl_gpio.h"
75
Claudio Scordinoe8faff72010-05-03 13:31:28 +010076static void atmel_start_rx(struct uart_port *port);
77static void atmel_stop_rx(struct uart_port *port);
78
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020079#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000080
81/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
82 * should coexist with the 8250 driver, such as if we have an external 16C550
83 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020084#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000085#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020086#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000087
88#else
89
90/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
91 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020092#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000093#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020094#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000095
96#endif
97
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020098#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000099
Chip Coldwella6670612008-02-08 04:21:06 -0800100struct atmel_dma_buffer {
101 unsigned char *buf;
102 dma_addr_t dma_addr;
103 unsigned int dma_size;
104 unsigned int ofs;
105};
106
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800107struct atmel_uart_char {
108 u16 status;
109 u16 ch;
110};
111
112#define ATMEL_SERIAL_RINGSIZE 1024
113
Andrew Victorafefc412006-06-19 19:53:19 +0100114/*
Alexandre Belloni9af92fb2015-09-10 11:29:03 +0200115 * at91: 6 USARTs and one DBGU port (SAM9260)
116 * avr32: 4
117 */
118#define ATMEL_MAX_UART 7
119
120/*
Andrew Victorafefc412006-06-19 19:53:19 +0100121 * We wrap our port structure around the generic uart_port.
122 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200123struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100124 struct uart_port uart; /* uart */
125 struct clk *clk; /* uart clock */
Anti Sullinf05596d2008-09-22 13:57:54 -0700126 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
127 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700128 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800129
Elen Song34df42f2013-07-22 16:30:27 +0800130 bool use_dma_rx; /* enable DMA receiver */
Elen Song64e22eb2013-07-22 16:30:24 +0800131 bool use_pdc_rx; /* enable PDC receiver */
Chip Coldwella6670612008-02-08 04:21:06 -0800132 short pdc_rx_idx; /* current PDC RX buffer */
133 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
134
Elen Song08f738b2013-07-22 16:30:26 +0800135 bool use_dma_tx; /* enable DMA transmitter */
Elen Song64e22eb2013-07-22 16:30:24 +0800136 bool use_pdc_tx; /* enable PDC transmitter */
Chip Coldwella6670612008-02-08 04:21:06 -0800137 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
138
Elen Song08f738b2013-07-22 16:30:26 +0800139 spinlock_t lock_tx; /* port lock */
Elen Song34df42f2013-07-22 16:30:27 +0800140 spinlock_t lock_rx; /* port lock */
Elen Song08f738b2013-07-22 16:30:26 +0800141 struct dma_chan *chan_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800142 struct dma_chan *chan_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800143 struct dma_async_tx_descriptor *desc_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800144 struct dma_async_tx_descriptor *desc_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800145 dma_cookie_t cookie_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800146 dma_cookie_t cookie_rx;
Elen Song08f738b2013-07-22 16:30:26 +0800147 struct scatterlist sg_tx;
Elen Song34df42f2013-07-22 16:30:27 +0800148 struct scatterlist sg_rx;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800149 struct tasklet_struct tasklet;
150 unsigned int irq_status;
151 unsigned int irq_status_prev;
Leilei Zhaod033e822015-04-09 10:48:15 +0800152 unsigned int status_change;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200153 unsigned int tx_len;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800154
155 struct circ_buf rx_ring;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100156
Richard Genoude0b0baa2014-05-13 20:20:44 +0200157 struct mctrl_gpios *gpios;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200158 int gpio_irq[UART_GPIO_MAX];
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100159 unsigned int tx_done_mask;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200160 u32 fifo_size;
161 u32 rts_high;
162 u32 rts_low;
Richard Genoudab5e4e42014-05-13 20:20:45 +0200163 bool ms_irq_enabled;
Elen Song055560b2013-07-22 16:30:29 +0800164 bool is_usart; /* usart or uart */
Elen Song2e68c222013-07-22 16:30:30 +0800165 struct timer_list uart_timer; /* uart timer */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +0100166
167 bool suspended;
168 unsigned int pending;
169 unsigned int pending_status;
170 spinlock_t lock_suspended;
171
Elen Songa930e522013-07-22 16:30:25 +0800172 int (*prepare_rx)(struct uart_port *port);
173 int (*prepare_tx)(struct uart_port *port);
174 void (*schedule_rx)(struct uart_port *port);
175 void (*schedule_tx)(struct uart_port *port);
176 void (*release_rx)(struct uart_port *port);
177 void (*release_tx)(struct uart_port *port);
Andrew Victorafefc412006-06-19 19:53:19 +0100178};
179
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200180static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +0100181static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
Andrew Victorafefc412006-06-19 19:53:19 +0100182
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000183#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200184static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000185#endif
186
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +0200187#if defined(CONFIG_OF)
188static const struct of_device_id atmel_serial_dt_ids[] = {
189 { .compatible = "atmel,at91rm9200-usart" },
190 { .compatible = "atmel,at91sam9260-usart" },
191 { /* sentinel */ }
192};
193
194MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
195#endif
196
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800197static inline struct atmel_uart_port *
198to_atmel_uart_port(struct uart_port *uart)
199{
200 return container_of(uart, struct atmel_uart_port, uart);
201}
202
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200203static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
204{
205 return __raw_readl(port->membase + reg);
206}
207
208static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
209{
210 __raw_writel(value, port->membase + reg);
211}
212
Cyrille Pitchena6499432015-07-30 16:33:38 +0200213#ifdef CONFIG_AVR32
214
215/* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
216static inline u8 atmel_uart_read_char(struct uart_port *port)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200217{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200218 return __raw_readl(port->membase + ATMEL_US_RHR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200219}
220
Cyrille Pitchena6499432015-07-30 16:33:38 +0200221static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200222{
Cyrille Pitchena6499432015-07-30 16:33:38 +0200223 __raw_writel(value, port->membase + ATMEL_US_THR);
Cyrille Pitchenb5199d42015-07-02 15:18:12 +0200224}
225
Cyrille Pitchena6499432015-07-30 16:33:38 +0200226#else
227
228static inline u8 atmel_uart_read_char(struct uart_port *port)
229{
230 return __raw_readb(port->membase + ATMEL_US_RHR);
231}
232
233static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
234{
235 __raw_writeb(value, port->membase + ATMEL_US_THR);
236}
237
238#endif
239
Chip Coldwella6670612008-02-08 04:21:06 -0800240#ifdef CONFIG_SERIAL_ATMEL_PDC
Elen Song64e22eb2013-07-22 16:30:24 +0800241static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800242{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800243 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800244
Elen Song64e22eb2013-07-22 16:30:24 +0800245 return atmel_port->use_pdc_rx;
Chip Coldwella6670612008-02-08 04:21:06 -0800246}
247
Elen Song64e22eb2013-07-22 16:30:24 +0800248static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800249{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800250 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800251
Elen Song64e22eb2013-07-22 16:30:24 +0800252 return atmel_port->use_pdc_tx;
Chip Coldwella6670612008-02-08 04:21:06 -0800253}
254#else
Elen Song64e22eb2013-07-22 16:30:24 +0800255static bool atmel_use_pdc_rx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800256{
257 return false;
258}
259
Elen Song64e22eb2013-07-22 16:30:24 +0800260static bool atmel_use_pdc_tx(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -0800261{
262 return false;
263}
264#endif
265
Elen Song08f738b2013-07-22 16:30:26 +0800266static bool atmel_use_dma_tx(struct uart_port *port)
267{
268 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
269
270 return atmel_port->use_dma_tx;
271}
272
Elen Song34df42f2013-07-22 16:30:27 +0800273static bool atmel_use_dma_rx(struct uart_port *port)
274{
275 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
276
277 return atmel_port->use_dma_rx;
278}
279
Richard Genoude0b0baa2014-05-13 20:20:44 +0200280static unsigned int atmel_get_lines_status(struct uart_port *port)
281{
282 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
283 unsigned int status, ret = 0;
284
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200285 status = atmel_uart_readl(port, ATMEL_US_CSR);
Richard Genoude0b0baa2014-05-13 20:20:44 +0200286
287 mctrl_gpio_get(atmel_port->gpios, &ret);
288
289 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
290 UART_GPIO_CTS))) {
291 if (ret & TIOCM_CTS)
292 status &= ~ATMEL_US_CTS;
293 else
294 status |= ATMEL_US_CTS;
295 }
296
297 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
298 UART_GPIO_DSR))) {
299 if (ret & TIOCM_DSR)
300 status &= ~ATMEL_US_DSR;
301 else
302 status |= ATMEL_US_DSR;
303 }
304
305 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
306 UART_GPIO_RI))) {
307 if (ret & TIOCM_RI)
308 status &= ~ATMEL_US_RI;
309 else
310 status |= ATMEL_US_RI;
311 }
312
313 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
314 UART_GPIO_DCD))) {
315 if (ret & TIOCM_CD)
316 status &= ~ATMEL_US_DCD;
317 else
318 status |= ATMEL_US_DCD;
319 }
320
321 return status;
322}
323
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100324/* Enable or disable the rs485 support */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100325static int atmel_config_rs485(struct uart_port *port,
326 struct serial_rs485 *rs485conf)
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100327{
328 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
329 unsigned int mode;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100330
331 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200332 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100333
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200334 mode = atmel_uart_readl(port, ATMEL_US_MR);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100335
336 /* Resetting serial mode to RS232 (0x0) */
337 mode &= ~ATMEL_US_USMODE;
338
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100339 port->rs485 = *rs485conf;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100340
341 if (rs485conf->flags & SER_RS485_ENABLED) {
342 dev_dbg(port->dev, "Setting UART to RS485\n");
343 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200344 atmel_uart_writel(port, ATMEL_US_TTGR,
345 rs485conf->delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100346 mode |= ATMEL_US_USMODE_RS485;
347 } else {
348 dev_dbg(port->dev, "Setting UART to RS232\n");
Elen Song64e22eb2013-07-22 16:30:24 +0800349 if (atmel_use_pdc_tx(port))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100350 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
351 ATMEL_US_TXBUFE;
352 else
353 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
354 }
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200355 atmel_uart_writel(port, ATMEL_US_MR, mode);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100356
357 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200358 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100359
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100360 return 0;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100361}
362
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000363/*
364 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
365 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200366static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000367{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200368 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
369 TIOCSER_TEMT :
370 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000371}
372
373/*
374 * Set state of the modem control output lines
375 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200376static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000377{
378 unsigned int control = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200379 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100380 unsigned int rts_paused, rts_ready;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000382
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100383 /* override mode to RS485 if needed, otherwise keep the current mode */
384 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200385 atmel_uart_writel(port, ATMEL_US_TTGR,
386 port->rs485.delay_rts_after_send);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100387 mode &= ~ATMEL_US_USMODE;
388 mode |= ATMEL_US_USMODE_RS485;
389 }
390
391 /* set the RTS line state according to the mode */
392 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
393 /* force RTS line to high level */
394 rts_paused = ATMEL_US_RTSEN;
395
396 /* give the control of the RTS line back to the hardware */
397 rts_ready = ATMEL_US_RTSDIS;
398 } else {
399 /* force RTS line to high level */
400 rts_paused = ATMEL_US_RTSDIS;
401
402 /* force RTS line to low level */
403 rts_ready = ATMEL_US_RTSEN;
404 }
405
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000406 if (mctrl & TIOCM_RTS)
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100407 control |= rts_ready;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000408 else
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100409 control |= rts_paused;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000410
411 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200412 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000413 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200414 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000415
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200416 atmel_uart_writel(port, ATMEL_US_CR, control);
Andrew Victorafefc412006-06-19 19:53:19 +0100417
Richard Genoude0b0baa2014-05-13 20:20:44 +0200418 mctrl_gpio_set(atmel_port->gpios, mctrl);
419
Andrew Victorafefc412006-06-19 19:53:19 +0100420 /* Local loopback mode? */
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +0100421 mode &= ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100422 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200423 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100424 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200425 mode |= ATMEL_US_CHMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100426
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200427 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000428}
429
430/*
431 * Get state of the modem control input lines
432 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200433static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000434{
Richard Genoude0b0baa2014-05-13 20:20:44 +0200435 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
436 unsigned int ret = 0, status;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200438 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000439
440 /*
441 * The control signals are active low.
442 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200443 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000444 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200445 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000446 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200447 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000448 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200449 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000450 ret |= TIOCM_RI;
451
Richard Genoude0b0baa2014-05-13 20:20:44 +0200452 return mctrl_gpio_get(atmel_port->gpios, &ret);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000453}
454
455/*
456 * Stop transmitting.
457 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200458static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000459{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100460 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
461
Elen Song64e22eb2013-07-22 16:30:24 +0800462 if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800463 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200464 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100465 }
466 /* Disable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200467 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100468
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100469 if ((port->rs485.flags & SER_RS485_ENABLED) &&
470 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100471 atmel_start_rx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000472}
473
474/*
475 * Start transmitting.
476 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200477static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000478{
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100479 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
480
Elen Song64e22eb2013-07-22 16:30:24 +0800481 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200482 if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
Chip Coldwella6670612008-02-08 04:21:06 -0800483 /* The transmitter is already running. Yes, we
484 really need this.*/
485 return;
486
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100487 if ((port->rs485.flags & SER_RS485_ENABLED) &&
488 !(port->rs485.flags & SER_RS485_RX_DURING_TX))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100489 atmel_stop_rx(port);
490
Chip Coldwella6670612008-02-08 04:21:06 -0800491 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200492 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100493 }
494 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200495 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100496}
497
498/*
499 * start receiving - port is in process of being opened.
500 */
501static void atmel_start_rx(struct uart_port *port)
502{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200503 /* reset status and receiver */
504 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100505
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200506 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200507
Elen Song64e22eb2013-07-22 16:30:24 +0800508 if (atmel_use_pdc_rx(port)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100509 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200510 atmel_uart_writel(port, ATMEL_US_IER,
511 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
512 port->read_status_mask);
513 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100514 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200515 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100516 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000517}
518
519/*
520 * Stop receiving - port is in process of being closed.
521 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200522static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000523{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200524 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
Siftar, Gabe57c36862012-03-29 15:40:05 +0200525
Elen Song64e22eb2013-07-22 16:30:24 +0800526 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800527 /* disable PDC receive */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200528 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
529 atmel_uart_writel(port, ATMEL_US_IDR,
530 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
531 port->read_status_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100532 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200533 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100534 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000535}
536
537/*
538 * Enable modem status interrupts
539 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200540static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000541{
Richard Genoudab5e4e42014-05-13 20:20:45 +0200542 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
543 uint32_t ier = 0;
544
545 /*
546 * Interrupt should not be enabled twice
547 */
548 if (atmel_port->ms_irq_enabled)
549 return;
550
551 atmel_port->ms_irq_enabled = true;
552
553 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
554 enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
555 else
556 ier |= ATMEL_US_CTSIC;
557
558 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
559 enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
560 else
561 ier |= ATMEL_US_DSRIC;
562
563 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
564 enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
565 else
566 ier |= ATMEL_US_RIIC;
567
568 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
569 enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
570 else
571 ier |= ATMEL_US_DCDIC;
572
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200573 atmel_uart_writel(port, ATMEL_US_IER, ier);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000574}
575
576/*
Richard Genoud35b675b2014-09-03 18:09:26 +0200577 * Disable modem status interrupts
578 */
579static void atmel_disable_ms(struct uart_port *port)
580{
581 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
582 uint32_t idr = 0;
583
584 /*
585 * Interrupt should not be disabled twice
586 */
587 if (!atmel_port->ms_irq_enabled)
588 return;
589
590 atmel_port->ms_irq_enabled = false;
591
592 if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
593 disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
594 else
595 idr |= ATMEL_US_CTSIC;
596
597 if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
598 disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
599 else
600 idr |= ATMEL_US_DSRIC;
601
602 if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
603 disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
604 else
605 idr |= ATMEL_US_RIIC;
606
607 if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
608 disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
609 else
610 idr |= ATMEL_US_DCDIC;
611
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200612 atmel_uart_writel(port, ATMEL_US_IDR, idr);
Richard Genoud35b675b2014-09-03 18:09:26 +0200613}
614
615/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000616 * Control the transmission of a break signal
617 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200618static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000619{
620 if (break_state != 0)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200621 /* start break */
622 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000623 else
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200624 /* stop break */
625 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000626}
627
628/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800629 * Stores the incoming character in the ring buffer
630 */
631static void
632atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
633 unsigned int ch)
634{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800635 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800636 struct circ_buf *ring = &atmel_port->rx_ring;
637 struct atmel_uart_char *c;
638
639 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
640 /* Buffer overflow, ignore char */
641 return;
642
643 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
644 c->status = status;
645 c->ch = ch;
646
647 /* Make sure the character is stored before we update head. */
648 smp_wmb();
649
650 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
651}
652
653/*
Chip Coldwella6670612008-02-08 04:21:06 -0800654 * Deal with parity, framing and overrun errors.
655 */
656static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
657{
658 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200659 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Chip Coldwella6670612008-02-08 04:21:06 -0800660
661 if (status & ATMEL_US_RXBRK) {
662 /* ignore side-effect */
663 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
664 port->icount.brk++;
665 }
666 if (status & ATMEL_US_PARE)
667 port->icount.parity++;
668 if (status & ATMEL_US_FRAME)
669 port->icount.frame++;
670 if (status & ATMEL_US_OVRE)
671 port->icount.overrun++;
672}
673
674/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000675 * Characters received (called from interrupt handler)
676 */
David Howells7d12e782006-10-05 14:55:46 +0100677static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000678{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800679 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800680 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000681
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200682 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200683 while (status & ATMEL_US_RXRDY) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200684 ch = atmel_uart_read_char(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000685
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000686 /*
687 * note that the error handling code is
688 * out of the main execution path
689 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700690 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
691 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
692 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800693
Remy Bohmerb843aa22008-02-08 04:21:01 -0800694 /* clear error */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200695 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800696
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700697 if (status & ATMEL_US_RXBRK
698 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700699 atmel_port->break_active = 1;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200700 atmel_uart_writel(port, ATMEL_US_IER,
701 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700702 } else {
703 /*
704 * This is either the end-of-break
705 * condition or we've received at
706 * least one character without RXBRK
707 * being set. In both cases, the next
708 * RXBRK will indicate start-of-break.
709 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200710 atmel_uart_writel(port, ATMEL_US_IDR,
711 ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700712 status &= ~ATMEL_US_RXBRK;
713 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100714 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000715 }
716
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800717 atmel_buffer_rx_char(port, status, ch);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200718 status = atmel_uart_readl(port, ATMEL_US_CSR);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000719 }
720
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800721 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000722}
723
724/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800725 * Transmit characters (called from tasklet with TXRDY interrupt
726 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000727 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200728static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000729{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700730 struct circ_buf *xmit = &port->state->xmit;
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100731 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000732
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200733 if (port->x_char &&
734 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200735 atmel_uart_write_char(port, port->x_char);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000736 port->icount.tx++;
737 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000738 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800739 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000740 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000741
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200742 while (atmel_uart_readl(port, ATMEL_US_CSR) &
743 atmel_port->tx_done_mask) {
Cyrille Pitchena6499432015-07-30 16:33:38 +0200744 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000745 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
746 port->icount.tx++;
747 if (uart_circ_empty(xmit))
748 break;
749 }
750
751 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
752 uart_write_wakeup(port);
753
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800754 if (!uart_circ_empty(xmit))
Claudio Scordinoe8faff72010-05-03 13:31:28 +0100755 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +0200756 atmel_uart_writel(port, ATMEL_US_IER,
757 atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000758}
759
Elen Song08f738b2013-07-22 16:30:26 +0800760static void atmel_complete_tx_dma(void *arg)
761{
762 struct atmel_uart_port *atmel_port = arg;
763 struct uart_port *port = &atmel_port->uart;
764 struct circ_buf *xmit = &port->state->xmit;
765 struct dma_chan *chan = atmel_port->chan_tx;
766 unsigned long flags;
767
768 spin_lock_irqsave(&port->lock, flags);
769
770 if (chan)
771 dmaengine_terminate_all(chan);
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200772 xmit->tail += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800773 xmit->tail &= UART_XMIT_SIZE - 1;
774
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200775 port->icount.tx += atmel_port->tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800776
777 spin_lock_irq(&atmel_port->lock_tx);
778 async_tx_ack(atmel_port->desc_tx);
779 atmel_port->cookie_tx = -EINVAL;
780 atmel_port->desc_tx = NULL;
781 spin_unlock_irq(&atmel_port->lock_tx);
782
783 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
784 uart_write_wakeup(port);
785
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100786 /*
787 * xmit is a circular buffer so, if we have just send data from
788 * xmit->tail to the end of xmit->buf, now we have to transmit the
789 * remaining data from the beginning of xmit->buf to xmit->head.
790 */
Elen Song08f738b2013-07-22 16:30:26 +0800791 if (!uart_circ_empty(xmit))
792 tasklet_schedule(&atmel_port->tasklet);
793
794 spin_unlock_irqrestore(&port->lock, flags);
795}
796
797static void atmel_release_tx_dma(struct uart_port *port)
798{
799 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
800 struct dma_chan *chan = atmel_port->chan_tx;
801
802 if (chan) {
803 dmaengine_terminate_all(chan);
804 dma_release_channel(chan);
805 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200806 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800807 }
808
809 atmel_port->desc_tx = NULL;
810 atmel_port->chan_tx = NULL;
811 atmel_port->cookie_tx = -EINVAL;
812}
813
814/*
815 * Called from tasklet with TXRDY interrupt is disabled.
816 */
817static void atmel_tx_dma(struct uart_port *port)
818{
819 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
820 struct circ_buf *xmit = &port->state->xmit;
821 struct dma_chan *chan = atmel_port->chan_tx;
822 struct dma_async_tx_descriptor *desc;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200823 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
824 unsigned int tx_len, part1_len, part2_len, sg_len;
825 dma_addr_t phys_addr;
Elen Song08f738b2013-07-22 16:30:26 +0800826
827 /* Make sure we have an idle channel */
828 if (atmel_port->desc_tx != NULL)
829 return;
830
831 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
832 /*
833 * DMA is idle now.
834 * Port xmit buffer is already mapped,
835 * and it is one page... Just adjust
836 * offsets and lengths. Since it is a circular buffer,
837 * we have to transmit till the end, and then the rest.
838 * Take the port lock to get a
839 * consistent xmit buffer state.
840 */
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200841 tx_len = CIRC_CNT_TO_END(xmit->head,
842 xmit->tail,
843 UART_XMIT_SIZE);
844
845 if (atmel_port->fifo_size) {
846 /* multi data mode */
847 part1_len = (tx_len & ~0x3); /* DWORD access */
848 part2_len = (tx_len & 0x3); /* BYTE access */
849 } else {
850 /* single data (legacy) mode */
851 part1_len = 0;
852 part2_len = tx_len; /* BYTE access only */
853 }
854
855 sg_init_table(sgl, 2);
856 sg_len = 0;
857 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
858 if (part1_len) {
859 sg = &sgl[sg_len++];
860 sg_dma_address(sg) = phys_addr;
861 sg_dma_len(sg) = part1_len;
862
863 phys_addr += part1_len;
864 }
865
866 if (part2_len) {
867 sg = &sgl[sg_len++];
868 sg_dma_address(sg) = phys_addr;
869 sg_dma_len(sg) = part2_len;
870 }
871
872 /*
873 * save tx_len so atmel_complete_tx_dma() will increase
874 * xmit->tail correctly
875 */
876 atmel_port->tx_len = tx_len;
Elen Song08f738b2013-07-22 16:30:26 +0800877
878 desc = dmaengine_prep_slave_sg(chan,
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200879 sgl,
880 sg_len,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +0100881 DMA_MEM_TO_DEV,
882 DMA_PREP_INTERRUPT |
883 DMA_CTRL_ACK);
Elen Song08f738b2013-07-22 16:30:26 +0800884 if (!desc) {
885 dev_err(port->dev, "Failed to send via dma!\n");
886 return;
887 }
888
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200889 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800890
891 atmel_port->desc_tx = desc;
892 desc->callback = atmel_complete_tx_dma;
893 desc->callback_param = atmel_port;
894 atmel_port->cookie_tx = dmaengine_submit(desc);
895
896 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +0100897 if (port->rs485.flags & SER_RS485_ENABLED) {
Elen Song08f738b2013-07-22 16:30:26 +0800898 /* DMA done, stop TX, start RX for RS485 */
899 atmel_start_rx(port);
900 }
901 }
902
903 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
904 uart_write_wakeup(port);
905}
906
907static int atmel_prepare_tx_dma(struct uart_port *port)
908{
909 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
910 dma_cap_mask_t mask;
911 struct dma_slave_config config;
912 int ret, nent;
913
914 dma_cap_zero(mask);
915 dma_cap_set(DMA_SLAVE, mask);
916
917 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
918 if (atmel_port->chan_tx == NULL)
919 goto chan_err;
920 dev_info(port->dev, "using %s for tx DMA transfers\n",
921 dma_chan_name(atmel_port->chan_tx));
922
923 spin_lock_init(&atmel_port->lock_tx);
924 sg_init_table(&atmel_port->sg_tx, 1);
925 /* UART circular tx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +0800926 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
Elen Song08f738b2013-07-22 16:30:26 +0800927 sg_set_page(&atmel_port->sg_tx,
928 virt_to_page(port->state->xmit.buf),
929 UART_XMIT_SIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200930 (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
Elen Song08f738b2013-07-22 16:30:26 +0800931 nent = dma_map_sg(port->dev,
932 &atmel_port->sg_tx,
933 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200934 DMA_TO_DEVICE);
Elen Song08f738b2013-07-22 16:30:26 +0800935
936 if (!nent) {
937 dev_dbg(port->dev, "need to release resource of dma\n");
938 goto chan_err;
939 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200940 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song08f738b2013-07-22 16:30:26 +0800941 sg_dma_len(&atmel_port->sg_tx),
942 port->state->xmit.buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +0200943 &sg_dma_address(&atmel_port->sg_tx));
Elen Song08f738b2013-07-22 16:30:26 +0800944 }
945
946 /* Configure the slave DMA */
947 memset(&config, 0, sizeof(config));
948 config.direction = DMA_MEM_TO_DEV;
Cyrille Pitchen5f258b32015-07-02 15:18:13 +0200949 config.dst_addr_width = (atmel_port->fifo_size) ?
950 DMA_SLAVE_BUSWIDTH_4_BYTES :
951 DMA_SLAVE_BUSWIDTH_1_BYTE;
Elen Song08f738b2013-07-22 16:30:26 +0800952 config.dst_addr = port->mapbase + ATMEL_US_THR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +0200953 config.dst_maxburst = 1;
Elen Song08f738b2013-07-22 16:30:26 +0800954
Maxime Ripard5483c102014-10-22 17:43:16 +0200955 ret = dmaengine_slave_config(atmel_port->chan_tx,
956 &config);
Elen Song08f738b2013-07-22 16:30:26 +0800957 if (ret) {
958 dev_err(port->dev, "DMA tx slave configuration failed\n");
959 goto chan_err;
960 }
961
962 return 0;
963
964chan_err:
965 dev_err(port->dev, "TX channel not available, switch to pio\n");
966 atmel_port->use_dma_tx = 0;
967 if (atmel_port->chan_tx)
968 atmel_release_tx_dma(port);
969 return -EINVAL;
970}
971
Elen Song34df42f2013-07-22 16:30:27 +0800972static void atmel_complete_rx_dma(void *arg)
973{
974 struct uart_port *port = arg;
975 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
976
977 tasklet_schedule(&atmel_port->tasklet);
978}
979
980static void atmel_release_rx_dma(struct uart_port *port)
981{
982 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
983 struct dma_chan *chan = atmel_port->chan_rx;
984
985 if (chan) {
986 dmaengine_terminate_all(chan);
987 dma_release_channel(chan);
988 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
Wolfram Sang48479142014-07-21 11:42:04 +0200989 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +0800990 }
991
992 atmel_port->desc_rx = NULL;
993 atmel_port->chan_rx = NULL;
994 atmel_port->cookie_rx = -EINVAL;
995}
996
997static void atmel_rx_from_dma(struct uart_port *port)
998{
999 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001000 struct tty_port *tport = &port->state->port;
Elen Song34df42f2013-07-22 16:30:27 +08001001 struct circ_buf *ring = &atmel_port->rx_ring;
1002 struct dma_chan *chan = atmel_port->chan_rx;
1003 struct dma_tx_state state;
1004 enum dma_status dmastat;
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001005 size_t count;
Elen Song34df42f2013-07-22 16:30:27 +08001006
1007
1008 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001009 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001010 dmastat = dmaengine_tx_status(chan,
1011 atmel_port->cookie_rx,
1012 &state);
1013 /* Restart a new tasklet if DMA status is error */
1014 if (dmastat == DMA_ERROR) {
1015 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001016 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001017 tasklet_schedule(&atmel_port->tasklet);
1018 return;
1019 }
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001020
1021 /* CPU claims ownership of RX DMA buffer */
1022 dma_sync_sg_for_cpu(port->dev,
1023 &atmel_port->sg_rx,
1024 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001025 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001026
1027 /*
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001028 * ring->head points to the end of data already written by the DMA.
1029 * ring->tail points to the beginning of data to be read by the
1030 * framework.
1031 * The current transfer size should not be larger than the dma buffer
1032 * length.
Elen Song34df42f2013-07-22 16:30:27 +08001033 */
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001034 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1035 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1036 /*
1037 * At this point ring->head may point to the first byte right after the
1038 * last byte of the dma buffer:
1039 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1040 *
1041 * However ring->tail must always points inside the dma buffer:
1042 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1043 *
1044 * Since we use a ring buffer, we have to handle the case
1045 * where head is lower than tail. In such a case, we first read from
1046 * tail to the end of the buffer then reset tail.
1047 */
1048 if (ring->head < ring->tail) {
1049 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
Elen Song34df42f2013-07-22 16:30:27 +08001050
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001051 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1052 ring->tail = 0;
Elen Song34df42f2013-07-22 16:30:27 +08001053 port->icount.rx += count;
1054 }
1055
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001056 /* Finally we read data from tail to head */
1057 if (ring->tail < ring->head) {
1058 count = ring->head - ring->tail;
1059
1060 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1061 /* Wrap ring->head if needed */
1062 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1063 ring->head = 0;
1064 ring->tail = ring->head;
1065 port->icount.rx += count;
1066 }
1067
1068 /* USART retreives ownership of RX DMA buffer */
1069 dma_sync_sg_for_device(port->dev,
1070 &atmel_port->sg_rx,
1071 1,
Cyrille Pitchen485819b2014-12-09 14:31:32 +01001072 DMA_FROM_DEVICE);
Cyrille Pitchen66f37aa2014-10-20 19:12:20 +02001073
1074 /*
1075 * Drop the lock here since it might end up calling
1076 * uart_start(), which takes the lock.
1077 */
1078 spin_unlock(&port->lock);
1079 tty_flip_buffer_push(tport);
1080 spin_lock(&port->lock);
1081
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001082 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001083}
1084
1085static int atmel_prepare_rx_dma(struct uart_port *port)
1086{
1087 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1088 struct dma_async_tx_descriptor *desc;
1089 dma_cap_mask_t mask;
1090 struct dma_slave_config config;
1091 struct circ_buf *ring;
1092 int ret, nent;
1093
1094 ring = &atmel_port->rx_ring;
1095
1096 dma_cap_zero(mask);
1097 dma_cap_set(DMA_CYCLIC, mask);
1098
1099 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1100 if (atmel_port->chan_rx == NULL)
1101 goto chan_err;
1102 dev_info(port->dev, "using %s for rx DMA transfers\n",
1103 dma_chan_name(atmel_port->chan_rx));
1104
1105 spin_lock_init(&atmel_port->lock_rx);
1106 sg_init_table(&atmel_port->sg_rx, 1);
1107 /* UART circular rx buffer is an aligned page. */
Leilei Zhao2c277052015-02-27 16:07:14 +08001108 BUG_ON(!PAGE_ALIGNED(ring->buf));
Elen Song34df42f2013-07-22 16:30:27 +08001109 sg_set_page(&atmel_port->sg_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001110 virt_to_page(ring->buf),
Leilei Zhaoa5108802015-02-27 16:07:15 +08001111 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001112 (unsigned long)ring->buf & ~PAGE_MASK);
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001113 nent = dma_map_sg(port->dev,
1114 &atmel_port->sg_rx,
1115 1,
1116 DMA_FROM_DEVICE);
Elen Song34df42f2013-07-22 16:30:27 +08001117
1118 if (!nent) {
1119 dev_dbg(port->dev, "need to release resource of dma\n");
1120 goto chan_err;
1121 } else {
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001122 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
Elen Song34df42f2013-07-22 16:30:27 +08001123 sg_dma_len(&atmel_port->sg_rx),
1124 ring->buf,
Uwe Kleine-Königc8d1f022015-09-30 10:19:38 +02001125 &sg_dma_address(&atmel_port->sg_rx));
Elen Song34df42f2013-07-22 16:30:27 +08001126 }
1127
1128 /* Configure the slave DMA */
1129 memset(&config, 0, sizeof(config));
1130 config.direction = DMA_DEV_TO_MEM;
1131 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1132 config.src_addr = port->mapbase + ATMEL_US_RHR;
Ludovic Desrochesa8d4e012015-04-16 16:58:12 +02001133 config.src_maxburst = 1;
Elen Song34df42f2013-07-22 16:30:27 +08001134
Maxime Ripard5483c102014-10-22 17:43:16 +02001135 ret = dmaengine_slave_config(atmel_port->chan_rx,
1136 &config);
Elen Song34df42f2013-07-22 16:30:27 +08001137 if (ret) {
1138 dev_err(port->dev, "DMA rx slave configuration failed\n");
1139 goto chan_err;
1140 }
1141 /*
1142 * Prepare a cyclic dma transfer, assign 2 descriptors,
1143 * each one is half ring buffer size
1144 */
1145 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
Cyrille Pitchen1842dc2e2014-12-09 14:31:36 +01001146 sg_dma_address(&atmel_port->sg_rx),
1147 sg_dma_len(&atmel_port->sg_rx),
1148 sg_dma_len(&atmel_port->sg_rx)/2,
1149 DMA_DEV_TO_MEM,
1150 DMA_PREP_INTERRUPT);
Elen Song34df42f2013-07-22 16:30:27 +08001151 desc->callback = atmel_complete_rx_dma;
1152 desc->callback_param = port;
1153 atmel_port->desc_rx = desc;
1154 atmel_port->cookie_rx = dmaengine_submit(desc);
1155
1156 return 0;
1157
1158chan_err:
1159 dev_err(port->dev, "RX channel not available, switch to pio\n");
1160 atmel_port->use_dma_rx = 0;
1161 if (atmel_port->chan_rx)
1162 atmel_release_rx_dma(port);
1163 return -EINVAL;
1164}
1165
Elen Song2e68c222013-07-22 16:30:30 +08001166static void atmel_uart_timer_callback(unsigned long data)
1167{
1168 struct uart_port *port = (void *)data;
1169 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1170
1171 tasklet_schedule(&atmel_port->tasklet);
1172 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
1173}
1174
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001175/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001176 * receive interrupt handler.
1177 */
1178static void
1179atmel_handle_receive(struct uart_port *port, unsigned int pending)
1180{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001181 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001182
Elen Song64e22eb2013-07-22 16:30:24 +08001183 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001184 /*
1185 * PDC receive. Just schedule the tasklet and let it
1186 * figure out the details.
1187 *
1188 * TODO: We're not handling error flags correctly at
1189 * the moment.
1190 */
1191 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001192 atmel_uart_writel(port, ATMEL_US_IDR,
1193 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
Chip Coldwella6670612008-02-08 04:21:06 -08001194 tasklet_schedule(&atmel_port->tasklet);
1195 }
1196
1197 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1198 ATMEL_US_FRAME | ATMEL_US_PARE))
1199 atmel_pdc_rxerr(port, pending);
1200 }
1201
Elen Song34df42f2013-07-22 16:30:27 +08001202 if (atmel_use_dma_rx(port)) {
1203 if (pending & ATMEL_US_TIMEOUT) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001204 atmel_uart_writel(port, ATMEL_US_IDR,
1205 ATMEL_US_TIMEOUT);
Elen Song34df42f2013-07-22 16:30:27 +08001206 tasklet_schedule(&atmel_port->tasklet);
1207 }
1208 }
1209
Remy Bohmerb843aa22008-02-08 04:21:01 -08001210 /* Interrupt receive */
1211 if (pending & ATMEL_US_RXRDY)
1212 atmel_rx_chars(port);
1213 else if (pending & ATMEL_US_RXBRK) {
1214 /*
1215 * End of break detected. If it came along with a
1216 * character, atmel_rx_chars will handle it.
1217 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001218 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1219 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001220 atmel_port->break_active = 0;
1221 }
1222}
1223
1224/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001225 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -08001226 */
1227static void
1228atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1229{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001230 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001231
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001232 if (pending & atmel_port->tx_done_mask) {
1233 /* Either PDC or interrupt transmission */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001234 atmel_uart_writel(port, ATMEL_US_IDR,
1235 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001236 tasklet_schedule(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001237 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001238}
1239
1240/*
1241 * status flags interrupt handler.
1242 */
1243static void
1244atmel_handle_status(struct uart_port *port, unsigned int pending,
1245 unsigned int status)
1246{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001247 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001248
Remy Bohmerb843aa22008-02-08 04:21:01 -08001249 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001250 | ATMEL_US_CTSIC)) {
1251 atmel_port->irq_status = status;
Leilei Zhaod033e822015-04-09 10:48:15 +08001252 atmel_port->status_change = atmel_port->irq_status ^
1253 atmel_port->irq_status_prev;
1254 atmel_port->irq_status_prev = status;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001255 tasklet_schedule(&atmel_port->tasklet);
1256 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001257}
1258
1259/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001260 * Interrupt handler
1261 */
David Howells7d12e782006-10-05 14:55:46 +01001262static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001263{
1264 struct uart_port *port = dev_id;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001265 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001266 unsigned int status, pending, mask, pass_counter = 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001267 bool gpio_handled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001268
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001269 spin_lock(&atmel_port->lock_suspended);
1270
Chip Coldwella6670612008-02-08 04:21:06 -08001271 do {
Richard Genoude0b0baa2014-05-13 20:20:44 +02001272 status = atmel_get_lines_status(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001273 mask = atmel_uart_readl(port, ATMEL_US_IMR);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001274 pending = status & mask;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001275 if (!gpio_handled) {
1276 /*
1277 * Dealing with GPIO interrupt
1278 */
1279 if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1280 pending |= ATMEL_US_CTSIC;
1281
1282 if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1283 pending |= ATMEL_US_DSRIC;
1284
1285 if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1286 pending |= ATMEL_US_RIIC;
1287
1288 if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1289 pending |= ATMEL_US_DCDIC;
1290
1291 gpio_handled = true;
1292 }
Chip Coldwella6670612008-02-08 04:21:06 -08001293 if (!pending)
1294 break;
1295
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001296 if (atmel_port->suspended) {
1297 atmel_port->pending |= pending;
1298 atmel_port->pending_status = status;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001299 atmel_uart_writel(port, ATMEL_US_IDR, mask);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001300 pm_system_wakeup();
1301 break;
1302 }
1303
Remy Bohmerb843aa22008-02-08 04:21:01 -08001304 atmel_handle_receive(port, pending);
1305 atmel_handle_status(port, pending, status);
1306 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -08001307 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001308
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001309 spin_unlock(&atmel_port->lock_suspended);
1310
Haavard Skinnemoen0400b692008-02-23 15:23:36 -08001311 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001312}
1313
Elen Songa930e522013-07-22 16:30:25 +08001314static void atmel_release_tx_pdc(struct uart_port *port)
1315{
1316 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1317 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1318
1319 dma_unmap_single(port->dev,
1320 pdc->dma_addr,
1321 pdc->dma_size,
1322 DMA_TO_DEVICE);
1323}
1324
Chip Coldwella6670612008-02-08 04:21:06 -08001325/*
1326 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1327 */
Elen Song64e22eb2013-07-22 16:30:24 +08001328static void atmel_tx_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001329{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001330 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001331 struct circ_buf *xmit = &port->state->xmit;
Chip Coldwella6670612008-02-08 04:21:06 -08001332 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1333 int count;
1334
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001335 /* nothing left to transmit? */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001336 if (atmel_uart_readl(port, ATMEL_PDC_TCR))
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001337 return;
1338
Chip Coldwella6670612008-02-08 04:21:06 -08001339 xmit->tail += pdc->ofs;
1340 xmit->tail &= UART_XMIT_SIZE - 1;
1341
1342 port->icount.tx += pdc->ofs;
1343 pdc->ofs = 0;
1344
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001345 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -08001346
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001347 /* disable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001348 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Michael Trimarchiba0657f2008-04-02 13:04:41 -07001349
Itai Levi1f140812009-01-15 13:50:43 -08001350 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001351 dma_sync_single_for_device(port->dev,
1352 pdc->dma_addr,
1353 pdc->dma_size,
1354 DMA_TO_DEVICE);
1355
1356 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1357 pdc->ofs = count;
1358
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001359 atmel_uart_writel(port, ATMEL_PDC_TPR,
1360 pdc->dma_addr + xmit->tail);
1361 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001362 /* re-enable PDC transmit */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001363 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001364 /* Enable interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001365 atmel_uart_writel(port, ATMEL_US_IER,
1366 atmel_port->tx_done_mask);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001367 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001368 if ((port->rs485.flags & SER_RS485_ENABLED) &&
1369 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
Claudio Scordinoe8faff72010-05-03 13:31:28 +01001370 /* DMA done, stop TX, start RX for RS485 */
1371 atmel_start_rx(port);
1372 }
Chip Coldwella6670612008-02-08 04:21:06 -08001373 }
1374
1375 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1376 uart_write_wakeup(port);
1377}
1378
Elen Songa930e522013-07-22 16:30:25 +08001379static int atmel_prepare_tx_pdc(struct uart_port *port)
1380{
1381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1382 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1383 struct circ_buf *xmit = &port->state->xmit;
1384
1385 pdc->buf = xmit->buf;
1386 pdc->dma_addr = dma_map_single(port->dev,
1387 pdc->buf,
1388 UART_XMIT_SIZE,
1389 DMA_TO_DEVICE);
1390 pdc->dma_size = UART_XMIT_SIZE;
1391 pdc->ofs = 0;
1392
1393 return 0;
1394}
1395
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001396static void atmel_rx_from_ring(struct uart_port *port)
1397{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001398 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001399 struct circ_buf *ring = &atmel_port->rx_ring;
1400 unsigned int flg;
1401 unsigned int status;
1402
1403 while (ring->head != ring->tail) {
1404 struct atmel_uart_char c;
1405
1406 /* Make sure c is loaded after head. */
1407 smp_rmb();
1408
1409 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1410
1411 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1412
1413 port->icount.rx++;
1414 status = c.status;
1415 flg = TTY_NORMAL;
1416
1417 /*
1418 * note that the error handling code is
1419 * out of the main execution path
1420 */
1421 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1422 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1423 if (status & ATMEL_US_RXBRK) {
1424 /* ignore side-effect */
1425 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1426
1427 port->icount.brk++;
1428 if (uart_handle_break(port))
1429 continue;
1430 }
1431 if (status & ATMEL_US_PARE)
1432 port->icount.parity++;
1433 if (status & ATMEL_US_FRAME)
1434 port->icount.frame++;
1435 if (status & ATMEL_US_OVRE)
1436 port->icount.overrun++;
1437
1438 status &= port->read_status_mask;
1439
1440 if (status & ATMEL_US_RXBRK)
1441 flg = TTY_BREAK;
1442 else if (status & ATMEL_US_PARE)
1443 flg = TTY_PARITY;
1444 else if (status & ATMEL_US_FRAME)
1445 flg = TTY_FRAME;
1446 }
1447
1448
1449 if (uart_handle_sysrq_char(port, c.ch))
1450 continue;
1451
1452 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1453 }
1454
1455 /*
1456 * Drop the lock here since it might end up calling
1457 * uart_start(), which takes the lock.
1458 */
1459 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001460 tty_flip_buffer_push(&port->state->port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001461 spin_lock(&port->lock);
1462}
1463
Elen Songa930e522013-07-22 16:30:25 +08001464static void atmel_release_rx_pdc(struct uart_port *port)
1465{
1466 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1467 int i;
1468
1469 for (i = 0; i < 2; i++) {
1470 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1471
1472 dma_unmap_single(port->dev,
1473 pdc->dma_addr,
1474 pdc->dma_size,
1475 DMA_FROM_DEVICE);
1476 kfree(pdc->buf);
1477 }
1478}
1479
Elen Song64e22eb2013-07-22 16:30:24 +08001480static void atmel_rx_from_pdc(struct uart_port *port)
Chip Coldwella6670612008-02-08 04:21:06 -08001481{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001482 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001483 struct tty_port *tport = &port->state->port;
Chip Coldwella6670612008-02-08 04:21:06 -08001484 struct atmel_dma_buffer *pdc;
1485 int rx_idx = atmel_port->pdc_rx_idx;
1486 unsigned int head;
1487 unsigned int tail;
1488 unsigned int count;
1489
1490 do {
1491 /* Reset the UART timeout early so that we don't miss one */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001492 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001493
1494 pdc = &atmel_port->pdc_rx[rx_idx];
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001495 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
Chip Coldwella6670612008-02-08 04:21:06 -08001496 tail = pdc->ofs;
1497
1498 /* If the PDC has switched buffers, RPR won't contain
1499 * any address within the current buffer. Since head
1500 * is unsigned, we just need a one-way comparison to
1501 * find out.
1502 *
1503 * In this case, we just need to consume the entire
1504 * buffer and resubmit it for DMA. This will clear the
1505 * ENDRX bit as well, so that we can safely re-enable
1506 * all interrupts below.
1507 */
1508 head = min(head, pdc->dma_size);
1509
1510 if (likely(head != tail)) {
1511 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1512 pdc->dma_size, DMA_FROM_DEVICE);
1513
1514 /*
1515 * head will only wrap around when we recycle
1516 * the DMA buffer, and when that happens, we
1517 * explicitly set tail to 0. So head will
1518 * always be greater than tail.
1519 */
1520 count = head - tail;
1521
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001522 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1523 count);
Chip Coldwella6670612008-02-08 04:21:06 -08001524
1525 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1526 pdc->dma_size, DMA_FROM_DEVICE);
1527
1528 port->icount.rx += count;
1529 pdc->ofs = head;
1530 }
1531
1532 /*
1533 * If the current buffer is full, we need to check if
1534 * the next one contains any additional data.
1535 */
1536 if (head >= pdc->dma_size) {
1537 pdc->ofs = 0;
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001538 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1539 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
Chip Coldwella6670612008-02-08 04:21:06 -08001540
1541 rx_idx = !rx_idx;
1542 atmel_port->pdc_rx_idx = rx_idx;
1543 }
1544 } while (head >= pdc->dma_size);
1545
1546 /*
1547 * Drop the lock here since it might end up calling
1548 * uart_start(), which takes the lock.
1549 */
1550 spin_unlock(&port->lock);
Jiri Slaby2e124b42013-01-03 15:53:06 +01001551 tty_flip_buffer_push(tport);
Chip Coldwella6670612008-02-08 04:21:06 -08001552 spin_lock(&port->lock);
1553
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001554 atmel_uart_writel(port, ATMEL_US_IER,
1555 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Chip Coldwella6670612008-02-08 04:21:06 -08001556}
1557
Elen Songa930e522013-07-22 16:30:25 +08001558static int atmel_prepare_rx_pdc(struct uart_port *port)
1559{
1560 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1561 int i;
1562
1563 for (i = 0; i < 2; i++) {
1564 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1565
1566 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1567 if (pdc->buf == NULL) {
1568 if (i != 0) {
1569 dma_unmap_single(port->dev,
1570 atmel_port->pdc_rx[0].dma_addr,
1571 PDC_BUFFER_SIZE,
1572 DMA_FROM_DEVICE);
1573 kfree(atmel_port->pdc_rx[0].buf);
1574 }
1575 atmel_port->use_pdc_rx = 0;
1576 return -ENOMEM;
1577 }
1578 pdc->dma_addr = dma_map_single(port->dev,
1579 pdc->buf,
1580 PDC_BUFFER_SIZE,
1581 DMA_FROM_DEVICE);
1582 pdc->dma_size = PDC_BUFFER_SIZE;
1583 pdc->ofs = 0;
1584 }
1585
1586 atmel_port->pdc_rx_idx = 0;
1587
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001588 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1589 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001590
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001591 atmel_uart_writel(port, ATMEL_PDC_RNPR,
1592 atmel_port->pdc_rx[1].dma_addr);
1593 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
Elen Songa930e522013-07-22 16:30:25 +08001594
1595 return 0;
1596}
1597
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001598/*
1599 * tasklet handling tty stuff outside the interrupt handler.
1600 */
1601static void atmel_tasklet_func(unsigned long data)
1602{
1603 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001604 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Leilei Zhaod033e822015-04-09 10:48:15 +08001605 unsigned int status = atmel_port->irq_status;
1606 unsigned int status_change = atmel_port->status_change;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001607
1608 /* The interrupt handler does not take the lock */
1609 spin_lock(&port->lock);
1610
Elen Songa930e522013-07-22 16:30:25 +08001611 atmel_port->schedule_tx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001612
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001613 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1614 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1615 /* TODO: All reads to CSR will clear these interrupts! */
1616 if (status_change & ATMEL_US_RI)
1617 port->icount.rng++;
1618 if (status_change & ATMEL_US_DSR)
1619 port->icount.dsr++;
1620 if (status_change & ATMEL_US_DCD)
1621 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1622 if (status_change & ATMEL_US_CTS)
1623 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1624
Alan Coxbdc04e32009-09-19 13:13:31 -07001625 wake_up_interruptible(&port->state->port.delta_msr_wait);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001626
Leilei Zhaod033e822015-04-09 10:48:15 +08001627 atmel_port->status_change = 0;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001628 }
1629
Elen Songa930e522013-07-22 16:30:25 +08001630 atmel_port->schedule_rx(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001631
1632 spin_unlock(&port->lock);
1633}
1634
Leilei Zhao4a1e8882015-02-27 16:07:16 +08001635static void atmel_init_property(struct atmel_uart_port *atmel_port,
Elen Song33d64c42013-07-22 16:30:28 +08001636 struct platform_device *pdev)
1637{
1638 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001639 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001640
1641 if (np) {
1642 /* DMA/PDC usage specification */
1643 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1644 if (of_get_property(np, "dmas", NULL)) {
1645 atmel_port->use_dma_rx = true;
1646 atmel_port->use_pdc_rx = false;
1647 } else {
1648 atmel_port->use_dma_rx = false;
1649 atmel_port->use_pdc_rx = true;
1650 }
1651 } else {
1652 atmel_port->use_dma_rx = false;
1653 atmel_port->use_pdc_rx = false;
1654 }
1655
1656 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1657 if (of_get_property(np, "dmas", NULL)) {
1658 atmel_port->use_dma_tx = true;
1659 atmel_port->use_pdc_tx = false;
1660 } else {
1661 atmel_port->use_dma_tx = false;
1662 atmel_port->use_pdc_tx = true;
1663 }
1664 } else {
1665 atmel_port->use_dma_tx = false;
1666 atmel_port->use_pdc_tx = false;
1667 }
1668
1669 } else {
1670 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1671 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1672 atmel_port->use_dma_rx = false;
1673 atmel_port->use_dma_tx = false;
1674 }
1675
Elen Song33d64c42013-07-22 16:30:28 +08001676}
1677
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001678static void atmel_init_rs485(struct uart_port *port,
Elen Song33d64c42013-07-22 16:30:28 +08001679 struct platform_device *pdev)
1680{
1681 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09001682 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Elen Song33d64c42013-07-22 16:30:28 +08001683
1684 if (np) {
1685 u32 rs485_delay[2];
1686 /* rs485 properties */
1687 if (of_property_read_u32_array(np, "rs485-rts-delay",
1688 rs485_delay, 2) == 0) {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001689 struct serial_rs485 *rs485conf = &port->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001690
1691 rs485conf->delay_rts_before_send = rs485_delay[0];
1692 rs485conf->delay_rts_after_send = rs485_delay[1];
1693 rs485conf->flags = 0;
1694
1695 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1696 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1697
1698 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1699 NULL))
1700 rs485conf->flags |= SER_RS485_ENABLED;
1701 }
1702 } else {
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01001703 port->rs485 = pdata->rs485;
Elen Song33d64c42013-07-22 16:30:28 +08001704 }
1705
1706}
1707
Elen Songa930e522013-07-22 16:30:25 +08001708static void atmel_set_ops(struct uart_port *port)
1709{
1710 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1711
Elen Song34df42f2013-07-22 16:30:27 +08001712 if (atmel_use_dma_rx(port)) {
1713 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1714 atmel_port->schedule_rx = &atmel_rx_from_dma;
1715 atmel_port->release_rx = &atmel_release_rx_dma;
1716 } else if (atmel_use_pdc_rx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001717 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1718 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1719 atmel_port->release_rx = &atmel_release_rx_pdc;
1720 } else {
1721 atmel_port->prepare_rx = NULL;
1722 atmel_port->schedule_rx = &atmel_rx_from_ring;
1723 atmel_port->release_rx = NULL;
1724 }
1725
Elen Song08f738b2013-07-22 16:30:26 +08001726 if (atmel_use_dma_tx(port)) {
1727 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1728 atmel_port->schedule_tx = &atmel_tx_dma;
1729 atmel_port->release_tx = &atmel_release_tx_dma;
1730 } else if (atmel_use_pdc_tx(port)) {
Elen Songa930e522013-07-22 16:30:25 +08001731 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1732 atmel_port->schedule_tx = &atmel_tx_pdc;
1733 atmel_port->release_tx = &atmel_release_tx_pdc;
1734 } else {
1735 atmel_port->prepare_tx = NULL;
1736 atmel_port->schedule_tx = &atmel_tx_chars;
1737 atmel_port->release_tx = NULL;
1738 }
1739}
1740
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001741/*
Elen Song055560b2013-07-22 16:30:29 +08001742 * Get ip name usart or uart
1743 */
Nicolas Ferre892db582013-10-17 17:37:11 +02001744static void atmel_get_ip_name(struct uart_port *port)
Elen Song055560b2013-07-22 16:30:29 +08001745{
1746 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001747 int name = atmel_uart_readl(port, ATMEL_US_NAME);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001748 u32 version;
Elen Song055560b2013-07-22 16:30:29 +08001749 int usart, uart;
1750 /* usart and uart ascii */
1751 usart = 0x55534152;
1752 uart = 0x44424755;
1753
1754 atmel_port->is_usart = false;
1755
1756 if (name == usart) {
1757 dev_dbg(port->dev, "This is usart\n");
1758 atmel_port->is_usart = true;
1759 } else if (name == uart) {
1760 dev_dbg(port->dev, "This is uart\n");
1761 atmel_port->is_usart = false;
1762 } else {
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001763 /* fallback for older SoCs: use version field */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001764 version = atmel_uart_readl(port, ATMEL_US_VERSION);
Nicolas Ferre731d9ca2013-10-17 17:37:12 +02001765 switch (version) {
1766 case 0x302:
1767 case 0x10213:
1768 dev_dbg(port->dev, "This version is usart\n");
1769 atmel_port->is_usart = true;
1770 break;
1771 case 0x203:
1772 case 0x10202:
1773 dev_dbg(port->dev, "This version is uart\n");
1774 atmel_port->is_usart = false;
1775 break;
1776 default:
1777 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1778 }
Elen Song055560b2013-07-22 16:30:29 +08001779 }
Elen Song055560b2013-07-22 16:30:29 +08001780}
1781
Richard Genoudab5e4e42014-05-13 20:20:45 +02001782static void atmel_free_gpio_irq(struct uart_port *port)
1783{
1784 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1785 enum mctrl_gpio_idx i;
1786
1787 for (i = 0; i < UART_GPIO_MAX; i++)
1788 if (atmel_port->gpio_irq[i] >= 0)
1789 free_irq(atmel_port->gpio_irq[i], port);
1790}
1791
1792static int atmel_request_gpio_irq(struct uart_port *port)
1793{
1794 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1795 int *irq = atmel_port->gpio_irq;
1796 enum mctrl_gpio_idx i;
1797 int err = 0;
1798
1799 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1800 if (irq[i] < 0)
1801 continue;
1802
1803 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1804 err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1805 "atmel_serial", port);
1806 if (err)
1807 dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1808 irq[i]);
1809 }
1810
1811 /*
1812 * If something went wrong, rollback.
1813 */
1814 while (err && (--i >= 0))
1815 if (irq[i] >= 0)
1816 free_irq(irq[i], port);
1817
1818 return err;
1819}
1820
Elen Song055560b2013-07-22 16:30:29 +08001821/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001822 * Perform initialization and enable port for reception
1823 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001824static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001825{
Elen Song33d64c42013-07-22 16:30:28 +08001826 struct platform_device *pdev = to_platform_device(port->dev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001827 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Alan Coxebd2c8f2009-09-19 13:13:28 -07001828 struct tty_struct *tty = port->state->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001829 int retval;
1830
1831 /*
1832 * Ensure that no interrupts are enabled otherwise when
1833 * request_irq() is called we could get stuck trying to
1834 * handle an unexpected interrupt
1835 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001836 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Richard Genoudab5e4e42014-05-13 20:20:45 +02001837 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001838
1839 /*
1840 * Allocate the IRQ
1841 */
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01001842 retval = request_irq(port->irq, atmel_interrupt,
1843 IRQF_SHARED | IRQF_COND_SUSPEND,
Haavard Skinnemoenae161062008-02-08 04:21:08 -08001844 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001845 if (retval) {
Richard Genoudddaa6032014-02-26 17:19:45 +01001846 dev_err(port->dev, "atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001847 return retval;
1848 }
1849
1850 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02001851 * Get the GPIO lines IRQ
1852 */
1853 retval = atmel_request_gpio_irq(port);
1854 if (retval)
1855 goto free_irq;
1856
Leilei Zhao1e125782015-02-27 16:07:18 +08001857 tasklet_enable(&atmel_port->tasklet);
1858
Richard Genoudab5e4e42014-05-13 20:20:45 +02001859 /*
Chip Coldwella6670612008-02-08 04:21:06 -08001860 * Initialize DMA (if necessary)
1861 */
Elen Song33d64c42013-07-22 16:30:28 +08001862 atmel_init_property(atmel_port, pdev);
Leilei Zhao4d9628a2015-02-27 16:07:17 +08001863 atmel_set_ops(port);
Elen Song33d64c42013-07-22 16:30:28 +08001864
Elen Songa930e522013-07-22 16:30:25 +08001865 if (atmel_port->prepare_rx) {
1866 retval = atmel_port->prepare_rx(port);
1867 if (retval < 0)
1868 atmel_set_ops(port);
Chip Coldwella6670612008-02-08 04:21:06 -08001869 }
1870
Elen Songa930e522013-07-22 16:30:25 +08001871 if (atmel_port->prepare_tx) {
1872 retval = atmel_port->prepare_tx(port);
1873 if (retval < 0)
1874 atmel_set_ops(port);
1875 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001876
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001877 /*
1878 * Enable FIFO when available
1879 */
1880 if (atmel_port->fifo_size) {
1881 unsigned int txrdym = ATMEL_US_ONE_DATA;
1882 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1883 unsigned int fmr;
1884
1885 atmel_uart_writel(port, ATMEL_US_CR,
1886 ATMEL_US_FIFOEN |
1887 ATMEL_US_RXFCLR |
1888 ATMEL_US_TXFLCLR);
1889
Cyrille Pitchen5f258b32015-07-02 15:18:13 +02001890 if (atmel_use_dma_tx(port))
1891 txrdym = ATMEL_US_FOUR_DATA;
1892
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02001893 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1894 if (atmel_port->rts_high &&
1895 atmel_port->rts_low)
1896 fmr |= ATMEL_US_FRTSC |
1897 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1898 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1899
1900 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1901 }
1902
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001903 /* Save current CSR for comparison in atmel_tasklet_func() */
Richard Genoude0b0baa2014-05-13 20:20:44 +02001904 atmel_port->irq_status_prev = atmel_get_lines_status(port);
Atsushi Nemoto27c0c8e2009-02-18 14:48:28 -08001905 atmel_port->irq_status = atmel_port->irq_status_prev;
1906
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001907 /*
1908 * Finally, enable the serial port
1909 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001910 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001911 /* enable xmit & rcvr */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001912 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +01001913
Marek Roszko8bc661b2014-01-10 10:33:11 +01001914 setup_timer(&atmel_port->uart_timer,
1915 atmel_uart_timer_callback,
1916 (unsigned long)port);
1917
Elen Song64e22eb2013-07-22 16:30:24 +08001918 if (atmel_use_pdc_rx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08001919 /* set UART timeout */
Elen Song2e68c222013-07-22 16:30:30 +08001920 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001921 mod_timer(&atmel_port->uart_timer,
1922 jiffies + uart_poll_timeout(port));
1923 /* set USART timeout */
1924 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001925 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1926 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Chip Coldwella6670612008-02-08 04:21:06 -08001927
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001928 atmel_uart_writel(port, ATMEL_US_IER,
1929 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001930 }
Chip Coldwella6670612008-02-08 04:21:06 -08001931 /* enable PDC controller */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001932 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
Elen Song34df42f2013-07-22 16:30:27 +08001933 } else if (atmel_use_dma_rx(port)) {
Elen Song2e68c222013-07-22 16:30:30 +08001934 /* set UART timeout */
1935 if (!atmel_port->is_usart) {
Elen Song2e68c222013-07-22 16:30:30 +08001936 mod_timer(&atmel_port->uart_timer,
1937 jiffies + uart_poll_timeout(port));
1938 /* set USART timeout */
1939 } else {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001940 atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1941 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
Elen Song34df42f2013-07-22 16:30:27 +08001942
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001943 atmel_uart_writel(port, ATMEL_US_IER,
1944 ATMEL_US_TIMEOUT);
Elen Song2e68c222013-07-22 16:30:30 +08001945 }
Chip Coldwella6670612008-02-08 04:21:06 -08001946 } else {
1947 /* enable receive only */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001948 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
Chip Coldwella6670612008-02-08 04:21:06 -08001949 }
Andrew Victorafefc412006-06-19 19:53:19 +01001950
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001951 return 0;
Richard Genoudab5e4e42014-05-13 20:20:45 +02001952
1953free_irq:
1954 free_irq(port->irq, port);
1955
1956 return retval;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001957}
1958
1959/*
Peter Hurley479e9b92014-10-16 16:54:18 -04001960 * Flush any TX data submitted for DMA. Called when the TX circular
1961 * buffer is reset.
1962 */
1963static void atmel_flush_buffer(struct uart_port *port)
1964{
1965 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1966
1967 if (atmel_use_pdc_tx(port)) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02001968 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
Peter Hurley479e9b92014-10-16 16:54:18 -04001969 atmel_port->pdc_tx.ofs = 0;
1970 }
1971}
1972
1973/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001974 * Disable the port
1975 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001976static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001977{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001978 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001979
Chip Coldwella6670612008-02-08 04:21:06 -08001980 /*
Marek Roszko8bc661b2014-01-10 10:33:11 +01001981 * Prevent any tasklets being scheduled during
1982 * cleanup
1983 */
1984 del_timer_sync(&atmel_port->uart_timer);
1985
1986 /*
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001987 * Clear out any scheduled tasklets before
1988 * we destroy the buffers
1989 */
Leilei Zhao1e125782015-02-27 16:07:18 +08001990 tasklet_disable(&atmel_port->tasklet);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01001991 tasklet_kill(&atmel_port->tasklet);
1992
1993 /*
1994 * Ensure everything is stopped and
1995 * disable all interrupts, port and break condition.
Chip Coldwella6670612008-02-08 04:21:06 -08001996 */
1997 atmel_stop_rx(port);
1998 atmel_stop_tx(port);
1999
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002000 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
2001 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Marek Roszko0cc7c6c2014-01-07 11:45:06 +01002002
2003
Chip Coldwella6670612008-02-08 04:21:06 -08002004 /*
2005 * Shut-down the DMA.
2006 */
Elen Songa930e522013-07-22 16:30:25 +08002007 if (atmel_port->release_rx)
2008 atmel_port->release_rx(port);
2009 if (atmel_port->release_tx)
2010 atmel_port->release_tx(port);
Chip Coldwella6670612008-02-08 04:21:06 -08002011
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002012 /*
Mark Deneenbb7e73c2014-01-07 11:45:09 +01002013 * Reset ring buffer pointers
2014 */
2015 atmel_port->rx_ring.head = 0;
2016 atmel_port->rx_ring.tail = 0;
2017
2018 /*
Richard Genoudab5e4e42014-05-13 20:20:45 +02002019 * Free the interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002020 */
2021 free_irq(port->irq, port);
Richard Genoudab5e4e42014-05-13 20:20:45 +02002022 atmel_free_gpio_irq(port);
2023
2024 atmel_port->ms_irq_enabled = false;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002025
Peter Hurley479e9b92014-10-16 16:54:18 -04002026 atmel_flush_buffer(port);
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002027}
2028
2029/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002030 * Power / Clock management.
2031 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002032static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2033 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002034{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002035 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002036
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002037 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002038 case 0:
2039 /*
2040 * Enable the peripheral clock for this serial port.
2041 * This is called on uart_open() or a resume event.
2042 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002043 clk_prepare_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -07002044
2045 /* re-enable interrupts if we disabled some on suspend */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002046 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002047 break;
2048 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -07002049 /* Back up the interrupt mask and disable all interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002050 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2051 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Anti Sullinf05596d2008-09-22 13:57:54 -07002052
Remy Bohmerb843aa22008-02-08 04:21:01 -08002053 /*
2054 * Disable the peripheral clock for this serial port.
2055 * This is called on uart_close() or a suspend event.
2056 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002057 clk_disable_unprepare(atmel_port->clk);
Remy Bohmerb843aa22008-02-08 04:21:01 -08002058 break;
2059 default:
Richard Genoudddaa6032014-02-26 17:19:45 +01002060 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002061 }
2062}
2063
2064/*
2065 * Change the port parameters
2066 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002067static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2068 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002069{
2070 unsigned long flags;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002071 unsigned int old_mode, mode, imr, quot, baud;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002072
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002073 /* save the current mode register */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002074 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002075
2076 /* reset the mode, clock divisor, parity, stop bits and data size */
2077 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2078 ATMEL_US_PAR | ATMEL_US_USMODE);
Andrew Victor03abeac2007-05-03 12:26:24 +01002079
Remy Bohmerb843aa22008-02-08 04:21:01 -08002080 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002081 quot = uart_get_divisor(port, baud);
2082
Remy Bohmerb843aa22008-02-08 04:21:01 -08002083 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01002084 quot /= 8;
2085 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2086 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002087
2088 /* byte size */
2089 switch (termios->c_cflag & CSIZE) {
2090 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002091 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002092 break;
2093 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002094 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002095 break;
2096 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002097 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002098 break;
2099 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002100 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002101 break;
2102 }
2103
2104 /* stop bits */
2105 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002106 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002107
2108 /* parity */
2109 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002110 /* Mark or Space parity */
2111 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002112 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002113 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002114 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002115 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002116 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002117 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002118 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002119 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002120 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002121 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002122
2123 spin_lock_irqsave(&port->lock, flags);
2124
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002125 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002126 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002127 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -04002128 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002129 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002130
Elen Song64e22eb2013-07-22 16:30:24 +08002131 if (atmel_use_pdc_rx(port))
Chip Coldwella6670612008-02-08 04:21:06 -08002132 /* need to enable error interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002133 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
Chip Coldwella6670612008-02-08 04:21:06 -08002134
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002135 /*
2136 * Characters to ignore
2137 */
2138 port->ignore_status_mask = 0;
2139 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002140 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002141 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002142 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002143 /*
2144 * If we're ignoring parity and break indicators,
2145 * ignore overruns too (for real raw support).
2146 */
2147 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002148 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002149 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08002150 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002151
2152 /* update the per-port timeout */
2153 uart_update_timeout(port, termios->c_cflag, baud);
2154
Haavard Skinnemoen0ccad872009-06-16 17:02:03 +01002155 /*
2156 * save/disable interrupts. The tty layer will ensure that the
2157 * transmitter is empty if requested by the caller, so there's
2158 * no need to wait for it here.
2159 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002160 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2161 atmel_uart_writel(port, ATMEL_US_IDR, -1);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002162
2163 /* disable receiver and transmitter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002164 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002165
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002166 /* mode */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002167 if (port->rs485.flags & SER_RS485_ENABLED) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002168 atmel_uart_writel(port, ATMEL_US_TTGR,
2169 port->rs485.delay_rts_after_send);
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002170 mode |= ATMEL_US_USMODE_RS485;
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002171 } else if (termios->c_cflag & CRTSCTS) {
2172 /* RS232 with hardware handshake (RTS/CTS) */
2173 mode |= ATMEL_US_USMODE_HWHS;
2174 } else {
2175 /* RS232 without hadware handshake */
2176 mode |= ATMEL_US_USMODE_NORMAL;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002177 }
2178
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002179 /* set the mode, clock divisor, parity, stop bits and data size */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002180 atmel_uart_writel(port, ATMEL_US_MR, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002181
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002182 /*
2183 * when switching the mode, set the RTS line state according to the
2184 * new mode, otherwise keep the former state
2185 */
2186 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2187 unsigned int rts_state;
2188
2189 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2190 /* let the hardware control the RTS line */
2191 rts_state = ATMEL_US_RTSDIS;
2192 } else {
2193 /* force RTS line to low level */
2194 rts_state = ATMEL_US_RTSEN;
2195 }
2196
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002197 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
Cyrille Pitchen1cf6e8f2014-12-09 14:31:35 +01002198 }
2199
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002200 /* set the baud rate */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002201 atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2202 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2203 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002204
2205 /* restore interrupts */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002206 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002207
2208 /* CTS flow-control and modem-status interrupts */
2209 if (UART_ENABLE_MS(port, termios->c_cflag))
Richard Genoud35b675b2014-09-03 18:09:26 +02002210 atmel_enable_ms(port);
2211 else
2212 atmel_disable_ms(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002213
2214 spin_unlock_irqrestore(&port->lock, flags);
2215}
2216
Peter Hurley732a84a2014-11-05 13:11:43 -05002217static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002218{
Peter Hurley732a84a2014-11-05 13:11:43 -05002219 if (termios->c_line == N_PPS) {
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002220 port->flags |= UPF_HARDPPS_CD;
Peter Hurleyd41510c2014-11-05 13:11:44 -05002221 spin_lock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002222 atmel_enable_ms(port);
Peter Hurleyd41510c2014-11-05 13:11:44 -05002223 spin_unlock_irq(&port->lock);
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002224 } else {
2225 port->flags &= ~UPF_HARDPPS_CD;
Peter Hurleycab68f82014-11-05 13:11:45 -05002226 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2227 spin_lock_irq(&port->lock);
2228 atmel_disable_ms(port);
2229 spin_unlock_irq(&port->lock);
2230 }
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002231 }
2232}
2233
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002234/*
2235 * Return string describing the specified port
2236 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002237static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002238{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002239 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002240}
2241
2242/*
2243 * Release the memory region(s) being used by 'port'.
2244 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002245static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002246{
Andrew Victorafefc412006-06-19 19:53:19 +01002247 struct platform_device *pdev = to_platform_device(port->dev);
2248 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2249
2250 release_mem_region(port->mapbase, size);
2251
2252 if (port->flags & UPF_IOREMAP) {
2253 iounmap(port->membase);
2254 port->membase = NULL;
2255 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002256}
2257
2258/*
2259 * Request the memory region(s) being used by 'port'.
2260 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002261static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002262{
Andrew Victorafefc412006-06-19 19:53:19 +01002263 struct platform_device *pdev = to_platform_device(port->dev);
2264 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002265
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002266 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01002267 return -EBUSY;
2268
2269 if (port->flags & UPF_IOREMAP) {
2270 port->membase = ioremap(port->mapbase, size);
2271 if (port->membase == NULL) {
2272 release_mem_region(port->mapbase, size);
2273 return -ENOMEM;
2274 }
2275 }
2276
2277 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002278}
2279
2280/*
2281 * Configure/autoconfigure the port.
2282 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002283static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002284{
2285 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002286 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002287 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002288 }
2289}
2290
2291/*
2292 * Verify the new serial_struct (for TIOCSSERIAL).
2293 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002294static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002295{
2296 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02002297 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002298 ret = -EINVAL;
2299 if (port->irq != ser->irq)
2300 ret = -EINVAL;
2301 if (ser->io_type != SERIAL_IO_MEM)
2302 ret = -EINVAL;
2303 if (port->uartclk / 16 != ser->baud_base)
2304 ret = -EINVAL;
Andre Przywara270c2ad2015-10-05 18:00:52 +01002305 if (port->mapbase != (unsigned long)ser->iomem_base)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002306 ret = -EINVAL;
2307 if (port->iobase != ser->port)
2308 ret = -EINVAL;
2309 if (ser->hub6 != 0)
2310 ret = -EINVAL;
2311 return ret;
2312}
2313
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002314#ifdef CONFIG_CONSOLE_POLL
2315static int atmel_poll_get_char(struct uart_port *port)
2316{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002317 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002318 cpu_relax();
2319
Cyrille Pitchena6499432015-07-30 16:33:38 +02002320 return atmel_uart_read_char(port);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002321}
2322
2323static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2324{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002325 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002326 cpu_relax();
2327
Cyrille Pitchena6499432015-07-30 16:33:38 +02002328 atmel_uart_write_char(port, ch);
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002329}
2330#endif
2331
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002332static struct uart_ops atmel_pops = {
2333 .tx_empty = atmel_tx_empty,
2334 .set_mctrl = atmel_set_mctrl,
2335 .get_mctrl = atmel_get_mctrl,
2336 .stop_tx = atmel_stop_tx,
2337 .start_tx = atmel_start_tx,
2338 .stop_rx = atmel_stop_rx,
2339 .enable_ms = atmel_enable_ms,
2340 .break_ctl = atmel_break_ctl,
2341 .startup = atmel_startup,
2342 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01002343 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002344 .set_termios = atmel_set_termios,
Viktar Palstsiuk42bd7a42011-02-09 15:26:13 +01002345 .set_ldisc = atmel_set_ldisc,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002346 .type = atmel_type,
2347 .release_port = atmel_release_port,
2348 .request_port = atmel_request_port,
2349 .config_port = atmel_config_port,
2350 .verify_port = atmel_verify_port,
2351 .pm = atmel_serial_pm,
Albin Tonnerre8fe2d542009-12-09 12:31:32 -08002352#ifdef CONFIG_CONSOLE_POLL
2353 .poll_get_char = atmel_poll_get_char,
2354 .poll_put_char = atmel_poll_put_char,
2355#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002356};
2357
Andrew Victorafefc412006-06-19 19:53:19 +01002358/*
2359 * Configure the port from the platform device resource info.
2360 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002361static int atmel_init_port(struct atmel_uart_port *atmel_port,
Remy Bohmerb843aa22008-02-08 04:21:01 -08002362 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002363{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002364 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002365 struct uart_port *port = &atmel_port->uart;
Jingoo Han574de552013-07-30 17:06:57 +09002366 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002367
Leilei Zhao4a1e8882015-02-27 16:07:16 +08002368 atmel_init_property(atmel_port, pdev);
2369 atmel_set_ops(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002370
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002371 atmel_init_rs485(port, pdev);
Elen Songa930e522013-07-22 16:30:25 +08002372
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002373 port->iotype = UPIO_MEM;
2374 port->flags = UPF_BOOT_AUTOCONF;
2375 port->ops = &atmel_pops;
2376 port->fifosize = 1;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002377 port->dev = &pdev->dev;
Andrew Victorafefc412006-06-19 19:53:19 +01002378 port->mapbase = pdev->resource[0].start;
2379 port->irq = pdev->resource[1].start;
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002380 port->rs485_config = atmel_config_rs485;
Andrew Victorafefc412006-06-19 19:53:19 +01002381
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002382 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2383 (unsigned long)port);
Leilei Zhao1e125782015-02-27 16:07:18 +08002384 tasklet_disable(&atmel_port->tasklet);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002385
2386 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2387
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002388 if (pdata && pdata->regs) {
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02002389 /* Already mapped by setup code */
Nicolas Ferre1acfc7e2011-10-12 18:06:57 +02002390 port->membase = pdata->regs;
Nicolas Ferre588edbf2011-10-12 18:06:58 +02002391 } else {
Andrew Victorafefc412006-06-19 19:53:19 +01002392 port->flags |= UPF_IOREMAP;
2393 port->membase = NULL;
2394 }
2395
Remy Bohmerb843aa22008-02-08 04:21:01 -08002396 /* for console, the clock could already be configured */
2397 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002398 atmel_port->clk = clk_get(&pdev->dev, "usart");
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002399 if (IS_ERR(atmel_port->clk)) {
2400 ret = PTR_ERR(atmel_port->clk);
2401 atmel_port->clk = NULL;
2402 return ret;
2403 }
2404 ret = clk_prepare_enable(atmel_port->clk);
2405 if (ret) {
2406 clk_put(atmel_port->clk);
2407 atmel_port->clk = NULL;
2408 return ret;
2409 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002410 port->uartclk = clk_get_rate(atmel_port->clk);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002411 clk_disable_unprepare(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002412 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01002413 }
Chip Coldwella6670612008-02-08 04:21:06 -08002414
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002415 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
Ricardo Ribalda Delgado13bd3e62014-11-06 09:22:56 +01002416 if (port->rs485.flags & SER_RS485_ENABLED)
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002417 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
Elen Song64e22eb2013-07-22 16:30:24 +08002418 else if (atmel_use_pdc_tx(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002419 port->fifosize = PDC_BUFFER_SIZE;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002420 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2421 } else {
2422 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2423 }
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002424
2425 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002426}
2427
Jean-Christophe PLAGNIOL-VILLARD69f6a272012-02-16 00:24:07 +08002428struct platform_device *atmel_default_console_device; /* the serial console device */
2429
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02002430#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002431static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00002432{
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002433 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08002434 cpu_relax();
Cyrille Pitchena6499432015-07-30 16:33:38 +02002435 atmel_uart_write_char(port, ch);
Russell Kingd3587882006-03-20 20:00:09 +00002436}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002437
2438/*
2439 * Interrupts are disabled on entering
2440 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002441static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002442{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002443 struct uart_port *port = &atmel_ports[co->index].uart;
Claudio Scordinoe8faff72010-05-03 13:31:28 +01002444 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Russell Kingd3587882006-03-20 20:00:09 +00002445 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07002446 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002447
2448 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002449 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002450 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002451 imr = atmel_uart_readl(port, ATMEL_US_IMR);
2452 atmel_uart_writel(port, ATMEL_US_IDR,
2453 ATMEL_US_RXRDY | atmel_port->tx_done_mask);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002454
Marc Pignat39d4c922008-04-02 13:04:42 -07002455 /* Store PDC transmit status and disable it */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002456 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2457 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
Marc Pignat39d4c922008-04-02 13:04:42 -07002458
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002459 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002460
2461 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002462 * Finally, wait for transmitter to become empty
2463 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002464 */
2465 do {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002466 status = atmel_uart_readl(port, ATMEL_US_CSR);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002467 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07002468
2469 /* Restore PDC transmit status */
2470 if (pdc_tx)
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002471 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
Marc Pignat39d4c922008-04-02 13:04:42 -07002472
Remy Bohmerb843aa22008-02-08 04:21:01 -08002473 /* set interrupts back the way they were */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002474 atmel_uart_writel(port, ATMEL_US_IER, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002475}
2476
2477/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08002478 * If the port was already initialised (eg, by a boot loader),
2479 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002480 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08002481static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2482 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002483{
2484 unsigned int mr, quot;
2485
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002486 /*
2487 * If the baud rate generator isn't running, the port wasn't
2488 * initialized by the boot loader.
2489 */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002490 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08002491 if (!quot)
2492 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002493
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002494 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002495 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002496 *bits = 8;
2497 else
2498 *bits = 7;
2499
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002500 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002501 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002502 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002503 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002504 *parity = 'o';
2505
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002506 /*
2507 * The serial core only rounds down when matching this to a
2508 * supported baud rate. Make sure we don't end up slightly
2509 * lower than one of those, as it would make us fall through
2510 * to a much lower baud rate than we really want.
2511 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02002512 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002513}
2514
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002515static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002516{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002517 int ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002518 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002519 int baud = 115200;
2520 int bits = 8;
2521 int parity = 'n';
2522 int flow = 'n';
2523
Remy Bohmerb843aa22008-02-08 04:21:01 -08002524 if (port->membase == NULL) {
2525 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01002526 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08002527 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002528
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002529 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2530 if (ret)
2531 return ret;
David Brownell06a7f052008-11-06 12:53:40 -08002532
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002533 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2534 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2535 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002536
2537 if (options)
2538 uart_parse_options(options, &baud, &parity, &bits, &flow);
2539 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002540 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002541
2542 return uart_set_options(port, co, baud, parity, bits, flow);
2543}
2544
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002545static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002546
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002547static struct console atmel_console = {
2548 .name = ATMEL_DEVICENAME,
2549 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002550 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002551 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002552 .flags = CON_PRINTBUFFER,
2553 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002554 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002555};
2556
David Brownell06a7f052008-11-06 12:53:40 -08002557#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002558
Andrew Victorafefc412006-06-19 19:53:19 +01002559/*
2560 * Early console initialization (before VM subsystem initialized).
2561 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002562static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002563{
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002564 int ret;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02002565 if (atmel_default_console_device) {
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002566 struct atmel_uart_data *pdata =
Jingoo Han574de552013-07-30 17:06:57 +09002567 dev_get_platdata(&atmel_default_console_device->dev);
Linus Torvaldsefb8d212011-10-26 15:11:09 +02002568 int id = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002569 struct atmel_uart_port *port = &atmel_ports[id];
Voss, Nikolaus0d0a3cc2011-08-10 14:02:29 +02002570
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002571 port->backup_imr = 0;
2572 port->uart.line = id;
2573
2574 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002575 ret = atmel_init_port(port, atmel_default_console_device);
2576 if (ret)
2577 return ret;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002578 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002579 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002580
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002581 return 0;
2582}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002583
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002584console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002585
Andrew Victorafefc412006-06-19 19:53:19 +01002586/*
2587 * Late console initialization.
2588 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002589static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002590{
Remy Bohmerb843aa22008-02-08 04:21:01 -08002591 if (atmel_default_console_device
2592 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002593 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01002594
2595 return 0;
2596}
Remy Bohmerb843aa22008-02-08 04:21:01 -08002597
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002598core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01002599
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002600static inline bool atmel_is_console_port(struct uart_port *port)
2601{
2602 return port->cons && port->cons->index == port->line;
2603}
2604
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002605#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002606#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002607
2608static inline bool atmel_is_console_port(struct uart_port *port)
2609{
2610 return false;
2611}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002612#endif
2613
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002614static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08002615 .owner = THIS_MODULE,
2616 .driver_name = "atmel_serial",
2617 .dev_name = ATMEL_DEVICENAME,
2618 .major = SERIAL_ATMEL_MAJOR,
2619 .minor = MINOR_START,
2620 .nr = ATMEL_MAX_UART,
2621 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002622};
2623
Andrew Victorafefc412006-06-19 19:53:19 +01002624#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01002625static bool atmel_serial_clk_will_stop(void)
2626{
2627#ifdef CONFIG_ARCH_AT91
2628 return at91_suspend_entering_slow_clock();
2629#else
2630 return false;
2631#endif
2632}
2633
Remy Bohmerb843aa22008-02-08 04:21:01 -08002634static int atmel_serial_suspend(struct platform_device *pdev,
2635 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002636{
Andrew Victorafefc412006-06-19 19:53:19 +01002637 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002638 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002639
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002640 if (atmel_is_console_port(port) && console_suspend_enabled) {
2641 /* Drain the TX shifter */
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002642 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2643 ATMEL_US_TXEMPTY))
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01002644 cpu_relax();
2645 }
2646
Anti Sullinf05596d2008-09-22 13:57:54 -07002647 /* we can not wake up if we're running on slow clock */
2648 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002649 if (atmel_serial_clk_will_stop()) {
2650 unsigned long flags;
2651
2652 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2653 atmel_port->suspended = true;
2654 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Anti Sullinf05596d2008-09-22 13:57:54 -07002655 device_set_wakeup_enable(&pdev->dev, 0);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002656 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002657
2658 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002659
2660 return 0;
2661}
2662
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002663static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002664{
2665 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002666 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002667 unsigned long flags;
2668
2669 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2670 if (atmel_port->pending) {
2671 atmel_handle_receive(port, atmel_port->pending);
2672 atmel_handle_status(port, atmel_port->pending,
2673 atmel_port->pending_status);
2674 atmel_handle_transmit(port, atmel_port->pending);
2675 atmel_port->pending = 0;
2676 }
2677 atmel_port->suspended = false;
2678 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
Andrew Victorafefc412006-06-19 19:53:19 +01002679
Anti Sullinf05596d2008-09-22 13:57:54 -07002680 uart_resume_port(&atmel_uart, port);
2681 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01002682
2683 return 0;
2684}
2685#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002686#define atmel_serial_suspend NULL
2687#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01002688#endif
2689
Richard Genoude0b0baa2014-05-13 20:20:44 +02002690static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2691{
Richard Genoudab5e4e42014-05-13 20:20:45 +02002692 enum mctrl_gpio_idx i;
2693 struct gpio_desc *gpiod;
2694
Uwe Kleine-König7d8c70d2015-09-30 10:19:40 +02002695 p->gpios = mctrl_gpio_init_noauto(dev, 0);
Uwe Kleine-König722ccf42015-02-12 15:24:38 +01002696 if (IS_ERR(p->gpios))
2697 return PTR_ERR(p->gpios);
Richard Genoude0b0baa2014-05-13 20:20:44 +02002698
Richard Genoudab5e4e42014-05-13 20:20:45 +02002699 for (i = 0; i < UART_GPIO_MAX; i++) {
2700 gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2701 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2702 p->gpio_irq[i] = gpiod_to_irq(gpiod);
2703 else
2704 p->gpio_irq[i] = -EINVAL;
2705 }
2706
Richard Genoude0b0baa2014-05-13 20:20:44 +02002707 return 0;
2708}
2709
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002710static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2711 struct platform_device *pdev)
2712{
2713 port->fifo_size = 0;
2714 port->rts_low = 0;
2715 port->rts_high = 0;
2716
2717 if (of_property_read_u32(pdev->dev.of_node,
2718 "atmel,fifo-size",
2719 &port->fifo_size))
2720 return;
2721
2722 if (!port->fifo_size)
2723 return;
2724
2725 if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2726 port->fifo_size = 0;
2727 dev_err(&pdev->dev, "Invalid FIFO size\n");
2728 return;
2729 }
2730
2731 /*
2732 * 0 <= rts_low <= rts_high <= fifo_size
2733 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2734 * to flush their internal TX FIFO, commonly up to 16 data, before
2735 * actually stopping to send new data. So we try to set the RTS High
2736 * Threshold to a reasonably high value respecting this 16 data
2737 * empirical rule when possible.
2738 */
2739 port->rts_high = max_t(int, port->fifo_size >> 1,
2740 port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2741 port->rts_low = max_t(int, port->fifo_size >> 2,
2742 port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2743
2744 dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2745 port->fifo_size);
2746 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2747 port->rts_high);
2748 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n",
2749 port->rts_low);
2750}
2751
Bill Pemberton9671f092012-11-19 13:21:50 -05002752static int atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002753{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002754 struct atmel_uart_port *port;
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002755 struct device_node *np = pdev->dev.of_node;
Jingoo Han574de552013-07-30 17:06:57 +09002756 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002757 void *data;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002758 int ret = -ENODEV;
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002759 bool rs485_enabled;
Andrew Victorafefc412006-06-19 19:53:19 +01002760
Haavard Skinnemoen9d09daf2009-10-26 16:50:02 -07002761 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002762
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002763 if (np)
2764 ret = of_alias_get_id(np, "serial");
2765 else
2766 if (pdata)
2767 ret = pdata->num;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002768
2769 if (ret < 0)
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002770 /* port id not found in platform data nor device-tree aliases:
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002771 * auto-enumerate it */
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002772 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002773
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002774 if (ret >= ATMEL_MAX_UART) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002775 ret = -ENODEV;
2776 goto err;
2777 }
2778
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002779 if (test_and_set_bit(ret, atmel_ports_in_use)) {
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002780 /* port already in use */
2781 ret = -EBUSY;
2782 goto err;
2783 }
2784
2785 port = &atmel_ports[ret];
Anti Sullinf05596d2008-09-22 13:57:54 -07002786 port->backup_imr = 0;
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002787 port->uart.line = ret;
Cyrille Pitchenb5199d42015-07-02 15:18:12 +02002788 atmel_serial_probe_fifos(port, pdev);
Linus Walleij354e57f2013-11-07 10:25:55 +01002789
Boris BREZILLON2c7af5b2015-03-02 10:18:18 +01002790 spin_lock_init(&port->lock_suspended);
2791
Richard Genoude0b0baa2014-05-13 20:20:44 +02002792 ret = atmel_init_gpios(port, &pdev->dev);
Uwe Kleine-König722ccf42015-02-12 15:24:38 +01002793 if (ret < 0) {
2794 dev_err(&pdev->dev, "Failed to initialize GPIOs.");
Uwe Kleine-König8f1bd8f2015-09-23 08:57:40 +02002795 goto err_clear_bit;
Uwe Kleine-König722ccf42015-02-12 15:24:38 +01002796 }
Anti Sullinf05596d2008-09-22 13:57:54 -07002797
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002798 ret = atmel_init_port(port, pdev);
2799 if (ret)
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002800 goto err_clear_bit;
Andrew Victorafefc412006-06-19 19:53:19 +01002801
Elen Song64e22eb2013-07-22 16:30:24 +08002802 if (!atmel_use_pdc_rx(&port->uart)) {
Chip Coldwella6670612008-02-08 04:21:06 -08002803 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08002804 data = kmalloc(sizeof(struct atmel_uart_char)
2805 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08002806 if (!data)
2807 goto err_alloc_ring;
2808 port->rx_ring.buf = data;
2809 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002810
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002811 rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2812
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002813 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002814 if (ret)
2815 goto err_add_port;
2816
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002817#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
David Brownell06a7f052008-11-06 12:53:40 -08002818 if (atmel_is_console_port(&port->uart)
2819 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2820 /*
2821 * The serial core enabled the clock for us, so undo
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002822 * the clk_prepare_enable() in atmel_console_setup()
David Brownell06a7f052008-11-06 12:53:40 -08002823 */
Boris BREZILLON91f8c2d2013-06-19 13:17:30 +02002824 clk_disable_unprepare(port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08002825 }
Albin Tonnerre8da14b52009-07-29 15:04:18 -07002826#endif
David Brownell06a7f052008-11-06 12:53:40 -08002827
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002828 device_init_wakeup(&pdev->dev, 1);
2829 platform_set_drvdata(pdev, port);
2830
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002831 /*
2832 * The peripheral clock has been disabled by atmel_init_port():
2833 * enable it before accessing I/O registers
2834 */
2835 clk_prepare_enable(port->clk);
2836
Ricardo Ribalda Delgadobd737f82014-11-06 09:23:00 +01002837 if (rs485_enabled) {
Cyrille Pitchen4e7decd2015-07-02 15:18:11 +02002838 atmel_uart_writel(&port->uart, ATMEL_US_MR,
2839 ATMEL_US_USMODE_NORMAL);
2840 atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
Claudio Scordino5dfbd1d72011-01-13 15:45:39 -08002841 }
2842
Elen Song055560b2013-07-22 16:30:29 +08002843 /*
2844 * Get port name of usart or uart
2845 */
Nicolas Ferre892db582013-10-17 17:37:11 +02002846 atmel_get_ip_name(&port->uart);
Elen Song055560b2013-07-22 16:30:29 +08002847
Cyrille Pitchend4f64182014-12-09 14:31:33 +01002848 /*
2849 * The peripheral clock can now safely be disabled till the port
2850 * is used
2851 */
2852 clk_disable_unprepare(port->clk);
2853
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002854 return 0;
2855
2856err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002857 kfree(port->rx_ring.buf);
2858 port->rx_ring.buf = NULL;
2859err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002860 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002861 clk_put(port->clk);
2862 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01002863 }
Cyrille Pitchen6fbb9bd2014-12-09 14:31:34 +01002864err_clear_bit:
2865 clear_bit(port->uart.line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002866err:
Andrew Victorafefc412006-06-19 19:53:19 +01002867 return ret;
2868}
2869
Bill Pembertonae8d8a12012-11-19 13:26:18 -05002870static int atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01002871{
2872 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08002873 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01002874 int ret = 0;
2875
Marek Roszkof50c995f2014-01-07 11:45:07 +01002876 tasklet_kill(&atmel_port->tasklet);
2877
Andrew Victorafefc412006-06-19 19:53:19 +01002878 device_init_wakeup(&pdev->dev, 0);
Andrew Victorafefc412006-06-19 19:53:19 +01002879
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002880 ret = uart_remove_one_port(&atmel_uart, port);
2881
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08002882 kfree(atmel_port->rx_ring.buf);
2883
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002884 /* "port" is allocated statically, so we shouldn't free it */
2885
Pawel Wieczorkiewicz503bded2013-02-20 17:26:20 +01002886 clear_bit(port->line, atmel_ports_in_use);
Nicolas Ferre4cbf9f42011-10-12 18:06:59 +02002887
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08002888 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01002889
2890 return ret;
2891}
2892
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002893static struct platform_driver atmel_serial_driver = {
2894 .probe = atmel_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05002895 .remove = atmel_serial_remove,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002896 .suspend = atmel_serial_suspend,
2897 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01002898 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02002899 .name = "atmel_usart",
Nicolas Ferre5fbe46b2011-10-12 18:07:00 +02002900 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
Andrew Victorafefc412006-06-19 19:53:19 +01002901 },
2902};
2903
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002904static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01002905{
2906 int ret;
2907
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002908 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002909 if (ret)
2910 return ret;
2911
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002912 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01002913 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002914 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01002915
2916 return ret;
2917}
2918
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002919static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002920{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002921 platform_driver_unregister(&atmel_serial_driver);
2922 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002923}
2924
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002925module_init(atmel_serial_init);
2926module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002927
2928MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02002929MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00002930MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07002931MODULE_ALIAS("platform:atmel_usart");