1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/init.h>
18#include <linux/console.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/slab.h>
22#include <linux/wait.h>
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26#include <linux/serial.h>
27#include <linux/serial_core.h>
28#include <linux/platform_device.h>
29#include <linux/device.h>
30#include <linux/clk.h>
31#include <linux/delay.h>
32#include <linux/io.h>
33#include <linux/of_device.h>
34#include <linux/dma-mapping.h>
35#include <linux/dmaengine.h>
36
37#include <linux/gpio/consumer.h>
38#include <linux/err.h>
39#include <linux/irq.h>
40#include "serial_mctrl_gpio.h"
41
42#define MXS_AUART_PORTS 5
43#define MXS_AUART_FIFO_SIZE 16
44
45#define SET_REG 0x4
46#define CLR_REG 0x8
47#define TOG_REG 0xc
48
49#define AUART_CTRL0 0x00000000
50#define AUART_CTRL1 0x00000010
51#define AUART_CTRL2 0x00000020
52#define AUART_LINECTRL 0x00000030
53#define AUART_LINECTRL2 0x00000040
54#define AUART_INTR 0x00000050
55#define AUART_DATA 0x00000060
56#define AUART_STAT 0x00000070
57#define AUART_DEBUG 0x00000080
58#define AUART_VERSION 0x00000090
59#define AUART_AUTOBAUD 0x000000a0
60
61#define AUART_CTRL0_SFTRST (1 << 31)
62#define AUART_CTRL0_CLKGATE (1 << 30)
63#define AUART_CTRL0_RXTO_ENABLE (1 << 27)
64#define AUART_CTRL0_RXTIMEOUT(v) (((v) & 0x7ff) << 16)
65#define AUART_CTRL0_XFER_COUNT(v) ((v) & 0xffff)
66
67#define AUART_CTRL1_XFER_COUNT(v) ((v) & 0xffff)
68
69#define AUART_CTRL2_DMAONERR (1 << 26)
70#define AUART_CTRL2_TXDMAE (1 << 25)
71#define AUART_CTRL2_RXDMAE (1 << 24)
72
73#define AUART_CTRL2_CTSEN (1 << 15)
74#define AUART_CTRL2_RTSEN (1 << 14)
75#define AUART_CTRL2_RTS (1 << 11)
76#define AUART_CTRL2_RXE (1 << 9)
77#define AUART_CTRL2_TXE (1 << 8)
78#define AUART_CTRL2_UARTEN (1 << 0)
79
80#define AUART_LINECTRL_BAUD_DIV_MAX 0x003fffc0
81#define AUART_LINECTRL_BAUD_DIV_MIN 0x000000ec
82#define AUART_LINECTRL_BAUD_DIVINT_SHIFT 16
83#define AUART_LINECTRL_BAUD_DIVINT_MASK 0xffff0000
84#define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
85#define AUART_LINECTRL_BAUD_DIVFRAC_SHIFT 8
86#define AUART_LINECTRL_BAUD_DIVFRAC_MASK 0x00003f00
87#define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
88#define AUART_LINECTRL_SPS (1 << 7)
89#define AUART_LINECTRL_WLEN_MASK 0x00000060
90#define AUART_LINECTRL_WLEN(v) ((((v) - 5) & 0x3) << 5)
91#define AUART_LINECTRL_FEN (1 << 4)
92#define AUART_LINECTRL_STP2 (1 << 3)
93#define AUART_LINECTRL_EPS (1 << 2)
94#define AUART_LINECTRL_PEN (1 << 1)
95#define AUART_LINECTRL_BRK (1 << 0)
96
97#define AUART_INTR_RTIEN (1 << 22)
98#define AUART_INTR_TXIEN (1 << 21)
99#define AUART_INTR_RXIEN (1 << 20)
100#define AUART_INTR_CTSMIEN (1 << 17)
101#define AUART_INTR_RTIS (1 << 6)
102#define AUART_INTR_TXIS (1 << 5)
103#define AUART_INTR_RXIS (1 << 4)
104#define AUART_INTR_CTSMIS (1 << 1)
105
106#define AUART_STAT_BUSY (1 << 29)
107#define AUART_STAT_CTS (1 << 28)
108#define AUART_STAT_TXFE (1 << 27)
109#define AUART_STAT_TXFF (1 << 25)
110#define AUART_STAT_RXFE (1 << 24)
111#define AUART_STAT_OERR (1 << 19)
112#define AUART_STAT_BERR (1 << 18)
113#define AUART_STAT_PERR (1 << 17)
114#define AUART_STAT_FERR (1 << 16)
115#define AUART_STAT_RXCOUNT_MASK 0xffff
116
117
118
119
120
121
122#define ASM9260_HW_CTRL0 0x0000
123
124
125
126
127#define ASM9260_BM_CTRL0_RXDMA_RUN BIT(28)
128
129#define ASM9260_BM_CTRL0_RXTO_SOURCE_STATUS BIT(25)
130
131
132
133
134
135
136#define ASM9260_BM_CTRL0_RXTO_ENABLE BIT(24)
137
138
139
140
141
142
143
144
145
146
147
148#define ASM9260_BM_CTRL0_RXTO_MASK (0xff << 16)
149
150#define ASM9260_BM_CTRL0_DEFAULT_RXTIMEOUT (20 << 16)
151
152
153#define ASM9260_HW_CTRL1 0x0010
154
155
156
157
158#define ASM9260_BM_CTRL1_TXDMA_RUN BIT(28)
159
160#define ASM9260_HW_CTRL2 0x0020
161
162
163
164
165
166
167
168
169
170#define ASM9260_BM_CTRL2_RXIFLSEL (7 << 20)
171#define ASM9260_BM_CTRL2_DEFAULT_RXIFLSEL (3 << 20)
172
173#define ASM9260_BM_CTRL2_TXIFLSEL (7 << 16)
174#define ASM9260_BM_CTRL2_DEFAULT_TXIFLSEL (2 << 16)
175
176#define ASM9260_BM_CTRL2_DTR BIT(10)
177
178#define ASM9260_BM_CTRL2_LBE BIT(7)
179#define ASM9260_BM_CTRL2_PORT_ENABLE BIT(0)
180
181#define ASM9260_HW_LINECTRL 0x0030
182
183
184
185
186
187
188#define ASM9260_BM_LCTRL_SPS BIT(7)
189
190#define ASM9260_BM_LCTRL_WLEN (3 << 5)
191#define ASM9260_BM_LCTRL_CHRL_5 (0 << 5)
192#define ASM9260_BM_LCTRL_CHRL_6 (1 << 5)
193#define ASM9260_BM_LCTRL_CHRL_7 (2 << 5)
194#define ASM9260_BM_LCTRL_CHRL_8 (3 << 5)
195
196
197
198
199
200#define ASM9260_HW_INTR 0x0040
201
202#define ASM9260_BM_INTR_TFEIEN BIT(27)
203
204#define ASM9260_BM_INTR_OEIEN BIT(26)
205
206#define ASM9260_BM_INTR_BEIEN BIT(25)
207
208#define ASM9260_BM_INTR_PEIEN BIT(24)
209
210#define ASM9260_BM_INTR_FEIEN BIT(23)
211
212
213#define ASM9260_BM_INTR_DSRMIEN BIT(19)
214
215#define ASM9260_BM_INTR_DCDMIEN BIT(18)
216
217#define ASM9260_BM_INTR_RIMIEN BIT(16)
218
219#define ASM9260_BM_INTR_ABTO BIT(13)
220#define ASM9260_BM_INTR_ABEO BIT(12)
221
222#define ASM9260_BM_INTR_TFEIS BIT(11)
223
224#define ASM9260_BM_INTR_OEIS BIT(10)
225
226#define ASM9260_BM_INTR_BEIS BIT(9)
227
228#define ASM9260_BM_INTR_PEIS BIT(8)
229
230#define ASM9260_BM_INTR_FEIS BIT(7)
231#define ASM9260_BM_INTR_DSRMIS BIT(3)
232#define ASM9260_BM_INTR_DCDMIS BIT(2)
233#define ASM9260_BM_INTR_RIMIS BIT(0)
234
235
236
237
238
239
240#define ASM9260_HW_DATA 0x0050
241
242#define ASM9260_HW_STAT 0x0060
243
244#define ASM9260_BM_STAT_PRESENT BIT(31)
245
246#define ASM9260_BM_STAT_HISPEED BIT(30)
247
248#define ASM9260_BM_STAT_RXFULL BIT(26)
249
250
251#define ASM9260_HW_DEBUG 0x0070
252
253#define ASM9260_BM_DEBUG_TXDMARUN BIT(5)
254#define ASM9260_BM_DEBUG_RXDMARUN BIT(4)
255
256#define ASM9260_BM_DEBUG_TXCMDEND BIT(3)
257#define ASM9260_BM_DEBUG_RXCMDEND BIT(2)
258
259#define ASM9260_BM_DEBUG_TXDMARQ BIT(1)
260#define ASM9260_BM_DEBUG_RXDMARQ BIT(0)
261
262#define ASM9260_HW_ILPR 0x0080
263
264#define ASM9260_HW_RS485CTRL 0x0090
265
266
267
268
269
270
271
272#define ASM9260_BM_RS485CTRL_ONIV BIT(5)
273
274#define ASM9260_BM_RS485CTRL_DIR_CTRL BIT(4)
275
276
277
278
279#define ASM9260_BM_RS485CTRL_PINSEL BIT(3)
280
281#define ASM9260_BM_RS485CTRL_AADEN BIT(2)
282
283#define ASM9260_BM_RS485CTRL_RXDIS BIT(1)
284
285#define ASM9260_BM_RS485CTRL_RS485EN BIT(0)
286
287#define ASM9260_HW_RS485ADRMATCH 0x00a0
288
289#define ASM9260_BM_RS485ADRMATCH_MASK (0xff << 0)
290
291#define ASM9260_HW_RS485DLY 0x00b0
292
293
294
295
296#define ASM9260_BM_RS485DLY_MASK (0xff << 0)
297
298#define ASM9260_HW_AUTOBAUD 0x00c0
299
300#define ASM9260_BM_AUTOBAUD_TO_INT_CLR BIT(9)
301
302#define ASM9260_BM_AUTOBAUD_EO_INT_CLR BIT(8)
303
304#define ASM9260_BM_AUTOBAUD_AUTORESTART BIT(2)
305
306#define ASM9260_BM_AUTOBAUD_MODE BIT(1)
307
308
309
310
311#define ASM9260_BM_AUTOBAUD_START BIT(0)
312
313#define ASM9260_HW_CTRL3 0x00d0
314#define ASM9260_BM_CTRL3_OUTCLK_DIV_MASK (0xffff << 16)
315
316
317
318
319#define ASM9260_BM_CTRL3_MASTERMODE BIT(6)
320
321#define ASM9260_BM_CTRL3_SYNCMODE BIT(4)
322
323#define ASM9260_BM_CTRL3_MSBF BIT(2)
324
325#define ASM9260_BM_CTRL3_BAUD8 BIT(1)
326
327#define ASM9260_BM_CTRL3_9BIT BIT(0)
328
329#define ASM9260_HW_ISO7816_CTRL 0x00e0
330
331#define ASM9260_BM_ISO7816CTRL_HS BIT(12)
332
333#define ASM9260_BM_ISO7816CTRL_DS_NACK BIT(8)
334#define ASM9260_BM_ISO7816CTRL_MAX_ITER_MASK (0xff << 4)
335
336#define ASM9260_BM_ISO7816CTRL_INACK BIT(3)
337#define ASM9260_BM_ISO7816CTRL_NEG_DATA BIT(2)
338
339#define ASM9260_BM_ISO7816CTRL_ENABLE BIT(0)
340
341#define ASM9260_HW_ISO7816_ERRCNT 0x00f0
342
343#define ASM9260_BM_ISO7816_NB_ERRORS_MASK (0xff << 0)
344
345#define ASM9260_HW_ISO7816_STATUS 0x0100
346
347#define ASM9260_BM_ISO7816_STAT_ITERATION BIT(0)
348
349
350
351static struct uart_driver auart_driver;
352
353enum mxs_auart_type {
354 IMX23_AUART,
355 IMX28_AUART,
356 ASM9260_AUART,
357};
358
359struct vendor_data {
360 const u16 *reg_offset;
361};
362
363enum {
364 REG_CTRL0,
365 REG_CTRL1,
366 REG_CTRL2,
367 REG_LINECTRL,
368 REG_LINECTRL2,
369 REG_INTR,
370 REG_DATA,
371 REG_STAT,
372 REG_DEBUG,
373 REG_VERSION,
374 REG_AUTOBAUD,
375
376
377 REG_ARRAY_SIZE,
378};
379
380static const u16 mxs_asm9260_offsets[REG_ARRAY_SIZE] = {
381 [REG_CTRL0] = ASM9260_HW_CTRL0,
382 [REG_CTRL1] = ASM9260_HW_CTRL1,
383 [REG_CTRL2] = ASM9260_HW_CTRL2,
384 [REG_LINECTRL] = ASM9260_HW_LINECTRL,
385 [REG_INTR] = ASM9260_HW_INTR,
386 [REG_DATA] = ASM9260_HW_DATA,
387 [REG_STAT] = ASM9260_HW_STAT,
388 [REG_DEBUG] = ASM9260_HW_DEBUG,
389 [REG_AUTOBAUD] = ASM9260_HW_AUTOBAUD,
390};
391
392static const u16 mxs_stmp37xx_offsets[REG_ARRAY_SIZE] = {
393 [REG_CTRL0] = AUART_CTRL0,
394 [REG_CTRL1] = AUART_CTRL1,
395 [REG_CTRL2] = AUART_CTRL2,
396 [REG_LINECTRL] = AUART_LINECTRL,
397 [REG_LINECTRL2] = AUART_LINECTRL2,
398 [REG_INTR] = AUART_INTR,
399 [REG_DATA] = AUART_DATA,
400 [REG_STAT] = AUART_STAT,
401 [REG_DEBUG] = AUART_DEBUG,
402 [REG_VERSION] = AUART_VERSION,
403 [REG_AUTOBAUD] = AUART_AUTOBAUD,
404};
405
406static const struct vendor_data vendor_alphascale_asm9260 = {
407 .reg_offset = mxs_asm9260_offsets,
408};
409
410static const struct vendor_data vendor_freescale_stmp37xx = {
411 .reg_offset = mxs_stmp37xx_offsets,
412};
413
414struct mxs_auart_port {
415 struct uart_port port;
416
417#define MXS_AUART_DMA_ENABLED 0x2
418#define MXS_AUART_DMA_TX_SYNC 2
419#define MXS_AUART_DMA_RX_READY 3
420#define MXS_AUART_RTSCTS 4
421 unsigned long flags;
422 unsigned int mctrl_prev;
423 enum mxs_auart_type devtype;
424 const struct vendor_data *vendor;
425
426 struct clk *clk;
427 struct clk *clk_ahb;
428 struct device *dev;
429
430
431 struct scatterlist tx_sgl;
432 struct dma_chan *tx_dma_chan;
433 void *tx_dma_buf;
434
435 struct scatterlist rx_sgl;
436 struct dma_chan *rx_dma_chan;
437 void *rx_dma_buf;
438
439 struct mctrl_gpios *gpios;
440 int gpio_irq[UART_GPIO_MAX];
441 bool ms_irq_enabled;
442};
443
444static const struct of_device_id mxs_auart_dt_ids[] = {
445 {
446 .compatible = "fsl,imx28-auart",
447 .data = (const void *)IMX28_AUART
448 }, {
449 .compatible = "fsl,imx23-auart",
450 .data = (const void *)IMX23_AUART
451 }, {
452 .compatible = "alphascale,asm9260-auart",
453 .data = (const void *)ASM9260_AUART
454 }, { }
455};
456MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);
457
458static inline int is_imx28_auart(struct mxs_auart_port *s)
459{
460 return s->devtype == IMX28_AUART;
461}
462
463static inline int is_asm9260_auart(struct mxs_auart_port *s)
464{
465 return s->devtype == ASM9260_AUART;
466}
467
468static inline bool auart_dma_enabled(struct mxs_auart_port *s)
469{
470 return s->flags & MXS_AUART_DMA_ENABLED;
471}
472
473static unsigned int mxs_reg_to_offset(const struct mxs_auart_port *uap,
474 unsigned int reg)
475{
476 return uap->vendor->reg_offset[reg];
477}
478
479static unsigned int mxs_read(const struct mxs_auart_port *uap,
480 unsigned int reg)
481{
482 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
483
484 return readl_relaxed(addr);
485}
486
487static void mxs_write(unsigned int val, struct mxs_auart_port *uap,
488 unsigned int reg)
489{
490 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
491
492 writel_relaxed(val, addr);
493}
494
495static void mxs_set(unsigned int val, struct mxs_auart_port *uap,
496 unsigned int reg)
497{
498 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
499
500 writel_relaxed(val, addr + SET_REG);
501}
502
503static void mxs_clr(unsigned int val, struct mxs_auart_port *uap,
504 unsigned int reg)
505{
506 void __iomem *addr = uap->port.membase + mxs_reg_to_offset(uap, reg);
507
508 writel_relaxed(val, addr + CLR_REG);
509}
510
511static void mxs_auart_stop_tx(struct uart_port *u);
512
513#define to_auart_port(u) container_of(u, struct mxs_auart_port, port)
514
515static void mxs_auart_tx_chars(struct mxs_auart_port *s);
516
517static void dma_tx_callback(void *param)
518{
519 struct mxs_auart_port *s = param;
520 struct circ_buf *xmit = &s->port.state->xmit;
521
522 dma_unmap_sg(s->dev, &s->tx_sgl, 1, DMA_TO_DEVICE);
523
524
525 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
526 smp_mb__after_atomic();
527
528
529 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
530 uart_write_wakeup(&s->port);
531
532 mxs_auart_tx_chars(s);
533}
534
535static int mxs_auart_dma_tx(struct mxs_auart_port *s, int size)
536{
537 struct dma_async_tx_descriptor *desc;
538 struct scatterlist *sgl = &s->tx_sgl;
539 struct dma_chan *channel = s->tx_dma_chan;
540 u32 pio;
541
542
543 pio = AUART_CTRL1_XFER_COUNT(size);
544 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)&pio,
545 1, DMA_TRANS_NONE, 0);
546 if (!desc) {
547 dev_err(s->dev, "step 1 error\n");
548 return -EINVAL;
549 }
550
551
552 sg_init_one(sgl, s->tx_dma_buf, size);
553 dma_map_sg(s->dev, sgl, 1, DMA_TO_DEVICE);
554 desc = dmaengine_prep_slave_sg(channel, sgl,
555 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
556 if (!desc) {
557 dev_err(s->dev, "step 2 error\n");
558 return -EINVAL;
559 }
560
561
562 desc->callback = dma_tx_callback;
563 desc->callback_param = s;
564 dmaengine_submit(desc);
565 dma_async_issue_pending(channel);
566 return 0;
567}
568
569static void mxs_auart_tx_chars(struct mxs_auart_port *s)
570{
571 struct circ_buf *xmit = &s->port.state->xmit;
572
573 if (auart_dma_enabled(s)) {
574 u32 i = 0;
575 int size;
576 void *buffer = s->tx_dma_buf;
577
578 if (test_and_set_bit(MXS_AUART_DMA_TX_SYNC, &s->flags))
579 return;
580
581 while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
582 size = min_t(u32, UART_XMIT_SIZE - i,
583 CIRC_CNT_TO_END(xmit->head,
584 xmit->tail,
585 UART_XMIT_SIZE));
586 memcpy(buffer + i, xmit->buf + xmit->tail, size);
587 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
588
589 i += size;
590 if (i >= UART_XMIT_SIZE)
591 break;
592 }
593
594 if (uart_tx_stopped(&s->port))
595 mxs_auart_stop_tx(&s->port);
596
597 if (i) {
598 mxs_auart_dma_tx(s, i);
599 } else {
600 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
601 smp_mb__after_atomic();
602 }
603 return;
604 }
605
606
607 while (!(mxs_read(s, REG_STAT) & AUART_STAT_TXFF)) {
608 if (s->port.x_char) {
609 s->port.icount.tx++;
610 mxs_write(s->port.x_char, s, REG_DATA);
611 s->port.x_char = 0;
612 continue;
613 }
614 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
615 s->port.icount.tx++;
616 mxs_write(xmit->buf[xmit->tail], s, REG_DATA);
617 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
618 } else
619 break;
620 }
621 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
622 uart_write_wakeup(&s->port);
623
624 if (uart_circ_empty(&(s->port.state->xmit)))
625 mxs_clr(AUART_INTR_TXIEN, s, REG_INTR);
626 else
627 mxs_set(AUART_INTR_TXIEN, s, REG_INTR);
628
629 if (uart_tx_stopped(&s->port))
630 mxs_auart_stop_tx(&s->port);
631}
632
633static void mxs_auart_rx_char(struct mxs_auart_port *s)
634{
635 int flag;
636 u32 stat;
637 u8 c;
638
639 c = mxs_read(s, REG_DATA);
640 stat = mxs_read(s, REG_STAT);
641
642 flag = TTY_NORMAL;
643 s->port.icount.rx++;
644
645 if (stat & AUART_STAT_BERR) {
646 s->port.icount.brk++;
647 if (uart_handle_break(&s->port))
648 goto out;
649 } else if (stat & AUART_STAT_PERR) {
650 s->port.icount.parity++;
651 } else if (stat & AUART_STAT_FERR) {
652 s->port.icount.frame++;
653 }
654
655
656
657
658 stat &= s->port.read_status_mask;
659
660 if (stat & AUART_STAT_BERR) {
661 flag = TTY_BREAK;
662 } else if (stat & AUART_STAT_PERR)
663 flag = TTY_PARITY;
664 else if (stat & AUART_STAT_FERR)
665 flag = TTY_FRAME;
666
667 if (stat & AUART_STAT_OERR)
668 s->port.icount.overrun++;
669
670 if (uart_handle_sysrq_char(&s->port, c))
671 goto out;
672
673 uart_insert_char(&s->port, stat, AUART_STAT_OERR, c, flag);
674out:
675 mxs_write(stat, s, REG_STAT);
676}
677
678static void mxs_auart_rx_chars(struct mxs_auart_port *s)
679{
680 u32 stat = 0;
681
682 for (;;) {
683 stat = mxs_read(s, REG_STAT);
684 if (stat & AUART_STAT_RXFE)
685 break;
686 mxs_auart_rx_char(s);
687 }
688
689 mxs_write(stat, s, REG_STAT);
690 tty_flip_buffer_push(&s->port.state->port);
691}
692
693static int mxs_auart_request_port(struct uart_port *u)
694{
695 return 0;
696}
697
698static int mxs_auart_verify_port(struct uart_port *u,
699 struct serial_struct *ser)
700{
701 if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
702 return -EINVAL;
703 return 0;
704}
705
706static void mxs_auart_config_port(struct uart_port *u, int flags)
707{
708}
709
710static const char *mxs_auart_type(struct uart_port *u)
711{
712 struct mxs_auart_port *s = to_auart_port(u);
713
714 return dev_name(s->dev);
715}
716
717static void mxs_auart_release_port(struct uart_port *u)
718{
719}
720
721static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
722{
723 struct mxs_auart_port *s = to_auart_port(u);
724
725 u32 ctrl = mxs_read(s, REG_CTRL2);
726
727 ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
728 if (mctrl & TIOCM_RTS) {
729 if (uart_cts_enabled(u))
730 ctrl |= AUART_CTRL2_RTSEN;
731 else
732 ctrl |= AUART_CTRL2_RTS;
733 }
734
735 mxs_write(ctrl, s, REG_CTRL2);
736
737 mctrl_gpio_set(s->gpios, mctrl);
738}
739
740#define MCTRL_ANY_DELTA (TIOCM_RI | TIOCM_DSR | TIOCM_CD | TIOCM_CTS)
741static u32 mxs_auart_modem_status(struct mxs_auart_port *s, u32 mctrl)
742{
743 u32 mctrl_diff;
744
745 mctrl_diff = mctrl ^ s->mctrl_prev;
746 s->mctrl_prev = mctrl;
747 if (mctrl_diff & MCTRL_ANY_DELTA && s->ms_irq_enabled &&
748 s->port.state != NULL) {
749 if (mctrl_diff & TIOCM_RI)
750 s->port.icount.rng++;
751 if (mctrl_diff & TIOCM_DSR)
752 s->port.icount.dsr++;
753 if (mctrl_diff & TIOCM_CD)
754 uart_handle_dcd_change(&s->port, mctrl & TIOCM_CD);
755 if (mctrl_diff & TIOCM_CTS)
756 uart_handle_cts_change(&s->port, mctrl & TIOCM_CTS);
757
758 wake_up_interruptible(&s->port.state->port.delta_msr_wait);
759 }
760 return mctrl;
761}
762
763static u32 mxs_auart_get_mctrl(struct uart_port *u)
764{
765 struct mxs_auart_port *s = to_auart_port(u);
766 u32 stat = mxs_read(s, REG_STAT);
767 u32 mctrl = 0;
768
769 if (stat & AUART_STAT_CTS)
770 mctrl |= TIOCM_CTS;
771
772 return mctrl_gpio_get(s->gpios, &mctrl);
773}
774
775
776
777
778static void mxs_auart_enable_ms(struct uart_port *port)
779{
780 struct mxs_auart_port *s = to_auart_port(port);
781
782
783
784
785 if (s->ms_irq_enabled)
786 return;
787
788 s->ms_irq_enabled = true;
789
790 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
791 enable_irq(s->gpio_irq[UART_GPIO_CTS]);
792
793
794 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
795 enable_irq(s->gpio_irq[UART_GPIO_DSR]);
796
797 if (s->gpio_irq[UART_GPIO_RI] >= 0)
798 enable_irq(s->gpio_irq[UART_GPIO_RI]);
799
800 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
801 enable_irq(s->gpio_irq[UART_GPIO_DCD]);
802}
803
804
805
806
807static void mxs_auart_disable_ms(struct uart_port *port)
808{
809 struct mxs_auart_port *s = to_auart_port(port);
810
811
812
813
814 if (!s->ms_irq_enabled)
815 return;
816
817 s->ms_irq_enabled = false;
818
819 if (s->gpio_irq[UART_GPIO_CTS] >= 0)
820 disable_irq(s->gpio_irq[UART_GPIO_CTS]);
821
822
823 if (s->gpio_irq[UART_GPIO_DSR] >= 0)
824 disable_irq(s->gpio_irq[UART_GPIO_DSR]);
825
826 if (s->gpio_irq[UART_GPIO_RI] >= 0)
827 disable_irq(s->gpio_irq[UART_GPIO_RI]);
828
829 if (s->gpio_irq[UART_GPIO_DCD] >= 0)
830 disable_irq(s->gpio_irq[UART_GPIO_DCD]);
831}
832
833static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s);
834static void dma_rx_callback(void *arg)
835{
836 struct mxs_auart_port *s = (struct mxs_auart_port *) arg;
837 struct tty_port *port = &s->port.state->port;
838 int count;
839 u32 stat;
840
841 dma_unmap_sg(s->dev, &s->rx_sgl, 1, DMA_FROM_DEVICE);
842
843 stat = mxs_read(s, REG_STAT);
844 stat &= ~(AUART_STAT_OERR | AUART_STAT_BERR |
845 AUART_STAT_PERR | AUART_STAT_FERR);
846
847 count = stat & AUART_STAT_RXCOUNT_MASK;
848 tty_insert_flip_string(port, s->rx_dma_buf, count);
849
850 mxs_write(stat, s, REG_STAT);
851 tty_flip_buffer_push(port);
852
853
854 mxs_auart_dma_prep_rx(s);
855}
856
857static int mxs_auart_dma_prep_rx(struct mxs_auart_port *s)
858{
859 struct dma_async_tx_descriptor *desc;
860 struct scatterlist *sgl = &s->rx_sgl;
861 struct dma_chan *channel = s->rx_dma_chan;
862 u32 pio[1];
863
864
865 pio[0] = AUART_CTRL0_RXTO_ENABLE
866 | AUART_CTRL0_RXTIMEOUT(0x80)
867 | AUART_CTRL0_XFER_COUNT(UART_XMIT_SIZE);
868 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
869 1, DMA_TRANS_NONE, 0);
870 if (!desc) {
871 dev_err(s->dev, "step 1 error\n");
872 return -EINVAL;
873 }
874
875
876 sg_init_one(sgl, s->rx_dma_buf, UART_XMIT_SIZE);
877 dma_map_sg(s->dev, sgl, 1, DMA_FROM_DEVICE);
878 desc = dmaengine_prep_slave_sg(channel, sgl, 1, DMA_DEV_TO_MEM,
879 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
880 if (!desc) {
881 dev_err(s->dev, "step 2 error\n");
882 return -1;
883 }
884
885
886 desc->callback = dma_rx_callback;
887 desc->callback_param = s;
888 dmaengine_submit(desc);
889 dma_async_issue_pending(channel);
890 return 0;
891}
892
893static void mxs_auart_dma_exit_channel(struct mxs_auart_port *s)
894{
895 if (s->tx_dma_chan) {
896 dma_release_channel(s->tx_dma_chan);
897 s->tx_dma_chan = NULL;
898 }
899 if (s->rx_dma_chan) {
900 dma_release_channel(s->rx_dma_chan);
901 s->rx_dma_chan = NULL;
902 }
903
904 kfree(s->tx_dma_buf);
905 kfree(s->rx_dma_buf);
906 s->tx_dma_buf = NULL;
907 s->rx_dma_buf = NULL;
908}
909
910static void mxs_auart_dma_exit(struct mxs_auart_port *s)
911{
912
913 mxs_clr(AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE | AUART_CTRL2_DMAONERR,
914 s, REG_CTRL2);
915
916 mxs_auart_dma_exit_channel(s);
917 s->flags &= ~MXS_AUART_DMA_ENABLED;
918 clear_bit(MXS_AUART_DMA_TX_SYNC, &s->flags);
919 clear_bit(MXS_AUART_DMA_RX_READY, &s->flags);
920}
921
922static int mxs_auart_dma_init(struct mxs_auart_port *s)
923{
924 if (auart_dma_enabled(s))
925 return 0;
926
927
928 s->rx_dma_chan = dma_request_slave_channel(s->dev, "rx");
929 if (!s->rx_dma_chan)
930 goto err_out;
931 s->rx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
932 if (!s->rx_dma_buf)
933 goto err_out;
934
935
936 s->tx_dma_chan = dma_request_slave_channel(s->dev, "tx");
937 if (!s->tx_dma_chan)
938 goto err_out;
939 s->tx_dma_buf = kzalloc(UART_XMIT_SIZE, GFP_KERNEL | GFP_DMA);
940 if (!s->tx_dma_buf)
941 goto err_out;
942
943
944 s->flags |= MXS_AUART_DMA_ENABLED;
945 dev_dbg(s->dev, "enabled the DMA support.");
946
947
948 s->port.fifosize = UART_XMIT_SIZE;
949
950 return 0;
951
952err_out:
953 mxs_auart_dma_exit_channel(s);
954 return -EINVAL;
955
956}
957
958#define RTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_RTS)
959#define CTS_AT_AUART() !mctrl_gpio_to_gpiod(s->gpios, UART_GPIO_CTS)
960static void mxs_auart_settermios(struct uart_port *u,
961 struct ktermios *termios,
962 struct ktermios *old)
963{
964 struct mxs_auart_port *s = to_auart_port(u);
965 u32 ctrl, ctrl2, div;
966 unsigned int cflag, baud, baud_min, baud_max;
967
968 cflag = termios->c_cflag;
969
970 ctrl = AUART_LINECTRL_FEN;
971 ctrl2 = mxs_read(s, REG_CTRL2);
972
973 ctrl |= AUART_LINECTRL_WLEN(tty_get_char_size(cflag));
974
975
976 if (cflag & PARENB) {
977 ctrl |= AUART_LINECTRL_PEN;
978 if ((cflag & PARODD) == 0)
979 ctrl |= AUART_LINECTRL_EPS;
980 if (cflag & CMSPAR)
981 ctrl |= AUART_LINECTRL_SPS;
982 }
983
984 u->read_status_mask = AUART_STAT_OERR;
985
986 if (termios->c_iflag & INPCK)
987 u->read_status_mask |= AUART_STAT_PERR;
988 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
989 u->read_status_mask |= AUART_STAT_BERR;
990
991
992
993
994 u->ignore_status_mask = 0;
995 if (termios->c_iflag & IGNPAR)
996 u->ignore_status_mask |= AUART_STAT_PERR;
997 if (termios->c_iflag & IGNBRK) {
998 u->ignore_status_mask |= AUART_STAT_BERR;
999
1000
1001
1002
1003 if (termios->c_iflag & IGNPAR)
1004 u->ignore_status_mask |= AUART_STAT_OERR;
1005 }
1006
1007
1008
1009
1010 if (cflag & CREAD)
1011 ctrl2 |= AUART_CTRL2_RXE;
1012 else
1013 ctrl2 &= ~AUART_CTRL2_RXE;
1014
1015
1016 if (cflag & CSTOPB)
1017 ctrl |= AUART_LINECTRL_STP2;
1018
1019
1020 ctrl2 &= ~(AUART_CTRL2_CTSEN | AUART_CTRL2_RTSEN);
1021 if (cflag & CRTSCTS) {
1022
1023
1024
1025
1026
1027
1028 if (is_imx28_auart(s)
1029 && test_bit(MXS_AUART_RTSCTS, &s->flags)) {
1030 if (!mxs_auart_dma_init(s))
1031
1032 ctrl2 |= AUART_CTRL2_TXDMAE | AUART_CTRL2_RXDMAE
1033 | AUART_CTRL2_DMAONERR;
1034 }
1035
1036
1037 ctrl2 |= AUART_CTRL2_RTSEN;
1038 if (CTS_AT_AUART())
1039 ctrl2 |= AUART_CTRL2_CTSEN;
1040 }
1041
1042
1043 if (is_asm9260_auart(s)) {
1044 baud = uart_get_baud_rate(u, termios, old,
1045 u->uartclk * 4 / 0x3FFFFF,
1046 u->uartclk / 16);
1047 div = u->uartclk * 4 / baud;
1048 } else {
1049 baud_min = DIV_ROUND_UP(u->uartclk * 32,
1050 AUART_LINECTRL_BAUD_DIV_MAX);
1051 baud_max = u->uartclk * 32 / AUART_LINECTRL_BAUD_DIV_MIN;
1052 baud = uart_get_baud_rate(u, termios, old, baud_min, baud_max);
1053 div = DIV_ROUND_CLOSEST(u->uartclk * 32, baud);
1054 }
1055
1056 ctrl |= AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
1057 ctrl |= AUART_LINECTRL_BAUD_DIVINT(div >> 6);
1058 mxs_write(ctrl, s, REG_LINECTRL);
1059
1060 mxs_write(ctrl2, s, REG_CTRL2);
1061
1062 uart_update_timeout(u, termios->c_cflag, baud);
1063
1064
1065 if (auart_dma_enabled(s) &&
1066 !test_and_set_bit(MXS_AUART_DMA_RX_READY, &s->flags)) {
1067 if (!mxs_auart_dma_prep_rx(s)) {
1068
1069 mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN,
1070 s, REG_INTR);
1071 } else {
1072 mxs_auart_dma_exit(s);
1073 dev_err(s->dev, "We can not start up the DMA.\n");
1074 }
1075 }
1076
1077
1078 if (UART_ENABLE_MS(u, termios->c_cflag))
1079 mxs_auart_enable_ms(u);
1080 else
1081 mxs_auart_disable_ms(u);
1082}
1083
1084static void mxs_auart_set_ldisc(struct uart_port *port,
1085 struct ktermios *termios)
1086{
1087 if (termios->c_line == N_PPS) {
1088 port->flags |= UPF_HARDPPS_CD;
1089 mxs_auart_enable_ms(port);
1090 } else {
1091 port->flags &= ~UPF_HARDPPS_CD;
1092 }
1093}
1094
1095static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
1096{
1097 u32 istat;
1098 struct mxs_auart_port *s = context;
1099 u32 mctrl_temp = s->mctrl_prev;
1100 u32 stat = mxs_read(s, REG_STAT);
1101
1102 istat = mxs_read(s, REG_INTR);
1103
1104
1105 mxs_clr(istat & (AUART_INTR_RTIS | AUART_INTR_TXIS | AUART_INTR_RXIS
1106 | AUART_INTR_CTSMIS), s, REG_INTR);
1107
1108
1109
1110
1111 if (irq == s->gpio_irq[UART_GPIO_CTS] ||
1112 irq == s->gpio_irq[UART_GPIO_DCD] ||
1113 irq == s->gpio_irq[UART_GPIO_DSR] ||
1114 irq == s->gpio_irq[UART_GPIO_RI])
1115 mxs_auart_modem_status(s,
1116 mctrl_gpio_get(s->gpios, &mctrl_temp));
1117
1118 if (istat & AUART_INTR_CTSMIS) {
1119 if (CTS_AT_AUART() && s->ms_irq_enabled)
1120 uart_handle_cts_change(&s->port,
1121 stat & AUART_STAT_CTS);
1122 mxs_clr(AUART_INTR_CTSMIS, s, REG_INTR);
1123 istat &= ~AUART_INTR_CTSMIS;
1124 }
1125
1126 if (istat & (AUART_INTR_RTIS | AUART_INTR_RXIS)) {
1127 if (!auart_dma_enabled(s))
1128 mxs_auart_rx_chars(s);
1129 istat &= ~(AUART_INTR_RTIS | AUART_INTR_RXIS);
1130 }
1131
1132 if (istat & AUART_INTR_TXIS) {
1133 mxs_auart_tx_chars(s);
1134 istat &= ~AUART_INTR_TXIS;
1135 }
1136
1137 return IRQ_HANDLED;
1138}
1139
1140static void mxs_auart_reset_deassert(struct mxs_auart_port *s)
1141{
1142 int i;
1143 unsigned int reg;
1144
1145 mxs_clr(AUART_CTRL0_SFTRST, s, REG_CTRL0);
1146
1147 for (i = 0; i < 10000; i++) {
1148 reg = mxs_read(s, REG_CTRL0);
1149 if (!(reg & AUART_CTRL0_SFTRST))
1150 break;
1151 udelay(3);
1152 }
1153 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1154}
1155
1156static void mxs_auart_reset_assert(struct mxs_auart_port *s)
1157{
1158 int i;
1159 u32 reg;
1160
1161 reg = mxs_read(s, REG_CTRL0);
1162
1163 if (reg & AUART_CTRL0_SFTRST)
1164 return;
1165
1166 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1167 mxs_set(AUART_CTRL0_SFTRST, s, REG_CTRL0);
1168
1169 for (i = 0; i < 1000; i++) {
1170 reg = mxs_read(s, REG_CTRL0);
1171
1172 if (reg & AUART_CTRL0_CLKGATE)
1173 return;
1174 udelay(10);
1175 }
1176
1177 dev_err(s->dev, "Failed to reset the unit.");
1178}
1179
1180static int mxs_auart_startup(struct uart_port *u)
1181{
1182 int ret;
1183 struct mxs_auart_port *s = to_auart_port(u);
1184
1185 ret = clk_prepare_enable(s->clk);
1186 if (ret)
1187 return ret;
1188
1189 if (uart_console(u)) {
1190 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1191 } else {
1192
1193 mxs_auart_reset_assert(s);
1194 mxs_auart_reset_deassert(s);
1195 }
1196
1197 mxs_set(AUART_CTRL2_UARTEN, s, REG_CTRL2);
1198
1199 mxs_write(AUART_INTR_RXIEN | AUART_INTR_RTIEN | AUART_INTR_CTSMIEN,
1200 s, REG_INTR);
1201
1202
1203 u->fifosize = MXS_AUART_FIFO_SIZE;
1204
1205
1206
1207
1208
1209 mxs_set(AUART_LINECTRL_FEN, s, REG_LINECTRL);
1210
1211
1212 mctrl_gpio_get(s->gpios, &s->mctrl_prev);
1213
1214 s->ms_irq_enabled = false;
1215 return 0;
1216}
1217
1218static void mxs_auart_shutdown(struct uart_port *u)
1219{
1220 struct mxs_auart_port *s = to_auart_port(u);
1221
1222 mxs_auart_disable_ms(u);
1223
1224 if (auart_dma_enabled(s))
1225 mxs_auart_dma_exit(s);
1226
1227 if (uart_console(u)) {
1228 mxs_clr(AUART_CTRL2_UARTEN, s, REG_CTRL2);
1229
1230 mxs_clr(AUART_INTR_RXIEN | AUART_INTR_RTIEN |
1231 AUART_INTR_CTSMIEN, s, REG_INTR);
1232 mxs_set(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1233 } else {
1234 mxs_auart_reset_assert(s);
1235 }
1236
1237 clk_disable_unprepare(s->clk);
1238}
1239
1240static unsigned int mxs_auart_tx_empty(struct uart_port *u)
1241{
1242 struct mxs_auart_port *s = to_auart_port(u);
1243
1244 if ((mxs_read(s, REG_STAT) &
1245 (AUART_STAT_TXFE | AUART_STAT_BUSY)) == AUART_STAT_TXFE)
1246 return TIOCSER_TEMT;
1247
1248 return 0;
1249}
1250
1251static void mxs_auart_start_tx(struct uart_port *u)
1252{
1253 struct mxs_auart_port *s = to_auart_port(u);
1254
1255
1256 mxs_set(AUART_CTRL2_TXE, s, REG_CTRL2);
1257
1258 mxs_auart_tx_chars(s);
1259}
1260
1261static void mxs_auart_stop_tx(struct uart_port *u)
1262{
1263 struct mxs_auart_port *s = to_auart_port(u);
1264
1265 mxs_clr(AUART_CTRL2_TXE, s, REG_CTRL2);
1266}
1267
1268static void mxs_auart_stop_rx(struct uart_port *u)
1269{
1270 struct mxs_auart_port *s = to_auart_port(u);
1271
1272 mxs_clr(AUART_CTRL2_RXE, s, REG_CTRL2);
1273}
1274
1275static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
1276{
1277 struct mxs_auart_port *s = to_auart_port(u);
1278
1279 if (ctl)
1280 mxs_set(AUART_LINECTRL_BRK, s, REG_LINECTRL);
1281 else
1282 mxs_clr(AUART_LINECTRL_BRK, s, REG_LINECTRL);
1283}
1284
1285static const struct uart_ops mxs_auart_ops = {
1286 .tx_empty = mxs_auart_tx_empty,
1287 .start_tx = mxs_auart_start_tx,
1288 .stop_tx = mxs_auart_stop_tx,
1289 .stop_rx = mxs_auart_stop_rx,
1290 .enable_ms = mxs_auart_enable_ms,
1291 .break_ctl = mxs_auart_break_ctl,
1292 .set_mctrl = mxs_auart_set_mctrl,
1293 .get_mctrl = mxs_auart_get_mctrl,
1294 .startup = mxs_auart_startup,
1295 .shutdown = mxs_auart_shutdown,
1296 .set_termios = mxs_auart_settermios,
1297 .set_ldisc = mxs_auart_set_ldisc,
1298 .type = mxs_auart_type,
1299 .release_port = mxs_auart_release_port,
1300 .request_port = mxs_auart_request_port,
1301 .config_port = mxs_auart_config_port,
1302 .verify_port = mxs_auart_verify_port,
1303};
1304
1305static struct mxs_auart_port *auart_port[MXS_AUART_PORTS];
1306
1307#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1308static void mxs_auart_console_putchar(struct uart_port *port, int ch)
1309{
1310 struct mxs_auart_port *s = to_auart_port(port);
1311 unsigned int to = 1000;
1312
1313 while (mxs_read(s, REG_STAT) & AUART_STAT_TXFF) {
1314 if (!to--)
1315 break;
1316 udelay(1);
1317 }
1318
1319 mxs_write(ch, s, REG_DATA);
1320}
1321
1322static void
1323auart_console_write(struct console *co, const char *str, unsigned int count)
1324{
1325 struct mxs_auart_port *s;
1326 struct uart_port *port;
1327 unsigned int old_ctrl0, old_ctrl2;
1328 unsigned int to = 20000;
1329
1330 if (co->index >= MXS_AUART_PORTS || co->index < 0)
1331 return;
1332
1333 s = auart_port[co->index];
1334 port = &s->port;
1335
1336 clk_enable(s->clk);
1337
1338
1339 old_ctrl2 = mxs_read(s, REG_CTRL2);
1340 old_ctrl0 = mxs_read(s, REG_CTRL0);
1341
1342 mxs_clr(AUART_CTRL0_CLKGATE, s, REG_CTRL0);
1343 mxs_set(AUART_CTRL2_UARTEN | AUART_CTRL2_TXE, s, REG_CTRL2);
1344
1345 uart_console_write(port, str, count, mxs_auart_console_putchar);
1346
1347
1348 while (mxs_read(s, REG_STAT) & AUART_STAT_BUSY) {
1349 udelay(1);
1350 if (!to--)
1351 break;
1352 }
1353
1354
1355
1356
1357
1358
1359
1360 if (!(mxs_read(s, REG_STAT) & AUART_STAT_BUSY)) {
1361 mxs_write(old_ctrl0, s, REG_CTRL0);
1362 mxs_write(old_ctrl2, s, REG_CTRL2);
1363 }
1364
1365 clk_disable(s->clk);
1366}
1367
1368static void __init
1369auart_console_get_options(struct mxs_auart_port *s, int *baud,
1370 int *parity, int *bits)
1371{
1372 struct uart_port *port = &s->port;
1373 unsigned int lcr_h, quot;
1374
1375 if (!(mxs_read(s, REG_CTRL2) & AUART_CTRL2_UARTEN))
1376 return;
1377
1378 lcr_h = mxs_read(s, REG_LINECTRL);
1379
1380 *parity = 'n';
1381 if (lcr_h & AUART_LINECTRL_PEN) {
1382 if (lcr_h & AUART_LINECTRL_EPS)
1383 *parity = 'e';
1384 else
1385 *parity = 'o';
1386 }
1387
1388 if ((lcr_h & AUART_LINECTRL_WLEN_MASK) == AUART_LINECTRL_WLEN(7))
1389 *bits = 7;
1390 else
1391 *bits = 8;
1392
1393 quot = ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVINT_MASK))
1394 >> (AUART_LINECTRL_BAUD_DIVINT_SHIFT - 6);
1395 quot |= ((mxs_read(s, REG_LINECTRL) & AUART_LINECTRL_BAUD_DIVFRAC_MASK))
1396 >> AUART_LINECTRL_BAUD_DIVFRAC_SHIFT;
1397 if (quot == 0)
1398 quot = 1;
1399
1400 *baud = (port->uartclk << 2) / quot;
1401}
1402
1403static int __init
1404auart_console_setup(struct console *co, char *options)
1405{
1406 struct mxs_auart_port *s;
1407 int baud = 9600;
1408 int bits = 8;
1409 int parity = 'n';
1410 int flow = 'n';
1411 int ret;
1412
1413
1414
1415
1416
1417
1418 if (co->index == -1 || co->index >= ARRAY_SIZE(auart_port))
1419 co->index = 0;
1420 s = auart_port[co->index];
1421 if (!s)
1422 return -ENODEV;
1423
1424 ret = clk_prepare_enable(s->clk);
1425 if (ret)
1426 return ret;
1427
1428 if (options)
1429 uart_parse_options(options, &baud, &parity, &bits, &flow);
1430 else
1431 auart_console_get_options(s, &baud, &parity, &bits);
1432
1433 ret = uart_set_options(&s->port, co, baud, parity, bits, flow);
1434
1435 clk_disable_unprepare(s->clk);
1436
1437 return ret;
1438}
1439
1440static struct console auart_console = {
1441 .name = "ttyAPP",
1442 .write = auart_console_write,
1443 .device = uart_console_device,
1444 .setup = auart_console_setup,
1445 .flags = CON_PRINTBUFFER,
1446 .index = -1,
1447 .data = &auart_driver,
1448};
1449#endif
1450
1451static struct uart_driver auart_driver = {
1452 .owner = THIS_MODULE,
1453 .driver_name = "ttyAPP",
1454 .dev_name = "ttyAPP",
1455 .major = 0,
1456 .minor = 0,
1457 .nr = MXS_AUART_PORTS,
1458#ifdef CONFIG_SERIAL_MXS_AUART_CONSOLE
1459 .cons = &auart_console,
1460#endif
1461};
1462
1463static void mxs_init_regs(struct mxs_auart_port *s)
1464{
1465 if (is_asm9260_auart(s))
1466 s->vendor = &vendor_alphascale_asm9260;
1467 else
1468 s->vendor = &vendor_freescale_stmp37xx;
1469}
1470
1471static int mxs_get_clks(struct mxs_auart_port *s,
1472 struct platform_device *pdev)
1473{
1474 int err;
1475
1476 if (!is_asm9260_auart(s)) {
1477 s->clk = devm_clk_get(&pdev->dev, NULL);
1478 return PTR_ERR_OR_ZERO(s->clk);
1479 }
1480
1481 s->clk = devm_clk_get(s->dev, "mod");
1482 if (IS_ERR(s->clk)) {
1483 dev_err(s->dev, "Failed to get \"mod\" clk\n");
1484 return PTR_ERR(s->clk);
1485 }
1486
1487 s->clk_ahb = devm_clk_get(s->dev, "ahb");
1488 if (IS_ERR(s->clk_ahb)) {
1489 dev_err(s->dev, "Failed to get \"ahb\" clk\n");
1490 return PTR_ERR(s->clk_ahb);
1491 }
1492
1493 err = clk_prepare_enable(s->clk_ahb);
1494 if (err) {
1495 dev_err(s->dev, "Failed to enable ahb_clk!\n");
1496 return err;
1497 }
1498
1499 err = clk_set_rate(s->clk, clk_get_rate(s->clk_ahb));
1500 if (err) {
1501 dev_err(s->dev, "Failed to set rate!\n");
1502 goto disable_clk_ahb;
1503 }
1504
1505 err = clk_prepare_enable(s->clk);
1506 if (err) {
1507 dev_err(s->dev, "Failed to enable clk!\n");
1508 goto disable_clk_ahb;
1509 }
1510
1511 return 0;
1512
1513disable_clk_ahb:
1514 clk_disable_unprepare(s->clk_ahb);
1515 return err;
1516}
1517
1518static int mxs_auart_init_gpios(struct mxs_auart_port *s, struct device *dev)
1519{
1520 enum mctrl_gpio_idx i;
1521 struct gpio_desc *gpiod;
1522
1523 s->gpios = mctrl_gpio_init_noauto(dev, 0);
1524 if (IS_ERR(s->gpios))
1525 return PTR_ERR(s->gpios);
1526
1527
1528 if (!RTS_AT_AUART() || !CTS_AT_AUART()) {
1529 if (test_bit(MXS_AUART_RTSCTS, &s->flags))
1530 dev_warn(dev,
1531 "DMA and flow control via gpio may cause some problems. DMA disabled!\n");
1532 clear_bit(MXS_AUART_RTSCTS, &s->flags);
1533 }
1534
1535 for (i = 0; i < UART_GPIO_MAX; i++) {
1536 gpiod = mctrl_gpio_to_gpiod(s->gpios, i);
1537 if (gpiod && (gpiod_get_direction(gpiod) == 1))
1538 s->gpio_irq[i] = gpiod_to_irq(gpiod);
1539 else
1540 s->gpio_irq[i] = -EINVAL;
1541 }
1542
1543 return 0;
1544}
1545
1546static void mxs_auart_free_gpio_irq(struct mxs_auart_port *s)
1547{
1548 enum mctrl_gpio_idx i;
1549
1550 for (i = 0; i < UART_GPIO_MAX; i++)
1551 if (s->gpio_irq[i] >= 0)
1552 free_irq(s->gpio_irq[i], s);
1553}
1554
1555static int mxs_auart_request_gpio_irq(struct mxs_auart_port *s)
1556{
1557 int *irq = s->gpio_irq;
1558 enum mctrl_gpio_idx i;
1559 int err = 0;
1560
1561 for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1562 if (irq[i] < 0)
1563 continue;
1564
1565 irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1566 err = request_irq(irq[i], mxs_auart_irq_handle,
1567 IRQ_TYPE_EDGE_BOTH, dev_name(s->dev), s);
1568 if (err)
1569 dev_err(s->dev, "%s - Can't get %d irq\n",
1570 __func__, irq[i]);
1571 }
1572
1573
1574
1575
1576
1577 while (err && (i-- > 0))
1578 if (irq[i] >= 0)
1579 free_irq(irq[i], s);
1580
1581 return err;
1582}
1583
1584static int mxs_auart_probe(struct platform_device *pdev)
1585{
1586 struct device_node *np = pdev->dev.of_node;
1587 struct mxs_auart_port *s;
1588 u32 version;
1589 int ret, irq;
1590 struct resource *r;
1591
1592 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
1593 if (!s)
1594 return -ENOMEM;
1595
1596 s->port.dev = &pdev->dev;
1597 s->dev = &pdev->dev;
1598
1599 ret = of_alias_get_id(np, "serial");
1600 if (ret < 0) {
1601 dev_err(&pdev->dev, "failed to get alias id: %d\n", ret);
1602 return ret;
1603 }
1604 s->port.line = ret;
1605
1606 if (of_get_property(np, "uart-has-rtscts", NULL) ||
1607 of_get_property(np, "fsl,uart-has-rtscts", NULL) )
1608 set_bit(MXS_AUART_RTSCTS, &s->flags);
1609
1610 if (s->port.line >= ARRAY_SIZE(auart_port)) {
1611 dev_err(&pdev->dev, "serial%d out of range\n", s->port.line);
1612 return -EINVAL;
1613 }
1614
1615 s->devtype = (enum mxs_auart_type)of_device_get_match_data(&pdev->dev);
1616
1617 ret = mxs_get_clks(s, pdev);
1618 if (ret)
1619 return ret;
1620
1621 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1622 if (!r) {
1623 ret = -ENXIO;
1624 goto out_disable_clks;
1625 }
1626
1627 s->port.mapbase = r->start;
1628 s->port.membase = ioremap(r->start, resource_size(r));
1629 if (!s->port.membase) {
1630 ret = -ENOMEM;
1631 goto out_disable_clks;
1632 }
1633 s->port.ops = &mxs_auart_ops;
1634 s->port.iotype = UPIO_MEM;
1635 s->port.fifosize = MXS_AUART_FIFO_SIZE;
1636 s->port.uartclk = clk_get_rate(s->clk);
1637 s->port.type = PORT_IMX;
1638 s->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_MXS_AUART_CONSOLE);
1639
1640 mxs_init_regs(s);
1641
1642 s->mctrl_prev = 0;
1643
1644 irq = platform_get_irq(pdev, 0);
1645 if (irq < 0) {
1646 ret = irq;
1647 goto out_iounmap;
1648 }
1649
1650 s->port.irq = irq;
1651 ret = devm_request_irq(&pdev->dev, irq, mxs_auart_irq_handle, 0,
1652 dev_name(&pdev->dev), s);
1653 if (ret)
1654 goto out_iounmap;
1655
1656 platform_set_drvdata(pdev, s);
1657
1658 ret = mxs_auart_init_gpios(s, &pdev->dev);
1659 if (ret) {
1660 dev_err(&pdev->dev, "Failed to initialize GPIOs.\n");
1661 goto out_iounmap;
1662 }
1663
1664
1665
1666
1667 ret = mxs_auart_request_gpio_irq(s);
1668 if (ret)
1669 goto out_iounmap;
1670
1671 auart_port[s->port.line] = s;
1672
1673 mxs_auart_reset_deassert(s);
1674
1675 ret = uart_add_one_port(&auart_driver, &s->port);
1676 if (ret)
1677 goto out_free_qpio_irq;
1678
1679
1680 if (is_asm9260_auart(s)) {
1681 dev_info(&pdev->dev, "Found APPUART ASM9260\n");
1682 } else {
1683 version = mxs_read(s, REG_VERSION);
1684 dev_info(&pdev->dev, "Found APPUART %d.%d.%d\n",
1685 (version >> 24) & 0xff,
1686 (version >> 16) & 0xff, version & 0xffff);
1687 }
1688
1689 return 0;
1690
1691out_free_qpio_irq:
1692 mxs_auart_free_gpio_irq(s);
1693 auart_port[pdev->id] = NULL;
1694
1695out_iounmap:
1696 iounmap(s->port.membase);
1697
1698out_disable_clks:
1699 if (is_asm9260_auart(s)) {
1700 clk_disable_unprepare(s->clk);
1701 clk_disable_unprepare(s->clk_ahb);
1702 }
1703 return ret;
1704}
1705
1706static int mxs_auart_remove(struct platform_device *pdev)
1707{
1708 struct mxs_auart_port *s = platform_get_drvdata(pdev);
1709
1710 uart_remove_one_port(&auart_driver, &s->port);
1711 auart_port[pdev->id] = NULL;
1712 mxs_auart_free_gpio_irq(s);
1713 iounmap(s->port.membase);
1714 if (is_asm9260_auart(s)) {
1715 clk_disable_unprepare(s->clk);
1716 clk_disable_unprepare(s->clk_ahb);
1717 }
1718
1719 return 0;
1720}
1721
1722static struct platform_driver mxs_auart_driver = {
1723 .probe = mxs_auart_probe,
1724 .remove = mxs_auart_remove,
1725 .driver = {
1726 .name = "mxs-auart",
1727 .of_match_table = mxs_auart_dt_ids,
1728 },
1729};
1730
1731static int __init mxs_auart_init(void)
1732{
1733 int r;
1734
1735 r = uart_register_driver(&auart_driver);
1736 if (r)
1737 goto out;
1738
1739 r = platform_driver_register(&mxs_auart_driver);
1740 if (r)
1741 goto out_err;
1742
1743 return 0;
1744out_err:
1745 uart_unregister_driver(&auart_driver);
1746out:
1747 return r;
1748}
1749
1750static void __exit mxs_auart_exit(void)
1751{
1752 platform_driver_unregister(&mxs_auart_driver);
1753 uart_unregister_driver(&auart_driver);
1754}
1755
1756module_init(mxs_auart_init);
1757module_exit(mxs_auart_exit);
1758MODULE_LICENSE("GPL");
1759MODULE_DESCRIPTION("Freescale MXS application uart driver");
1760MODULE_ALIAS("platform:mxs-auart");
1761