blob: 0296c1c3b70e7350e578eda01b0d4e7cd696ca11 [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
45#define MXS_AUART_PORTS 5
Hector Palacios9987f762013-10-03 09:32:03 +020046#define MXS_AUART_FIFO_SIZE 16
Sascha Hauer47d37d62011-01-11 15:54:54 +010047
48#define AUART_CTRL0 0x00000000
49#define AUART_CTRL0_SET 0x00000004
50#define AUART_CTRL0_CLR 0x00000008
51#define AUART_CTRL0_TOG 0x0000000c
52#define AUART_CTRL1 0x00000010
53#define AUART_CTRL1_SET 0x00000014
54#define AUART_CTRL1_CLR 0x00000018
55#define AUART_CTRL1_TOG 0x0000001c
56#define AUART_CTRL2 0x00000020
57#define AUART_CTRL2_SET 0x00000024
58#define AUART_CTRL2_CLR 0x00000028
59#define AUART_CTRL2_TOG 0x0000002c
60#define AUART_LINECTRL 0x00000030
61#define AUART_LINECTRL_SET 0x00000034
62#define AUART_LINECTRL_CLR 0x00000038
63#define AUART_LINECTRL_TOG 0x0000003c
64#define AUART_LINECTRL2 0x00000040
65#define AUART_LINECTRL2_SET 0x00000044
66#define AUART_LINECTRL2_CLR 0x00000048
67#define AUART_LINECTRL2_TOG 0x0000004c
68#define AUART_INTR 0x00000050
69#define AUART_INTR_SET 0x00000054
70#define AUART_INTR_CLR 0x00000058
71#define AUART_INTR_TOG 0x0000005c
72#define AUART_DATA 0x00000060
73#define AUART_STAT 0x00000070
74#define AUART_DEBUG 0x00000080
75#define AUART_VERSION 0x00000090
76#define AUART_AUTOBAUD 0x000000a0
77
78#define AUART_CTRL0_SFTRST (1 << 31)
79#define AUART_CTRL0_CLKGATE (1 << 30)
Huang Shijiee8001632012-11-16 16:03:53 +080080#define AUART_CTRL0_RXTO_ENABLE (1 << 27)
81#define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
82#define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
83
84#define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
85
86#define AUART_CTRL2_DMAONERR (1 << 26)
87#define AUART_CTRL2_TXDMAE (1 << 25)
88#define AUART_CTRL2_RXDMAE (1 << 24)
Sascha Hauer47d37d62011-01-11 15:54:54 +010089
90#define AUART_CTRL2_CTSEN (1 << 15)
Huang Shijie00592022012-08-08 10:37:59 +080091#define AUART_CTRL2_RTSEN (1 << 14)
Sascha Hauer47d37d62011-01-11 15:54:54 +010092#define AUART_CTRL2_RTS (1 << 11)
93#define AUART_CTRL2_RXE (1 << 9)
94#define AUART_CTRL2_TXE (1 << 8)
95#define AUART_CTRL2_UARTEN (1 << 0)
96
97#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
98#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
99#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
100#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
101#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
102#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
103#define AUART_LINECTRL_WLEN_MASK 0x00000060
104#define AUART_LINECTRL_WLEN(v) (((v) & 0x3) << 5)
105#define AUART_LINECTRL_FEN (1 << 4)
106#define AUART_LINECTRL_STP2 (1 << 3)
107#define AUART_LINECTRL_EPS (1 << 2)
108#define AUART_LINECTRL_PEN (1 << 1)
109#define AUART_LINECTRL_BRK (1 << 0)
110
111#define AUART_INTR_RTIEN (1 << 22)
112#define AUART_INTR_TXIEN (1 << 21)
113#define AUART_INTR_RXIEN (1 << 20)
114#define AUART_INTR_CTSMIEN (1 << 17)
115#define AUART_INTR_RTIS (1 << 6)
116#define AUART_INTR_TXIS (1 << 5)
117#define AUART_INTR_RXIS (1 << 4)
118#define AUART_INTR_CTSMIS (1 << 1)
119
120#define AUART_STAT_BUSY (1 << 29)
121#define AUART_STAT_CTS (1 << 28)
122#define AUART_STAT_TXFE (1 << 27)
123#define AUART_STAT_TXFF (1 << 25)
124#define AUART_STAT_RXFE (1 << 24)
125#define AUART_STAT_OERR (1 << 19)
126#define AUART_STAT_BERR (1 << 18)
127#define AUART_STAT_PERR (1 << 17)
128#define AUART_STAT_FERR (1 << 16)
Huang Shijiee8001632012-11-16 16:03:53 +0800129#define AUART_STAT_RXCOUNT_MASK 0xffff
Sascha Hauer47d37d62011-01-11 15:54:54 +0100130
131static struct uart_driver auart_driver;
132
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800133enum mxs_auart_type {
134 IMX23_AUART,
135 IMX28_AUART,
136};
137
Sascha Hauer47d37d62011-01-11 15:54:54 +0100138struct mxs_auart_port {
139 struct uart_port port;
140
Huang Shijiee8001632012-11-16 16:03:53 +0800141#define MXS_AUART_DMA_ENABLED 0x2
142#define MXS_AUART_DMA_TX_SYNC 2 /* bit 2 */
143#define MXS_AUART_DMA_RX_READY 3 /* bit 3 */
Huang Shijie8418e672013-08-03 10:09:14 -0400144#define MXS_AUART_RTSCTS 4 /* bit 4 */
Huang Shijiee8001632012-11-16 16:03:53 +0800145 unsigned long flags;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100146 unsigned int ctrl;
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800147 enum mxs_auart_type devtype;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100148
149 unsigned int irq;
150
151 struct clk *clk;
152 struct device *dev;
Huang Shijiee8001632012-11-16 16:03:53 +0800153
154 /* for DMA */
Huang Shijiee8001632012-11-16 16:03:53 +0800155 struct scatterlist tx_sgl;
156 struct dma_chan *tx_dma_chan;
157 void *tx_dma_buf;
158
159 struct scatterlist rx_sgl;
160 struct dma_chan *rx_dma_chan;
161 void *rx_dma_buf;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100162};
163
Huang Shijief4b1f03b2012-11-16 16:03:52 +0800164static struct platform_device_id mxs_auart_devtype[] = {
165 { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
166 { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
167 { /* sentinel */ }
168};
169MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);
170
171static struct of_device_id mxs_auart_dt_ids[] = {
172 {
173 .compatible = "fsl,imx28-auart",
174 .data = &mxs_auart_devtype[IMX28_AUART]
175 }, {
176 .compatible = "fsl,imx23-auart",
177 .data = &mxs_auart_devtype[IMX23_AUART]
178 }, { /* sentinel */ }
179};
180MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
181
182static inline int is_imx28_auart(struct mxs_auart_port *s)
183{
184 return s->devtype == IMX28_AUART;
185}
186
Huang Shijiee8001632012-11-16 16:03:53 +0800187static inline bool auart_dma_enabled(struct mxs_auart_port *s)
188{
189 return s->flags & MXS_AUART_DMA_ENABLED;
190}
191
Sascha Hauer47d37d62011-01-11 15:54:54 +0100192static void mxs_auart_stop_tx(struct uart_port *u);
193
194#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
195
Huang Shijiee8001632012-11-16 16:03:53 +0800196static void mxs_auart_tx_chars(struct mxs_auart_port *s);
197
198static void dma_tx_callback(void *param)
199{
200 struct mxs_auart_port *s = param;
201 struct circ_buf *xmit = &s->port.state->xmit;
202
203 dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
204
205 /* clear the bit used to serialize the DMA tx. */
206 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100207 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800208
209 /* wake up the possible processes. */
210 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
211 uart_write_wakeup(&s->port);
212
213 mxs_auart_tx_chars(s);
214}
215
216static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
217{
218 struct dma_async_tx_descriptor *desc;
219 struct scatterlist *sgl = &s->tx_sgl;
220 struct dma_chan *channel = s->tx_dma_chan;
221 u32 pio;
222
223 /* [1] : send PIO. Note, the first pio word is CTRL1. */
224 pio = AUART_CTRL1_XFER_COUNT(size);
225 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
226 1, DMA_TRANS_NONE, 0);
227 if (!desc) {
228 dev_err(s->dev, "step 1 error\n");
229 return -EINVAL;
230 }
231
232 /* [2] : set DMA buffer. */
233 sg_init_one(sgl, s->tx_dma_buf, size);
234 dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
235 desc = dmaengine_prep_slave_sg(channel, sgl,
236 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
237 if (!desc) {
238 dev_err(s->dev, "step 2 error\n");
239 return -EINVAL;
240 }
241
242 /* [3] : submit the DMA */
243 desc->callback = dma_tx_callback;
244 desc->callback_param = s;
245 dmaengine_submit(desc);
246 dma_async_issue_pending(channel);
247 return 0;
248}
249
250static void mxs_auart_tx_chars(struct mxs_auart_port *s)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100251{
252 struct circ_buf *xmit = &s->port.state->xmit;
253
Huang Shijiee8001632012-11-16 16:03:53 +0800254 if (auart_dma_enabled(s)) {
fabio.estevam@freescale.com87b8bed2013-01-07 23:11:06 -0200255 u32 i = 0;
Huang Shijiee8001632012-11-16 16:03:53 +0800256 int size;
257 void *buffer = s->tx_dma_buf;
258
259 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
260 return;
261
262 while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
263 size = min_t(u32, UART_XMIT_SIZE - i,
264 CIRC_CNT_TO_END(xmit->head,
265 xmit->tail,
266 UART_XMIT_SIZE));
267 memcpy(buffer + i, xmit->buf + xmit->tail, size);
268 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
269
270 i += size;
271 if (i >= UART_XMIT_SIZE)
272 break;
273 }
274
275 if (uart_tx_stopped(&s->port))
276 mxs_auart_stop_tx(&s->port);
277
278 if (i) {
279 mxs_auart_dma_tx(s, i);
280 } else {
281 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100282 smp_mb__after_atomic();
Huang Shijiee8001632012-11-16 16:03:53 +0800283 }
284 return;
285 }
286
287
Sascha Hauer47d37d62011-01-11 15:54:54 +0100288 while (!(readl(s->port.membase + AUART_STAT) &
289 AUART_STAT_TXFF)) {
290 if (s->port.x_char) {
291 s->port.icount.tx++;
292 writel(s->port.x_char,
293 s->port.membase + AUART_DATA);
294 s->port.x_char = 0;
295 continue;
296 }
297 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
298 s->port.icount.tx++;
299 writel(xmit->buf[xmit->tail],
300 s->port.membase + AUART_DATA);
301 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100302 } else
303 break;
304 }
Uwe Kleine-Königd0758a22011-11-22 14:22:56 +0100305 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
306 uart_write_wakeup(&s->port);
307
Sascha Hauer47d37d62011-01-11 15:54:54 +0100308 if (uart_circ_empty(&(s->port.state->xmit)))
309 writel(AUART_INTR_TXIEN,
310 s->port.membase + AUART_INTR_CLR);
311 else
312 writel(AUART_INTR_TXIEN,
313 s->port.membase + AUART_INTR_SET);
314
315 if (uart_tx_stopped(&s->port))
316 mxs_auart_stop_tx(&s->port);
317}
318
319static void mxs_auart_rx_char(struct mxs_auart_port *s)
320{
321 int flag;
322 u32 stat;
323 u8 c;
324
325 c = readl(s->port.membase + AUART_DATA);
326 stat = readl(s->port.membase + AUART_STAT);
327
328 flag = TTY_NORMAL;
329 s->port.icount.rx++;
330
331 if (stat & AUART_STAT_BERR) {
332 s->port.icount.brk++;
333 if (uart_handle_break(&s->port))
334 goto out;
335 } else if (stat & AUART_STAT_PERR) {
336 s->port.icount.parity++;
337 } else if (stat & AUART_STAT_FERR) {
338 s->port.icount.frame++;
339 }
340
341 /*
342 * Mask off conditions which should be ingored.
343 */
344 stat &= s->port.read_status_mask;
345
346 if (stat & AUART_STAT_BERR) {
347 flag = TTY_BREAK;
348 } else if (stat & AUART_STAT_PERR)
349 flag = TTY_PARITY;
350 else if (stat & AUART_STAT_FERR)
351 flag = TTY_FRAME;
352
353 if (stat & AUART_STAT_OERR)
354 s->port.icount.overrun++;
355
356 if (uart_handle_sysrq_char(&s->port, c))
357 goto out;
358
359 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
360out:
361 writel(stat, s->port.membase + AUART_STAT);
362}
363
364static void mxs_auart_rx_chars(struct mxs_auart_port *s)
365{
Sascha Hauer47d37d62011-01-11 15:54:54 +0100366 u32 stat = 0;
367
368 for (;;) {
369 stat = readl(s->port.membase + AUART_STAT);
370 if (stat & AUART_STAT_RXFE)
371 break;
372 mxs_auart_rx_char(s);
373 }
374
375 writel(stat, s->port.membase + AUART_STAT);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100376 tty_flip_buffer_push(&s->port.state->port);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100377}
378
379static int mxs_auart_request_port(struct uart_port *u)
380{
381 return 0;
382}
383
384static int mxs_auart_verify_port(struct uart_port *u,
385 struct serial_struct *ser)
386{
387 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
388 return -EINVAL;
389 return 0;
390}
391
392static void mxs_auart_config_port(struct uart_port *u, int flags)
393{
394}
395
396static const char *mxs_auart_type(struct uart_port *u)
397{
398 struct mxs_auart_port *s = to_auart_port(u);
399
400 return dev_name(s->dev);
401}
402
403static void mxs_auart_release_port(struct uart_port *u)
404{
405}
406
407static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
408{
409 struct mxs_auart_port *s = to_auart_port(u);
410
411 u32 ctrl = readl(u->membase + AUART_CTRL2);
412
Steffen Trumtrara6833212012-12-13 14:27:43 +0100413 ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
Huang Shijie00592022012-08-08 10:37:59 +0800414 if (mctrl & TIOCM_RTS) {
Peter Hurley299245a2014-09-10 15:06:24 -0400415 if (uart_cts_enabled(u))
Huang Shijie00592022012-08-08 10:37:59 +0800416 ctrl |= AUART_CTRL2_RTSEN;
Steffen Trumtrara6833212012-12-13 14:27:43 +0100417 else
418 ctrl |= AUART_CTRL2_RTS;
Huang Shijie00592022012-08-08 10:37:59 +0800419 }
420
Sascha Hauer47d37d62011-01-11 15:54:54 +0100421 s->ctrl = mctrl;
422 writel(ctrl, u->membase + AUART_CTRL2);
423}
424
425static u32 mxs_auart_get_mctrl(struct uart_port *u)
426{
427 struct mxs_auart_port *s = to_auart_port(u);
428 u32 stat = readl(u->membase + AUART_STAT);
429 int ctrl2 = readl(u->membase + AUART_CTRL2);
430 u32 mctrl = s->ctrl;
431
432 mctrl &= ~TIOCM_CTS;
433 if (stat & AUART_STAT_CTS)
434 mctrl |= TIOCM_CTS;
435
436 if (ctrl2 & AUART_CTRL2_RTS)
437 mctrl |= TIOCM_RTS;
438
439 return mctrl;
440}
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
Sascha Hauer47d37d62011-01-11 15:54:54 +0100567static void mxs_auart_settermios(struct uart_port *u,
568 struct ktermios *termios,
569 struct ktermios *old)
570{
Huang Shijiee8001632012-11-16 16:03:53 +0800571 struct mxs_auart_port *s = to_auart_port(u);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100572 u32 bm, ctrl, ctrl2, div;
573 unsigned int cflag, baud;
574
575 cflag = termios->c_cflag;
576
577 ctrl = AUART_LINECTRL_FEN;
578 ctrl2 = readl(u->membase + AUART_CTRL2);
579
580 /* byte size */
581 switch (cflag & CSIZE) {
582 case CS5:
583 bm = 0;
584 break;
585 case CS6:
586 bm = 1;
587 break;
588 case CS7:
589 bm = 2;
590 break;
591 case CS8:
592 bm = 3;
593 break;
594 default:
595 return;
596 }
597
598 ctrl |= AUART_LINECTRL_WLEN(bm);
599
600 /* parity */
601 if (cflag & PARENB) {
602 ctrl |= AUART_LINECTRL_PEN;
603 if ((cflag & PARODD) == 0)
604 ctrl |= AUART_LINECTRL_EPS;
605 }
606
607 u->read_status_mask = 0;
608
609 if (termios->c_iflag & INPCK)
610 u->read_status_mask |= AUART_STAT_PERR;
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400611 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Sascha Hauer47d37d62011-01-11 15:54:54 +0100612 u->read_status_mask |= AUART_STAT_BERR;
613
614 /*
615 * Characters to ignore
616 */
617 u->ignore_status_mask = 0;
618 if (termios->c_iflag & IGNPAR)
619 u->ignore_status_mask |= AUART_STAT_PERR;
620 if (termios->c_iflag & IGNBRK) {
621 u->ignore_status_mask |= AUART_STAT_BERR;
622 /*
623 * If we're ignoring parity and break indicators,
624 * ignore overruns too (for real raw support).
625 */
626 if (termios->c_iflag & IGNPAR)
627 u->ignore_status_mask |= AUART_STAT_OERR;
628 }
629
630 /*
631 * ignore all characters if CREAD is not set
632 */
633 if (cflag & CREAD)
634 ctrl2 |= AUART_CTRL2_RXE;
635 else
636 ctrl2 &= ~AUART_CTRL2_RXE;
637
638 /* figure out the stop bits requested */
639 if (cflag & CSTOPB)
640 ctrl |= AUART_LINECTRL_STP2;
641
642 /* figure out the hardware flow control settings */
Huang Shijiee8001632012-11-16 16:03:53 +0800643 if (cflag & CRTSCTS) {
644 /*
645 * The DMA has a bug(see errata:2836) in mx23.
646 * So we can not implement the DMA for auart in mx23,
647 * we can only implement the DMA support for auart
648 * in mx28.
649 */
Huang Shijieafab2202013-08-03 10:09:15 -0400650 if (is_imx28_auart(s)
Huang Shijie8418e672013-08-03 10:09:14 -0400651 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
Huang Shijiee8001632012-11-16 16:03:53 +0800652 if (!mxs_auart_dma_init(s))
653 /* enable DMA tranfer */
654 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
655 | AUART_CTRL2_DMAONERR;
656 }
Huang Shijie00592022012-08-08 10:37:59 +0800657 ctrl2 |= AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN;
Huang Shijiee8001632012-11-16 16:03:53 +0800658 } else {
Huang Shijie00592022012-08-08 10:37:59 +0800659 ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
Huang Shijiee8001632012-11-16 16:03:53 +0800660 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100661
662 /* set baud rate */
663 baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
664 div = u->uartclk * 32 / baud;
665 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
666 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
667
668 writel(ctrl, u->membase + AUART_LINECTRL);
669 writel(ctrl2, u->membase + AUART_CTRL2);
Lothar Waßmann8b979f72012-05-03 11:37:12 +0200670
671 uart_update_timeout(u, termios->c_cflag, baud);
Huang Shijiee8001632012-11-16 16:03:53 +0800672
673 /* prepare for the DMA RX. */
674 if (auart_dma_enabled(s) &&
675 !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
676 if (!mxs_auart_dma_prep_rx(s)) {
677 /* Disable the normal RX interrupt. */
Huang Shijiea5919442012-11-22 15:06:29 +0800678 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
679 u->membase + AUART_INTR_CLR);
Huang Shijiee8001632012-11-16 16:03:53 +0800680 } else {
681 mxs_auart_dma_exit(s);
682 dev_err(s->dev, "We can not start up the DMA.\n");
683 }
684 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100685}
686
687static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
688{
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200689 u32 istat;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100690 struct mxs_auart_port *s = context;
691 u32 stat = readl(s->port.membase + AUART_STAT);
692
Uwe Kleine-Königd970d7f2013-07-04 11:28:51 +0200693 istat = readl(s->port.membase + AUART_INTR);
694
695 /* ack irq */
696 writel(istat & (AUART_INTR_RTIS
697 | AUART_INTR_TXIS
698 | AUART_INTR_RXIS
699 | AUART_INTR_CTSMIS),
700 s->port.membase + AUART_INTR_CLR);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100701
702 if (istat & AUART_INTR_CTSMIS) {
703 uart_handle_cts_change(&s->port, stat & AUART_STAT_CTS);
704 writel(AUART_INTR_CTSMIS,
705 s->port.membase + AUART_INTR_CLR);
706 istat &= ~AUART_INTR_CTSMIS;
707 }
708
709 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
Huang Shijiea5919442012-11-22 15:06:29 +0800710 if (!auart_dma_enabled(s))
711 mxs_auart_rx_chars(s);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100712 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
713 }
714
715 if (istat & AUART_INTR_TXIS) {
716 mxs_auart_tx_chars(s);
717 istat &= ~AUART_INTR_TXIS;
718 }
719
Sascha Hauer47d37d62011-01-11 15:54:54 +0100720 return IRQ_HANDLED;
721}
722
723static void mxs_auart_reset(struct uart_port *u)
724{
725 int i;
726 unsigned int reg;
727
728 writel(AUART_CTRL0_SFTRST, u->membase + AUART_CTRL0_CLR);
729
730 for (i = 0; i < 10000; i++) {
731 reg = readl(u->membase + AUART_CTRL0);
732 if (!(reg & AUART_CTRL0_SFTRST))
733 break;
734 udelay(3);
735 }
736 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
737}
738
739static int mxs_auart_startup(struct uart_port *u)
740{
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200741 int ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100742 struct mxs_auart_port *s = to_auart_port(u);
743
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200744 ret = clk_prepare_enable(s->clk);
745 if (ret)
746 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100747
748 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_CLR);
749
750 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_SET);
751
752 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
753 u->membase + AUART_INTR);
754
Hector Palacios9987f762013-10-03 09:32:03 +0200755 /* Reset FIFO size (it could have changed if DMA was enabled) */
756 u->fifosize = MXS_AUART_FIFO_SIZE;
757
Sascha Hauer47d37d62011-01-11 15:54:54 +0100758 /*
759 * Enable fifo so all four bytes of a DMA word are written to
760 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
761 */
762 writel(AUART_LINECTRL_FEN, u->membase + AUART_LINECTRL_SET);
763
764 return 0;
765}
766
767static void mxs_auart_shutdown(struct uart_port *u)
768{
769 struct mxs_auart_port *s = to_auart_port(u);
770
Huang Shijiee8001632012-11-16 16:03:53 +0800771 if (auart_dma_enabled(s))
772 mxs_auart_dma_exit(s);
773
Sascha Hauer47d37d62011-01-11 15:54:54 +0100774 writel(AUART_CTRL2_UARTEN, u->membase + AUART_CTRL2_CLR);
775
Sascha Hauer47d37d62011-01-11 15:54:54 +0100776 writel(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
777 u->membase + AUART_INTR_CLR);
778
Huang Shijie851b7142012-09-06 22:38:40 -0400779 writel(AUART_CTRL0_CLKGATE, u->membase + AUART_CTRL0_SET);
780
Shawn Guoa4813772011-12-20 14:10:29 +0800781 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100782}
783
784static unsigned int mxs_auart_tx_empty(struct uart_port *u)
785{
786 if (readl(u->membase + AUART_STAT) & AUART_STAT_TXFE)
787 return TIOCSER_TEMT;
788 else
789 return 0;
790}
791
792static void mxs_auart_start_tx(struct uart_port *u)
793{
794 struct mxs_auart_port *s = to_auart_port(u);
795
796 /* enable transmitter */
797 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_SET);
798
799 mxs_auart_tx_chars(s);
800}
801
802static void mxs_auart_stop_tx(struct uart_port *u)
803{
804 writel(AUART_CTRL2_TXE, u->membase + AUART_CTRL2_CLR);
805}
806
807static void mxs_auart_stop_rx(struct uart_port *u)
808{
809 writel(AUART_CTRL2_RXE, u->membase + AUART_CTRL2_CLR);
810}
811
812static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
813{
814 if (ctl)
815 writel(AUART_LINECTRL_BRK,
816 u->membase + AUART_LINECTRL_SET);
817 else
818 writel(AUART_LINECTRL_BRK,
819 u->membase + AUART_LINECTRL_CLR);
820}
821
Sascha Hauer47d37d62011-01-11 15:54:54 +0100822static struct uart_ops mxs_auart_ops = {
823 .tx_empty = mxs_auart_tx_empty,
824 .start_tx = mxs_auart_start_tx,
825 .stop_tx = mxs_auart_stop_tx,
826 .stop_rx = mxs_auart_stop_rx,
Sascha Hauer47d37d62011-01-11 15:54:54 +0100827 .break_ctl = mxs_auart_break_ctl,
828 .set_mctrl = mxs_auart_set_mctrl,
829 .get_mctrl = mxs_auart_get_mctrl,
830 .startup = mxs_auart_startup,
831 .shutdown = mxs_auart_shutdown,
832 .set_termios = mxs_auart_settermios,
833 .type = mxs_auart_type,
834 .release_port = mxs_auart_release_port,
835 .request_port = mxs_auart_request_port,
836 .config_port = mxs_auart_config_port,
837 .verify_port = mxs_auart_verify_port,
838};
839
840static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
841
842#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
843static void mxs_auart_console_putchar(struct uart_port *port, int ch)
844{
845 unsigned int to = 1000;
846
847 while (readl(port->membase + AUART_STAT) & AUART_STAT_TXFF) {
848 if (!to--)
849 break;
850 udelay(1);
851 }
852
853 writel(ch, port->membase + AUART_DATA);
854}
855
856static void
857auart_console_write(struct console *co, const char *str, unsigned int count)
858{
859 struct mxs_auart_port *s;
860 struct uart_port *port;
861 unsigned int old_ctrl0, old_ctrl2;
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200862 unsigned int to = 20000;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100863
Wolfram Sang4829e762013-04-19 21:12:17 +0200864 if (co->index >= MXS_AUART_PORTS || co->index < 0)
Sascha Hauer47d37d62011-01-11 15:54:54 +0100865 return;
866
867 s = auart_port[co->index];
868 port = &s->port;
869
870 clk_enable(s->clk);
871
872 /* First save the CR then disable the interrupts */
873 old_ctrl2 = readl(port->membase + AUART_CTRL2);
874 old_ctrl0 = readl(port->membase + AUART_CTRL0);
875
876 writel(AUART_CTRL0_CLKGATE,
877 port->membase + AUART_CTRL0_CLR);
878 writel(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE,
879 port->membase + AUART_CTRL2_SET);
880
881 uart_console_write(port, str, count, mxs_auart_console_putchar);
882
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200883 /* Finally, wait for transmitter to become empty ... */
Sascha Hauer47d37d62011-01-11 15:54:54 +0100884 while (readl(port->membase + AUART_STAT) & AUART_STAT_BUSY) {
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200885 udelay(1);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100886 if (!to--)
887 break;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100888 }
889
Uwe Kleine-König079a0362013-06-28 11:49:41 +0200890 /*
891 * ... and restore the TCR if we waited long enough for the transmitter
892 * to be idle. This might keep the transmitter enabled although it is
893 * unused, but that is better than to disable it while it is still
894 * transmitting.
895 */
896 if (!(readl(port->membase + AUART_STAT) & AUART_STAT_BUSY)) {
897 writel(old_ctrl0, port->membase + AUART_CTRL0);
898 writel(old_ctrl2, port->membase + AUART_CTRL2);
899 }
Sascha Hauer47d37d62011-01-11 15:54:54 +0100900
901 clk_disable(s->clk);
902}
903
904static void __init
905auart_console_get_options(struct uart_port *port, int *baud,
906 int *parity, int *bits)
907{
908 unsigned int lcr_h, quot;
909
910 if (!(readl(port->membase + AUART_CTRL2) & AUART_CTRL2_UARTEN))
911 return;
912
913 lcr_h = readl(port->membase + AUART_LINECTRL);
914
915 *parity = 'n';
916 if (lcr_h & AUART_LINECTRL_PEN) {
917 if (lcr_h & AUART_LINECTRL_EPS)
918 *parity = 'e';
919 else
920 *parity = 'o';
921 }
922
923 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(2))
924 *bits = 7;
925 else
926 *bits = 8;
927
928 quot = ((readl(port->membase + AUART_LINECTRL)
929 & AUART_LINECTRL_BAUD_DIVINT_MASK))
930 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
931 quot |= ((readl(port->membase + AUART_LINECTRL)
932 & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
933 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
934 if (quot == 0)
935 quot = 1;
936
937 *baud = (port->uartclk << 2) / quot;
938}
939
940static int __init
941auart_console_setup(struct console *co, char *options)
942{
943 struct mxs_auart_port *s;
944 int baud = 9600;
945 int bits = 8;
946 int parity = 'n';
947 int flow = 'n';
948 int ret;
949
950 /*
951 * Check whether an invalid uart number has been specified, and
952 * if so, search for the first available port that does have
953 * console support.
954 */
955 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
956 co->index = 0;
957 s = auart_port[co->index];
958 if (!s)
959 return -ENODEV;
960
Fabio Estevam9bbc3dc2013-12-02 01:17:58 -0200961 ret = clk_prepare_enable(s->clk);
962 if (ret)
963 return ret;
Sascha Hauer47d37d62011-01-11 15:54:54 +0100964
965 if (options)
966 uart_parse_options(options, &baud, &parity, &bits, &flow);
967 else
968 auart_console_get_options(&s->port, &baud, &parity, &bits);
969
970 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
971
Shawn Guoa4813772011-12-20 14:10:29 +0800972 clk_disable_unprepare(s->clk);
Sascha Hauer47d37d62011-01-11 15:54:54 +0100973
974 return ret;
975}
976
977static struct console auart_console = {
978 .name = "ttyAPP",
979 .write = auart_console_write,
980 .device = uart_console_device,
981 .setup = auart_console_setup,
982 .flags = CON_PRINTBUFFER,
983 .index = -1,
984 .data = &auart_driver,
985};
986#endif
987
988static struct uart_driver auart_driver = {
989 .owner = THIS_MODULE,
990 .driver_name = "ttyAPP",
991 .dev_name = "ttyAPP",
992 .major = 0,
993 .minor = 0,
994 .nr = MXS_AUART_PORTS,
995#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
996 .cons = &auart_console,
997#endif
998};
999
Fabio Estevam1ea66072012-06-18 10:06:09 -03001000/*
1001 * This function returns 1 if pdev isn't a device instatiated by dt, 0 if it
1002 * could successfully get all information from dt or a negative errno.
1003 */
1004static int serial_mxs_probe_dt(struct mxs_auart_port *s,
1005 struct platform_device *pdev)
1006{
1007 struct device_node *np = pdev->dev.of_node;
1008 int ret;
1009
1010 if (!np)
1011 /* no device tree device */
1012 return 1;
1013
1014 ret = of_alias_get_id(np, "serial");
1015 if (ret < 0) {
1016 dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
1017 return ret;
1018 }
1019 s->port.line = ret;
1020
Huang Shijie8418e672013-08-03 10:09:14 -04001021 if (of_get_property(np, "fsl,uart-has-rtscts", NULL))
1022 set_bit(MXS_AUART_RTSCTS, &s->flags);
1023
Fabio Estevam1ea66072012-06-18 10:06:09 -03001024 return 0;
1025}
1026
Bill Pemberton9671f092012-11-19 13:21:50 -05001027static int mxs_auart_probe(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001028{
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001029 const struct of_device_id *of_id =
1030 of_match_device(mxs_auart_dt_ids, &pdev->dev);
Sascha Hauer47d37d62011-01-11 15:54:54 +01001031 struct mxs_auart_port *s;
1032 u32 version;
1033 int ret = 0;
1034 struct resource *r;
1035
1036 s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
1037 if (!s) {
1038 ret = -ENOMEM;
1039 goto out;
1040 }
1041
Fabio Estevam1ea66072012-06-18 10:06:09 -03001042 ret = serial_mxs_probe_dt(s, pdev);
1043 if (ret > 0)
1044 s->port.line = pdev->id < 0 ? 0 : pdev->id;
1045 else if (ret < 0)
1046 goto out_free;
1047
Huang Shijief4b1f03b2012-11-16 16:03:52 +08001048 if (of_id) {
1049 pdev->id_entry = of_id->data;
1050 s->devtype = pdev->id_entry->driver_data;
1051 }
1052
Sascha Hauer47d37d62011-01-11 15:54:54 +01001053 s->clk = clk_get(&pdev->dev, NULL);
1054 if (IS_ERR(s->clk)) {
1055 ret = PTR_ERR(s->clk);
1056 goto out_free;
1057 }
1058
1059 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1060 if (!r) {
1061 ret = -ENXIO;
1062 goto out_free_clk;
1063 }
1064
1065 s->port.mapbase = r->start;
1066 s->port.membase = ioremap(r->start, resource_size(r));
1067 s->port.ops = &mxs_auart_ops;
1068 s->port.iotype = UPIO_MEM;
Hector Palacios9987f762013-10-03 09:32:03 +02001069 s->port.fifosize = MXS_AUART_FIFO_SIZE;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001070 s->port.uartclk = clk_get_rate(s->clk);
1071 s->port.type = PORT_IMX;
Wolfram Sang4c24f2c2013-04-19 21:06:20 +02001072 s->port.dev = s->dev = &pdev->dev;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001073
Sascha Hauer47d37d62011-01-11 15:54:54 +01001074 s->ctrl = 0;
1075
1076 s->irq = platform_get_irq(pdev, 0);
1077 s->port.irq = s->irq;
1078 ret = request_irq(s->irq, mxs_auart_irq_handle, 0, dev_name(&pdev->dev), s);
1079 if (ret)
1080 goto out_free_clk;
1081
1082 platform_set_drvdata(pdev, s);
1083
Fabio Estevam1ea66072012-06-18 10:06:09 -03001084 auart_port[s->port.line] = s;
Sascha Hauer47d37d62011-01-11 15:54:54 +01001085
1086 mxs_auart_reset(&s->port);
1087
1088 ret = uart_add_one_port(&auart_driver, &s->port);
1089 if (ret)
1090 goto out_free_irq;
1091
1092 version = readl(s->port.membase + AUART_VERSION);
1093 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
1094 (version >> 24) & 0xff,
1095 (version >> 16) & 0xff, version & 0xffff);
1096
1097 return 0;
1098
1099out_free_irq:
1100 auart_port[pdev->id] = NULL;
1101 free_irq(s->irq, s);
1102out_free_clk:
1103 clk_put(s->clk);
1104out_free:
1105 kfree(s);
1106out:
1107 return ret;
1108}
1109
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001110static int mxs_auart_remove(struct platform_device *pdev)
Sascha Hauer47d37d62011-01-11 15:54:54 +01001111{
1112 struct mxs_auart_port *s = platform_get_drvdata(pdev);
1113
1114 uart_remove_one_port(&auart_driver, &s->port);
1115
1116 auart_port[pdev->id] = NULL;
1117
1118 clk_put(s->clk);
1119 free_irq(s->irq, s);
1120 kfree(s);
1121
1122 return 0;
1123}
1124
1125static struct platform_driver mxs_auart_driver = {
1126 .probe = mxs_auart_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001127 .remove = mxs_auart_remove,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001128 .driver = {
1129 .name = "mxs-auart",
1130 .owner = THIS_MODULE,
Fabio Estevam1ea66072012-06-18 10:06:09 -03001131 .of_match_table = mxs_auart_dt_ids,
Sascha Hauer47d37d62011-01-11 15:54:54 +01001132 },
1133};
1134
1135static int __init mxs_auart_init(void)
1136{
1137 int r;
1138
1139 r = uart_register_driver(&auart_driver);
1140 if (r)
1141 goto out;
1142
1143 r = platform_driver_register(&mxs_auart_driver);
1144 if (r)
1145 goto out_err;
1146
1147 return 0;
1148out_err:
1149 uart_unregister_driver(&auart_driver);
1150out:
1151 return r;
1152}
1153
1154static void __exit mxs_auart_exit(void)
1155{
1156 platform_driver_unregister(&mxs_auart_driver);
1157 uart_unregister_driver(&auart_driver);
1158}
1159
1160module_init(mxs_auart_init);
1161module_exit(mxs_auart_exit);
1162MODULE_LICENSE("GPL");
1163MODULE_DESCRIPTION("Freescale MXS application uart driver");
Fabio Estevam1ea66072012-06-18 10:06:09 -03001164MODULE_ALIAS("platform:mxs-auart");