Merge tag 'libnvdimm-for-4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[deliverable/linux.git] / drivers / tty / serial / imx.c
CommitLineData
1da177e4 1/*
f890cef2 2 * Driver for Motorola/Freescale IMX serial ports
1da177e4 3 *
f890cef2 4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
1da177e4 5 *
f890cef2
UKK
6 * Author: Sascha Hauer <sascha@saschahauer.de>
7 * Copyright (C) 2004 Pengutronix
1da177e4
LT
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
1da177e4 18 */
1da177e4
LT
19
20#if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
21#define SUPPORT_SYSRQ
22#endif
23
24#include <linux/module.h>
25#include <linux/ioport.h>
26#include <linux/init.h>
27#include <linux/console.h>
28#include <linux/sysrq.h>
d052d1be 29#include <linux/platform_device.h>
1da177e4
LT
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/serial_core.h>
33#include <linux/serial.h>
38a41fdf 34#include <linux/clk.h>
b6e49138 35#include <linux/delay.h>
534fca06 36#include <linux/rational.h>
5a0e3ad6 37#include <linux/slab.h>
22698aa2
SG
38#include <linux/of.h>
39#include <linux/of_device.h>
e32a9f8f 40#include <linux/io.h>
b4cdc8f6 41#include <linux/dma-mapping.h>
1da177e4 42
1da177e4 43#include <asm/irq.h>
82906b13 44#include <linux/platform_data/serial-imx.h>
b4cdc8f6 45#include <linux/platform_data/dma-imx.h>
1da177e4 46
58362d5b
UKK
47#include "serial_mctrl_gpio.h"
48
ff4bfb21
SH
49/* Register definitions */
50#define URXD0 0x0 /* Receiver Register */
51#define URTX0 0x40 /* Transmitter Register */
52#define UCR1 0x80 /* Control Register 1 */
53#define UCR2 0x84 /* Control Register 2 */
54#define UCR3 0x88 /* Control Register 3 */
55#define UCR4 0x8c /* Control Register 4 */
56#define UFCR 0x90 /* FIFO Control Register */
57#define USR1 0x94 /* Status Register 1 */
58#define USR2 0x98 /* Status Register 2 */
59#define UESC 0x9c /* Escape Character Register */
60#define UTIM 0xa0 /* Escape Timer Register */
61#define UBIR 0xa4 /* BRM Incremental Register */
62#define UBMR 0xa8 /* BRM Modulator Register */
63#define UBRC 0xac /* Baud Rate Count Register */
fe6b540a
SG
64#define IMX21_ONEMS 0xb0 /* One Millisecond register */
65#define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
66#define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
ff4bfb21
SH
67
68/* UART Control Register Bit Fields.*/
55d8693a 69#define URXD_DUMMY_READ (1<<16)
82313e66
SK
70#define URXD_CHARRDY (1<<15)
71#define URXD_ERR (1<<14)
72#define URXD_OVRRUN (1<<13)
73#define URXD_FRMERR (1<<12)
74#define URXD_BRK (1<<11)
75#define URXD_PRERR (1<<10)
26c47412 76#define URXD_RX_DATA (0xFF<<0)
82313e66
SK
77#define UCR1_ADEN (1<<15) /* Auto detect interrupt */
78#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
79#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
80#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
b4cdc8f6 81#define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
82313e66
SK
82#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
83#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
84#define UCR1_IREN (1<<7) /* Infrared interface enable */
85#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
86#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
87#define UCR1_SNDBRK (1<<4) /* Send break */
88#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
89#define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
b4cdc8f6 90#define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
82313e66
SK
91#define UCR1_DOZE (1<<1) /* Doze */
92#define UCR1_UARTEN (1<<0) /* UART enabled */
93#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
94#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
95#define UCR2_CTSC (1<<13) /* CTS pin control */
96#define UCR2_CTS (1<<12) /* Clear to send */
97#define UCR2_ESCEN (1<<11) /* Escape enable */
98#define UCR2_PREN (1<<8) /* Parity enable */
99#define UCR2_PROE (1<<7) /* Parity odd/even */
100#define UCR2_STPB (1<<6) /* Stop */
101#define UCR2_WS (1<<5) /* Word size */
102#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
103#define UCR2_ATEN (1<<3) /* Aging Timer Enable */
104#define UCR2_TXEN (1<<2) /* Transmitter enabled */
105#define UCR2_RXEN (1<<1) /* Receiver enabled */
106#define UCR2_SRST (1<<0) /* SW reset */
107#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
108#define UCR3_PARERREN (1<<12) /* Parity enable */
109#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
110#define UCR3_DSR (1<<10) /* Data set ready */
111#define UCR3_DCD (1<<9) /* Data carrier detect */
112#define UCR3_RI (1<<8) /* Ring indicator */
b38cb7d2 113#define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
82313e66
SK
114#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
115#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
116#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
27e16501 117#define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
82313e66
SK
118#define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
119#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
120#define UCR3_BPEN (1<<0) /* Preset registers enable */
121#define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
122#define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
123#define UCR4_INVR (1<<9) /* Inverted infrared reception */
124#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
125#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
126#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
b4cdc8f6 127#define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
82313e66
SK
128#define UCR4_IRSC (1<<5) /* IR special case */
129#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
130#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
131#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
132#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
133#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
134#define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
135#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
136#define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
137#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
138#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
139#define USR1_RTSS (1<<14) /* RTS pin status */
140#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
141#define USR1_RTSD (1<<12) /* RTS delta */
142#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
143#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
144#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
86a04ba6 145#define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
27e16501 146#define USR1_DTRD (1<<7) /* DTR Delta */
82313e66
SK
147#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
148#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
149#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
150#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
151#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
152#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
153#define USR2_IDLE (1<<12) /* Idle condition */
90ebc483
UKK
154#define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
155#define USR2_RIIN (1<<9) /* Ring Indicator Input */
82313e66
SK
156#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
157#define USR2_WAKE (1<<7) /* Wake */
90ebc483 158#define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
82313e66
SK
159#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
160#define USR2_TXDC (1<<3) /* Transmitter complete */
161#define USR2_BRCD (1<<2) /* Break condition */
162#define USR2_ORE (1<<1) /* Overrun error */
163#define USR2_RDR (1<<0) /* Recv data ready */
164#define UTS_FRCPERR (1<<13) /* Force parity error */
165#define UTS_LOOP (1<<12) /* Loop tx and rx */
166#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
167#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
168#define UTS_TXFULL (1<<4) /* TxFIFO full */
169#define UTS_RXFULL (1<<3) /* RxFIFO full */
170#define UTS_SOFTRST (1<<0) /* Software reset */
ff4bfb21 171
1da177e4 172/* We've been assigned a range on the "Low-density serial ports" major */
82313e66
SK
173#define SERIAL_IMX_MAJOR 207
174#define MINOR_START 16
e3d13ff4 175#define DEV_NAME "ttymxc"
1da177e4 176
1da177e4
LT
177/*
178 * This determines how often we check the modem status signals
179 * for any change. They generally aren't connected to an IRQ
180 * so we have to poll them. We also check immediately before
181 * filling the TX fifo incase CTS has been dropped.
182 */
183#define MCTRL_TIMEOUT (250*HZ/1000)
184
185#define DRIVER_NAME "IMX-uart"
186
dbff4e9e
SH
187#define UART_NR 8
188
f95661b2 189/* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
fe6b540a
SG
190enum imx_uart_type {
191 IMX1_UART,
192 IMX21_UART,
a496e628 193 IMX6Q_UART,
fe6b540a
SG
194};
195
196/* device type dependent stuff */
197struct imx_uart_data {
198 unsigned uts_reg;
199 enum imx_uart_type devtype;
200};
201
1da177e4
LT
202struct imx_port {
203 struct uart_port port;
204 struct timer_list timer;
205 unsigned int old_status;
26bbb3ff 206 unsigned int have_rtscts:1;
20ff2fe6 207 unsigned int dte_mode:1;
b6e49138
FG
208 unsigned int irda_inv_rx:1;
209 unsigned int irda_inv_tx:1;
210 unsigned short trcv_delay; /* transceiver delay */
3a9465fa
SH
211 struct clk *clk_ipg;
212 struct clk *clk_per;
7d0b066f 213 const struct imx_uart_data *devdata;
b4cdc8f6 214
58362d5b
UKK
215 struct mctrl_gpios *gpios;
216
b4cdc8f6
HS
217 /* DMA fields */
218 unsigned int dma_is_inited:1;
219 unsigned int dma_is_enabled:1;
220 unsigned int dma_is_rxing:1;
221 unsigned int dma_is_txing:1;
222 struct dma_chan *dma_chan_rx, *dma_chan_tx;
223 struct scatterlist rx_sgl, tx_sgl[2];
224 void *rx_buf;
7cb92fd2 225 unsigned int tx_bytes;
b4cdc8f6 226 unsigned int dma_tx_nents;
9ce4f8f3 227 wait_queue_head_t dma_wait;
90bb6bd3 228 unsigned int saved_reg[10];
c868cbb7 229 bool context_saved;
1da177e4
LT
230};
231
0ad5a814
DB
232struct imx_port_ucrs {
233 unsigned int ucr1;
234 unsigned int ucr2;
235 unsigned int ucr3;
236};
237
fe6b540a
SG
238static struct imx_uart_data imx_uart_devdata[] = {
239 [IMX1_UART] = {
240 .uts_reg = IMX1_UTS,
241 .devtype = IMX1_UART,
242 },
243 [IMX21_UART] = {
244 .uts_reg = IMX21_UTS,
245 .devtype = IMX21_UART,
246 },
a496e628
HS
247 [IMX6Q_UART] = {
248 .uts_reg = IMX21_UTS,
249 .devtype = IMX6Q_UART,
250 },
fe6b540a
SG
251};
252
31ada047 253static const struct platform_device_id imx_uart_devtype[] = {
fe6b540a
SG
254 {
255 .name = "imx1-uart",
256 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX1_UART],
257 }, {
258 .name = "imx21-uart",
259 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX21_UART],
a496e628
HS
260 }, {
261 .name = "imx6q-uart",
262 .driver_data = (kernel_ulong_t) &imx_uart_devdata[IMX6Q_UART],
fe6b540a
SG
263 }, {
264 /* sentinel */
265 }
266};
267MODULE_DEVICE_TABLE(platform, imx_uart_devtype);
268
ad3d4fdc 269static const struct of_device_id imx_uart_dt_ids[] = {
a496e628 270 { .compatible = "fsl,imx6q-uart", .data = &imx_uart_devdata[IMX6Q_UART], },
22698aa2
SG
271 { .compatible = "fsl,imx1-uart", .data = &imx_uart_devdata[IMX1_UART], },
272 { .compatible = "fsl,imx21-uart", .data = &imx_uart_devdata[IMX21_UART], },
273 { /* sentinel */ }
274};
275MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
276
fe6b540a
SG
277static inline unsigned uts_reg(struct imx_port *sport)
278{
279 return sport->devdata->uts_reg;
280}
281
282static inline int is_imx1_uart(struct imx_port *sport)
283{
284 return sport->devdata->devtype == IMX1_UART;
285}
286
287static inline int is_imx21_uart(struct imx_port *sport)
288{
289 return sport->devdata->devtype == IMX21_UART;
290}
291
a496e628
HS
292static inline int is_imx6q_uart(struct imx_port *sport)
293{
294 return sport->devdata->devtype == IMX6Q_UART;
295}
44a75411 296/*
297 * Save and restore functions for UCR1, UCR2 and UCR3 registers
298 */
93d94b37 299#if defined(CONFIG_SERIAL_IMX_CONSOLE)
44a75411 300static void imx_port_ucrs_save(struct uart_port *port,
301 struct imx_port_ucrs *ucr)
302{
303 /* save control registers */
304 ucr->ucr1 = readl(port->membase + UCR1);
305 ucr->ucr2 = readl(port->membase + UCR2);
306 ucr->ucr3 = readl(port->membase + UCR3);
307}
308
309static void imx_port_ucrs_restore(struct uart_port *port,
310 struct imx_port_ucrs *ucr)
311{
312 /* restore control registers */
313 writel(ucr->ucr1, port->membase + UCR1);
314 writel(ucr->ucr2, port->membase + UCR2);
315 writel(ucr->ucr3, port->membase + UCR3);
316}
e8bfa760 317#endif
44a75411 318
58362d5b
UKK
319static void imx_port_rts_active(struct imx_port *sport, unsigned long *ucr2)
320{
321 *ucr2 &= ~UCR2_CTSC;
322 *ucr2 |= UCR2_CTS;
323
324 mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS);
325}
326
327static void imx_port_rts_inactive(struct imx_port *sport, unsigned long *ucr2)
328{
329 *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
330
331 mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS);
332}
333
334static void imx_port_rts_auto(struct imx_port *sport, unsigned long *ucr2)
335{
336 *ucr2 |= UCR2_CTSC;
337}
338
1da177e4
LT
339/*
340 * interrupts disabled on entry
341 */
b129a8cc 342static void imx_stop_tx(struct uart_port *port)
1da177e4
LT
343{
344 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21
SH
345 unsigned long temp;
346
9ce4f8f3
GKH
347 /*
348 * We are maybe in the SMP context, so if the DMA TX thread is running
349 * on other cpu, we have to wait for it to finish.
350 */
351 if (sport->dma_is_enabled && sport->dma_is_txing)
352 return;
b4cdc8f6 353
17b8f2a3
UKK
354 temp = readl(port->membase + UCR1);
355 writel(temp & ~UCR1_TXMPTYEN, port->membase + UCR1);
356
357 /* in rs485 mode disable transmitter if shifter is empty */
358 if (port->rs485.flags & SER_RS485_ENABLED &&
359 readl(port->membase + USR2) & USR2_TXDC) {
360 temp = readl(port->membase + UCR2);
361 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
58362d5b 362 imx_port_rts_inactive(sport, &temp);
17b8f2a3 363 else
58362d5b 364 imx_port_rts_active(sport, &temp);
7d1cadca 365 temp |= UCR2_RXEN;
17b8f2a3
UKK
366 writel(temp, port->membase + UCR2);
367
368 temp = readl(port->membase + UCR4);
369 temp &= ~UCR4_TCEN;
370 writel(temp, port->membase + UCR4);
371 }
1da177e4
LT
372}
373
374/*
375 * interrupts disabled on entry
376 */
377static void imx_stop_rx(struct uart_port *port)
378{
379 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21
SH
380 unsigned long temp;
381
45564a66
HS
382 if (sport->dma_is_enabled && sport->dma_is_rxing) {
383 if (sport->port.suspended) {
384 dmaengine_terminate_all(sport->dma_chan_rx);
385 sport->dma_is_rxing = 0;
386 } else {
387 return;
388 }
389 }
b4cdc8f6 390
ff4bfb21 391 temp = readl(sport->port.membase + UCR2);
82313e66 392 writel(temp & ~UCR2_RXEN, sport->port.membase + UCR2);
85878399
HS
393
394 /* disable the `Receiver Ready Interrrupt` */
395 temp = readl(sport->port.membase + UCR1);
396 writel(temp & ~UCR1_RRDYEN, sport->port.membase + UCR1);
1da177e4
LT
397}
398
399/*
400 * Set the modem control timer to fire immediately.
401 */
402static void imx_enable_ms(struct uart_port *port)
403{
404 struct imx_port *sport = (struct imx_port *)port;
405
406 mod_timer(&sport->timer, jiffies);
58362d5b
UKK
407
408 mctrl_gpio_enable_ms(sport->gpios);
1da177e4
LT
409}
410
91a1a909 411static void imx_dma_tx(struct imx_port *sport);
1da177e4
LT
412static inline void imx_transmit_buffer(struct imx_port *sport)
413{
ebd2c8f6 414 struct circ_buf *xmit = &sport->port.state->xmit;
91a1a909 415 unsigned long temp;
1da177e4 416
5e42e9a3
PH
417 if (sport->port.x_char) {
418 /* Send next char */
419 writel(sport->port.x_char, sport->port.membase + URTX0);
7e2fb5aa
JW
420 sport->port.icount.tx++;
421 sport->port.x_char = 0;
5e42e9a3
PH
422 return;
423 }
424
425 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
426 imx_stop_tx(&sport->port);
427 return;
428 }
429
91a1a909
JW
430 if (sport->dma_is_enabled) {
431 /*
432 * We've just sent a X-char Ensure the TX DMA is enabled
433 * and the TX IRQ is disabled.
434 **/
435 temp = readl(sport->port.membase + UCR1);
436 temp &= ~UCR1_TXMPTYEN;
437 if (sport->dma_is_txing) {
438 temp |= UCR1_TDMAEN;
439 writel(temp, sport->port.membase + UCR1);
440 } else {
441 writel(temp, sport->port.membase + UCR1);
442 imx_dma_tx(sport);
443 }
444 }
445
4e4e6602 446 while (!uart_circ_empty(xmit) &&
5e42e9a3 447 !(readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)) {
1da177e4
LT
448 /* send xmit->buf[xmit->tail]
449 * out the port here */
ff4bfb21 450 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
d3810cd4 451 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
1da177e4 452 sport->port.icount.tx++;
8c0b254b 453 }
1da177e4 454
97775731
FG
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(&sport->port);
457
1da177e4 458 if (uart_circ_empty(xmit))
b129a8cc 459 imx_stop_tx(&sport->port);
1da177e4
LT
460}
461
b4cdc8f6
HS
462static void dma_tx_callback(void *data)
463{
464 struct imx_port *sport = data;
465 struct scatterlist *sgl = &sport->tx_sgl[0];
466 struct circ_buf *xmit = &sport->port.state->xmit;
467 unsigned long flags;
a2c718ce 468 unsigned long temp;
b4cdc8f6 469
42f752b3 470 spin_lock_irqsave(&sport->port.lock, flags);
b4cdc8f6 471
42f752b3 472 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
b4cdc8f6 473
a2c718ce
DB
474 temp = readl(sport->port.membase + UCR1);
475 temp &= ~UCR1_TDMAEN;
476 writel(temp, sport->port.membase + UCR1);
477
b4cdc8f6 478 /* update the stat */
b4cdc8f6
HS
479 xmit->tail = (xmit->tail + sport->tx_bytes) & (UART_XMIT_SIZE - 1);
480 sport->port.icount.tx += sport->tx_bytes;
b4cdc8f6
HS
481
482 dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
483
42f752b3
DB
484 sport->dma_is_txing = 0;
485
486 spin_unlock_irqrestore(&sport->port.lock, flags);
487
d64b8607
JW
488 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
489 uart_write_wakeup(&sport->port);
9ce4f8f3
GKH
490
491 if (waitqueue_active(&sport->dma_wait)) {
492 wake_up(&sport->dma_wait);
493 dev_dbg(sport->port.dev, "exit in %s.\n", __func__);
494 return;
495 }
0bbc9b81
JW
496
497 spin_lock_irqsave(&sport->port.lock, flags);
498 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
499 imx_dma_tx(sport);
500 spin_unlock_irqrestore(&sport->port.lock, flags);
b4cdc8f6
HS
501}
502
7cb92fd2 503static void imx_dma_tx(struct imx_port *sport)
b4cdc8f6 504{
b4cdc8f6
HS
505 struct circ_buf *xmit = &sport->port.state->xmit;
506 struct scatterlist *sgl = sport->tx_sgl;
507 struct dma_async_tx_descriptor *desc;
508 struct dma_chan *chan = sport->dma_chan_tx;
509 struct device *dev = sport->port.dev;
a2c718ce 510 unsigned long temp;
b4cdc8f6
HS
511 int ret;
512
42f752b3 513 if (sport->dma_is_txing)
b4cdc8f6
HS
514 return;
515
b4cdc8f6 516 sport->tx_bytes = uart_circ_chars_pending(xmit);
b4cdc8f6 517
7942f857
DB
518 if (xmit->tail < xmit->head) {
519 sport->dma_tx_nents = 1;
520 sg_init_one(sgl, xmit->buf + xmit->tail, sport->tx_bytes);
521 } else {
b4cdc8f6
HS
522 sport->dma_tx_nents = 2;
523 sg_init_table(sgl, 2);
524 sg_set_buf(sgl, xmit->buf + xmit->tail,
525 UART_XMIT_SIZE - xmit->tail);
526 sg_set_buf(sgl + 1, xmit->buf, xmit->head);
b4cdc8f6 527 }
b4cdc8f6
HS
528
529 ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
530 if (ret == 0) {
531 dev_err(dev, "DMA mapping error for TX.\n");
532 return;
533 }
534 desc = dmaengine_prep_slave_sg(chan, sgl, sport->dma_tx_nents,
535 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
536 if (!desc) {
24649821
DB
537 dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
538 DMA_TO_DEVICE);
b4cdc8f6
HS
539 dev_err(dev, "We cannot prepare for the TX slave dma!\n");
540 return;
541 }
542 desc->callback = dma_tx_callback;
543 desc->callback_param = sport;
544
545 dev_dbg(dev, "TX: prepare to send %lu bytes by DMA.\n",
546 uart_circ_chars_pending(xmit));
a2c718ce
DB
547
548 temp = readl(sport->port.membase + UCR1);
549 temp |= UCR1_TDMAEN;
550 writel(temp, sport->port.membase + UCR1);
551
b4cdc8f6
HS
552 /* fire it */
553 sport->dma_is_txing = 1;
554 dmaengine_submit(desc);
555 dma_async_issue_pending(chan);
556 return;
557}
558
1da177e4
LT
559/*
560 * interrupts disabled on entry
561 */
b129a8cc 562static void imx_start_tx(struct uart_port *port)
1da177e4
LT
563{
564 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21 565 unsigned long temp;
1da177e4 566
17b8f2a3 567 if (port->rs485.flags & SER_RS485_ENABLED) {
17b8f2a3
UKK
568 temp = readl(port->membase + UCR2);
569 if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
58362d5b 570 imx_port_rts_inactive(sport, &temp);
17b8f2a3 571 else
58362d5b 572 imx_port_rts_active(sport, &temp);
7d1cadca
BS
573 if (!(port->rs485.flags & SER_RS485_RX_DURING_TX))
574 temp &= ~UCR2_RXEN;
17b8f2a3
UKK
575 writel(temp, port->membase + UCR2);
576
58362d5b 577 /* enable transmitter and shifter empty irq */
17b8f2a3
UKK
578 temp = readl(port->membase + UCR4);
579 temp |= UCR4_TCEN;
580 writel(temp, port->membase + UCR4);
581 }
582
b4cdc8f6
HS
583 if (!sport->dma_is_enabled) {
584 temp = readl(sport->port.membase + UCR1);
585 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
586 }
1da177e4 587
b4cdc8f6 588 if (sport->dma_is_enabled) {
91a1a909
JW
589 if (sport->port.x_char) {
590 /* We have X-char to send, so enable TX IRQ and
591 * disable TX DMA to let TX interrupt to send X-char */
592 temp = readl(sport->port.membase + UCR1);
593 temp &= ~UCR1_TDMAEN;
594 temp |= UCR1_TXMPTYEN;
595 writel(temp, sport->port.membase + UCR1);
596 return;
597 }
598
5e42e9a3
PH
599 if (!uart_circ_empty(&port->state->xmit) &&
600 !uart_tx_stopped(port))
601 imx_dma_tx(sport);
b4cdc8f6
HS
602 return;
603 }
1da177e4
LT
604}
605
7d12e780 606static irqreturn_t imx_rtsint(int irq, void *dev_id)
ceca629e 607{
15aafa2f 608 struct imx_port *sport = dev_id;
5680e941 609 unsigned int val;
ceca629e
SH
610 unsigned long flags;
611
612 spin_lock_irqsave(&sport->port.lock, flags);
613
ff4bfb21 614 writel(USR1_RTSD, sport->port.membase + USR1);
5680e941 615 val = readl(sport->port.membase + USR1) & USR1_RTSS;
ceca629e 616 uart_handle_cts_change(&sport->port, !!val);
bdc04e31 617 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
ceca629e
SH
618
619 spin_unlock_irqrestore(&sport->port.lock, flags);
620 return IRQ_HANDLED;
621}
622
7d12e780 623static irqreturn_t imx_txint(int irq, void *dev_id)
1da177e4 624{
15aafa2f 625 struct imx_port *sport = dev_id;
1da177e4
LT
626 unsigned long flags;
627
82313e66 628 spin_lock_irqsave(&sport->port.lock, flags);
1da177e4 629 imx_transmit_buffer(sport);
82313e66 630 spin_unlock_irqrestore(&sport->port.lock, flags);
1da177e4
LT
631 return IRQ_HANDLED;
632}
633
7d12e780 634static irqreturn_t imx_rxint(int irq, void *dev_id)
1da177e4
LT
635{
636 struct imx_port *sport = dev_id;
82313e66 637 unsigned int rx, flg, ignored = 0;
92a19f9c 638 struct tty_port *port = &sport->port.state->port;
ff4bfb21 639 unsigned long flags, temp;
1da177e4 640
82313e66 641 spin_lock_irqsave(&sport->port.lock, flags);
1da177e4 642
0d3c3938 643 while (readl(sport->port.membase + USR2) & USR2_RDR) {
1da177e4
LT
644 flg = TTY_NORMAL;
645 sport->port.icount.rx++;
646
0d3c3938
SH
647 rx = readl(sport->port.membase + URXD0);
648
ff4bfb21 649 temp = readl(sport->port.membase + USR2);
864eeed0 650 if (temp & USR2_BRCD) {
94d32f99 651 writel(USR2_BRCD, sport->port.membase + USR2);
864eeed0
SH
652 if (uart_handle_break(&sport->port))
653 continue;
1da177e4
LT
654 }
655
d3810cd4 656 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
864eeed0
SH
657 continue;
658
019dc9ea
HW
659 if (unlikely(rx & URXD_ERR)) {
660 if (rx & URXD_BRK)
661 sport->port.icount.brk++;
662 else if (rx & URXD_PRERR)
864eeed0
SH
663 sport->port.icount.parity++;
664 else if (rx & URXD_FRMERR)
665 sport->port.icount.frame++;
666 if (rx & URXD_OVRRUN)
667 sport->port.icount.overrun++;
668
669 if (rx & sport->port.ignore_status_mask) {
670 if (++ignored > 100)
671 goto out;
672 continue;
673 }
674
8d267fd9 675 rx &= (sport->port.read_status_mask | 0xFF);
864eeed0 676
019dc9ea
HW
677 if (rx & URXD_BRK)
678 flg = TTY_BREAK;
679 else if (rx & URXD_PRERR)
864eeed0
SH
680 flg = TTY_PARITY;
681 else if (rx & URXD_FRMERR)
682 flg = TTY_FRAME;
683 if (rx & URXD_OVRRUN)
684 flg = TTY_OVERRUN;
1da177e4 685
864eeed0
SH
686#ifdef SUPPORT_SYSRQ
687 sport->port.sysrq = 0;
688#endif
689 }
1da177e4 690
55d8693a
JW
691 if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
692 goto out;
693
9b289932
MS
694 if (tty_insert_flip_char(port, rx, flg) == 0)
695 sport->port.icount.buf_overrun++;
864eeed0 696 }
1da177e4
LT
697
698out:
82313e66 699 spin_unlock_irqrestore(&sport->port.lock, flags);
2e124b4a 700 tty_flip_buffer_push(port);
1da177e4 701 return IRQ_HANDLED;
1da177e4
LT
702}
703
7cb92fd2 704static int start_rx_dma(struct imx_port *sport);
b4cdc8f6
HS
705/*
706 * If the RXFIFO is filled with some data, and then we
707 * arise a DMA operation to receive them.
708 */
709static void imx_dma_rxint(struct imx_port *sport)
710{
711 unsigned long temp;
73631813
JW
712 unsigned long flags;
713
714 spin_lock_irqsave(&sport->port.lock, flags);
b4cdc8f6
HS
715
716 temp = readl(sport->port.membase + USR2);
717 if ((temp & USR2_RDR) && !sport->dma_is_rxing) {
718 sport->dma_is_rxing = 1;
719
86a04ba6 720 /* disable the receiver ready and aging timer interrupts */
b4cdc8f6
HS
721 temp = readl(sport->port.membase + UCR1);
722 temp &= ~(UCR1_RRDYEN);
723 writel(temp, sport->port.membase + UCR1);
724
86a04ba6
LS
725 temp = readl(sport->port.membase + UCR2);
726 temp &= ~(UCR2_ATEN);
727 writel(temp, sport->port.membase + UCR2);
728
b4cdc8f6 729 /* tell the DMA to receive the data. */
7cb92fd2 730 start_rx_dma(sport);
b4cdc8f6 731 }
73631813
JW
732
733 spin_unlock_irqrestore(&sport->port.lock, flags);
b4cdc8f6
HS
734}
735
66f95884
UKK
736/*
737 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
738 */
739static unsigned int imx_get_hwmctrl(struct imx_port *sport)
740{
741 unsigned int tmp = TIOCM_DSR;
742 unsigned usr1 = readl(sport->port.membase + USR1);
743
744 if (usr1 & USR1_RTSS)
745 tmp |= TIOCM_CTS;
746
747 /* in DCE mode DCDIN is always 0 */
748 if (!(usr1 & USR2_DCDIN))
749 tmp |= TIOCM_CAR;
750
751 if (sport->dte_mode)
752 if (!(readl(sport->port.membase + USR2) & USR2_RIIN))
753 tmp |= TIOCM_RI;
754
755 return tmp;
756}
757
758/*
759 * Handle any change of modem status signal since we were last called.
760 */
761static void imx_mctrl_check(struct imx_port *sport)
762{
763 unsigned int status, changed;
764
765 status = imx_get_hwmctrl(sport);
766 changed = status ^ sport->old_status;
767
768 if (changed == 0)
769 return;
770
771 sport->old_status = status;
772
773 if (changed & TIOCM_RI && status & TIOCM_RI)
774 sport->port.icount.rng++;
775 if (changed & TIOCM_DSR)
776 sport->port.icount.dsr++;
777 if (changed & TIOCM_CAR)
778 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
779 if (changed & TIOCM_CTS)
780 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
781
782 wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
783}
784
e3d13ff4
SH
785static irqreturn_t imx_int(int irq, void *dev_id)
786{
787 struct imx_port *sport = dev_id;
788 unsigned int sts;
f1f836e4 789 unsigned int sts2;
4d845a62 790 irqreturn_t ret = IRQ_NONE;
e3d13ff4
SH
791
792 sts = readl(sport->port.membase + USR1);
17b8f2a3 793 sts2 = readl(sport->port.membase + USR2);
e3d13ff4 794
86a04ba6 795 if (sts & (USR1_RRDY | USR1_AGTIM)) {
b4cdc8f6
HS
796 if (sport->dma_is_enabled)
797 imx_dma_rxint(sport);
798 else
799 imx_rxint(irq, dev_id);
4d845a62 800 ret = IRQ_HANDLED;
b4cdc8f6 801 }
e3d13ff4 802
17b8f2a3
UKK
803 if ((sts & USR1_TRDY &&
804 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN) ||
805 (sts2 & USR2_TXDC &&
4d845a62 806 readl(sport->port.membase + UCR4) & UCR4_TCEN)) {
e3d13ff4 807 imx_txint(irq, dev_id);
4d845a62
UKK
808 ret = IRQ_HANDLED;
809 }
e3d13ff4 810
27e16501
UKK
811 if (sts & USR1_DTRD) {
812 unsigned long flags;
813
814 if (sts & USR1_DTRD)
815 writel(USR1_DTRD, sport->port.membase + USR1);
816
817 spin_lock_irqsave(&sport->port.lock, flags);
818 imx_mctrl_check(sport);
819 spin_unlock_irqrestore(&sport->port.lock, flags);
820
821 ret = IRQ_HANDLED;
822 }
823
4d845a62 824 if (sts & USR1_RTSD) {
e3d13ff4 825 imx_rtsint(irq, dev_id);
4d845a62
UKK
826 ret = IRQ_HANDLED;
827 }
e3d13ff4 828
4d845a62 829 if (sts & USR1_AWAKE) {
db1a9b55 830 writel(USR1_AWAKE, sport->port.membase + USR1);
4d845a62
UKK
831 ret = IRQ_HANDLED;
832 }
db1a9b55 833
f1f836e4 834 if (sts2 & USR2_ORE) {
f1f836e4 835 sport->port.icount.overrun++;
91555ce9 836 writel(USR2_ORE, sport->port.membase + USR2);
4d845a62 837 ret = IRQ_HANDLED;
f1f836e4
AS
838 }
839
4d845a62 840 return ret;
e3d13ff4
SH
841}
842
1da177e4
LT
843/*
844 * Return TIOCSER_TEMT when transmitter is not busy.
845 */
846static unsigned int imx_tx_empty(struct uart_port *port)
847{
848 struct imx_port *sport = (struct imx_port *)port;
1ce43e58 849 unsigned int ret;
1da177e4 850
1ce43e58 851 ret = (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
1da177e4 852
1ce43e58
HS
853 /* If the TX DMA is working, return 0. */
854 if (sport->dma_is_enabled && sport->dma_is_txing)
855 ret = 0;
856
857 return ret;
1da177e4
LT
858}
859
58362d5b
UKK
860static unsigned int imx_get_mctrl(struct uart_port *port)
861{
862 struct imx_port *sport = (struct imx_port *)port;
863 unsigned int ret = imx_get_hwmctrl(sport);
864
865 mctrl_gpio_get(sport->gpios, &ret);
866
867 return ret;
868}
869
1da177e4
LT
870static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
871{
d3810cd4 872 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21
SH
873 unsigned long temp;
874
17b8f2a3
UKK
875 if (!(port->rs485.flags & SER_RS485_ENABLED)) {
876 temp = readl(sport->port.membase + UCR2);
877 temp &= ~(UCR2_CTS | UCR2_CTSC);
878 if (mctrl & TIOCM_RTS)
879 temp |= UCR2_CTS | UCR2_CTSC;
880 writel(temp, sport->port.membase + UCR2);
881 }
6b471a98 882
90ebc483
UKK
883 temp = readl(sport->port.membase + UCR3) & ~UCR3_DSR;
884 if (!(mctrl & TIOCM_DTR))
885 temp |= UCR3_DSR;
886 writel(temp, sport->port.membase + UCR3);
887
6b471a98
HS
888 temp = readl(sport->port.membase + uts_reg(sport)) & ~UTS_LOOP;
889 if (mctrl & TIOCM_LOOP)
890 temp |= UTS_LOOP;
891 writel(temp, sport->port.membase + uts_reg(sport));
58362d5b
UKK
892
893 mctrl_gpio_set(sport->gpios, mctrl);
1da177e4
LT
894}
895
896/*
897 * Interrupts always disabled.
898 */
899static void imx_break_ctl(struct uart_port *port, int break_state)
900{
901 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21 902 unsigned long flags, temp;
1da177e4
LT
903
904 spin_lock_irqsave(&sport->port.lock, flags);
905
ff4bfb21
SH
906 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
907
82313e66 908 if (break_state != 0)
ff4bfb21
SH
909 temp |= UCR1_SNDBRK;
910
911 writel(temp, sport->port.membase + UCR1);
1da177e4
LT
912
913 spin_unlock_irqrestore(&sport->port.lock, flags);
914}
915
cc568849
UKK
916/*
917 * This is our per-port timeout handler, for checking the
918 * modem status signals.
919 */
920static void imx_timeout(unsigned long data)
921{
922 struct imx_port *sport = (struct imx_port *)data;
923 unsigned long flags;
924
925 if (sport->port.state) {
926 spin_lock_irqsave(&sport->port.lock, flags);
927 imx_mctrl_check(sport);
928 spin_unlock_irqrestore(&sport->port.lock, flags);
929
930 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
931 }
932}
933
b4cdc8f6 934#define RX_BUF_SIZE (PAGE_SIZE)
b4cdc8f6
HS
935static void imx_rx_dma_done(struct imx_port *sport)
936{
937 unsigned long temp;
73631813
JW
938 unsigned long flags;
939
940 spin_lock_irqsave(&sport->port.lock, flags);
b4cdc8f6 941
86a04ba6 942 /* re-enable interrupts to get notified when new symbols are incoming */
b4cdc8f6
HS
943 temp = readl(sport->port.membase + UCR1);
944 temp |= UCR1_RRDYEN;
945 writel(temp, sport->port.membase + UCR1);
946
86a04ba6
LS
947 temp = readl(sport->port.membase + UCR2);
948 temp |= UCR2_ATEN;
949 writel(temp, sport->port.membase + UCR2);
950
b4cdc8f6 951 sport->dma_is_rxing = 0;
9ce4f8f3
GKH
952
953 /* Is the shutdown waiting for us? */
954 if (waitqueue_active(&sport->dma_wait))
955 wake_up(&sport->dma_wait);
73631813
JW
956
957 spin_unlock_irqrestore(&sport->port.lock, flags);
b4cdc8f6
HS
958}
959
960/*
905c0dec 961 * There are two kinds of RX DMA interrupts(such as in the MX6Q):
b4cdc8f6 962 * [1] the RX DMA buffer is full.
905c0dec 963 * [2] the aging timer expires
b4cdc8f6 964 *
905c0dec
LS
965 * Condition [2] is triggered when a character has been sitting in the FIFO
966 * for at least 8 byte durations.
b4cdc8f6
HS
967 */
968static void dma_rx_callback(void *data)
969{
970 struct imx_port *sport = data;
971 struct dma_chan *chan = sport->dma_chan_rx;
972 struct scatterlist *sgl = &sport->rx_sgl;
7cb92fd2 973 struct tty_port *port = &sport->port.state->port;
b4cdc8f6
HS
974 struct dma_tx_state state;
975 enum dma_status status;
976 unsigned int count;
977
978 /* unmap it first */
979 dma_unmap_sg(sport->port.dev, sgl, 1, DMA_FROM_DEVICE);
980
f0ef8834 981 status = dmaengine_tx_status(chan, (dma_cookie_t)0, &state);
b4cdc8f6 982 count = RX_BUF_SIZE - state.residue;
392bceed 983
b4cdc8f6
HS
984 dev_dbg(sport->port.dev, "We get %d bytes.\n", count);
985
986 if (count) {
9b289932
MS
987 if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
988 int bytes = tty_insert_flip_string(port, sport->rx_buf,
989 count);
990
991 if (bytes != count)
992 sport->port.icount.buf_overrun++;
993 }
7cb92fd2 994 tty_flip_buffer_push(port);
abc7882a 995 sport->port.icount.rx += count;
976b39cd 996 }
7cb92fd2 997
976b39cd
LS
998 /*
999 * Restart RX DMA directly if more data is available in order to skip
1000 * the roundtrip through the IRQ handler. If there is some data already
1001 * in the FIFO, DMA needs to be restarted soon anyways.
1002 *
1003 * Otherwise stop the DMA and reactivate FIFO IRQs to restart DMA once
1004 * data starts to arrive again.
1005 */
1006 if (readl(sport->port.membase + USR2) & USR2_RDR)
7cb92fd2 1007 start_rx_dma(sport);
976b39cd 1008 else
b4cdc8f6
HS
1009 imx_rx_dma_done(sport);
1010}
1011
1012static int start_rx_dma(struct imx_port *sport)
1013{
1014 struct scatterlist *sgl = &sport->rx_sgl;
1015 struct dma_chan *chan = sport->dma_chan_rx;
1016 struct device *dev = sport->port.dev;
1017 struct dma_async_tx_descriptor *desc;
1018 int ret;
1019
1020 sg_init_one(sgl, sport->rx_buf, RX_BUF_SIZE);
1021 ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
1022 if (ret == 0) {
1023 dev_err(dev, "DMA mapping error for RX.\n");
1024 return -EINVAL;
1025 }
1026 desc = dmaengine_prep_slave_sg(chan, sgl, 1, DMA_DEV_TO_MEM,
1027 DMA_PREP_INTERRUPT);
1028 if (!desc) {
24649821 1029 dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
b4cdc8f6
HS
1030 dev_err(dev, "We cannot prepare for the RX slave dma!\n");
1031 return -EINVAL;
1032 }
1033 desc->callback = dma_rx_callback;
1034 desc->callback_param = sport;
1035
1036 dev_dbg(dev, "RX: prepare for the DMA.\n");
1037 dmaengine_submit(desc);
1038 dma_async_issue_pending(chan);
1039 return 0;
1040}
1041
cc32382d
LS
1042#define TXTL_DEFAULT 2 /* reset default */
1043#define RXTL_DEFAULT 1 /* reset default */
184bd70b
LS
1044#define TXTL_DMA 8 /* DMA burst setting */
1045#define RXTL_DMA 9 /* DMA burst setting */
cc32382d
LS
1046
1047static void imx_setup_ufcr(struct imx_port *sport,
1048 unsigned char txwl, unsigned char rxwl)
1049{
1050 unsigned int val;
1051
1052 /* set receiver / transmitter trigger level */
1053 val = readl(sport->port.membase + UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
1054 val |= txwl << UFCR_TXTL_SHF | rxwl;
1055 writel(val, sport->port.membase + UFCR);
1056}
1057
b4cdc8f6
HS
1058static void imx_uart_dma_exit(struct imx_port *sport)
1059{
1060 if (sport->dma_chan_rx) {
1061 dma_release_channel(sport->dma_chan_rx);
1062 sport->dma_chan_rx = NULL;
1063
1064 kfree(sport->rx_buf);
1065 sport->rx_buf = NULL;
1066 }
1067
1068 if (sport->dma_chan_tx) {
1069 dma_release_channel(sport->dma_chan_tx);
1070 sport->dma_chan_tx = NULL;
1071 }
1072
1073 sport->dma_is_inited = 0;
1074}
1075
1076static int imx_uart_dma_init(struct imx_port *sport)
1077{
b09c74ae 1078 struct dma_slave_config slave_config = {};
b4cdc8f6
HS
1079 struct device *dev = sport->port.dev;
1080 int ret;
1081
1082 /* Prepare for RX : */
1083 sport->dma_chan_rx = dma_request_slave_channel(dev, "rx");
1084 if (!sport->dma_chan_rx) {
1085 dev_dbg(dev, "cannot get the DMA channel.\n");
1086 ret = -EINVAL;
1087 goto err;
1088 }
1089
1090 slave_config.direction = DMA_DEV_TO_MEM;
1091 slave_config.src_addr = sport->port.mapbase + URXD0;
1092 slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
184bd70b
LS
1093 /* one byte less than the watermark level to enable the aging timer */
1094 slave_config.src_maxburst = RXTL_DMA - 1;
b4cdc8f6
HS
1095 ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
1096 if (ret) {
1097 dev_err(dev, "error in RX dma configuration.\n");
1098 goto err;
1099 }
1100
1101 sport->rx_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1102 if (!sport->rx_buf) {
b4cdc8f6
HS
1103 ret = -ENOMEM;
1104 goto err;
1105 }
b4cdc8f6
HS
1106
1107 /* Prepare for TX : */
1108 sport->dma_chan_tx = dma_request_slave_channel(dev, "tx");
1109 if (!sport->dma_chan_tx) {
1110 dev_err(dev, "cannot get the TX DMA channel!\n");
1111 ret = -EINVAL;
1112 goto err;
1113 }
1114
1115 slave_config.direction = DMA_MEM_TO_DEV;
1116 slave_config.dst_addr = sport->port.mapbase + URTX0;
1117 slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
184bd70b 1118 slave_config.dst_maxburst = TXTL_DMA;
b4cdc8f6
HS
1119 ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
1120 if (ret) {
1121 dev_err(dev, "error in TX dma configuration.");
1122 goto err;
1123 }
1124
1125 sport->dma_is_inited = 1;
1126
1127 return 0;
1128err:
1129 imx_uart_dma_exit(sport);
1130 return ret;
1131}
1132
1133static void imx_enable_dma(struct imx_port *sport)
1134{
1135 unsigned long temp;
b4cdc8f6 1136
9ce4f8f3
GKH
1137 init_waitqueue_head(&sport->dma_wait);
1138
b4cdc8f6
HS
1139 /* set UCR1 */
1140 temp = readl(sport->port.membase + UCR1);
905c0dec 1141 temp |= UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN;
b4cdc8f6
HS
1142 writel(temp, sport->port.membase + UCR1);
1143
86a04ba6
LS
1144 temp = readl(sport->port.membase + UCR2);
1145 temp |= UCR2_ATEN;
1146 writel(temp, sport->port.membase + UCR2);
1147
184bd70b
LS
1148 imx_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
1149
b4cdc8f6
HS
1150 sport->dma_is_enabled = 1;
1151}
1152
1153static void imx_disable_dma(struct imx_port *sport)
1154{
1155 unsigned long temp;
b4cdc8f6
HS
1156
1157 /* clear UCR1 */
1158 temp = readl(sport->port.membase + UCR1);
1159 temp &= ~(UCR1_RDMAEN | UCR1_TDMAEN | UCR1_ATDMAEN);
1160 writel(temp, sport->port.membase + UCR1);
1161
1162 /* clear UCR2 */
1163 temp = readl(sport->port.membase + UCR2);
86a04ba6 1164 temp &= ~(UCR2_CTSC | UCR2_CTS | UCR2_ATEN);
b4cdc8f6
HS
1165 writel(temp, sport->port.membase + UCR2);
1166
184bd70b
LS
1167 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1168
b4cdc8f6 1169 sport->dma_is_enabled = 0;
b4cdc8f6
HS
1170}
1171
1c5250d6
VL
1172/* half the RX buffer size */
1173#define CTSTL 16
1174
1da177e4
LT
1175static int imx_startup(struct uart_port *port)
1176{
1177 struct imx_port *sport = (struct imx_port *)port;
458e2c82 1178 int retval, i;
ff4bfb21 1179 unsigned long flags, temp;
1da177e4 1180
1cf93e0d
HS
1181 retval = clk_prepare_enable(sport->clk_per);
1182 if (retval)
cb0f0a5f 1183 return retval;
1cf93e0d
HS
1184 retval = clk_prepare_enable(sport->clk_ipg);
1185 if (retval) {
1186 clk_disable_unprepare(sport->clk_per);
cb0f0a5f 1187 return retval;
0c375501 1188 }
28eb4274 1189
cc32382d 1190 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
1da177e4
LT
1191
1192 /* disable the DREN bit (Data Ready interrupt enable) before
1193 * requesting IRQs
1194 */
ff4bfb21 1195 temp = readl(sport->port.membase + UCR4);
b6e49138 1196
1c5250d6 1197 /* set the trigger level for CTS */
82313e66
SK
1198 temp &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
1199 temp |= CTSTL << UCR4_CTSTL_SHF;
1c5250d6 1200
ff4bfb21 1201 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
1da177e4 1202
7e11577e
LS
1203 /* Can we enable the DMA support? */
1204 if (is_imx6q_uart(sport) && !uart_console(port) &&
1205 !sport->dma_is_inited)
1206 imx_uart_dma_init(sport);
1207
53794183 1208 spin_lock_irqsave(&sport->port.lock, flags);
772f8991 1209 /* Reset fifo's and state machines */
458e2c82
FE
1210 i = 100;
1211
1212 temp = readl(sport->port.membase + UCR2);
1213 temp &= ~UCR2_SRST;
1214 writel(temp, sport->port.membase + UCR2);
1215
1216 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
1217 udelay(1);
b6e49138 1218
1da177e4
LT
1219 /*
1220 * Finally, clear and enable interrupts
1221 */
27e16501 1222 writel(USR1_RTSD | USR1_DTRD, sport->port.membase + USR1);
91555ce9 1223 writel(USR2_ORE, sport->port.membase + USR2);
ff4bfb21 1224
7e11577e
LS
1225 if (sport->dma_is_inited && !sport->dma_is_enabled)
1226 imx_enable_dma(sport);
1227
ff4bfb21 1228 temp = readl(sport->port.membase + UCR1);
789d5258 1229 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
b6e49138 1230
ff4bfb21 1231 writel(temp, sport->port.membase + UCR1);
1da177e4 1232
6f026d6b
JW
1233 temp = readl(sport->port.membase + UCR4);
1234 temp |= UCR4_OREN;
1235 writel(temp, sport->port.membase + UCR4);
1236
ff4bfb21
SH
1237 temp = readl(sport->port.membase + UCR2);
1238 temp |= (UCR2_RXEN | UCR2_TXEN);
bff09b09
LS
1239 if (!sport->have_rtscts)
1240 temp |= UCR2_IRTS;
16804d68
UKK
1241 /*
1242 * make sure the edge sensitive RTS-irq is disabled,
1243 * we're using RTSD instead.
1244 */
1245 if (!is_imx1_uart(sport))
1246 temp &= ~UCR2_RTSEN;
ff4bfb21 1247 writel(temp, sport->port.membase + UCR2);
1da177e4 1248
a496e628 1249 if (!is_imx1_uart(sport)) {
37d6fb62 1250 temp = readl(sport->port.membase + UCR3);
16804d68
UKK
1251
1252 /*
1253 * The effect of RI and DCD differs depending on the UFCR_DCEDTE
1254 * bit. In DCE mode they control the outputs, in DTE mode they
1255 * enable the respective irqs. At least the DCD irq cannot be
1256 * cleared on i.MX25 at least, so it's not usable and must be
1257 * disabled. I don't have test hardware to check if RI has the
1258 * same problem but I consider this likely so it's disabled for
1259 * now, too.
1260 */
1261 temp |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP |
27e16501 1262 UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
16804d68
UKK
1263
1264 if (sport->dte_mode)
1265 temp &= ~(UCR3_RI | UCR3_DCD);
1266
37d6fb62
SH
1267 writel(temp, sport->port.membase + UCR3);
1268 }
4411805b 1269
1da177e4
LT
1270 /*
1271 * Enable modem status interrupts
1272 */
1da177e4 1273 imx_enable_ms(&sport->port);
82313e66 1274 spin_unlock_irqrestore(&sport->port.lock, flags);
1da177e4
LT
1275
1276 return 0;
1da177e4
LT
1277}
1278
1279static void imx_shutdown(struct uart_port *port)
1280{
1281 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21 1282 unsigned long temp;
9ec1882d 1283 unsigned long flags;
1da177e4 1284
b4cdc8f6 1285 if (sport->dma_is_enabled) {
a4688bcd
HS
1286 int ret;
1287
9ce4f8f3 1288 /* We have to wait for the DMA to finish. */
a4688bcd 1289 ret = wait_event_interruptible(sport->dma_wait,
9ce4f8f3 1290 !sport->dma_is_rxing && !sport->dma_is_txing);
a4688bcd
HS
1291 if (ret != 0) {
1292 sport->dma_is_rxing = 0;
1293 sport->dma_is_txing = 0;
1294 dmaengine_terminate_all(sport->dma_chan_tx);
1295 dmaengine_terminate_all(sport->dma_chan_rx);
1296 }
73631813 1297 spin_lock_irqsave(&sport->port.lock, flags);
a4688bcd 1298 imx_stop_tx(port);
b4cdc8f6
HS
1299 imx_stop_rx(port);
1300 imx_disable_dma(sport);
73631813 1301 spin_unlock_irqrestore(&sport->port.lock, flags);
b4cdc8f6
HS
1302 imx_uart_dma_exit(sport);
1303 }
1304
58362d5b
UKK
1305 mctrl_gpio_disable_ms(sport->gpios);
1306
9ec1882d 1307 spin_lock_irqsave(&sport->port.lock, flags);
2e146392
FG
1308 temp = readl(sport->port.membase + UCR2);
1309 temp &= ~(UCR2_TXEN);
1310 writel(temp, sport->port.membase + UCR2);
9ec1882d 1311 spin_unlock_irqrestore(&sport->port.lock, flags);
2e146392 1312
1da177e4
LT
1313 /*
1314 * Stop our timer.
1315 */
1316 del_timer_sync(&sport->timer);
1317
1da177e4
LT
1318 /*
1319 * Disable all interrupts, port and break condition.
1320 */
1321
9ec1882d 1322 spin_lock_irqsave(&sport->port.lock, flags);
ff4bfb21
SH
1323 temp = readl(sport->port.membase + UCR1);
1324 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
b6e49138 1325
ff4bfb21 1326 writel(temp, sport->port.membase + UCR1);
9ec1882d 1327 spin_unlock_irqrestore(&sport->port.lock, flags);
28eb4274 1328
1cf93e0d
HS
1329 clk_disable_unprepare(sport->clk_per);
1330 clk_disable_unprepare(sport->clk_ipg);
1da177e4
LT
1331}
1332
eb56b7ed
HS
1333static void imx_flush_buffer(struct uart_port *port)
1334{
1335 struct imx_port *sport = (struct imx_port *)port;
82e86ae9 1336 struct scatterlist *sgl = &sport->tx_sgl[0];
a2c718ce 1337 unsigned long temp;
4f86a95d 1338 int i = 100, ubir, ubmr, uts;
eb56b7ed 1339
82e86ae9
DB
1340 if (!sport->dma_chan_tx)
1341 return;
1342
1343 sport->tx_bytes = 0;
1344 dmaengine_terminate_all(sport->dma_chan_tx);
1345 if (sport->dma_is_txing) {
1346 dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
1347 DMA_TO_DEVICE);
a2c718ce
DB
1348 temp = readl(sport->port.membase + UCR1);
1349 temp &= ~UCR1_TDMAEN;
1350 writel(temp, sport->port.membase + UCR1);
82e86ae9 1351 sport->dma_is_txing = false;
eb56b7ed 1352 }
934084a9
FE
1353
1354 /*
1355 * According to the Reference Manual description of the UART SRST bit:
1356 * "Reset the transmit and receive state machines,
1357 * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
1358 * and UTS[6-3]". As we don't need to restore the old values from
1359 * USR1, USR2, URXD, UTXD, only save/restore the other four registers
1360 */
1361 ubir = readl(sport->port.membase + UBIR);
1362 ubmr = readl(sport->port.membase + UBMR);
934084a9
FE
1363 uts = readl(sport->port.membase + IMX21_UTS);
1364
1365 temp = readl(sport->port.membase + UCR2);
1366 temp &= ~UCR2_SRST;
1367 writel(temp, sport->port.membase + UCR2);
1368
1369 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) && (--i > 0))
1370 udelay(1);
1371
1372 /* Restore the registers */
1373 writel(ubir, sport->port.membase + UBIR);
1374 writel(ubmr, sport->port.membase + UBMR);
934084a9 1375 writel(uts, sport->port.membase + IMX21_UTS);
eb56b7ed
HS
1376}
1377
1da177e4 1378static void
606d099c
AC
1379imx_set_termios(struct uart_port *port, struct ktermios *termios,
1380 struct ktermios *old)
1da177e4
LT
1381{
1382 struct imx_port *sport = (struct imx_port *)port;
1383 unsigned long flags;
58362d5b
UKK
1384 unsigned long ucr2, old_ucr1, old_ucr2;
1385 unsigned int baud, quot;
1da177e4 1386 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
58362d5b 1387 unsigned long div, ufcr;
534fca06 1388 unsigned long num, denom;
d7f8d437 1389 uint64_t tdiv64;
1da177e4 1390
1da177e4
LT
1391 /*
1392 * We only support CS7 and CS8.
1393 */
1394 while ((termios->c_cflag & CSIZE) != CS7 &&
1395 (termios->c_cflag & CSIZE) != CS8) {
1396 termios->c_cflag &= ~CSIZE;
1397 termios->c_cflag |= old_csize;
1398 old_csize = CS8;
1399 }
1400
1401 if ((termios->c_cflag & CSIZE) == CS8)
1402 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
1403 else
1404 ucr2 = UCR2_SRST | UCR2_IRTS;
1405
1406 if (termios->c_cflag & CRTSCTS) {
82313e66 1407 if (sport->have_rtscts) {
5b802344 1408 ucr2 &= ~UCR2_IRTS;
17b8f2a3 1409
12fe59f9 1410 if (port->rs485.flags & SER_RS485_ENABLED) {
17b8f2a3
UKK
1411 /*
1412 * RTS is mandatory for rs485 operation, so keep
1413 * it under manual control and keep transmitter
1414 * disabled.
1415 */
58362d5b
UKK
1416 if (port->rs485.flags &
1417 SER_RS485_RTS_AFTER_SEND)
1418 imx_port_rts_inactive(sport, &ucr2);
1419 else
1420 imx_port_rts_active(sport, &ucr2);
12fe59f9 1421 } else {
58362d5b 1422 imx_port_rts_auto(sport, &ucr2);
12fe59f9 1423 }
5b802344
SH
1424 } else {
1425 termios->c_cflag &= ~CRTSCTS;
1426 }
58362d5b 1427 } else if (port->rs485.flags & SER_RS485_ENABLED) {
17b8f2a3 1428 /* disable transmitter */
58362d5b
UKK
1429 if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1430 imx_port_rts_inactive(sport, &ucr2);
1431 else
1432 imx_port_rts_active(sport, &ucr2);
1433 }
1434
1da177e4
LT
1435
1436 if (termios->c_cflag & CSTOPB)
1437 ucr2 |= UCR2_STPB;
1438 if (termios->c_cflag & PARENB) {
1439 ucr2 |= UCR2_PREN;
3261e362 1440 if (termios->c_cflag & PARODD)
1da177e4
LT
1441 ucr2 |= UCR2_PROE;
1442 }
1443
995234da
EM
1444 del_timer_sync(&sport->timer);
1445
1da177e4
LT
1446 /*
1447 * Ask the core to calculate the divisor for us.
1448 */
036bb15e 1449 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
1da177e4
LT
1450 quot = uart_get_divisor(port, baud);
1451
1452 spin_lock_irqsave(&sport->port.lock, flags);
1453
1454 sport->port.read_status_mask = 0;
1455 if (termios->c_iflag & INPCK)
1456 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
1457 if (termios->c_iflag & (BRKINT | PARMRK))
1458 sport->port.read_status_mask |= URXD_BRK;
1459
1460 /*
1461 * Characters to ignore
1462 */
1463 sport->port.ignore_status_mask = 0;
1464 if (termios->c_iflag & IGNPAR)
865cea85 1465 sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
1da177e4
LT
1466 if (termios->c_iflag & IGNBRK) {
1467 sport->port.ignore_status_mask |= URXD_BRK;
1468 /*
1469 * If we're ignoring parity and break indicators,
1470 * ignore overruns too (for real raw support).
1471 */
1472 if (termios->c_iflag & IGNPAR)
1473 sport->port.ignore_status_mask |= URXD_OVRRUN;
1474 }
1475
55d8693a
JW
1476 if ((termios->c_cflag & CREAD) == 0)
1477 sport->port.ignore_status_mask |= URXD_DUMMY_READ;
1478
1da177e4
LT
1479 /*
1480 * Update the per-port timeout.
1481 */
1482 uart_update_timeout(port, termios->c_cflag, baud);
1483
1484 /*
1485 * disable interrupts and drain transmitter
1486 */
ff4bfb21
SH
1487 old_ucr1 = readl(sport->port.membase + UCR1);
1488 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
1489 sport->port.membase + UCR1);
1da177e4 1490
82313e66 1491 while (!(readl(sport->port.membase + USR2) & USR2_TXDC))
1da177e4
LT
1492 barrier();
1493
1494 /* then, disable everything */
86a04ba6
LS
1495 old_ucr2 = readl(sport->port.membase + UCR2);
1496 writel(old_ucr2 & ~(UCR2_TXEN | UCR2_RXEN),
ff4bfb21 1497 sport->port.membase + UCR2);
86a04ba6 1498 old_ucr2 &= (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN);
1da177e4 1499
afe9cbb1
UKK
1500 /* custom-baudrate handling */
1501 div = sport->port.uartclk / (baud * 16);
1502 if (baud == 38400 && quot != div)
1503 baud = sport->port.uartclk / (quot * 16);
1504
1505 div = sport->port.uartclk / (baud * 16);
1506 if (div > 7)
1507 div = 7;
1508 if (!div)
036bb15e
SH
1509 div = 1;
1510
534fca06
OS
1511 rational_best_approximation(16 * div * baud, sport->port.uartclk,
1512 1 << 16, 1 << 16, &num, &denom);
036bb15e 1513
eab4f5af
AC
1514 tdiv64 = sport->port.uartclk;
1515 tdiv64 *= num;
1516 do_div(tdiv64, denom * 16 * div);
1517 tty_termios_encode_baud_rate(termios,
1a2c4b31 1518 (speed_t)tdiv64, (speed_t)tdiv64);
d7f8d437 1519
534fca06
OS
1520 num -= 1;
1521 denom -= 1;
036bb15e
SH
1522
1523 ufcr = readl(sport->port.membase + UFCR);
b6e49138 1524 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
20ff2fe6
HS
1525 if (sport->dte_mode)
1526 ufcr |= UFCR_DCEDTE;
036bb15e
SH
1527 writel(ufcr, sport->port.membase + UFCR);
1528
534fca06
OS
1529 writel(num, sport->port.membase + UBIR);
1530 writel(denom, sport->port.membase + UBMR);
1531
a496e628 1532 if (!is_imx1_uart(sport))
37d6fb62 1533 writel(sport->port.uartclk / div / 1000,
fe6b540a 1534 sport->port.membase + IMX21_ONEMS);
ff4bfb21
SH
1535
1536 writel(old_ucr1, sport->port.membase + UCR1);
1da177e4 1537
ff4bfb21 1538 /* set the parity, stop bits and data size */
86a04ba6 1539 writel(ucr2 | old_ucr2, sport->port.membase + UCR2);
1da177e4
LT
1540
1541 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
1542 imx_enable_ms(&sport->port);
1543
1544 spin_unlock_irqrestore(&sport->port.lock, flags);
1545}
1546
1547static const char *imx_type(struct uart_port *port)
1548{
1549 struct imx_port *sport = (struct imx_port *)port;
1550
1551 return sport->port.type == PORT_IMX ? "IMX" : NULL;
1552}
1553
1da177e4
LT
1554/*
1555 * Configure/autoconfigure the port.
1556 */
1557static void imx_config_port(struct uart_port *port, int flags)
1558{
1559 struct imx_port *sport = (struct imx_port *)port;
1560
da82f997 1561 if (flags & UART_CONFIG_TYPE)
1da177e4
LT
1562 sport->port.type = PORT_IMX;
1563}
1564
1565/*
1566 * Verify the new serial_struct (for TIOCSSERIAL).
1567 * The only change we allow are to the flags and type, and
1568 * even then only between PORT_IMX and PORT_UNKNOWN
1569 */
1570static int
1571imx_verify_port(struct uart_port *port, struct serial_struct *ser)
1572{
1573 struct imx_port *sport = (struct imx_port *)port;
1574 int ret = 0;
1575
1576 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1577 ret = -EINVAL;
1578 if (sport->port.irq != ser->irq)
1579 ret = -EINVAL;
1580 if (ser->io_type != UPIO_MEM)
1581 ret = -EINVAL;
1582 if (sport->port.uartclk / 16 != ser->baud_base)
1583 ret = -EINVAL;
a50c44ce 1584 if (sport->port.mapbase != (unsigned long)ser->iomem_base)
1da177e4
LT
1585 ret = -EINVAL;
1586 if (sport->port.iobase != ser->port)
1587 ret = -EINVAL;
1588 if (ser->hub6 != 0)
1589 ret = -EINVAL;
1590 return ret;
1591}
1592
01f56abd 1593#if defined(CONFIG_CONSOLE_POLL)
6b8bdad9
DT
1594
1595static int imx_poll_init(struct uart_port *port)
1596{
1597 struct imx_port *sport = (struct imx_port *)port;
1598 unsigned long flags;
1599 unsigned long temp;
1600 int retval;
1601
1602 retval = clk_prepare_enable(sport->clk_ipg);
1603 if (retval)
1604 return retval;
1605 retval = clk_prepare_enable(sport->clk_per);
1606 if (retval)
1607 clk_disable_unprepare(sport->clk_ipg);
1608
cc32382d 1609 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
6b8bdad9
DT
1610
1611 spin_lock_irqsave(&sport->port.lock, flags);
1612
1613 temp = readl(sport->port.membase + UCR1);
1614 if (is_imx1_uart(sport))
1615 temp |= IMX1_UCR1_UARTCLKEN;
1616 temp |= UCR1_UARTEN | UCR1_RRDYEN;
1617 temp &= ~(UCR1_TXMPTYEN | UCR1_RTSDEN);
1618 writel(temp, sport->port.membase + UCR1);
1619
1620 temp = readl(sport->port.membase + UCR2);
1621 temp |= UCR2_RXEN;
1622 writel(temp, sport->port.membase + UCR2);
1623
1624 spin_unlock_irqrestore(&sport->port.lock, flags);
1625
1626 return 0;
1627}
1628
01f56abd
SA
1629static int imx_poll_get_char(struct uart_port *port)
1630{
f968ef34 1631 if (!(readl_relaxed(port->membase + USR2) & USR2_RDR))
26c47412 1632 return NO_POLL_CHAR;
01f56abd 1633
f968ef34 1634 return readl_relaxed(port->membase + URXD0) & URXD_RX_DATA;
01f56abd
SA
1635}
1636
1637static void imx_poll_put_char(struct uart_port *port, unsigned char c)
1638{
01f56abd
SA
1639 unsigned int status;
1640
01f56abd
SA
1641 /* drain */
1642 do {
f968ef34 1643 status = readl_relaxed(port->membase + USR1);
01f56abd
SA
1644 } while (~status & USR1_TRDY);
1645
1646 /* write */
f968ef34 1647 writel_relaxed(c, port->membase + URTX0);
01f56abd
SA
1648
1649 /* flush */
1650 do {
f968ef34 1651 status = readl_relaxed(port->membase + USR2);
01f56abd 1652 } while (~status & USR2_TXDC);
01f56abd
SA
1653}
1654#endif
1655
17b8f2a3
UKK
1656static int imx_rs485_config(struct uart_port *port,
1657 struct serial_rs485 *rs485conf)
1658{
1659 struct imx_port *sport = (struct imx_port *)port;
7d1cadca 1660 unsigned long temp;
17b8f2a3
UKK
1661
1662 /* unimplemented */
1663 rs485conf->delay_rts_before_send = 0;
1664 rs485conf->delay_rts_after_send = 0;
17b8f2a3
UKK
1665
1666 /* RTS is required to control the transmitter */
1667 if (!sport->have_rtscts)
1668 rs485conf->flags &= ~SER_RS485_ENABLED;
1669
1670 if (rs485conf->flags & SER_RS485_ENABLED) {
17b8f2a3
UKK
1671 /* disable transmitter */
1672 temp = readl(sport->port.membase + UCR2);
17b8f2a3 1673 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
58362d5b 1674 imx_port_rts_inactive(sport, &temp);
17b8f2a3 1675 else
58362d5b 1676 imx_port_rts_active(sport, &temp);
17b8f2a3
UKK
1677 writel(temp, sport->port.membase + UCR2);
1678 }
1679
7d1cadca
BS
1680 /* Make sure Rx is enabled in case Tx is active with Rx disabled */
1681 if (!(rs485conf->flags & SER_RS485_ENABLED) ||
1682 rs485conf->flags & SER_RS485_RX_DURING_TX) {
1683 temp = readl(sport->port.membase + UCR2);
1684 temp |= UCR2_RXEN;
1685 writel(temp, sport->port.membase + UCR2);
1686 }
1687
17b8f2a3
UKK
1688 port->rs485 = *rs485conf;
1689
1690 return 0;
1691}
1692
1da177e4
LT
1693static struct uart_ops imx_pops = {
1694 .tx_empty = imx_tx_empty,
1695 .set_mctrl = imx_set_mctrl,
1696 .get_mctrl = imx_get_mctrl,
1697 .stop_tx = imx_stop_tx,
1698 .start_tx = imx_start_tx,
1699 .stop_rx = imx_stop_rx,
1700 .enable_ms = imx_enable_ms,
1701 .break_ctl = imx_break_ctl,
1702 .startup = imx_startup,
1703 .shutdown = imx_shutdown,
eb56b7ed 1704 .flush_buffer = imx_flush_buffer,
1da177e4
LT
1705 .set_termios = imx_set_termios,
1706 .type = imx_type,
1da177e4
LT
1707 .config_port = imx_config_port,
1708 .verify_port = imx_verify_port,
01f56abd 1709#if defined(CONFIG_CONSOLE_POLL)
6b8bdad9 1710 .poll_init = imx_poll_init,
01f56abd
SA
1711 .poll_get_char = imx_poll_get_char,
1712 .poll_put_char = imx_poll_put_char,
1713#endif
1da177e4
LT
1714};
1715
dbff4e9e 1716static struct imx_port *imx_ports[UART_NR];
1da177e4
LT
1717
1718#ifdef CONFIG_SERIAL_IMX_CONSOLE
d358788f
RK
1719static void imx_console_putchar(struct uart_port *port, int ch)
1720{
1721 struct imx_port *sport = (struct imx_port *)port;
ff4bfb21 1722
fe6b540a 1723 while (readl(sport->port.membase + uts_reg(sport)) & UTS_TXFULL)
d358788f 1724 barrier();
ff4bfb21
SH
1725
1726 writel(ch, sport->port.membase + URTX0);
d358788f 1727}
1da177e4
LT
1728
1729/*
1730 * Interrupts are disabled on entering
1731 */
1732static void
1733imx_console_write(struct console *co, const char *s, unsigned int count)
1734{
dbff4e9e 1735 struct imx_port *sport = imx_ports[co->index];
0ad5a814
DB
1736 struct imx_port_ucrs old_ucr;
1737 unsigned int ucr1;
f30e8260 1738 unsigned long flags = 0;
677fe555 1739 int locked = 1;
1cf93e0d
HS
1740 int retval;
1741
0c727a42 1742 retval = clk_enable(sport->clk_per);
1cf93e0d
HS
1743 if (retval)
1744 return;
0c727a42 1745 retval = clk_enable(sport->clk_ipg);
1cf93e0d 1746 if (retval) {
0c727a42 1747 clk_disable(sport->clk_per);
1cf93e0d
HS
1748 return;
1749 }
9ec1882d 1750
677fe555
TG
1751 if (sport->port.sysrq)
1752 locked = 0;
1753 else if (oops_in_progress)
1754 locked = spin_trylock_irqsave(&sport->port.lock, flags);
1755 else
1756 spin_lock_irqsave(&sport->port.lock, flags);
1da177e4
LT
1757
1758 /*
0ad5a814 1759 * First, save UCR1/2/3 and then disable interrupts
1da177e4 1760 */
0ad5a814
DB
1761 imx_port_ucrs_save(&sport->port, &old_ucr);
1762 ucr1 = old_ucr.ucr1;
1da177e4 1763
fe6b540a
SG
1764 if (is_imx1_uart(sport))
1765 ucr1 |= IMX1_UCR1_UARTCLKEN;
37d6fb62
SH
1766 ucr1 |= UCR1_UARTEN;
1767 ucr1 &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
1768
1769 writel(ucr1, sport->port.membase + UCR1);
ff4bfb21 1770
0ad5a814 1771 writel(old_ucr.ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1da177e4 1772
d358788f 1773 uart_console_write(&sport->port, s, count, imx_console_putchar);
1da177e4
LT
1774
1775 /*
1776 * Finally, wait for transmitter to become empty
0ad5a814 1777 * and restore UCR1/2/3
1da177e4 1778 */
ff4bfb21 1779 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1da177e4 1780
0ad5a814 1781 imx_port_ucrs_restore(&sport->port, &old_ucr);
9ec1882d 1782
677fe555
TG
1783 if (locked)
1784 spin_unlock_irqrestore(&sport->port.lock, flags);
1cf93e0d 1785
0c727a42
FE
1786 clk_disable(sport->clk_ipg);
1787 clk_disable(sport->clk_per);
1da177e4
LT
1788}
1789
1790/*
1791 * If the port was already initialised (eg, by a boot loader),
1792 * try to determine the current setup.
1793 */
1794static void __init
1795imx_console_get_options(struct imx_port *sport, int *baud,
1796 int *parity, int *bits)
1797{
587897f5 1798
2e2eb509 1799 if (readl(sport->port.membase + UCR1) & UCR1_UARTEN) {
1da177e4 1800 /* ok, the port was enabled */
82313e66 1801 unsigned int ucr2, ubir, ubmr, uartclk;
587897f5
SH
1802 unsigned int baud_raw;
1803 unsigned int ucfr_rfdiv;
1da177e4 1804
ff4bfb21 1805 ucr2 = readl(sport->port.membase + UCR2);
1da177e4
LT
1806
1807 *parity = 'n';
1808 if (ucr2 & UCR2_PREN) {
1809 if (ucr2 & UCR2_PROE)
1810 *parity = 'o';
1811 else
1812 *parity = 'e';
1813 }
1814
1815 if (ucr2 & UCR2_WS)
1816 *bits = 8;
1817 else
1818 *bits = 7;
1819
ff4bfb21
SH
1820 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1821 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
587897f5 1822
ff4bfb21 1823 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
587897f5
SH
1824 if (ucfr_rfdiv == 6)
1825 ucfr_rfdiv = 7;
1826 else
1827 ucfr_rfdiv = 6 - ucfr_rfdiv;
1828
3a9465fa 1829 uartclk = clk_get_rate(sport->clk_per);
587897f5
SH
1830 uartclk /= ucfr_rfdiv;
1831
1832 { /*
1833 * The next code provides exact computation of
1834 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1835 * without need of float support or long long division,
1836 * which would be required to prevent 32bit arithmetic overflow
1837 */
1838 unsigned int mul = ubir + 1;
1839 unsigned int div = 16 * (ubmr + 1);
1840 unsigned int rem = uartclk % div;
1841
1842 baud_raw = (uartclk / div) * mul;
1843 baud_raw += (rem * mul + div / 2) / div;
1844 *baud = (baud_raw + 50) / 100 * 100;
1845 }
1846
82313e66 1847 if (*baud != baud_raw)
50bbdba3 1848 pr_info("Console IMX rounded baud rate from %d to %d\n",
587897f5 1849 baud_raw, *baud);
1da177e4
LT
1850 }
1851}
1852
1853static int __init
1854imx_console_setup(struct console *co, char *options)
1855{
1856 struct imx_port *sport;
1857 int baud = 9600;
1858 int bits = 8;
1859 int parity = 'n';
1860 int flow = 'n';
1cf93e0d 1861 int retval;
1da177e4
LT
1862
1863 /*
1864 * Check whether an invalid uart number has been specified, and
1865 * if so, search for the first available port that does have
1866 * console support.
1867 */
1868 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1869 co->index = 0;
dbff4e9e 1870 sport = imx_ports[co->index];
82313e66 1871 if (sport == NULL)
e76afc4e 1872 return -ENODEV;
1da177e4 1873
1cf93e0d
HS
1874 /* For setting the registers, we only need to enable the ipg clock. */
1875 retval = clk_prepare_enable(sport->clk_ipg);
1876 if (retval)
1877 goto error_console;
1878
1da177e4
LT
1879 if (options)
1880 uart_parse_options(options, &baud, &parity, &bits, &flow);
1881 else
1882 imx_console_get_options(sport, &baud, &parity, &bits);
1883
cc32382d 1884 imx_setup_ufcr(sport, TXTL_DEFAULT, RXTL_DEFAULT);
587897f5 1885
1cf93e0d
HS
1886 retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
1887
0c727a42
FE
1888 clk_disable(sport->clk_ipg);
1889 if (retval) {
1890 clk_unprepare(sport->clk_ipg);
1891 goto error_console;
1892 }
1893
1894 retval = clk_prepare(sport->clk_per);
1895 if (retval)
1896 clk_disable_unprepare(sport->clk_ipg);
1cf93e0d
HS
1897
1898error_console:
1899 return retval;
1da177e4
LT
1900}
1901
9f4426dd 1902static struct uart_driver imx_reg;
1da177e4 1903static struct console imx_console = {
e3d13ff4 1904 .name = DEV_NAME,
1da177e4
LT
1905 .write = imx_console_write,
1906 .device = uart_console_device,
1907 .setup = imx_console_setup,
1908 .flags = CON_PRINTBUFFER,
1909 .index = -1,
1910 .data = &imx_reg,
1911};
1912
1da177e4 1913#define IMX_CONSOLE &imx_console
913c6c0e
LS
1914
1915#ifdef CONFIG_OF
1916static void imx_console_early_putchar(struct uart_port *port, int ch)
1917{
1918 while (readl_relaxed(port->membase + IMX21_UTS) & UTS_TXFULL)
1919 cpu_relax();
1920
1921 writel_relaxed(ch, port->membase + URTX0);
1922}
1923
1924static void imx_console_early_write(struct console *con, const char *s,
1925 unsigned count)
1926{
1927 struct earlycon_device *dev = con->data;
1928
1929 uart_console_write(&dev->port, s, count, imx_console_early_putchar);
1930}
1931
1932static int __init
1933imx_console_early_setup(struct earlycon_device *dev, const char *opt)
1934{
1935 if (!dev->port.membase)
1936 return -ENODEV;
1937
1938 dev->con->write = imx_console_early_write;
1939
1940 return 0;
1941}
1942OF_EARLYCON_DECLARE(ec_imx6q, "fsl,imx6q-uart", imx_console_early_setup);
1943OF_EARLYCON_DECLARE(ec_imx21, "fsl,imx21-uart", imx_console_early_setup);
1944#endif
1945
1da177e4
LT
1946#else
1947#define IMX_CONSOLE NULL
1948#endif
1949
1950static struct uart_driver imx_reg = {
1951 .owner = THIS_MODULE,
1952 .driver_name = DRIVER_NAME,
e3d13ff4 1953 .dev_name = DEV_NAME,
1da177e4
LT
1954 .major = SERIAL_IMX_MAJOR,
1955 .minor = MINOR_START,
1956 .nr = ARRAY_SIZE(imx_ports),
1957 .cons = IMX_CONSOLE,
1958};
1959
22698aa2 1960#ifdef CONFIG_OF
20bb8095
UKK
1961/*
1962 * This function returns 1 iff pdev isn't a device instatiated by dt, 0 iff it
1963 * could successfully get all information from dt or a negative errno.
1964 */
22698aa2
SG
1965static int serial_imx_probe_dt(struct imx_port *sport,
1966 struct platform_device *pdev)
1967{
1968 struct device_node *np = pdev->dev.of_node;
ff05967a 1969 int ret;
22698aa2 1970
5f8b9043
LC
1971 sport->devdata = of_device_get_match_data(&pdev->dev);
1972 if (!sport->devdata)
20bb8095
UKK
1973 /* no device tree device */
1974 return 1;
22698aa2 1975
ff05967a
SG
1976 ret = of_alias_get_id(np, "serial");
1977 if (ret < 0) {
1978 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
a197a191 1979 return ret;
ff05967a
SG
1980 }
1981 sport->port.line = ret;
22698aa2 1982
1006ed7e
GU
1983 if (of_get_property(np, "uart-has-rtscts", NULL) ||
1984 of_get_property(np, "fsl,uart-has-rtscts", NULL) /* deprecated */)
22698aa2
SG
1985 sport->have_rtscts = 1;
1986
20ff2fe6
HS
1987 if (of_get_property(np, "fsl,dte-mode", NULL))
1988 sport->dte_mode = 1;
1989
22698aa2
SG
1990 return 0;
1991}
1992#else
1993static inline int serial_imx_probe_dt(struct imx_port *sport,
1994 struct platform_device *pdev)
1995{
20bb8095 1996 return 1;
22698aa2
SG
1997}
1998#endif
1999
2000static void serial_imx_probe_pdata(struct imx_port *sport,
2001 struct platform_device *pdev)
2002{
574de559 2003 struct imxuart_platform_data *pdata = dev_get_platdata(&pdev->dev);
22698aa2
SG
2004
2005 sport->port.line = pdev->id;
2006 sport->devdata = (struct imx_uart_data *) pdev->id_entry->driver_data;
2007
2008 if (!pdata)
2009 return;
2010
2011 if (pdata->flags & IMXUART_HAVE_RTSCTS)
2012 sport->have_rtscts = 1;
22698aa2
SG
2013}
2014
2582d8c1 2015static int serial_imx_probe(struct platform_device *pdev)
1da177e4 2016{
dbff4e9e 2017 struct imx_port *sport;
dbff4e9e 2018 void __iomem *base;
8a61f0c7 2019 int ret = 0, reg;
dbff4e9e 2020 struct resource *res;
842633bd 2021 int txirq, rxirq, rtsirq;
dbff4e9e 2022
42d34191 2023 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
dbff4e9e
SH
2024 if (!sport)
2025 return -ENOMEM;
5b802344 2026
22698aa2 2027 ret = serial_imx_probe_dt(sport, pdev);
20bb8095 2028 if (ret > 0)
22698aa2 2029 serial_imx_probe_pdata(sport, pdev);
20bb8095 2030 else if (ret < 0)
42d34191 2031 return ret;
22698aa2 2032
dbff4e9e 2033 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
da82f997
AS
2034 base = devm_ioremap_resource(&pdev->dev, res);
2035 if (IS_ERR(base))
2036 return PTR_ERR(base);
dbff4e9e 2037
842633bd
UKK
2038 rxirq = platform_get_irq(pdev, 0);
2039 txirq = platform_get_irq(pdev, 1);
2040 rtsirq = platform_get_irq(pdev, 2);
2041
dbff4e9e
SH
2042 sport->port.dev = &pdev->dev;
2043 sport->port.mapbase = res->start;
2044 sport->port.membase = base;
2045 sport->port.type = PORT_IMX,
2046 sport->port.iotype = UPIO_MEM;
842633bd 2047 sport->port.irq = rxirq;
dbff4e9e
SH
2048 sport->port.fifosize = 32;
2049 sport->port.ops = &imx_pops;
17b8f2a3
UKK
2050 sport->port.rs485_config = imx_rs485_config;
2051 sport->port.rs485.flags =
2052 SER_RS485_RTS_ON_SEND | SER_RS485_RX_DURING_TX;
dbff4e9e 2053 sport->port.flags = UPF_BOOT_AUTOCONF;
dbff4e9e
SH
2054 init_timer(&sport->timer);
2055 sport->timer.function = imx_timeout;
2056 sport->timer.data = (unsigned long)sport;
38a41fdf 2057
58362d5b
UKK
2058 sport->gpios = mctrl_gpio_init(&sport->port, 0);
2059 if (IS_ERR(sport->gpios))
2060 return PTR_ERR(sport->gpios);
2061
3a9465fa
SH
2062 sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
2063 if (IS_ERR(sport->clk_ipg)) {
2064 ret = PTR_ERR(sport->clk_ipg);
833462e9 2065 dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
42d34191 2066 return ret;
38a41fdf 2067 }
38a41fdf 2068
3a9465fa
SH
2069 sport->clk_per = devm_clk_get(&pdev->dev, "per");
2070 if (IS_ERR(sport->clk_per)) {
2071 ret = PTR_ERR(sport->clk_per);
833462e9 2072 dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
42d34191 2073 return ret;
3a9465fa
SH
2074 }
2075
3a9465fa 2076 sport->port.uartclk = clk_get_rate(sport->clk_per);
dbff4e9e 2077
8a61f0c7
FE
2078 /* For register access, we only need to enable the ipg clock. */
2079 ret = clk_prepare_enable(sport->clk_ipg);
2080 if (ret)
2081 return ret;
2082
2083 /* Disable interrupts before requesting them */
2084 reg = readl_relaxed(sport->port.membase + UCR1);
2085 reg &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN |
2086 UCR1_TXMPTYEN | UCR1_RTSDEN);
2087 writel_relaxed(reg, sport->port.membase + UCR1);
2088
2089 clk_disable_unprepare(sport->clk_ipg);
2090
c0d1c6b0
FE
2091 /*
2092 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
2093 * chips only have one interrupt.
2094 */
842633bd
UKK
2095 if (txirq > 0) {
2096 ret = devm_request_irq(&pdev->dev, rxirq, imx_rxint, 0,
c0d1c6b0
FE
2097 dev_name(&pdev->dev), sport);
2098 if (ret)
2099 return ret;
2100
842633bd 2101 ret = devm_request_irq(&pdev->dev, txirq, imx_txint, 0,
c0d1c6b0
FE
2102 dev_name(&pdev->dev), sport);
2103 if (ret)
2104 return ret;
c0d1c6b0 2105 } else {
842633bd 2106 ret = devm_request_irq(&pdev->dev, rxirq, imx_int, 0,
c0d1c6b0
FE
2107 dev_name(&pdev->dev), sport);
2108 if (ret)
2109 return ret;
2110 }
2111
22698aa2 2112 imx_ports[sport->port.line] = sport;
5b802344 2113
0a86a86b 2114 platform_set_drvdata(pdev, sport);
5b802344 2115
45af780a 2116 return uart_add_one_port(&imx_reg, &sport->port);
1da177e4
LT
2117}
2118
2582d8c1 2119static int serial_imx_remove(struct platform_device *pdev)
1da177e4 2120{
2582d8c1 2121 struct imx_port *sport = platform_get_drvdata(pdev);
1da177e4 2122
45af780a 2123 return uart_remove_one_port(&imx_reg, &sport->port);
1da177e4
LT
2124}
2125
c868cbb7
EV
2126static void serial_imx_restore_context(struct imx_port *sport)
2127{
2128 if (!sport->context_saved)
2129 return;
2130
2131 writel(sport->saved_reg[4], sport->port.membase + UFCR);
2132 writel(sport->saved_reg[5], sport->port.membase + UESC);
2133 writel(sport->saved_reg[6], sport->port.membase + UTIM);
2134 writel(sport->saved_reg[7], sport->port.membase + UBIR);
2135 writel(sport->saved_reg[8], sport->port.membase + UBMR);
2136 writel(sport->saved_reg[9], sport->port.membase + IMX21_UTS);
2137 writel(sport->saved_reg[0], sport->port.membase + UCR1);
2138 writel(sport->saved_reg[1] | UCR2_SRST, sport->port.membase + UCR2);
2139 writel(sport->saved_reg[2], sport->port.membase + UCR3);
2140 writel(sport->saved_reg[3], sport->port.membase + UCR4);
2141 sport->context_saved = false;
2142}
2143
2144static void serial_imx_save_context(struct imx_port *sport)
2145{
2146 /* Save necessary regs */
2147 sport->saved_reg[0] = readl(sport->port.membase + UCR1);
2148 sport->saved_reg[1] = readl(sport->port.membase + UCR2);
2149 sport->saved_reg[2] = readl(sport->port.membase + UCR3);
2150 sport->saved_reg[3] = readl(sport->port.membase + UCR4);
2151 sport->saved_reg[4] = readl(sport->port.membase + UFCR);
2152 sport->saved_reg[5] = readl(sport->port.membase + UESC);
2153 sport->saved_reg[6] = readl(sport->port.membase + UTIM);
2154 sport->saved_reg[7] = readl(sport->port.membase + UBIR);
2155 sport->saved_reg[8] = readl(sport->port.membase + UBMR);
2156 sport->saved_reg[9] = readl(sport->port.membase + IMX21_UTS);
2157 sport->context_saved = true;
2158}
2159
189550b8
EV
2160static void serial_imx_enable_wakeup(struct imx_port *sport, bool on)
2161{
2162 unsigned int val;
2163
2164 val = readl(sport->port.membase + UCR3);
2165 if (on)
2166 val |= UCR3_AWAKEN;
2167 else
2168 val &= ~UCR3_AWAKEN;
2169 writel(val, sport->port.membase + UCR3);
bc85734b
EV
2170
2171 val = readl(sport->port.membase + UCR1);
2172 if (on)
2173 val |= UCR1_RTSDEN;
2174 else
2175 val &= ~UCR1_RTSDEN;
2176 writel(val, sport->port.membase + UCR1);
189550b8
EV
2177}
2178
90bb6bd3
SW
2179static int imx_serial_port_suspend_noirq(struct device *dev)
2180{
2181 struct platform_device *pdev = to_platform_device(dev);
2182 struct imx_port *sport = platform_get_drvdata(pdev);
2183 int ret;
2184
2185 ret = clk_enable(sport->clk_ipg);
2186 if (ret)
2187 return ret;
2188
c868cbb7 2189 serial_imx_save_context(sport);
90bb6bd3
SW
2190
2191 clk_disable(sport->clk_ipg);
2192
2193 return 0;
2194}
2195
2196static int imx_serial_port_resume_noirq(struct device *dev)
2197{
2198 struct platform_device *pdev = to_platform_device(dev);
2199 struct imx_port *sport = platform_get_drvdata(pdev);
2200 int ret;
2201
2202 ret = clk_enable(sport->clk_ipg);
2203 if (ret)
2204 return ret;
2205
c868cbb7 2206 serial_imx_restore_context(sport);
90bb6bd3
SW
2207
2208 clk_disable(sport->clk_ipg);
2209
2210 return 0;
2211}
2212
2213static int imx_serial_port_suspend(struct device *dev)
2214{
2215 struct platform_device *pdev = to_platform_device(dev);
2216 struct imx_port *sport = platform_get_drvdata(pdev);
90bb6bd3
SW
2217
2218 /* enable wakeup from i.MX UART */
189550b8 2219 serial_imx_enable_wakeup(sport, true);
90bb6bd3
SW
2220
2221 uart_suspend_port(&imx_reg, &sport->port);
2222
29add68d
MF
2223 /* Needed to enable clock in suspend_noirq */
2224 return clk_prepare(sport->clk_ipg);
90bb6bd3
SW
2225}
2226
2227static int imx_serial_port_resume(struct device *dev)
2228{
2229 struct platform_device *pdev = to_platform_device(dev);
2230 struct imx_port *sport = platform_get_drvdata(pdev);
90bb6bd3
SW
2231
2232 /* disable wakeup from i.MX UART */
189550b8 2233 serial_imx_enable_wakeup(sport, false);
90bb6bd3
SW
2234
2235 uart_resume_port(&imx_reg, &sport->port);
2236
29add68d
MF
2237 clk_unprepare(sport->clk_ipg);
2238
90bb6bd3
SW
2239 return 0;
2240}
2241
2242static const struct dev_pm_ops imx_serial_port_pm_ops = {
2243 .suspend_noirq = imx_serial_port_suspend_noirq,
2244 .resume_noirq = imx_serial_port_resume_noirq,
2245 .suspend = imx_serial_port_suspend,
2246 .resume = imx_serial_port_resume,
2247};
2248
3ae5eaec 2249static struct platform_driver serial_imx_driver = {
d3810cd4
OS
2250 .probe = serial_imx_probe,
2251 .remove = serial_imx_remove,
1da177e4 2252
fe6b540a 2253 .id_table = imx_uart_devtype,
3ae5eaec 2254 .driver = {
d3810cd4 2255 .name = "imx-uart",
22698aa2 2256 .of_match_table = imx_uart_dt_ids,
90bb6bd3 2257 .pm = &imx_serial_port_pm_ops,
3ae5eaec 2258 },
1da177e4
LT
2259};
2260
2261static int __init imx_serial_init(void)
2262{
f0fd1b73 2263 int ret = uart_register_driver(&imx_reg);
1da177e4 2264
1da177e4
LT
2265 if (ret)
2266 return ret;
2267
3ae5eaec 2268 ret = platform_driver_register(&serial_imx_driver);
1da177e4
LT
2269 if (ret != 0)
2270 uart_unregister_driver(&imx_reg);
2271
f227824e 2272 return ret;
1da177e4
LT
2273}
2274
2275static void __exit imx_serial_exit(void)
2276{
c889b896 2277 platform_driver_unregister(&serial_imx_driver);
4b300c36 2278 uart_unregister_driver(&imx_reg);
1da177e4
LT
2279}
2280
2281module_init(imx_serial_init);
2282module_exit(imx_serial_exit);
2283
2284MODULE_AUTHOR("Sascha Hauer");
2285MODULE_DESCRIPTION("IMX generic serial port driver");
2286MODULE_LICENSE("GPL");
e169c139 2287MODULE_ALIAS("platform:imx-uart");
This page took 1.472601 seconds and 5 git commands to generate.