blob: 5922cb435a87ccf10959bbe46d669b479a8e8aac [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 Uzycki7c573d72014-10-10 18:53:25 +020045#include <linux/err.h>
46#include "serial_mctrl_gpio.h"
47
Sascha Hauer47d37d62011-01-11 15:54:54 +010048#define MXS_AUART_PORTS 5
Hector Palacios9987f762013-10-03 09:32:03 +020049#define MXS_AUART_FIFO_SIZE 16
Sascha Hauer47d37d62011-01-11 15:54:54 +010050
51#define AUART_CTRL0 0x00000000
52#define AUART_CTRL0_SET 0x00000004
53#define AUART_CTRL0_CLR 0x00000008
54#define AUART_CTRL0_TOG 0x0000000c
55#define AUART_CTRL1 0x00000010
56#define AUART_CTRL1_SET 0x00000014
57#define AUART_CTRL1_CLR 0x00000018
58#define AUART_CTRL1_TOG 0x0000001c
59#define AUART_CTRL2 0x00000020
60#define AUART_CTRL2_SET 0x00000024
61#define AUART_CTRL2_CLR 0x00000028
62#define AUART_CTRL2_TOG 0x0000002c
63#define AUART_LINECTRL 0x00000030
64#define AUART_LINECTRL_SET 0x00000034
65#define AUART_LINECTRL_CLR 0x00000038
66#define AUART_LINECTRL_TOG 0x0000003c
67#define AUART_LINECTRL2 0x00000040
68#define AUART_LINECTRL2_SET 0x00000044
69#define AUART_LINECTRL2_CLR 0x00000048
70#define AUART_LINECTRL2_TOG 0x0000004c
71#define AUART_INTR 0x00000050
72#define AUART_INTR_SET 0x00000054
73#define AUART_INTR_CLR 0x00000058
74#define AUART_INTR_TOG 0x0000005c
75#define AUART_DATA 0x00000060
76#define AUART_STAT 0x00000070
77#define AUART_DEBUG 0x00000080
78#define AUART_VERSION 0x00000090
79#define AUART_AUTOBAUD 0x000000a0
80
81#define AUART_CTRL0_SFTRST (1 << 31)
82#define AUART_CTRL0_CLKGATE (1 << 30)
Huang Shijiee8001632012-11-16 16:03:53 +080083#define AUART_CTRL0_RXTO_ENABLE (1 << 27)
84#define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
85#define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
86
87#define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
88
89#define AUART_CTRL2_DMAONERR (1 << 26)
90#define AUART_CTRL2_TXDMAE (1 << 25)
91#define AUART_CTRL2_RXDMAE (1 << 24)
Sascha Hauer47d37d62011-01-11 15:54:54 +010092
93#define AUART_CTRL2_CTSEN (1 << 15)
Huang Shijie00592022012-08-08 10:37:59 +080094#define AUART_CTRL2_RTSEN (1 << 14)
Sascha Hauer47d37d62011-01-11 15:54:54 +010095#define AUART_CTRL2_RTS (1 << 11)
96#define AUART_CTRL2_RXE (1 << 9)
97#define AUART_CTRL2_TXE (1 << 8)
98#define AUART_CTRL2_UARTEN (1 << 0)
99
100#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
101#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
102#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
103#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
104#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
105#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
106#define AUART_LINECTRL_WLEN_MASK 0x00000060
107#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
108#define AUART_LINECTRL_FEN (1 << 4)
109#define AUART_LINECTRL_STP2 (1 << 3)
110#define AUART_LINECTRL_EPS (1 << 2)
111#define AUART_LINECTRL_PEN (1 << 1)
112#define AUART_LINECTRL_BRK (1 << 0)
113
114#define AUART_INTR_RTIEN (1 << 22)
115#define AUART_INTR_TXIEN (1 << 21)
116#define AUART_INTR_RXIEN (1 << 20)
117#define AUART_INTR_CTSMIEN (1 << 17)
118#define AUART_INTR_RTIS (1 << 6)
119#define AUART_INTR_TXIS (1 << 5)
120#define AUART_INTR_RXIS (1 << 4)
121#define AUART_INTR_CTSMIS (1 << 1)
122
123#define AUART_STAT_BUSY (1 << 29)
124#define AUART_STAT_CTS (1 << 28)
125#define AUART_STAT_TXFE (1 << 27)
126#define AUART_STAT_TXFF (1 << 25)
127#define AUART_STAT_RXFE (1 << 24)
128#define AUART_STAT_OERR (1 << 19)
129#define AUART_STAT_BERR (1 << 18)
130#define AUART_STAT_PERR (1 << 17)
131#define AUART_STAT_FERR (1 << 16)
Huang Shijiee8001632012-11-16 16:03:53 +0800132#define AUART_STAT_RXCOUNT_MASK 0xffff
Sascha Hauer47d37d62011-01-11 15:54:54 +0100133
134static struct uart_driver auart_driver;
135
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800136enum mxs_auart_type {
137 IMX23_AUART,
138 IMX28_AUART,
139};
140
Sascha Hauer47d37d62011-01-11 15:54:54 +0100141struct mxs_auart_port {
142 struct uart_port port;
143
Huang Shijiee8001632012-11-16 16:03:53 +0800144#define MXS_AUART_DMA_ENABLED 0x2
145#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
146#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
Huang Shijie8418e672013-08-03 10:09:14 -0400147#define MXS_AUART_RTSCTS 4 /* bit 4 */
Huang Shijiee8001632012-11-16 16:03:53 +0800148 unsigned long flags;
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800149 enum mxs_auart_type devtype;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100150
151 unsigned int irq;
152
153 struct clk *clk;
154 struct device *dev;
Huang Shijiee8001632012-11-16 16:03:53 +0800155
156 /* for DMA */
Huang Shijiee8001632012-11-16 16:03:53 +0800157 struct scatterlist tx_sgl;
158 struct dma_chan *tx_dma_chan;
159 void *tx_dma_buf;
160
161 struct scatterlist rx_sgl;
162 struct dma_chan *rx_dma_chan;
163 void *rx_dma_buf;
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200164
165 struct mctrl_gpios *gpios;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100166};
167
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800168static struct platform_device_id mxs_auart_devtype[] = {
169 { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
170 { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
171 { /* sentinel */ }
172};
173MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
174
175static struct of_device_id mxs_auart_dt_ids[] = {
176 {
177 .compatible = "fsl,imx28-auart",
178 .data = &mxs_auart_devtype[IMX28_AUART]
179 }, {
180 .compatible = "fsl,imx23-auart",
181 .data = &mxs_auart_devtype[IMX23_AUART]
182 }, { /* sentinel */ }
183};
184MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
185
186static inline int is_imx28_auart(struct mxs_auart_port *s)
187{
188 return s->devtype == IMX28_AUART;
189}
190
Huang Shijiee8001632012-11-16 16:03:53 +0800191static inline bool auart_dma_enabled(struct mxs_auart_port *s)
192{
193 return s->flags & MXS_AUART_DMA_ENABLED;
194}
195
Sascha Hauer47d37d62011-01-11 15:54:54 +0100196static void mxs_auart_stop_tx(struct uart_port *u);
197
198#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
199
Huang Shijiee8001632012-11-16 16:03:53 +0800200static void mxs_auart_tx_chars(struct mxs_auart_port *s);
201
202static void dma_tx_callback(void *param)
203{
204 struct mxs_auart_port *s = param;
205 struct circ_buf *xmit = &s->port.state->xmit;
206
207 dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
208
209 /* clear the bit used to serialize the DMA tx. */
210 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100211 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800212
213 /* wake up the possible processes. */
214 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
215 uart_write_wakeup(&s->port);
216
217 mxs_auart_tx_chars(s);
218}
219
220static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
221{
222 struct dma_async_tx_descriptor *desc;
223 struct scatterlist *sgl = &s->tx_sgl;
224 struct dma_chan *channel = s->tx_dma_chan;
225 u32 pio;
226
227 /* [1] : send PIO. Note, the first pio word is CTRL1. */
228 pio = AUART_CTRL1_XFER_COUNT(size);
229 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
230 1, DMA_TRANS_NONE, 0);
231 if (!desc) {
232 dev_err(s->dev, "step 1 error\n");
233 return -EINVAL;
234 }
235
236 /* [2] : set DMA buffer. */
237 sg_init_one(sgl, s->tx_dma_buf, size);
238 dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
239 desc = dmaengine_prep_slave_sg(channel, sgl,
240 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
241 if (!desc) {
242 dev_err(s->dev, "step 2 error\n");
243 return -EINVAL;
244 }
245
246 /* [3] : submit the DMA */
247 desc->callback = dma_tx_callback;
248 desc->callback_param = s;
249 dmaengine_submit(desc);
250 dma_async_issue_pending(channel);
251 return 0;
252}
253
254static void mxs_auart_tx_chars(struct mxs_auart_port *s)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100255{
256 struct circ_buf *xmit = &s->port.state->xmit;
257
Huang Shijiee8001632012-11-16 16:03:53 +0800258 if (auart_dma_enabled(s)) {
fabio.estevam@freescale.com87b8bed2013-01-07 23:11:06 -0200259 u32 i = 0;
Huang Shijiee8001632012-11-16 16:03:53 +0800260 int size;
261 void *buffer = s->tx_dma_buf;
262
263 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
264 return;
265
266 while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
267 size = min_t(u32, UART_XMIT_SIZE - i,
268 CIRC_CNT_TO_END(xmit->head,
269 xmit->tail,
270 UART_XMIT_SIZE));
271 memcpy(buffer + i, xmit->buf + xmit->tail, size);
272 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
273
274 i += size;
275 if (i >= UART_XMIT_SIZE)
276 break;
277 }
278
279 if (uart_tx_stopped(&s->port))
280 mxs_auart_stop_tx(&s->port);
281
282 if (i) {
283 mxs_auart_dma_tx(s, i);
284 } else {
285 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100286 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800287 }
288 return;
289 }
290
291
Sascha Hauer47d37d62011-01-11 15:54:54 +0100292 while (!(readl(s->port.membase + AUART_STAT) &
293 AUART_STAT_TXFF)) {
294 if (s->port.x_char) {
295 s->port.icount.tx++;
296 writel(s->port.x_char,
297 s->port.membase + AUART_DATA);
298 s->port.x_char = 0;
299 continue;
300 }
301 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
302 s->port.icount.tx++;
303 writel(xmit->buf[xmit->tail],
304 s->port.membase + AUART_DATA);
305 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100306 } else
307 break;
308 }
Uwe Kleine-Königd0758a22011-11-22 14:22:56 +0100309 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
310 uart_write_wakeup(&s->port);
311
Sascha Hauer47d37d62011-01-11 15:54:54 +0100312 if (uart_circ_empty(&(s->port.state->xmit)))
313 writel(AUART_INTR_TXIEN,
314 s->port.membase + AUART_INTR_CLR);
315 else
316 writel(AUART_INTR_TXIEN,
317 s->port.membase + AUART_INTR_SET);
318
319 if (uart_tx_stopped(&s->port))
320 mxs_auart_stop_tx(&s->port);
321}
322
323static void mxs_auart_rx_char(struct mxs_auart_port *s)
324{
325 int flag;
326 u32 stat;
327 u8 c;
328
329 c = readl(s->port.membase + AUART_DATA);
330 stat = readl(s->port.membase + AUART_STAT);
331
332 flag = TTY_NORMAL;
333 s->port.icount.rx++;
334
335 if (stat & AUART_STAT_BERR) {
336 s->port.icount.brk++;
337 if (uart_handle_break(&s->port))
338 goto out;
339 } else if (stat & AUART_STAT_PERR) {
340 s->port.icount.parity++;
341 } else if (stat & AUART_STAT_FERR) {
342 s->port.icount.frame++;
343 }
344
345 /*
346 * Mask off conditions which should be ingored.
347 */
348 stat &= s->port.read_status_mask;
349
350 if (stat & AUART_STAT_BERR) {
351 flag = TTY_BREAK;
352 } else if (stat & AUART_STAT_PERR)
353 flag = TTY_PARITY;
354 else if (stat & AUART_STAT_FERR)
355 flag = TTY_FRAME;
356
357 if (stat & AUART_STAT_OERR)
358 s->port.icount.overrun++;
359
360 if (uart_handle_sysrq_char(&s->port, c))
361 goto out;
362
363 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
364out:
365 writel(stat, s->port.membase + AUART_STAT);
366}
367
368static void mxs_auart_rx_chars(struct mxs_auart_port *s)
369{
Sascha Hauer47d37d62011-01-11 15:54:54 +0100370 u32 stat = 0;
371
372 for (;;) {
373 stat = readl(s->port.membase + AUART_STAT);
374 if (stat & AUART_STAT_RXFE)
375 break;
376 mxs_auart_rx_char(s);
377 }
378
379 writel(stat, s->port.membase + AUART_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100380 tty_flip_buffer_push(&s->port.state->port);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100381}
382
383static int mxs_auart_request_port(struct uart_port *u)
384{
385 return 0;
386}
387
388static int mxs_auart_verify_port(struct uart_port *u,
389 struct serial_struct *ser)
390{
391 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
392 return -EINVAL;
393 return 0;
394}
395
396static void mxs_auart_config_port(struct uart_port *u, int flags)
397{
398}
399
400static const char *mxs_auart_type(struct uart_port *u)
401{
402 struct mxs_auart_port *s = to_auart_port(u);
403
404 return dev_name(s->dev);
405}
406
407static void mxs_auart_release_port(struct uart_port *u)
408{
409}
410
411static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
412{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200413 struct mxs_auart_port *s = to_auart_port(u);
414
Sascha Hauer47d37d62011-01-11 15:54:54 +0100415 u32 ctrl = readl(u->membase + AUART_CTRL2);
416
Steffen Trumtrara6833212012-12-13 14:27:43 +0100417 ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
Huang Shijie00592022012-08-08 10:37:59 +0800418 if (mctrl & TIOCM_RTS) {
Peter Hurley299245a2014-09-10 15:06:24 -0400419 if (uart_cts_enabled(u))
Huang Shijie00592022012-08-08 10:37:59 +0800420 ctrl |= AUART_CTRL2_RTSEN;
Steffen Trumtrara6833212012-12-13 14:27:43 +0100421 else
422 ctrl |= AUART_CTRL2_RTS;
Huang Shijie00592022012-08-08 10:37:59 +0800423 }
424
Sascha Hauer47d37d62011-01-11 15:54:54 +0100425 writel(ctrl, u->membase + AUART_CTRL2);
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200426
427 mctrl_gpio_set(s->gpios, mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100428}
429
430static u32 mxs_auart_get_mctrl(struct uart_port *u)
431{
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200432 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100433 u32 stat = readl(u->membase + AUART_STAT);
Janusz Uzycki42b4eba2014-10-10 18:53:24 +0200434 u32 mctrl = 0;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100435
Sascha Hauer47d37d62011-01-11 15:54:54 +0100436 if (stat & AUART_STAT_CTS)
437 mctrl |= TIOCM_CTS;
438
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200439 return mctrl_gpio_get(s->gpios, &mctrl);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100440}
441
Huang Shijiee8001632012-11-16 16:03:53 +0800442static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
443static void dma_rx_callback(void *arg)
444{
445 struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100446 struct tty_port *port = &s->port.state->port;
Huang Shijiee8001632012-11-16 16:03:53 +0800447 int count;
448 u32 stat;
449
Huang Shijied7ffb932012-11-22 15:06:30 +0800450 dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
451
Huang Shijiee8001632012-11-16 16:03:53 +0800452 stat = readl(s->port.membase + AUART_STAT);
453 stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
454 AUART_STAT_PERR | AUART_STAT_FERR);
455
456 count = stat & AUART_STAT_RXCOUNT_MASK;
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100457 tty_insert_flip_string(port, s->rx_dma_buf, count);
Huang Shijiee8001632012-11-16 16:03:53 +0800458
459 writel(stat, s->port.membase + AUART_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100460 tty_flip_buffer_push(port);
Huang Shijiee8001632012-11-16 16:03:53 +0800461
462 /* start the next DMA for RX. */
463 mxs_auart_dma_prep_rx(s);
464}
465
466static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
467{
468 struct dma_async_tx_descriptor *desc;
469 struct scatterlist *sgl = &s->rx_sgl;
470 struct dma_chan *channel = s->rx_dma_chan;
471 u32 pio[1];
472
473 /* [1] : send PIO */
474 pio[0] = AUART_CTRL0_RXTO_ENABLE
475 | AUART_CTRL0_RXTIMEOUT(0x80)
476 | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
477 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
478 1, DMA_TRANS_NONE, 0);
479 if (!desc) {
480 dev_err(s->dev, "step 1 error\n");
481 return -EINVAL;
482 }
483
484 /* [2] : send DMA request */
485 sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
486 dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
487 desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
488 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
489 if (!desc) {
490 dev_err(s->dev, "step 2 error\n");
491 return -1;
492 }
493
494 /* [3] : submit the DMA, but do not issue it. */
495 desc->callback = dma_rx_callback;
496 desc->callback_param = s;
497 dmaengine_submit(desc);
498 dma_async_issue_pending(channel);
499 return 0;
500}
501
502static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
503{
504 if (s->tx_dma_chan) {
505 dma_release_channel(s->tx_dma_chan);
506 s->tx_dma_chan = NULL;
507 }
508 if (s->rx_dma_chan) {
509 dma_release_channel(s->rx_dma_chan);
510 s->rx_dma_chan = NULL;
511 }
512
513 kfree(s->tx_dma_buf);
514 kfree(s->rx_dma_buf);
515 s->tx_dma_buf = NULL;
516 s->rx_dma_buf = NULL;
517}
518
519static void mxs_auart_dma_exit(struct mxs_auart_port *s)
520{
521
522 writel(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
523 s->port.membase + AUART_CTRL2_CLR);
524
525 mxs_auart_dma_exit_channel(s);
526 s->flags &= ~MXS_AUART_DMA_ENABLED;
527 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
528 clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
529}
530
531static int mxs_auart_dma_init(struct mxs_auart_port *s)
532{
Huang Shijiee8001632012-11-16 16:03:53 +0800533 if (auart_dma_enabled(s))
534 return 0;
535
Huang Shijiee8001632012-11-16 16:03:53 +0800536 /* init for RX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800537 s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
Huang Shijiee8001632012-11-16 16:03:53 +0800538 if (!s->rx_dma_chan)
539 goto err_out;
540 s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
541 if (!s->rx_dma_buf)
542 goto err_out;
543
544 /* init for TX */
Shawn Guobcc20f92013-02-26 13:47:41 +0800545 s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
Huang Shijiee8001632012-11-16 16:03:53 +0800546 if (!s->tx_dma_chan)
547 goto err_out;
548 s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
549 if (!s->tx_dma_buf)
550 goto err_out;
551
552 /* set the flags */
553 s->flags |= MXS_AUART_DMA_ENABLED;
554 dev_dbg(s->dev, "enabled the DMA support.");
555
Hector Palacios9987f762013-10-03 09:32:03 +0200556 /* The DMA buffer is now the FIFO the TTY subsystem can use */
557 s->port.fifosize = UART_XMIT_SIZE;
558
Huang Shijiee8001632012-11-16 16:03:53 +0800559 return 0;
560
561err_out:
562 mxs_auart_dma_exit_channel(s);
563 return -EINVAL;
564
565}
566
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200567#define RTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
568 UART_GPIO_RTS))
569#define CTS_AT_AUART() IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(s->gpios, \
570 UART_GPIO_CTS))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100571static void mxs_auart_settermios(struct uart_port *u,
572 struct ktermios *termios,
573 struct ktermios *old)
574{
Huang Shijiee8001632012-11-16 16:03:53 +0800575 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100576 u32 bm, ctrl, ctrl2, div;
577 unsigned int cflag, baud;
578
579 cflag = termios->c_cflag;
580
581 ctrl = AUART_LINECTRL_FEN;
582 ctrl2 = readl(u->membase + AUART_CTRL2);
583
584 /* byte size */
585 switch (cflag & CSIZE) {
586 case CS5:
587 bm = 0;
588 break;
589 case CS6:
590 bm = 1;
591 break;
592 case CS7:
593 bm = 2;
594 break;
595 case CS8:
596 bm = 3;
597 break;
598 default:
599 return;
600 }
601
602 ctrl |= AUART_LINECTRL_WLEN(bm);
603
604 /* parity */
605 if (cflag & PARENB) {
606 ctrl |= AUART_LINECTRL_PEN;
607 if ((cflag & PARODD) == 0)
608 ctrl |= AUART_LINECTRL_EPS;
609 }
610
611 u->read_status_mask = 0;
612
613 if (termios->c_iflag & INPCK)
614 u->read_status_mask |= AUART_STAT_PERR;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400615 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100616 u->read_status_mask |= AUART_STAT_BERR;
617
618 /*
619 * Characters to ignore
620 */
621 u->ignore_status_mask = 0;
622 if (termios->c_iflag & IGNPAR)
623 u->ignore_status_mask |= AUART_STAT_PERR;
624 if (termios->c_iflag & IGNBRK) {
625 u->ignore_status_mask |= AUART_STAT_BERR;
626 /*
627 * If we're ignoring parity and break indicators,
628 * ignore overruns too (for real raw support).
629 */
630 if (termios->c_iflag & IGNPAR)
631 u->ignore_status_mask |= AUART_STAT_OERR;
632 }
633
634 /*
635 * ignore all characters if CREAD is not set
636 */
637 if (cflag & CREAD)
638 ctrl2 |= AUART_CTRL2_RXE;
639 else
640 ctrl2 &= ~AUART_CTRL2_RXE;
641
642 /* figure out the stop bits requested */
643 if (cflag & CSTOPB)
644 ctrl |= AUART_LINECTRL_STP2;
645
646 /* figure out the hardware flow control settings */
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200647 ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
Huang Shijiee8001632012-11-16 16:03:53 +0800648 if (cflag & CRTSCTS) {
649 /*
650 * The DMA has a bug(see errata:2836) in mx23.
651 * So we can not implement the DMA for auart in mx23,
652 * we can only implement the DMA support for auart
653 * in mx28.
654 */
Huang Shijieafab2202013-08-03 10:09:15 -0400655 if (is_imx28_auart(s)
Huang Shijie8418e672013-08-03 10:09:14 -0400656 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
Huang Shijiee8001632012-11-16 16:03:53 +0800657 if (!mxs_auart_dma_init(s))
658 /* enable DMA tranfer */
659 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
660 | AUART_CTRL2_DMAONERR;
661 }
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200662 /* Even if RTS is GPIO line RTSEN can be enabled because
663 * the pinctrl configuration decides about RTS pin function */
664 ctrl2 |= AUART_CTRL2_RTSEN;
665 if (CTS_AT_AUART())
666 ctrl2 |= AUART_CTRL2_CTSEN;
Huang Shijiee8001632012-11-16 16:03:53 +0800667 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100668
669 /* set baud rate */
670 baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
671 div = u->uartclk * 32 / baud;
672 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
673 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
674
675 writel(ctrl, u->membase + AUART_LINECTRL);
676 writel(ctrl2, u->membase + AUART_CTRL2);
Lothar Waßmann8b979f72012-05-03 11:37:12 +0200677
678 uart_update_timeout(u, termios->c_cflag, baud);
Huang Shijiee8001632012-11-16 16:03:53 +0800679
680 /* prepare for the DMA RX. */
681 if (auart_dma_enabled(s) &&
682 !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
683 if (!mxs_auart_dma_prep_rx(s)) {
684 /* Disable the normal RX interrupt. */
Huang Shijiea5919442012-11-22 15:06:29 +0800685 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
686 u->membase + AUART_INTR_CLR);
Huang Shijiee8001632012-11-16 16:03:53 +0800687 } else {
688 mxs_auart_dma_exit(s);
689 dev_err(s->dev, "We can not start up the DMA.\n");
690 }
691 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100692}
693
694static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
695{
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200696 u32 istat;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100697 struct mxs_auart_port *s = context;
698 u32 stat = readl(s->port.membase + AUART_STAT);
699
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200700 istat = readl(s->port.membase + AUART_INTR);
701
702 /* ack irq */
703 writel(istat & (AUART_INTR_RTIS
704 | AUART_INTR_TXIS
705 | AUART_INTR_RXIS
706 | AUART_INTR_CTSMIS),
707 s->port.membase + AUART_INTR_CLR);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100708
709 if (istat & AUART_INTR_CTSMIS) {
Janusz Uzycki7c573d72014-10-10 18:53:25 +0200710 if (CTS_AT_AUART())
711 uart_handle_cts_change(&s->port,
712 stat & AUART_STAT_CTS);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100713 writel(AUART_INTR_CTSMIS,
714 s->port.membase + AUART_INTR_CLR);
715 istat &= ~AUART_INTR_CTSMIS;
716 }
717
718 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
Huang Shijiea5919442012-11-22 15:06:29 +0800719 if (!auart_dma_enabled(s))
720 mxs_auart_rx_chars(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100721 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
722 }
723
724 if (istat & AUART_INTR_TXIS) {
725 mxs_auart_tx_chars(s);
726 istat &= ~AUART_INTR_TXIS;
727 }
728
Sascha Hauer47d37d62011-01-11 15:54:54 +0100729 return IRQ_HANDLED;
730}
731
732static void mxs_auart_reset(struct uart_port *u)
733{
734 int i;
735 unsigned int reg;
736
737 writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
738
739 for (i = 0; i < 10000; i++) {
740 reg = readl(u->membase + AUART_CTRL0);
741 if (!(reg & AUART_CTRL0_SFTRST))
742 break;
743 udelay(3);
744 }
745 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
746}
747
748static int mxs_auart_startup(struct uart_port *u)
749{
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200750 int ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100751 struct mxs_auart_port *s = to_auart_port(u);
752
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200753 ret = clk_prepare_enable(s->clk);
754 if (ret)
755 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100756
757 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
758
759 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
760
761 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
762 u->membase + AUART_INTR);
763
Hector Palacios9987f762013-10-03 09:32:03 +0200764 /* Reset FIFO size (it could have changed if DMA was enabled) */
765 u->fifosize = MXS_AUART_FIFO_SIZE;
766
Sascha Hauer47d37d62011-01-11 15:54:54 +0100767 /*
768 * Enable fifo so all four bytes of a DMA word are written to
769 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
770 */
771 writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
772
773 return 0;
774}
775
776static void mxs_auart_shutdown(struct uart_port *u)
777{
778 struct mxs_auart_port *s = to_auart_port(u);
779
Huang Shijiee8001632012-11-16 16:03:53 +0800780 if (auart_dma_enabled(s))
781 mxs_auart_dma_exit(s);
782
Sascha Hauer47d37d62011-01-11 15:54:54 +0100783 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
784
Sascha Hauer47d37d62011-01-11 15:54:54 +0100785 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
786 u->membase + AUART_INTR_CLR);
787
Huang Shijie851b7142012-09-06 22:38:40 -0400788 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
789
Shawn Guoa4813772011-12-20 14:10:29 +0800790 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100791}
792
793static unsigned int mxs_auart_tx_empty(struct uart_port *u)
794{
795 if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
796 return TIOCSER_TEMT;
797 else
798 return 0;
799}
800
801static void mxs_auart_start_tx(struct uart_port *u)
802{
803 struct mxs_auart_port *s = to_auart_port(u);
804
805 /* enable transmitter */
806 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
807
808 mxs_auart_tx_chars(s);
809}
810
811static void mxs_auart_stop_tx(struct uart_port *u)
812{
813 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
814}
815
816static void mxs_auart_stop_rx(struct uart_port *u)
817{
818 writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
819}
820
821static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
822{
823 if (ctl)
824 writel(AUART_LINECTRL_BRK,
825 u->membase + AUART_LINECTRL_SET);
826 else
827 writel(AUART_LINECTRL_BRK,
828 u->membase + AUART_LINECTRL_CLR);
829}
830
Sascha Hauer47d37d62011-01-11 15:54:54 +0100831static struct uart_ops mxs_auart_ops = {
832 .tx_empty = mxs_auart_tx_empty,
833 .start_tx = mxs_auart_start_tx,
834 .stop_tx = mxs_auart_stop_tx,
835 .stop_rx = mxs_auart_stop_rx,
Sascha Hauer47d37d62011-01-11 15:54:54 +0100836 .break_ctl = mxs_auart_break_ctl,
837 .set_mctrl = mxs_auart_set_mctrl,
838 .get_mctrl = mxs_auart_get_mctrl,
839 .startup = mxs_auart_startup,
840 .shutdown = mxs_auart_shutdown,
841 .set_termios = mxs_auart_settermios,
842 .type = mxs_auart_type,
843 .release_port = mxs_auart_release_port,
844 .request_port = mxs_auart_request_port,
845 .config_port = mxs_auart_config_port,
846 .verify_port = mxs_auart_verify_port,
847};
848
849static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
850
851#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
852static void mxs_auart_console_putchar(struct uart_port *port, int ch)
853{
854 unsigned int to = 1000;
855
856 while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
857 if (!to--)
858 break;
859 udelay(1);
860 }
861
862 writel(ch, port->membase + AUART_DATA);
863}
864
865static void
866auart_console_write(struct console *co, const char *str, unsigned int count)
867{
868 struct mxs_auart_port *s;
869 struct uart_port *port;
870 unsigned int old_ctrl0, old_ctrl2;
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200871 unsigned int to = 20000;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100872
Wolfram Sang4829e762013-04-19 21:12:17 +0200873 if (co->index >= MXS_AUART_PORTS || co->index < 0)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100874 return;
875
876 s = auart_port[co->index];
877 port = &s->port;
878
879 clk_enable(s->clk);
880
881 /* First save the CR then disable the interrupts */
882 old_ctrl2 = readl(port->membase + AUART_CTRL2);
883 old_ctrl0 = readl(port->membase + AUART_CTRL0);
884
885 writel(AUART_CTRL0_CLKGATE,
886 port->membase + AUART_CTRL0_CLR);
887 writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
888 port->membase + AUART_CTRL2_SET);
889
890 uart_console_write(port, str, count, mxs_auart_console_putchar);
891
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200892 /* Finally, wait for transmitter to become empty ... */
Sascha Hauer47d37d62011-01-11 15:54:54 +0100893 while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200894 udelay(1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100895 if (!to--)
896 break;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100897 }
898
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200899 /*
900 * ... and restore the TCR if we waited long enough for the transmitter
901 * to be idle. This might keep the transmitter enabled although it is
902 * unused, but that is better than to disable it while it is still
903 * transmitting.
904 */
905 if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
906 writel(old_ctrl0, port->membase + AUART_CTRL0);
907 writel(old_ctrl2, port->membase + AUART_CTRL2);
908 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100909
910 clk_disable(s->clk);
911}
912
913static void __init
914auart_console_get_options(struct uart_port *port, int *baud,
915 int *parity, int *bits)
916{
917 unsigned int lcr_h, quot;
918
919 if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
920 return;
921
922 lcr_h = readl(port->membase + AUART_LINECTRL);
923
924 *parity = 'n';
925 if (lcr_h & AUART_LINECTRL_PEN) {
926 if (lcr_h & AUART_LINECTRL_EPS)
927 *parity = 'e';
928 else
929 *parity = 'o';
930 }
931
932 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
933 *bits = 7;
934 else
935 *bits = 8;
936
937 quot = ((readl(port->membase + AUART_LINECTRL)
938 & AUART_LINECTRL_BAUD_DIVINT_MASK))
939 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
940 quot |= ((readl(port->membase + AUART_LINECTRL)
941 & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
942 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
943 if (quot == 0)
944 quot = 1;
945
946 *baud = (port->uartclk << 2) / quot;
947}
948
949static int __init
950auart_console_setup(struct console *co, char *options)
951{
952 struct mxs_auart_port *s;
953 int baud = 9600;
954 int bits = 8;
955 int parity = 'n';
956 int flow = 'n';
957 int ret;
958
959 /*
960 * Check whether an invalid uart number has been specified, and
961 * if so, search for the first available port that does have
962 * console support.
963 */
964 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
965 co->index = 0;
966 s = auart_port[co->index];
967 if (!s)
968 return -ENODEV;
969
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200970 ret = clk_prepare_enable(s->clk);
971 if (ret)
972 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100973
974 if (options)
975 uart_parse_options(options, &baud, &parity, &bits, &flow);
976 else
977 auart_console_get_options(&s->port, &baud, &parity, &bits);
978
979 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
980
Shawn Guoa4813772011-12-20 14:10:29 +0800981 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100982
983 return ret;
984}
985
986static struct console auart_console = {
987 .name = "ttyAPP",
988 .write = auart_console_write,
989 .device = uart_console_device,
990 .setup = auart_console_setup,
991 .flags = CON_PRINTBUFFER,
992 .index = -1,
993 .data = &auart_driver,
994};
995#endif
996
997static struct uart_driver auart_driver = {
998 .owner = THIS_MODULE,
999 .driver_name = "ttyAPP",
1000 .dev_name = "ttyAPP",
1001 .major = 0,
1002 .minor = 0,
1003 .nr = MXS_AUART_PORTS,
1004#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1005 .cons = &auart_console,
1006#endif
1007};
1008
Fabio Estevam1ea66072012-06-18 10:06:09 -03001009/*
1010 * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
1011 * could successfully get all information from dt or a negative errno.
1012 */
1013static int serial_mxs_probe_dt(struct mxs_auart_port *s,
1014 struct platform_device *pdev)
1015{
1016 struct device_node *np = pdev->dev.of_node;
1017 int ret;
1018
1019 if (!np)
1020 /* no device tree device */
1021 return 1;
1022
1023 ret = of_alias_get_id(np, "serial");
1024 if (ret < 0) {
1025 dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
1026 return ret;
1027 }
1028 s->port.line = ret;
1029
Huang Shijie8418e672013-08-03 10:09:14 -04001030 if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
1031 set_bit(MXS_AUART_RTSCTS, &s->flags);
1032
Fabio Estevam1ea66072012-06-18 10:06:09 -03001033 return 0;
1034}
1035
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001036static bool mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
1037{
1038 s->gpios = mctrl_gpio_init(dev, 0);
1039 if (IS_ERR_OR_NULL(s->gpios))
1040 return false;
1041
1042 /* Block (enabled before) DMA option if RTS or CTS is GPIO line */
1043 if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
1044 if (test_bit(MXS_AUART_RTSCTS, &s->flags))
1045 dev_warn(dev,
1046 "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
1047 clear_bit(MXS_AUART_RTSCTS, &s->flags);
1048 }
1049
1050 return true;
1051}
1052
Bill Pemberton9671f092012-11-19 13:21:50 -05001053static int mxs_auart_probe(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001054{
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001055 const struct of_device_id *of_id =
1056 of_match_device(mxs_auart_dt_ids, &pdev->dev);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001057 struct mxs_auart_port *s;
1058 u32 version;
1059 int ret = 0;
1060 struct resource *r;
1061
1062 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
1063 if (!s) {
1064 ret = -ENOMEM;
1065 goto out;
1066 }
1067
Fabio Estevam1ea66072012-06-18 10:06:09 -03001068 ret = serial_mxs_probe_dt(s, pdev);
1069 if (ret > 0)
1070 s->port.line = pdev->id < 0 ? 0 : pdev->id;
1071 else if (ret < 0)
1072 goto out_free;
1073
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001074 if (of_id) {
1075 pdev->id_entry = of_id->data;
1076 s->devtype = pdev->id_entry->driver_data;
1077 }
1078
Sascha Hauer47d37d62011-01-11 15:54:54 +01001079 s->clk = clk_get(&pdev->dev, NULL);
1080 if (IS_ERR(s->clk)) {
1081 ret = PTR_ERR(s->clk);
1082 goto out_free;
1083 }
1084
1085 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1086 if (!r) {
1087 ret = -ENXIO;
1088 goto out_free_clk;
1089 }
1090
1091 s->port.mapbase = r->start;
1092 s->port.membase = ioremap(r->start, resource_size(r));
1093 s->port.ops = &mxs_auart_ops;
1094 s->port.iotype = UPIO_MEM;
Hector Palacios9987f762013-10-03 09:32:03 +02001095 s->port.fifosize = MXS_AUART_FIFO_SIZE;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001096 s->port.uartclk = clk_get_rate(s->clk);
1097 s->port.type = PORT_IMX;
Wolfram Sang4c24f2c2013-04-19 21:06:20 +02001098 s->port.dev = s->dev = &pdev->dev;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001099
Sascha Hauer47d37d62011-01-11 15:54:54 +01001100 s->irq = platform_get_irq(pdev, 0);
1101 s->port.irq = s->irq;
1102 ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
1103 if (ret)
1104 goto out_free_clk;
1105
1106 platform_set_drvdata(pdev, s);
1107
Janusz Uzycki7c573d72014-10-10 18:53:25 +02001108 if (!mxs_auart_init_gpios(s, &pdev->dev))
1109 dev_err(&pdev->dev,
1110 "Failed to initialize GPIOs. The serial port may not work as expected\n");
1111
Fabio Estevam1ea66072012-06-18 10:06:09 -03001112 auart_port[s->port.line] = s;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001113
1114 mxs_auart_reset(&s->port);
1115
1116 ret = uart_add_one_port(&auart_driver, &s->port);
1117 if (ret)
1118 goto out_free_irq;
1119
1120 version = readl(s->port.membase + AUART_VERSION);
1121 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
1122 (version >> 24) & 0xff,
1123 (version >> 16) & 0xff, version & 0xffff);
1124
1125 return 0;
1126
1127out_free_irq:
1128 auart_port[pdev->id] = NULL;
1129 free_irq(s->irq, s);
1130out_free_clk:
1131 clk_put(s->clk);
1132out_free:
1133 kfree(s);
1134out:
1135 return ret;
1136}
1137
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001138static int mxs_auart_remove(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001139{
1140 struct mxs_auart_port *s = platform_get_drvdata(pdev);
1141
1142 uart_remove_one_port(&auart_driver, &s->port);
1143
1144 auart_port[pdev->id] = NULL;
1145
1146 clk_put(s->clk);
1147 free_irq(s->irq, s);
1148 kfree(s);
1149
1150 return 0;
1151}
1152
1153static struct platform_driver mxs_auart_driver = {
1154 .probe = mxs_auart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001155 .remove = mxs_auart_remove,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001156 .driver = {
1157 .name = "mxs-auart",
1158 .owner = THIS_MODULE,
Fabio Estevam1ea66072012-06-18 10:06:09 -03001159 .of_match_table = mxs_auart_dt_ids,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001160 },
1161};
1162
1163static int __init mxs_auart_init(void)
1164{
1165 int r;
1166
1167 r = uart_register_driver(&auart_driver);
1168 if (r)
1169 goto out;
1170
1171 r = platform_driver_register(&mxs_auart_driver);
1172 if (r)
1173 goto out_err;
1174
1175 return 0;
1176out_err:
1177 uart_unregister_driver(&auart_driver);
1178out:
1179 return r;
1180}
1181
1182static void __exit mxs_auart_exit(void)
1183{
1184 platform_driver_unregister(&mxs_auart_driver);
1185 uart_unregister_driver(&auart_driver);
1186}
1187
1188module_init(mxs_auart_init);
1189module_exit(mxs_auart_exit);
1190MODULE_LICENSE("GPL");
1191MODULE_DESCRIPTION("Freescale MXS application uart driver");
Fabio Estevam1ea66072012-06-18 10:06:09 -03001192MODULE_ALIAS("platform:mxs-auart");