blob: 386b528940560396158fdd17673ada88a7163589 [file] [log] [blame]
Sascha Hauer47d37d62011-01-11 15:54:54 +01001/*
2 * Freescale STMP37XX/STMP378X Application UART driver
3 *
4 * Author: dmitry pervushin <dimka@embeddedalley.com>
5 *
6 * Copyright 2008-2010 Freescale Semiconductor, Inc.
7 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
8 *
9 * The code contained herein is licensed under the GNU General Public
10 * License. You may obtain a copy of the GNU General Public License
11 * Version 2 or later at the following locations:
12 *
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
15 */
16
Janusz Uzycki914d3b12014-10-10 13:13:28 +020017#if defined(CONFIG_SERIAL_MXS_AUART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
18#define SUPPORT_SYSRQ
19#endif
20
Sascha Hauer47d37d62011-01-11 15:54:54 +010021#include <linux/kernel.h>
Sascha Hauer47d37d62011-01-11 15:54:54 +010022#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/console.h>
25#include <linux/interrupt.h>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/wait.h>
29#include <linux/tty.h>
30#include <linux/tty_driver.h>
31#include <linux/tty_flip.h>
32#include <linux/serial.h>
33#include <linux/serial_core.h>
34#include <linux/platform_device.h>
35#include <linux/device.h>
36#include <linux/clk.h>
37#include <linux/delay.h>
38#include <linux/io.h>
Fabio Estevam1ea66072012-06-18 10:06:09 -030039#include <linux/of_device.h>
Huang Shijiee8001632012-11-16 16:03:53 +080040#include <linux/dma-mapping.h>
Shawn Guobcc20f92013-02-26 13:47:41 +080041#include <linux/dmaengine.h>
Sascha Hauer47d37d62011-01-11 15:54:54 +010042
43#include <asm/cacheflush.h>
44
Janusz Uzyckif9e42392014-10-10 18:53:26 +020045#include <linux/gpio.h>
46#include <linux/gpio/consumer.h>
Janusz Uzycki7c573d72014-10-10 18:53:25 +020047#include <linux/err.h>
Janusz Uzyckif9e42392014-10-10 18:53:26 +020048#include <linux/irq.h>
Janusz Uzycki7c573d72014-10-10 18:53:25 +020049#include "serial_mctrl_gpio.h"
50
Sascha Hauer47d37d62011-01-11 15:54:54 +010051#define MXS_AUART_PORTS 5
Hector Palacios9987f762013-10-03 09:32:03 +020052#define MXS_AUART_FIFO_SIZE 16
Sascha Hauer47d37d62011-01-11 15:54:54 +010053
54#define AUART_CTRL0 0x00000000
55#define AUART_CTRL0_SET 0x00000004
56#define AUART_CTRL0_CLR 0x00000008
57#define AUART_CTRL0_TOG 0x0000000c
58#define AUART_CTRL1 0x00000010
59#define AUART_CTRL1_SET 0x00000014
60#define AUART_CTRL1_CLR 0x00000018
61#define AUART_CTRL1_TOG 0x0000001c
62#define AUART_CTRL2 0x00000020
63#define AUART_CTRL2_SET 0x00000024
64#define AUART_CTRL2_CLR 0x00000028
65#define AUART_CTRL2_TOG 0x0000002c
66#define AUART_LINECTRL 0x00000030
67#define AUART_LINECTRL_SET 0x00000034
68#define AUART_LINECTRL_CLR 0x00000038
69#define AUART_LINECTRL_TOG 0x0000003c
70#define AUART_LINECTRL2 0x00000040
71#define AUART_LINECTRL2_SET 0x00000044
72#define AUART_LINECTRL2_CLR 0x00000048
73#define AUART_LINECTRL2_TOG 0x0000004c
74#define AUART_INTR 0x00000050
75#define AUART_INTR_SET 0x00000054
76#define AUART_INTR_CLR 0x00000058
77#define AUART_INTR_TOG 0x0000005c
78#define AUART_DATA 0x00000060
79#define AUART_STAT 0x00000070
80#define AUART_DEBUG 0x00000080
81#define AUART_VERSION 0x00000090
82#define AUART_AUTOBAUD 0x000000a0
83
84#define AUART_CTRL0_SFTRST (1 << 31)
85#define AUART_CTRL0_CLKGATE (1 << 30)
Huang Shijiee8001632012-11-16 16:03:53 +080086#define AUART_CTRL0_RXTO_ENABLE (1 << 27)
87#define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
88#define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
89
90#define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
91
92#define AUART_CTRL2_DMAONERR (1 << 26)
93#define AUART_CTRL2_TXDMAE (1 << 25)
94#define AUART_CTRL2_RXDMAE (1 << 24)
Sascha Hauer47d37d62011-01-11 15:54:54 +010095
96#define AUART_CTRL2_CTSEN (1 << 15)
Huang Shijie00592022012-08-08 10:37:59 +080097#define AUART_CTRL2_RTSEN (1 << 14)
Sascha Hauer47d37d62011-01-11 15:54:54 +010098#define AUART_CTRL2_RTS (1 << 11)
99#define AUART_CTRL2_RXE (1 << 9)
100#define AUART_CTRL2_TXE (1 << 8)
101#define AUART_CTRL2_UARTEN (1 << 0)
102
103#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
104#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
105#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
106#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
107#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
108#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
109#define AUART_LINECTRL_WLEN_MASK 0x00000060
110#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
111#define AUART_LINECTRL_FEN (1 << 4)
112#define AUART_LINECTRL_STP2 (1 << 3)
113#define AUART_LINECTRL_EPS (1 << 2)
114#define AUART_LINECTRL_PEN (1 << 1)
115#define AUART_LINECTRL_BRK (1 << 0)
116
117#define AUART_INTR_RTIEN (1 << 22)
118#define AUART_INTR_TXIEN (1 << 21)
119#define AUART_INTR_RXIEN (1 << 20)
120#define AUART_INTR_CTSMIEN (1 << 17)
121#define AUART_INTR_RTIS (1 << 6)
122#define AUART_INTR_TXIS (1 << 5)
123#define AUART_INTR_RXIS (1 << 4)
124#define AUART_INTR_CTSMIS (1 << 1)
125
126#define AUART_STAT_BUSY (1 << 29)
127#define AUART_STAT_CTS (1 << 28)
128#define AUART_STAT_TXFE (1 << 27)
129#define AUART_STAT_TXFF (1 << 25)
130#define AUART_STAT_RXFE (1 << 24)
131#define AUART_STAT_OERR (1 << 19)
132#define AUART_STAT_BERR (1 << 18)
133#define AUART_STAT_PERR (1 << 17)
134#define AUART_STAT_FERR (1 << 16)
Huang Shijiee8001632012-11-16 16:03:53 +0800135#define AUART_STAT_RXCOUNT_MASK 0xffff
Sascha Hauer47d37d62011-01-11 15:54:54 +0100136
137static struct uart_driver auart_driver;
138
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800139enum mxs_auart_type {
140 IMX23_AUART,
141 IMX28_AUART,
142};
143
Sascha Hauer47d37d62011-01-11 15:54:54 +0100144struct mxs_auart_port {
145 struct uart_port port;
146
Huang Shijiee8001632012-11-16 16:03:53 +0800147#define MXS_AUART_DMA_ENABLED 0x2
148#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
149#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
Huang Shijie8418e672013-08-03 10:09:14 -0400150#define MXS_AUART_RTSCTS 4 /* bit 4 */
Huang Shijiee8001632012-11-16 16:03:53 +0800151 unsigned long flags;
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200152 unsigned int mctrl_prev;
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800153 enum mxs_auart_type devtype;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100154
155 unsigned int irq;
156
157 struct clk *clk;
158 struct device *dev;
Huang Shijiee8001632012-11-16 16:03:53 +0800159
160 /* for DMA */
Huang Shijiee8001632012-11-16 16:03:53 +0800161 struct scatterlist tx_sgl;
162 struct dma_chan *tx_dma_chan;
163 void *tx_dma_buf;
164
165 struct scatterlist rx_sgl;
166 struct dma_chan *rx_dma_chan;
167 void *rx_dma_buf;
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200168
169 struct mctrl_gpios *gpios;
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200170 int gpio_irq[UART_GPIO_MAX];
171 bool ms_irq_enabled;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100172};
173
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800174static struct platform_device_id mxs_auart_devtype[] = {
175 { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
176 { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
177 { /* sentinel */ }
178};
179MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
180
181static struct of_device_id mxs_auart_dt_ids[] = {
182 {
183 .compatible = "fsl,imx28-auart",
184 .data = &mxs_auart_devtype[IMX28_AUART]
185 }, {
186 .compatible = "fsl,imx23-auart",
187 .data = &mxs_auart_devtype[IMX23_AUART]
188 }, { /* sentinel */ }
189};
190MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
191
192static inline int is_imx28_auart(struct mxs_auart_port *s)
193{
194 return s->devtype == IMX28_AUART;
195}
196
Huang Shijiee8001632012-11-16 16:03:53 +0800197static inline bool auart_dma_enabled(struct mxs_auart_port *s)
198{
199 return s->flags & MXS_AUART_DMA_ENABLED;
200}
201
Sascha Hauer47d37d62011-01-11 15:54:54 +0100202static void mxs_auart_stop_tx(struct uart_port *u);
203
204#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
205
Huang Shijiee8001632012-11-16 16:03:53 +0800206static void mxs_auart_tx_chars(struct mxs_auart_port *s);
207
208static void dma_tx_callback(void *param)
209{
210 struct mxs_auart_port *s = param;
211 struct circ_buf *xmit = &s->port.state->xmit;
212
213 dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
214
215 /* clear the bit used to serialize the DMA tx. */
216 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100217 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800218
219 /* wake up the possible processes. */
220 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
221 uart_write_wakeup(&s->port);
222
223 mxs_auart_tx_chars(s);
224}
225
226static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
227{
228 struct dma_async_tx_descriptor *desc;
229 struct scatterlist *sgl = &s->tx_sgl;
230 struct dma_chan *channel = s->tx_dma_chan;
231 u32 pio;
232
233 /* [1] : send PIO. Note, the first pio word is CTRL1. */
234 pio = AUART_CTRL1_XFER_COUNT(size);
235 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
236 1, DMA_TRANS_NONE, 0);
237 if (!desc) {
238 dev_err(s->dev, "step 1 error\n");
239 return -EINVAL;
240 }
241
242 /* [2] : set DMA buffer. */
243 sg_init_one(sgl, s->tx_dma_buf, size);
244 dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
245 desc = dmaengine_prep_slave_sg(channel, sgl,
246 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
247 if (!desc) {
248 dev_err(s->dev, "step 2 error\n");
249 return -EINVAL;
250 }
251
252 /* [3] : submit the DMA */
253 desc->callback = dma_tx_callback;
254 desc->callback_param = s;
255 dmaengine_submit(desc);
256 dma_async_issue_pending(channel);
257 return 0;
258}
259
260static void mxs_auart_tx_chars(struct mxs_auart_port *s)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100261{
262 struct circ_buf *xmit = &s->port.state->xmit;
263
Huang Shijiee8001632012-11-16 16:03:53 +0800264 if (auart_dma_enabled(s)) {
fabio.estevam@freescale.com87b8bed2013-01-07 23:11:06 -0200265 u32 i = 0;
Huang Shijiee8001632012-11-16 16:03:53 +0800266 int size;
267 void *buffer = s->tx_dma_buf;
268
269 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
270 return;
271
272 while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
273 size = min_t(u32, UART_XMIT_SIZE - i,
274 CIRC_CNT_TO_END(xmit->head,
275 xmit->tail,
276 UART_XMIT_SIZE));
277 memcpy(buffer + i, xmit->buf + xmit->tail, size);
278 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
279
280 i += size;
281 if (i >= UART_XMIT_SIZE)
282 break;
283 }
284
285 if (uart_tx_stopped(&s->port))
286 mxs_auart_stop_tx(&s->port);
287
288 if (i) {
289 mxs_auart_dma_tx(s, i);
290 } else {
291 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100292 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800293 }
294 return;
295 }
296
297
Sascha Hauer47d37d62011-01-11 15:54:54 +0100298 while (!(readl(s->port.membase + AUART_STAT) &
299 AUART_STAT_TXFF)) {
300 if (s->port.x_char) {
301 s->port.icount.tx++;
302 writel(s->port.x_char,
303 s->port.membase + AUART_DATA);
304 s->port.x_char = 0;
305 continue;
306 }
307 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
308 s->port.icount.tx++;
309 writel(xmit->buf[xmit->tail],
310 s->port.membase + AUART_DATA);
311 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100312 } else
313 break;
314 }
Uwe Kleine-Königd0758a22011-11-22 14:22:56 +0100315 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
316 uart_write_wakeup(&s->port);
317
Sascha Hauer47d37d62011-01-11 15:54:54 +0100318 if (uart_circ_empty(&(s->port.state->xmit)))
319 writel(AUART_INTR_TXIEN,
320 s->port.membase + AUART_INTR_CLR);
321 else
322 writel(AUART_INTR_TXIEN,
323 s->port.membase + AUART_INTR_SET);
324
325 if (uart_tx_stopped(&s->port))
326 mxs_auart_stop_tx(&s->port);
327}
328
329static void mxs_auart_rx_char(struct mxs_auart_port *s)
330{
331 int flag;
332 u32 stat;
333 u8 c;
334
335 c = readl(s->port.membase + AUART_DATA);
336 stat = readl(s->port.membase + AUART_STAT);
337
338 flag = TTY_NORMAL;
339 s->port.icount.rx++;
340
341 if (stat & AUART_STAT_BERR) {
342 s->port.icount.brk++;
343 if (uart_handle_break(&s->port))
344 goto out;
345 } else if (stat & AUART_STAT_PERR) {
346 s->port.icount.parity++;
347 } else if (stat & AUART_STAT_FERR) {
348 s->port.icount.frame++;
349 }
350
351 /*
352 * Mask off conditions which should be ingored.
353 */
354 stat &= s->port.read_status_mask;
355
356 if (stat & AUART_STAT_BERR) {
357 flag = TTY_BREAK;
358 } else if (stat & AUART_STAT_PERR)
359 flag = TTY_PARITY;
360 else if (stat & AUART_STAT_FERR)
361 flag = TTY_FRAME;
362
363 if (stat & AUART_STAT_OERR)
364 s->port.icount.overrun++;
365
366 if (uart_handle_sysrq_char(&s->port, c))
367 goto out;
368
369 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
370out:
371 writel(stat, s->port.membase + AUART_STAT);
372}
373
374static void mxs_auart_rx_chars(struct mxs_auart_port *s)
375{
Sascha Hauer47d37d62011-01-11 15:54:54 +0100376 u32 stat = 0;
377
378 for (;;) {
379 stat = readl(s->port.membase + AUART_STAT);
380 if (stat & AUART_STAT_RXFE)
381 break;
382 mxs_auart_rx_char(s);
383 }
384
385 writel(stat, s->port.membase + AUART_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100386 tty_flip_buffer_push(&s->port.state->port);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100387}
388
389static int mxs_auart_request_port(struct uart_port *u)
390{
391 return 0;
392}
393
394static int mxs_auart_verify_port(struct uart_port *u,
395 struct serial_struct *ser)
396{
397 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
398 return -EINVAL;
399 return 0;
400}
401
402static void mxs_auart_config_port(struct uart_port *u, int flags)
403{
404}
405
406static const char *mxs_auart_type(struct uart_port *u)
407{
408 struct mxs_auart_port *s = to_auart_port(u);
409
410 return dev_name(s->dev);
411}
412
413static void mxs_auart_release_port(struct uart_port *u)
414{
415}
416
417static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
418{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200419 struct mxs_auart_port *s = to_auart_port(u);
420
Sascha Hauer47d37d62011-01-11 15:54:54 +0100421 u32 ctrl = readl(u->membase + AUART_CTRL2);
422
Steffen Trumtrara6833212012-12-13 14:27:43 +0100423 ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
Huang Shijie00592022012-08-08 10:37:59 +0800424 if (mctrl & TIOCM_RTS) {
Peter Hurley299245a2014-09-10 15:06:24 -0400425 if (uart_cts_enabled(u))
Huang Shijie00592022012-08-08 10:37:59 +0800426 ctrl |= AUART_CTRL2_RTSEN;
Steffen Trumtrara6833212012-12-13 14:27:43 +0100427 else
428 ctrl |= AUART_CTRL2_RTS;
Huang Shijie00592022012-08-08 10:37:59 +0800429 }
430
Sascha Hauer47d37d62011-01-11 15:54:54 +0100431 writel(ctrl, u->membase + AUART_CTRL2);
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200432
433 mctrl_gpio_set(s->gpios, mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100434}
435
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200436#define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
437static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
438{
439 u32 mctrl_diff;
440
441 mctrl_diff = mctrl ^ s->mctrl_prev;
442 s->mctrl_prev = mctrl;
443 if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
444 s->port.state != NULL) {
445 if (mctrl_diff & TIOCM_RI)
446 s->port.icount.rng++;
447 if (mctrl_diff & TIOCM_DSR)
448 s->port.icount.dsr++;
449 if (mctrl_diff & TIOCM_CD)
450 uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
451 if (mctrl_diff & TIOCM_CTS)
452 uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
453
454 wake_up_interruptible(&s->port.state->port.delta_msr_wait);
455 }
456 return mctrl;
457}
458
Sascha Hauer47d37d62011-01-11 15:54:54 +0100459static u32 mxs_auart_get_mctrl(struct uart_port *u)
460{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200461 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100462 u32 stat = readl(u->membase + AUART_STAT);
Janusz Uzycki42b4eba2014-10-10 18:53:24 +0200463 u32 mctrl = 0;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100464
Sascha Hauer47d37d62011-01-11 15:54:54 +0100465 if (stat & AUART_STAT_CTS)
466 mctrl |= TIOCM_CTS;
467
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200468 return mctrl_gpio_get(s->gpios, &mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100469}
470
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200471/*
472 * Enable modem status interrupts
473 */
474static void mxs_auart_enable_ms(struct uart_port *port)
475{
476 struct mxs_auart_port *s = to_auart_port(port);
477
478 /*
479 * Interrupt should not be enabled twice
480 */
481 if (s->ms_irq_enabled)
482 return;
483
484 s->ms_irq_enabled = true;
485
486 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
487 enable_irq(s->gpio_irq[UART_GPIO_CTS]);
488 /* TODO: enable AUART_INTR_CTSMIEN otherwise */
489
490 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
491 enable_irq(s->gpio_irq[UART_GPIO_DSR]);
492
493 if (s->gpio_irq[UART_GPIO_RI] >= 0)
494 enable_irq(s->gpio_irq[UART_GPIO_RI]);
495
496 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
497 enable_irq(s->gpio_irq[UART_GPIO_DCD]);
498}
499
500/*
501 * Disable modem status interrupts
502 */
503static void mxs_auart_disable_ms(struct uart_port *port)
504{
505 struct mxs_auart_port *s = to_auart_port(port);
506
507 /*
508 * Interrupt should not be disabled twice
509 */
510 if (!s->ms_irq_enabled)
511 return;
512
513 s->ms_irq_enabled = false;
514
515 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
516 disable_irq(s->gpio_irq[UART_GPIO_CTS]);
517 /* TODO: disable AUART_INTR_CTSMIEN otherwise */
518
519 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
520 disable_irq(s->gpio_irq[UART_GPIO_DSR]);
521
522 if (s->gpio_irq[UART_GPIO_RI] >= 0)
523 disable_irq(s->gpio_irq[UART_GPIO_RI]);
524
525 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
526 disable_irq(s->gpio_irq[UART_GPIO_DCD]);
527}
528
Huang Shijiee8001632012-11-16 16:03:53 +0800529static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
530static void dma_rx_callback(void *arg)
531{
532 struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100533 struct tty_port *port = &s->port.state->port;
Huang Shijiee8001632012-11-16 16:03:53 +0800534 int count;
535 u32 stat;
536
Huang Shijied7ffb932012-11-22 15:06:30 +0800537 dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
538
Huang Shijiee8001632012-11-16 16:03:53 +0800539 stat = readl(s->port.membase + AUART_STAT);
540 stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
541 AUART_STAT_PERR | AUART_STAT_FERR);
542
543 count = stat & AUART_STAT_RXCOUNT_MASK;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100544 tty_insert_flip_string(port, s->rx_dma_buf, count);
Huang Shijiee8001632012-11-16 16:03:53 +0800545
546 writel(stat, s->port.membase + AUART_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100547 tty_flip_buffer_push(port);
Huang Shijiee8001632012-11-16 16:03:53 +0800548
549 /* start the next DMA for RX. */
550 mxs_auart_dma_prep_rx(s);
551}
552
553static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
554{
555 struct dma_async_tx_descriptor *desc;
556 struct scatterlist *sgl = &s->rx_sgl;
557 struct dma_chan *channel = s->rx_dma_chan;
558 u32 pio[1];
559
560 /* [1] : send PIO */
561 pio[0] = AUART_CTRL0_RXTO_ENABLE
562 | AUART_CTRL0_RXTIMEOUT(0x80)
563 | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
564 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
565 1, DMA_TRANS_NONE, 0);
566 if (!desc) {
567 dev_err(s->dev, "step 1 error\n");
568 return -EINVAL;
569 }
570
571 /* [2] : send DMA request */
572 sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
573 dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
574 desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
575 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
576 if (!desc) {
577 dev_err(s->dev, "step 2 error\n");
578 return -1;
579 }
580
581 /* [3] : submit the DMA, but do not issue it. */
582 desc->callback = dma_rx_callback;
583 desc->callback_param = s;
584 dmaengine_submit(desc);
585 dma_async_issue_pending(channel);
586 return 0;
587}
588
589static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
590{
591 if (s->tx_dma_chan) {
592 dma_release_channel(s->tx_dma_chan);
593 s->tx_dma_chan = NULL;
594 }
595 if (s->rx_dma_chan) {
596 dma_release_channel(s->rx_dma_chan);
597 s->rx_dma_chan = NULL;
598 }
599
600 kfree(s->tx_dma_buf);
601 kfree(s->rx_dma_buf);
602 s->tx_dma_buf = NULL;
603 s->rx_dma_buf = NULL;
604}
605
606static void mxs_auart_dma_exit(struct mxs_auart_port *s)
607{
608
609 writel(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
610 s->port.membase + AUART_CTRL2_CLR);
611
612 mxs_auart_dma_exit_channel(s);
613 s->flags &= ~MXS_AUART_DMA_ENABLED;
614 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
615 clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
616}
617
618static int mxs_auart_dma_init(struct mxs_auart_port *s)
619{
Huang Shijiee8001632012-11-16 16:03:53 +0800620 if (auart_dma_enabled(s))
621 return 0;
622
Huang Shijiee8001632012-11-16 16:03:53 +0800623 /* init for RX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800624 s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
Huang Shijiee8001632012-11-16 16:03:53 +0800625 if (!s->rx_dma_chan)
626 goto err_out;
627 s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
628 if (!s->rx_dma_buf)
629 goto err_out;
630
631 /* init for TX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800632 s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
Huang Shijiee8001632012-11-16 16:03:53 +0800633 if (!s->tx_dma_chan)
634 goto err_out;
635 s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
636 if (!s->tx_dma_buf)
637 goto err_out;
638
639 /* set the flags */
640 s->flags |= MXS_AUART_DMA_ENABLED;
641 dev_dbg(s->dev, "enabled the DMA support.");
642
Hector Palacios9987f762013-10-03 09:32:03 +0200643 /* The DMA buffer is now the FIFO the TTY subsystem can use */
644 s->port.fifosize = UART_XMIT_SIZE;
645
Huang Shijiee8001632012-11-16 16:03:53 +0800646 return 0;
647
648err_out:
649 mxs_auart_dma_exit_channel(s);
650 return -EINVAL;
651
652}
653
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200654#define RTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
655 UART_GPIO_RTS))
656#define CTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
657 UART_GPIO_CTS))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100658static void mxs_auart_settermios(struct uart_port *u,
659 struct ktermios *termios,
660 struct ktermios *old)
661{
Huang Shijiee8001632012-11-16 16:03:53 +0800662 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100663 u32 bm, ctrl, ctrl2, div;
664 unsigned int cflag, baud;
665
666 cflag = termios->c_cflag;
667
668 ctrl = AUART_LINECTRL_FEN;
669 ctrl2 = readl(u->membase + AUART_CTRL2);
670
671 /* byte size */
672 switch (cflag & CSIZE) {
673 case CS5:
674 bm = 0;
675 break;
676 case CS6:
677 bm = 1;
678 break;
679 case CS7:
680 bm = 2;
681 break;
682 case CS8:
683 bm = 3;
684 break;
685 default:
686 return;
687 }
688
689 ctrl |= AUART_LINECTRL_WLEN(bm);
690
691 /* parity */
692 if (cflag & PARENB) {
693 ctrl |= AUART_LINECTRL_PEN;
694 if ((cflag & PARODD) == 0)
695 ctrl |= AUART_LINECTRL_EPS;
696 }
697
698 u->read_status_mask = 0;
699
700 if (termios->c_iflag & INPCK)
701 u->read_status_mask |= AUART_STAT_PERR;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400702 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100703 u->read_status_mask |= AUART_STAT_BERR;
704
705 /*
706 * Characters to ignore
707 */
708 u->ignore_status_mask = 0;
709 if (termios->c_iflag & IGNPAR)
710 u->ignore_status_mask |= AUART_STAT_PERR;
711 if (termios->c_iflag & IGNBRK) {
712 u->ignore_status_mask |= AUART_STAT_BERR;
713 /*
714 * If we're ignoring parity and break indicators,
715 * ignore overruns too (for real raw support).
716 */
717 if (termios->c_iflag & IGNPAR)
718 u->ignore_status_mask |= AUART_STAT_OERR;
719 }
720
721 /*
722 * ignore all characters if CREAD is not set
723 */
724 if (cflag & CREAD)
725 ctrl2 |= AUART_CTRL2_RXE;
726 else
727 ctrl2 &= ~AUART_CTRL2_RXE;
728
729 /* figure out the stop bits requested */
730 if (cflag & CSTOPB)
731 ctrl |= AUART_LINECTRL_STP2;
732
733 /* figure out the hardware flow control settings */
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200734 ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
Huang Shijiee8001632012-11-16 16:03:53 +0800735 if (cflag & CRTSCTS) {
736 /*
737 * The DMA has a bug(see errata:2836) in mx23.
738 * So we can not implement the DMA for auart in mx23,
739 * we can only implement the DMA support for auart
740 * in mx28.
741 */
Huang Shijieafab2202013-08-03 10:09:15 -0400742 if (is_imx28_auart(s)
Huang Shijie8418e672013-08-03 10:09:14 -0400743 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
Huang Shijiee8001632012-11-16 16:03:53 +0800744 if (!mxs_auart_dma_init(s))
745 /* enable DMA tranfer */
746 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
747 | AUART_CTRL2_DMAONERR;
748 }
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200749 /* Even if RTS is GPIO line RTSEN can be enabled because
750 * the pinctrl configuration decides about RTS pin function */
751 ctrl2 |= AUART_CTRL2_RTSEN;
752 if (CTS_AT_AUART())
753 ctrl2 |= AUART_CTRL2_CTSEN;
Huang Shijiee8001632012-11-16 16:03:53 +0800754 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100755
756 /* set baud rate */
757 baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
758 div = u->uartclk * 32 / baud;
759 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
760 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
761
762 writel(ctrl, u->membase + AUART_LINECTRL);
763 writel(ctrl2, u->membase + AUART_CTRL2);
Lothar Waßmann8b979f72012-05-03 11:37:12 +0200764
765 uart_update_timeout(u, termios->c_cflag, baud);
Huang Shijiee8001632012-11-16 16:03:53 +0800766
767 /* prepare for the DMA RX. */
768 if (auart_dma_enabled(s) &&
769 !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
770 if (!mxs_auart_dma_prep_rx(s)) {
771 /* Disable the normal RX interrupt. */
Huang Shijiea5919442012-11-22 15:06:29 +0800772 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
773 u->membase + AUART_INTR_CLR);
Huang Shijiee8001632012-11-16 16:03:53 +0800774 } else {
775 mxs_auart_dma_exit(s);
776 dev_err(s->dev, "We can not start up the DMA.\n");
777 }
778 }
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200779
780 /* CTS flow-control and modem-status interrupts */
781 if (UART_ENABLE_MS(u, termios->c_cflag))
782 mxs_auart_enable_ms(u);
783 else
784 mxs_auart_disable_ms(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100785}
786
Janusz Uzycki36a26272014-10-10 18:53:27 +0200787static void mxs_auart_set_ldisc(struct uart_port *port, int new)
788{
789 if (new == N_PPS) {
790 port->flags |= UPF_HARDPPS_CD;
791 mxs_auart_enable_ms(port);
792 } else {
793 port->flags &= ~UPF_HARDPPS_CD;
794 }
795}
796
Sascha Hauer47d37d62011-01-11 15:54:54 +0100797static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
798{
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200799 u32 istat;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100800 struct mxs_auart_port *s = context;
801 u32 stat = readl(s->port.membase + AUART_STAT);
802
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200803 istat = readl(s->port.membase + AUART_INTR);
804
805 /* ack irq */
806 writel(istat & (AUART_INTR_RTIS
807 | AUART_INTR_TXIS
808 | AUART_INTR_RXIS
809 | AUART_INTR_CTSMIS),
810 s->port.membase + AUART_INTR_CLR);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100811
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200812 /*
813 * Dealing with GPIO interrupt
814 */
815 if (irq == s->gpio_irq[UART_GPIO_CTS] ||
816 irq == s->gpio_irq[UART_GPIO_DCD] ||
817 irq == s->gpio_irq[UART_GPIO_DSR] ||
818 irq == s->gpio_irq[UART_GPIO_RI])
819 mxs_auart_modem_status(s,
820 mctrl_gpio_get(s->gpios, &s->mctrl_prev));
821
Sascha Hauer47d37d62011-01-11 15:54:54 +0100822 if (istat & AUART_INTR_CTSMIS) {
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200823 if (CTS_AT_AUART() && s->ms_irq_enabled)
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200824 uart_handle_cts_change(&s->port,
825 stat & AUART_STAT_CTS);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100826 writel(AUART_INTR_CTSMIS,
827 s->port.membase + AUART_INTR_CLR);
828 istat &= ~AUART_INTR_CTSMIS;
829 }
830
831 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
Huang Shijiea5919442012-11-22 15:06:29 +0800832 if (!auart_dma_enabled(s))
833 mxs_auart_rx_chars(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100834 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
835 }
836
837 if (istat & AUART_INTR_TXIS) {
838 mxs_auart_tx_chars(s);
839 istat &= ~AUART_INTR_TXIS;
840 }
841
Sascha Hauer47d37d62011-01-11 15:54:54 +0100842 return IRQ_HANDLED;
843}
844
845static void mxs_auart_reset(struct uart_port *u)
846{
847 int i;
848 unsigned int reg;
849
850 writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
851
852 for (i = 0; i < 10000; i++) {
853 reg = readl(u->membase + AUART_CTRL0);
854 if (!(reg & AUART_CTRL0_SFTRST))
855 break;
856 udelay(3);
857 }
858 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
859}
860
861static int mxs_auart_startup(struct uart_port *u)
862{
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200863 int ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100864 struct mxs_auart_port *s = to_auart_port(u);
865
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200866 ret = clk_prepare_enable(s->clk);
867 if (ret)
868 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100869
870 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
871
872 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
873
874 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
875 u->membase + AUART_INTR);
876
Hector Palacios9987f762013-10-03 09:32:03 +0200877 /* Reset FIFO size (it could have changed if DMA was enabled) */
878 u->fifosize = MXS_AUART_FIFO_SIZE;
879
Sascha Hauer47d37d62011-01-11 15:54:54 +0100880 /*
881 * Enable fifo so all four bytes of a DMA word are written to
882 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
883 */
884 writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
885
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200886 /* get initial status of modem lines */
887 mctrl_gpio_get(s->gpios, &s->mctrl_prev);
888
889 s->ms_irq_enabled = false;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100890 return 0;
891}
892
893static void mxs_auart_shutdown(struct uart_port *u)
894{
895 struct mxs_auart_port *s = to_auart_port(u);
896
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200897 mxs_auart_disable_ms(u);
898
Huang Shijiee8001632012-11-16 16:03:53 +0800899 if (auart_dma_enabled(s))
900 mxs_auart_dma_exit(s);
901
Sascha Hauer47d37d62011-01-11 15:54:54 +0100902 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
903
Sascha Hauer47d37d62011-01-11 15:54:54 +0100904 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
905 u->membase + AUART_INTR_CLR);
906
Huang Shijie851b7142012-09-06 22:38:40 -0400907 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
908
Shawn Guoa4813772011-12-20 14:10:29 +0800909 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100910}
911
912static unsigned int mxs_auart_tx_empty(struct uart_port *u)
913{
914 if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
915 return TIOCSER_TEMT;
916 else
917 return 0;
918}
919
920static void mxs_auart_start_tx(struct uart_port *u)
921{
922 struct mxs_auart_port *s = to_auart_port(u);
923
924 /* enable transmitter */
925 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
926
927 mxs_auart_tx_chars(s);
928}
929
930static void mxs_auart_stop_tx(struct uart_port *u)
931{
932 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
933}
934
935static void mxs_auart_stop_rx(struct uart_port *u)
936{
937 writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
938}
939
940static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
941{
942 if (ctl)
943 writel(AUART_LINECTRL_BRK,
944 u->membase + AUART_LINECTRL_SET);
945 else
946 writel(AUART_LINECTRL_BRK,
947 u->membase + AUART_LINECTRL_CLR);
948}
949
Sascha Hauer47d37d62011-01-11 15:54:54 +0100950static struct uart_ops mxs_auart_ops = {
951 .tx_empty = mxs_auart_tx_empty,
952 .start_tx = mxs_auart_start_tx,
953 .stop_tx = mxs_auart_stop_tx,
954 .stop_rx = mxs_auart_stop_rx,
Janusz Uzyckif9e42392014-10-10 18:53:26 +0200955 .enable_ms = mxs_auart_enable_ms,
Sascha Hauer47d37d62011-01-11 15:54:54 +0100956 .break_ctl = mxs_auart_break_ctl,
957 .set_mctrl = mxs_auart_set_mctrl,
958 .get_mctrl = mxs_auart_get_mctrl,
959 .startup = mxs_auart_startup,
960 .shutdown = mxs_auart_shutdown,
961 .set_termios = mxs_auart_settermios,
Janusz Uzycki36a26272014-10-10 18:53:27 +0200962 .set_ldisc = mxs_auart_set_ldisc,
Sascha Hauer47d37d62011-01-11 15:54:54 +0100963 .type = mxs_auart_type,
964 .release_port = mxs_auart_release_port,
965 .request_port = mxs_auart_request_port,
966 .config_port = mxs_auart_config_port,
967 .verify_port = mxs_auart_verify_port,
968};
969
970static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
971
972#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
973static void mxs_auart_console_putchar(struct uart_port *port, int ch)
974{
975 unsigned int to = 1000;
976
977 while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
978 if (!to--)
979 break;
980 udelay(1);
981 }
982
983 writel(ch, port->membase + AUART_DATA);
984}
985
986static void
987auart_console_write(struct console *co, const char *str, unsigned int count)
988{
989 struct mxs_auart_port *s;
990 struct uart_port *port;
991 unsigned int old_ctrl0, old_ctrl2;
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200992 unsigned int to = 20000;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100993
Wolfram Sang4829e762013-04-19 21:12:17 +0200994 if (co->index >= MXS_AUART_PORTS || co->index < 0)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100995 return;
996
997 s = auart_port[co->index];
998 port = &s->port;
999
1000 clk_enable(s->clk);
1001
1002 /* First save the CR then disable the interrupts */
1003 old_ctrl2 = readl(port->membase + AUART_CTRL2);
1004 old_ctrl0 = readl(port->membase + AUART_CTRL0);
1005
1006 writel(AUART_CTRL0_CLKGATE,
1007 port->membase + AUART_CTRL0_CLR);
1008 writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
1009 port->membase + AUART_CTRL2_SET);
1010
1011 uart_console_write(port, str, count, mxs_auart_console_putchar);
1012
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001013 /* Finally, wait for transmitter to become empty ... */
Sascha Hauer47d37d62011-01-11 15:54:54 +01001014 while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001015 udelay(1);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001016 if (!to--)
1017 break;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001018 }
1019
Uwe Kleine-König079a0362013-06-28 11:49:41 +02001020 /*
1021 * ... and restore the TCR if we waited long enough for the transmitter
1022 * to be idle. This might keep the transmitter enabled although it is
1023 * unused, but that is better than to disable it while it is still
1024 * transmitting.
1025 */
1026 if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
1027 writel(old_ctrl0, port->membase + AUART_CTRL0);
1028 writel(old_ctrl2, port->membase + AUART_CTRL2);
1029 }
Sascha Hauer47d37d62011-01-11 15:54:54 +01001030
1031 clk_disable(s->clk);
1032}
1033
1034static void __init
1035auart_console_get_options(struct uart_port *port, int *baud,
1036 int *parity, int *bits)
1037{
1038 unsigned int lcr_h, quot;
1039
1040 if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
1041 return;
1042
1043 lcr_h = readl(port->membase + AUART_LINECTRL);
1044
1045 *parity = 'n';
1046 if (lcr_h & AUART_LINECTRL_PEN) {
1047 if (lcr_h & AUART_LINECTRL_EPS)
1048 *parity = 'e';
1049 else
1050 *parity = 'o';
1051 }
1052
1053 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
1054 *bits = 7;
1055 else
1056 *bits = 8;
1057
1058 quot = ((readl(port->membase + AUART_LINECTRL)
1059 & AUART_LINECTRL_BAUD_DIVINT_MASK))
1060 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
1061 quot |= ((readl(port->membase + AUART_LINECTRL)
1062 & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
1063 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
1064 if (quot == 0)
1065 quot = 1;
1066
1067 *baud = (port->uartclk << 2) / quot;
1068}
1069
1070static int __init
1071auart_console_setup(struct console *co, char *options)
1072{
1073 struct mxs_auart_port *s;
1074 int baud = 9600;
1075 int bits = 8;
1076 int parity = 'n';
1077 int flow = 'n';
1078 int ret;
1079
1080 /*
1081 * Check whether an invalid uart number has been specified, and
1082 * if so, search for the first available port that does have
1083 * console support.
1084 */
1085 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
1086 co->index = 0;
1087 s = auart_port[co->index];
1088 if (!s)
1089 return -ENODEV;
1090
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -02001091 ret = clk_prepare_enable(s->clk);
1092 if (ret)
1093 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001094
1095 if (options)
1096 uart_parse_options(options, &baud, &parity, &bits, &flow);
1097 else
1098 auart_console_get_options(&s->port, &baud, &parity, &bits);
1099
1100 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
1101
Shawn Guoa4813772011-12-20 14:10:29 +08001102 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001103
1104 return ret;
1105}
1106
1107static struct console auart_console = {
1108 .name = "ttyAPP",
1109 .write = auart_console_write,
1110 .device = uart_console_device,
1111 .setup = auart_console_setup,
1112 .flags = CON_PRINTBUFFER,
1113 .index = -1,
1114 .data = &auart_driver,
1115};
1116#endif
1117
1118static struct uart_driver auart_driver = {
1119 .owner = THIS_MODULE,
1120 .driver_name = "ttyAPP",
1121 .dev_name = "ttyAPP",
1122 .major = 0,
1123 .minor = 0,
1124 .nr = MXS_AUART_PORTS,
1125#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1126 .cons = &auart_console,
1127#endif
1128};
1129
Fabio Estevam1ea66072012-06-18 10:06:09 -03001130/*
1131 * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
1132 * could successfully get all information from dt or a negative errno.
1133 */
1134static int serial_mxs_probe_dt(struct mxs_auart_port *s,
1135 struct platform_device *pdev)
1136{
1137 struct device_node *np = pdev->dev.of_node;
1138 int ret;
1139
1140 if (!np)
1141 /* no device tree device */
1142 return 1;
1143
1144 ret = of_alias_get_id(np, "serial");
1145 if (ret < 0) {
1146 dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
1147 return ret;
1148 }
1149 s->port.line = ret;
1150
Huang Shijie8418e672013-08-03 10:09:14 -04001151 if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
1152 set_bit(MXS_AUART_RTSCTS, &s->flags);
1153
Fabio Estevam1ea66072012-06-18 10:06:09 -03001154 return 0;
1155}
1156
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001157static bool mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
1158{
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001159 enum mctrl_gpio_idx i;
1160 struct gpio_desc *gpiod;
1161
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001162 s->gpios = mctrl_gpio_init(dev, 0);
1163 if (IS_ERR_OR_NULL(s->gpios))
1164 return false;
1165
1166 /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
1167 if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
1168 if (test_bit(MXS_AUART_RTSCTS, &s->flags))
1169 dev_warn(dev,
1170 "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
1171 clear_bit(MXS_AUART_RTSCTS, &s->flags);
1172 }
1173
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001174 for (i = 0; i < UART_GPIO_MAX; i++) {
1175 gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
1176 if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
1177 s->gpio_irq[i] = gpiod_to_irq(gpiod);
1178 else
1179 s->gpio_irq[i] = -EINVAL;
1180 }
1181
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001182 return true;
1183}
1184
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001185static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
1186{
1187 enum mctrl_gpio_idx i;
1188
1189 for (i = 0; i < UART_GPIO_MAX; i++)
1190 if (s->gpio_irq[i] >= 0)
1191 free_irq(s->gpio_irq[i], s);
1192}
1193
1194static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
1195{
1196 int *irq = s->gpio_irq;
1197 enum mctrl_gpio_idx i;
1198 int err = 0;
1199
1200 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1201 if (irq[i] < 0)
1202 continue;
1203
1204 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1205 err = request_irq(irq[i], mxs_auart_irq_handle,
1206 IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
1207 if (err)
1208 dev_err(s->dev, "%s - Can't get %d irq\n",
1209 __func__, irq[i]);
1210 }
1211
1212 /*
1213 * If something went wrong, rollback.
1214 */
1215 while (err && (--i >= 0))
1216 if (irq[i] >= 0)
1217 free_irq(irq[i], s);
1218
1219 return err;
1220}
1221
Bill Pemberton9671f092012-11-19 13:21:50 -05001222static int mxs_auart_probe(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001223{
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001224 const struct of_device_id *of_id =
1225 of_match_device(mxs_auart_dt_ids, &pdev->dev);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001226 struct mxs_auart_port *s;
1227 u32 version;
1228 int ret = 0;
1229 struct resource *r;
1230
1231 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
1232 if (!s) {
1233 ret = -ENOMEM;
1234 goto out;
1235 }
1236
Fabio Estevam1ea66072012-06-18 10:06:09 -03001237 ret = serial_mxs_probe_dt(s, pdev);
1238 if (ret > 0)
1239 s->port.line = pdev->id < 0 ? 0 : pdev->id;
1240 else if (ret < 0)
1241 goto out_free;
1242
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001243 if (of_id) {
1244 pdev->id_entry = of_id->data;
1245 s->devtype = pdev->id_entry->driver_data;
1246 }
1247
Sascha Hauer47d37d62011-01-11 15:54:54 +01001248 s->clk = clk_get(&pdev->dev, NULL);
1249 if (IS_ERR(s->clk)) {
1250 ret = PTR_ERR(s->clk);
1251 goto out_free;
1252 }
1253
1254 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1255 if (!r) {
1256 ret = -ENXIO;
1257 goto out_free_clk;
1258 }
1259
1260 s->port.mapbase = r->start;
1261 s->port.membase = ioremap(r->start, resource_size(r));
1262 s->port.ops = &mxs_auart_ops;
1263 s->port.iotype = UPIO_MEM;
Hector Palacios9987f762013-10-03 09:32:03 +02001264 s->port.fifosize = MXS_AUART_FIFO_SIZE;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001265 s->port.uartclk = clk_get_rate(s->clk);
1266 s->port.type = PORT_IMX;
Wolfram Sang4c24f2c2013-04-19 21:06:20 +02001267 s->port.dev = s->dev = &pdev->dev;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001268
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001269 s->mctrl_prev = 0;
1270
Sascha Hauer47d37d62011-01-11 15:54:54 +01001271 s->irq = platform_get_irq(pdev, 0);
1272 s->port.irq = s->irq;
1273 ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
1274 if (ret)
1275 goto out_free_clk;
1276
1277 platform_set_drvdata(pdev, s);
1278
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001279 if (!mxs_auart_init_gpios(s, &pdev->dev))
1280 dev_err(&pdev->dev,
1281 "Failed to initialize GPIOs. The serial port may not work as expected\n");
1282
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001283 /*
1284 * Get the GPIO lines IRQ
1285 */
1286 ret = mxs_auart_request_gpio_irq(s);
1287 if (ret)
1288 goto out_free_irq;
1289
Fabio Estevam1ea66072012-06-18 10:06:09 -03001290 auart_port[s->port.line] = s;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001291
1292 mxs_auart_reset(&s->port);
1293
1294 ret = uart_add_one_port(&auart_driver, &s->port);
1295 if (ret)
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001296 goto out_free_gpio_irq;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001297
1298 version = readl(s->port.membase + AUART_VERSION);
1299 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
1300 (version >> 24) & 0xff,
1301 (version >> 16) & 0xff, version & 0xffff);
1302
1303 return 0;
1304
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001305out_free_gpio_irq:
1306 mxs_auart_free_gpio_irq(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001307out_free_irq:
1308 auart_port[pdev->id] = NULL;
1309 free_irq(s->irq, s);
1310out_free_clk:
1311 clk_put(s->clk);
1312out_free:
1313 kfree(s);
1314out:
1315 return ret;
1316}
1317
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001318static int mxs_auart_remove(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001319{
1320 struct mxs_auart_port *s = platform_get_drvdata(pdev);
1321
1322 uart_remove_one_port(&auart_driver, &s->port);
1323
1324 auart_port[pdev->id] = NULL;
1325
Janusz Uzyckif9e42392014-10-10 18:53:26 +02001326 mxs_auart_free_gpio_irq(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001327 clk_put(s->clk);
1328 free_irq(s->irq, s);
1329 kfree(s);
1330
1331 return 0;
1332}
1333
1334static struct platform_driver mxs_auart_driver = {
1335 .probe = mxs_auart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001336 .remove = mxs_auart_remove,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001337 .driver = {
1338 .name = "mxs-auart",
1339 .owner = THIS_MODULE,
Fabio Estevam1ea66072012-06-18 10:06:09 -03001340 .of_match_table = mxs_auart_dt_ids,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001341 },
1342};
1343
1344static int __init mxs_auart_init(void)
1345{
1346 int r;
1347
1348 r = uart_register_driver(&auart_driver);
1349 if (r)
1350 goto out;
1351
1352 r = platform_driver_register(&mxs_auart_driver);
1353 if (r)
1354 goto out_err;
1355
1356 return 0;
1357out_err:
1358 uart_unregister_driver(&auart_driver);
1359out:
1360 return r;
1361}
1362
1363static void __exit mxs_auart_exit(void)
1364{
1365 platform_driver_unregister(&mxs_auart_driver);
1366 uart_unregister_driver(&auart_driver);
1367}
1368
1369module_init(mxs_auart_init);
1370module_exit(mxs_auart_exit);
1371MODULE_LICENSE("GPL");
1372MODULE_DESCRIPTION("Freescale MXS application uart driver");
Fabio Estevam1ea66072012-06-18 10:06:09 -03001373MODULE_ALIAS("platform:mxs-auart");