f24f8a2073d97d4673ceed106deddce9cf758baf
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / bfin_uart.c
1 /*
2  * Blackfin On-Chip Serial Driver
3  *
4  * Copyright 2006-2010 Analog Devices Inc.
5  *
6  * Enter bugs at http://blackfin.uclinux.org/
7  *
8  * Licensed under the GPL-2 or later.
9  */
10
11 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12 #define SUPPORT_SYSRQ
13 #endif
14
15 #define DRIVER_NAME "bfin-uart"
16 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
17
18 #include <linux/module.h>
19 #include <linux/ioport.h>
20 #include <linux/gfp.h>
21 #include <linux/io.h>
22 #include <linux/init.h>
23 #include <linux/console.h>
24 #include <linux/sysrq.h>
25 #include <linux/platform_device.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial_core.h>
29 #include <linux/gpio.h>
30 #include <linux/irq.h>
31 #include <linux/kgdb.h>
32 #include <linux/slab.h>
33 #include <linux/dma-mapping.h>
34
35 #include <asm/portmux.h>
36 #include <asm/cacheflush.h>
37 #include <asm/dma.h>
38
39 #define port_membase(uart)     (((struct bfin_serial_port *)(uart))->port.membase)
40 #define get_lsr_cache(uart)    (((struct bfin_serial_port *)(uart))->lsr)
41 #define put_lsr_cache(uart, v) (((struct bfin_serial_port *)(uart))->lsr = (v))
42 #include <asm/bfin_serial.h>
43
44 #ifdef CONFIG_SERIAL_BFIN_MODULE
45 # undef CONFIG_EARLY_PRINTK
46 #endif
47
48 #ifdef CONFIG_SERIAL_BFIN_MODULE
49 # undef CONFIG_EARLY_PRINTK
50 #endif
51
52 /* UART name and device definitions */
53 #define BFIN_SERIAL_DEV_NAME    "ttyBF"
54 #define BFIN_SERIAL_MAJOR       204
55 #define BFIN_SERIAL_MINOR       64
56
57 static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
58
59 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
60         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
61
62 # ifndef CONFIG_SERIAL_BFIN_PIO
63 #  error KGDB only support UART in PIO mode.
64 # endif
65
66 static int kgdboc_port_line;
67 static int kgdboc_break_enabled;
68 #endif
69 /*
70  * Setup for console. Argument comes from the menuconfig
71  */
72 #define DMA_RX_XCOUNT           512
73 #define DMA_RX_YCOUNT           (PAGE_SIZE / DMA_RX_XCOUNT)
74
75 #define DMA_RX_FLUSH_JIFFIES    (HZ / 50)
76
77 #ifdef CONFIG_SERIAL_BFIN_DMA
78 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
79 #else
80 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
81 #endif
82
83 static void bfin_serial_reset_irda(struct uart_port *port);
84
85 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
86         defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
87 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
88 {
89         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
90         if (uart->cts_pin < 0)
91                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
92
93         /* CTS PIN is negative assertive. */
94         if (UART_GET_CTS(uart))
95                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
96         else
97                 return TIOCM_DSR | TIOCM_CAR;
98 }
99
100 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
101 {
102         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
103         if (uart->rts_pin < 0)
104                 return;
105
106         /* RTS PIN is negative assertive. */
107         if (mctrl & TIOCM_RTS)
108                 UART_ENABLE_RTS(uart);
109         else
110                 UART_DISABLE_RTS(uart);
111 }
112
113 /*
114  * Handle any change of modem status signal.
115  */
116 static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
117 {
118         struct bfin_serial_port *uart = dev_id;
119         unsigned int status;
120
121         status = bfin_serial_get_mctrl(&uart->port);
122         uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
123 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
124         uart->scts = 1;
125         UART_CLEAR_SCTS(uart);
126         UART_CLEAR_IER(uart, EDSSI);
127 #endif
128
129         return IRQ_HANDLED;
130 }
131 #else
132 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
133 {
134         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
135 }
136
137 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
138 {
139 }
140 #endif
141
142 /*
143  * interrupts are disabled on entry
144  */
145 static void bfin_serial_stop_tx(struct uart_port *port)
146 {
147         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
148 #ifdef CONFIG_SERIAL_BFIN_DMA
149         struct circ_buf *xmit = &uart->port.state->xmit;
150 #endif
151
152         while (!(UART_GET_LSR(uart) & TEMT))
153                 cpu_relax();
154
155 #ifdef CONFIG_SERIAL_BFIN_DMA
156         disable_dma(uart->tx_dma_channel);
157         xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
158         uart->port.icount.tx += uart->tx_count;
159         uart->tx_count = 0;
160         uart->tx_done = 1;
161 #else
162 #ifdef CONFIG_BF54x
163         /* Clear TFI bit */
164         UART_PUT_LSR(uart, TFI);
165 #endif
166         UART_CLEAR_IER(uart, ETBEI);
167 #endif
168 }
169
170 /*
171  * port is locked and interrupts are disabled
172  */
173 static void bfin_serial_start_tx(struct uart_port *port)
174 {
175         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
176         struct tty_struct *tty = uart->port.state->port.tty;
177
178 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
179         if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
180                 uart->scts = 0;
181                 uart_handle_cts_change(&uart->port, uart->scts);
182         }
183 #endif
184
185         /*
186          * To avoid losting RX interrupt, we reset IR function
187          * before sending data.
188          */
189         if (tty->termios->c_line == N_IRDA)
190                 bfin_serial_reset_irda(port);
191
192 #ifdef CONFIG_SERIAL_BFIN_DMA
193         if (uart->tx_done)
194                 bfin_serial_dma_tx_chars(uart);
195 #else
196         UART_SET_IER(uart, ETBEI);
197         bfin_serial_tx_chars(uart);
198 #endif
199 }
200
201 /*
202  * Interrupts are enabled
203  */
204 static void bfin_serial_stop_rx(struct uart_port *port)
205 {
206         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
207
208         UART_CLEAR_IER(uart, ERBFI);
209 }
210
211 /*
212  * Set the modem control timer to fire immediately.
213  */
214 static void bfin_serial_enable_ms(struct uart_port *port)
215 {
216 }
217
218
219 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
220 # define UART_GET_ANOMALY_THRESHOLD(uart)    ((uart)->anomaly_threshold)
221 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
222 #else
223 # define UART_GET_ANOMALY_THRESHOLD(uart)    0
224 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
225 #endif
226
227 #ifdef CONFIG_SERIAL_BFIN_PIO
228 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
229 {
230         struct tty_struct *tty = NULL;
231         unsigned int status, ch, flg;
232         static struct timeval anomaly_start = { .tv_sec = 0 };
233
234         status = UART_GET_LSR(uart);
235         UART_CLEAR_LSR(uart);
236
237         ch = UART_GET_CHAR(uart);
238         uart->port.icount.rx++;
239
240 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
241         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
242         if (kgdb_connected && kgdboc_port_line == uart->port.line
243                 && kgdboc_break_enabled)
244                 if (ch == 0x3) {/* Ctrl + C */
245                         kgdb_breakpoint();
246                         return;
247                 }
248
249         if (!uart->port.state || !uart->port.state->port.tty)
250                 return;
251 #endif
252         tty = uart->port.state->port.tty;
253
254         if (ANOMALY_05000363) {
255                 /* The BF533 (and BF561) family of processors have a nice anomaly
256                  * where they continuously generate characters for a "single" break.
257                  * We have to basically ignore this flood until the "next" valid
258                  * character comes across.  Due to the nature of the flood, it is
259                  * not possible to reliably catch bytes that are sent too quickly
260                  * after this break.  So application code talking to the Blackfin
261                  * which sends a break signal must allow at least 1.5 character
262                  * times after the end of the break for things to stabilize.  This
263                  * timeout was picked as it must absolutely be larger than 1
264                  * character time +/- some percent.  So 1.5 sounds good.  All other
265                  * Blackfin families operate properly.  Woo.
266                  */
267                 if (anomaly_start.tv_sec) {
268                         struct timeval curr;
269                         suseconds_t usecs;
270
271                         if ((~ch & (~ch + 1)) & 0xff)
272                                 goto known_good_char;
273
274                         do_gettimeofday(&curr);
275                         if (curr.tv_sec - anomaly_start.tv_sec > 1)
276                                 goto known_good_char;
277
278                         usecs = 0;
279                         if (curr.tv_sec != anomaly_start.tv_sec)
280                                 usecs += USEC_PER_SEC;
281                         usecs += curr.tv_usec - anomaly_start.tv_usec;
282
283                         if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
284                                 goto known_good_char;
285
286                         if (ch)
287                                 anomaly_start.tv_sec = 0;
288                         else
289                                 anomaly_start = curr;
290
291                         return;
292
293  known_good_char:
294                         status &= ~BI;
295                         anomaly_start.tv_sec = 0;
296                 }
297         }
298
299         if (status & BI) {
300                 if (ANOMALY_05000363)
301                         if (bfin_revid() < 5)
302                                 do_gettimeofday(&anomaly_start);
303                 uart->port.icount.brk++;
304                 if (uart_handle_break(&uart->port))
305                         goto ignore_char;
306                 status &= ~(PE | FE);
307         }
308         if (status & PE)
309                 uart->port.icount.parity++;
310         if (status & OE)
311                 uart->port.icount.overrun++;
312         if (status & FE)
313                 uart->port.icount.frame++;
314
315         status &= uart->port.read_status_mask;
316
317         if (status & BI)
318                 flg = TTY_BREAK;
319         else if (status & PE)
320                 flg = TTY_PARITY;
321         else if (status & FE)
322                 flg = TTY_FRAME;
323         else
324                 flg = TTY_NORMAL;
325
326         if (uart_handle_sysrq_char(&uart->port, ch))
327                 goto ignore_char;
328
329         uart_insert_char(&uart->port, status, OE, ch, flg);
330
331  ignore_char:
332         tty_flip_buffer_push(tty);
333 }
334
335 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
336 {
337         struct circ_buf *xmit = &uart->port.state->xmit;
338
339         if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
340 #ifdef CONFIG_BF54x
341                 /* Clear TFI bit */
342                 UART_PUT_LSR(uart, TFI);
343 #endif
344                 /* Anomaly notes:
345                  *  05000215 -  we always clear ETBEI within last UART TX
346                  *              interrupt to end a string. It is always set
347                  *              when start a new tx.
348                  */
349                 UART_CLEAR_IER(uart, ETBEI);
350                 return;
351         }
352
353         if (uart->port.x_char) {
354                 UART_PUT_CHAR(uart, uart->port.x_char);
355                 uart->port.icount.tx++;
356                 uart->port.x_char = 0;
357         }
358
359         while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
360                 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
361                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
362                 uart->port.icount.tx++;
363         }
364
365         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
366                 uart_write_wakeup(&uart->port);
367 }
368
369 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
370 {
371         struct bfin_serial_port *uart = dev_id;
372
373         while (UART_GET_LSR(uart) & DR)
374                 bfin_serial_rx_chars(uart);
375
376         return IRQ_HANDLED;
377 }
378
379 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
380 {
381         struct bfin_serial_port *uart = dev_id;
382
383 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
384         if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
385                 uart->scts = 0;
386                 uart_handle_cts_change(&uart->port, uart->scts);
387         }
388 #endif
389         spin_lock(&uart->port.lock);
390         if (UART_GET_LSR(uart) & THRE)
391                 bfin_serial_tx_chars(uart);
392         spin_unlock(&uart->port.lock);
393
394         return IRQ_HANDLED;
395 }
396 #endif
397
398 #ifdef CONFIG_SERIAL_BFIN_DMA
399 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
400 {
401         struct circ_buf *xmit = &uart->port.state->xmit;
402
403         uart->tx_done = 0;
404
405         if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
406                 uart->tx_count = 0;
407                 uart->tx_done = 1;
408                 return;
409         }
410
411         if (uart->port.x_char) {
412                 UART_PUT_CHAR(uart, uart->port.x_char);
413                 uart->port.icount.tx++;
414                 uart->port.x_char = 0;
415         }
416
417         uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
418         if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
419                 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
420         blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
421                                         (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
422         set_dma_config(uart->tx_dma_channel,
423                 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
424                         INTR_ON_BUF,
425                         DIMENSION_LINEAR,
426                         DATA_SIZE_8,
427                         DMA_SYNC_RESTART));
428         set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
429         set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
430         set_dma_x_modify(uart->tx_dma_channel, 1);
431         SSYNC();
432         enable_dma(uart->tx_dma_channel);
433
434         UART_SET_IER(uart, ETBEI);
435 }
436
437 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
438 {
439         struct tty_struct *tty = uart->port.state->port.tty;
440         int i, flg, status;
441
442         status = UART_GET_LSR(uart);
443         UART_CLEAR_LSR(uart);
444
445         uart->port.icount.rx +=
446                 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
447                 UART_XMIT_SIZE);
448
449         if (status & BI) {
450                 uart->port.icount.brk++;
451                 if (uart_handle_break(&uart->port))
452                         goto dma_ignore_char;
453                 status &= ~(PE | FE);
454         }
455         if (status & PE)
456                 uart->port.icount.parity++;
457         if (status & OE)
458                 uart->port.icount.overrun++;
459         if (status & FE)
460                 uart->port.icount.frame++;
461
462         status &= uart->port.read_status_mask;
463
464         if (status & BI)
465                 flg = TTY_BREAK;
466         else if (status & PE)
467                 flg = TTY_PARITY;
468         else if (status & FE)
469                 flg = TTY_FRAME;
470         else
471                 flg = TTY_NORMAL;
472
473         for (i = uart->rx_dma_buf.tail; ; i++) {
474                 if (i >= UART_XMIT_SIZE)
475                         i = 0;
476                 if (i == uart->rx_dma_buf.head)
477                         break;
478                 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
479                         uart_insert_char(&uart->port, status, OE,
480                                 uart->rx_dma_buf.buf[i], flg);
481         }
482
483  dma_ignore_char:
484         tty_flip_buffer_push(tty);
485 }
486
487 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
488 {
489         int x_pos, pos;
490
491         dma_disable_irq_nosync(uart->rx_dma_channel);
492         spin_lock_bh(&uart->rx_lock);
493
494         /* 2D DMA RX buffer ring is used. Because curr_y_count and
495          * curr_x_count can't be read as an atomic operation,
496          * curr_y_count should be read before curr_x_count. When
497          * curr_x_count is read, curr_y_count may already indicate
498          * next buffer line. But, the position calculated here is
499          * still indicate the old line. The wrong position data may
500          * be smaller than current buffer tail, which cause garbages
501          * are received if it is not prohibit.
502          */
503         uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
504         x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
505         uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
506         if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
507                 uart->rx_dma_nrows = 0;
508         x_pos = DMA_RX_XCOUNT - x_pos;
509         if (x_pos == DMA_RX_XCOUNT)
510                 x_pos = 0;
511
512         pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
513         /* Ignore receiving data if new position is in the same line of
514          * current buffer tail and small.
515          */
516         if (pos > uart->rx_dma_buf.tail ||
517                 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
518                 uart->rx_dma_buf.head = pos;
519                 bfin_serial_dma_rx_chars(uart);
520                 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
521         }
522
523         spin_unlock_bh(&uart->rx_lock);
524         dma_enable_irq(uart->rx_dma_channel);
525
526         mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
527 }
528
529 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
530 {
531         struct bfin_serial_port *uart = dev_id;
532         struct circ_buf *xmit = &uart->port.state->xmit;
533
534 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
535         if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
536                 uart->scts = 0;
537                 uart_handle_cts_change(&uart->port, uart->scts);
538         }
539 #endif
540
541         spin_lock(&uart->port.lock);
542         if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
543                 disable_dma(uart->tx_dma_channel);
544                 clear_dma_irqstat(uart->tx_dma_channel);
545                 /* Anomaly notes:
546                  *  05000215 -  we always clear ETBEI within last UART TX
547                  *              interrupt to end a string. It is always set
548                  *              when start a new tx.
549                  */
550                 UART_CLEAR_IER(uart, ETBEI);
551                 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
552                 uart->port.icount.tx += uart->tx_count;
553
554                 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
555                         uart_write_wakeup(&uart->port);
556
557                 bfin_serial_dma_tx_chars(uart);
558         }
559
560         spin_unlock(&uart->port.lock);
561         return IRQ_HANDLED;
562 }
563
564 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
565 {
566         struct bfin_serial_port *uart = dev_id;
567         unsigned short irqstat;
568         int x_pos, pos;
569
570         spin_lock(&uart->rx_lock);
571         irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
572         clear_dma_irqstat(uart->rx_dma_channel);
573
574         uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
575         x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
576         uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
577         if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
578                 uart->rx_dma_nrows = 0;
579
580         pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
581         if (pos > uart->rx_dma_buf.tail ||
582                 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
583                 uart->rx_dma_buf.head = pos;
584                 bfin_serial_dma_rx_chars(uart);
585                 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
586         }
587
588         spin_unlock(&uart->rx_lock);
589
590         return IRQ_HANDLED;
591 }
592 #endif
593
594 /*
595  * Return TIOCSER_TEMT when transmitter is not busy.
596  */
597 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
598 {
599         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
600         unsigned short lsr;
601
602         lsr = UART_GET_LSR(uart);
603         if (lsr & TEMT)
604                 return TIOCSER_TEMT;
605         else
606                 return 0;
607 }
608
609 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
610 {
611         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
612         u16 lcr = UART_GET_LCR(uart);
613         if (break_state)
614                 lcr |= SB;
615         else
616                 lcr &= ~SB;
617         UART_PUT_LCR(uart, lcr);
618         SSYNC();
619 }
620
621 static int bfin_serial_startup(struct uart_port *port)
622 {
623         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
624
625 #ifdef CONFIG_SERIAL_BFIN_DMA
626         dma_addr_t dma_handle;
627
628         if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
629                 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
630                 return -EBUSY;
631         }
632
633         if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
634                 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
635                 free_dma(uart->rx_dma_channel);
636                 return -EBUSY;
637         }
638
639         set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
640         set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
641
642         uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
643         uart->rx_dma_buf.head = 0;
644         uart->rx_dma_buf.tail = 0;
645         uart->rx_dma_nrows = 0;
646
647         set_dma_config(uart->rx_dma_channel,
648                 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
649                                 INTR_ON_ROW, DIMENSION_2D,
650                                 DATA_SIZE_8,
651                                 DMA_SYNC_RESTART));
652         set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
653         set_dma_x_modify(uart->rx_dma_channel, 1);
654         set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
655         set_dma_y_modify(uart->rx_dma_channel, 1);
656         set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
657         enable_dma(uart->rx_dma_channel);
658
659         uart->rx_dma_timer.data = (unsigned long)(uart);
660         uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
661         uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
662         add_timer(&(uart->rx_dma_timer));
663 #else
664 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
665         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
666         if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
667                 kgdboc_break_enabled = 0;
668         else {
669 # endif
670         if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
671              "BFIN_UART_RX", uart)) {
672                 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
673                 return -EBUSY;
674         }
675
676         if (request_irq
677             (uart->tx_irq, bfin_serial_tx_int, 0,
678              "BFIN_UART_TX", uart)) {
679                 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
680                 free_irq(uart->rx_irq, uart);
681                 return -EBUSY;
682         }
683
684 # ifdef CONFIG_BF54x
685         {
686                 /*
687                  * UART2 and UART3 on BF548 share interrupt PINs and DMA
688                  * controllers with SPORT2 and SPORT3. UART rx and tx
689                  * interrupts are generated in PIO mode only when configure
690                  * their peripheral mapping registers properly, which means
691                  * request corresponding DMA channels in PIO mode as well.
692                  */
693                 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
694
695                 switch (uart->rx_irq) {
696                 case IRQ_UART3_RX:
697                         uart_dma_ch_rx = CH_UART3_RX;
698                         uart_dma_ch_tx = CH_UART3_TX;
699                         break;
700                 case IRQ_UART2_RX:
701                         uart_dma_ch_rx = CH_UART2_RX;
702                         uart_dma_ch_tx = CH_UART2_TX;
703                         break;
704                 default:
705                         uart_dma_ch_rx = uart_dma_ch_tx = 0;
706                         break;
707                 };
708
709                 if (uart_dma_ch_rx &&
710                         request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
711                         printk(KERN_NOTICE"Fail to attach UART interrupt\n");
712                         free_irq(uart->rx_irq, uart);
713                         free_irq(uart->tx_irq, uart);
714                         return -EBUSY;
715                 }
716                 if (uart_dma_ch_tx &&
717                         request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
718                         printk(KERN_NOTICE "Fail to attach UART interrupt\n");
719                         free_dma(uart_dma_ch_rx);
720                         free_irq(uart->rx_irq, uart);
721                         free_irq(uart->tx_irq, uart);
722                         return -EBUSY;
723                 }
724         }
725 # endif
726 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
727         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
728         }
729 # endif
730 #endif
731
732 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
733         if (uart->cts_pin >= 0) {
734                 if (request_irq(gpio_to_irq(uart->cts_pin),
735                         bfin_serial_mctrl_cts_int,
736                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
737                         0, "BFIN_UART_CTS", uart)) {
738                         uart->cts_pin = -1;
739                         pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
740                 }
741         }
742         if (uart->rts_pin >= 0) {
743                 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
744                         pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
745                         uart->rts_pin = -1;
746                 } else
747                         gpio_direction_output(uart->rts_pin, 0);
748         }
749 #endif
750 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
751         if (uart->cts_pin >= 0 && request_irq(uart->status_irq,
752                 bfin_serial_mctrl_cts_int,
753                 0, "BFIN_UART_MODEM_STATUS", uart)) {
754                 uart->cts_pin = -1;
755                 pr_info("Unable to attach BlackFin UART Modem Status interrupt.\n");
756         }
757
758         /* CTS RTS PINs are negative assertive. */
759         UART_PUT_MCR(uart, ACTS);
760         UART_SET_IER(uart, EDSSI);
761 #endif
762
763         UART_SET_IER(uart, ERBFI);
764         return 0;
765 }
766
767 static void bfin_serial_shutdown(struct uart_port *port)
768 {
769         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
770
771 #ifdef CONFIG_SERIAL_BFIN_DMA
772         disable_dma(uart->tx_dma_channel);
773         free_dma(uart->tx_dma_channel);
774         disable_dma(uart->rx_dma_channel);
775         free_dma(uart->rx_dma_channel);
776         del_timer(&(uart->rx_dma_timer));
777         dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
778 #else
779 #ifdef CONFIG_BF54x
780         switch (uart->port.irq) {
781         case IRQ_UART3_RX:
782                 free_dma(CH_UART3_RX);
783                 free_dma(CH_UART3_TX);
784                 break;
785         case IRQ_UART2_RX:
786                 free_dma(CH_UART2_RX);
787                 free_dma(CH_UART2_TX);
788                 break;
789         default:
790                 break;
791         };
792 #endif
793         free_irq(uart->rx_irq, uart);
794         free_irq(uart->tx_irq, uart);
795 #endif
796
797 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
798         if (uart->cts_pin >= 0)
799                 free_irq(gpio_to_irq(uart->cts_pin), uart);
800         if (uart->rts_pin >= 0)
801                 gpio_free(uart->rts_pin);
802 #endif
803 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
804         if (uart->cts_pin >= 0)
805                 free_irq(uart->status_irq, uart);
806 #endif
807 }
808
809 static void
810 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
811                    struct ktermios *old)
812 {
813         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
814         unsigned long flags;
815         unsigned int baud, quot;
816         unsigned short val, ier, lcr = 0;
817
818         switch (termios->c_cflag & CSIZE) {
819         case CS8:
820                 lcr = WLS(8);
821                 break;
822         case CS7:
823                 lcr = WLS(7);
824                 break;
825         case CS6:
826                 lcr = WLS(6);
827                 break;
828         case CS5:
829                 lcr = WLS(5);
830                 break;
831         default:
832                 printk(KERN_ERR "%s: word lengh not supported\n",
833                         __func__);
834         }
835
836         /* Anomaly notes:
837          *  05000231 -  STOP bit is always set to 1 whatever the user is set.
838          */
839         if (termios->c_cflag & CSTOPB) {
840                 if (ANOMALY_05000231)
841                         printk(KERN_WARNING "STOP bits other than 1 is not "
842                                 "supported in case of anomaly 05000231.\n");
843                 else
844                         lcr |= STB;
845         }
846         if (termios->c_cflag & PARENB)
847                 lcr |= PEN;
848         if (!(termios->c_cflag & PARODD))
849                 lcr |= EPS;
850         if (termios->c_cflag & CMSPAR)
851                 lcr |= STP;
852
853         spin_lock_irqsave(&uart->port.lock, flags);
854
855         port->read_status_mask = OE;
856         if (termios->c_iflag & INPCK)
857                 port->read_status_mask |= (FE | PE);
858         if (termios->c_iflag & (BRKINT | PARMRK))
859                 port->read_status_mask |= BI;
860
861         /*
862          * Characters to ignore
863          */
864         port->ignore_status_mask = 0;
865         if (termios->c_iflag & IGNPAR)
866                 port->ignore_status_mask |= FE | PE;
867         if (termios->c_iflag & IGNBRK) {
868                 port->ignore_status_mask |= BI;
869                 /*
870                  * If we're ignoring parity and break indicators,
871                  * ignore overruns too (for real raw support).
872                  */
873                 if (termios->c_iflag & IGNPAR)
874                         port->ignore_status_mask |= OE;
875         }
876
877         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
878         quot = uart_get_divisor(port, baud);
879
880         /* If discipline is not IRDA, apply ANOMALY_05000230 */
881         if (termios->c_line != N_IRDA)
882                 quot -= ANOMALY_05000230;
883
884         UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
885
886         /* Disable UART */
887         ier = UART_GET_IER(uart);
888         UART_DISABLE_INTS(uart);
889
890         /* Set DLAB in LCR to Access DLL and DLH */
891         UART_SET_DLAB(uart);
892
893         UART_PUT_DLL(uart, quot & 0xFF);
894         UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
895         SSYNC();
896
897         /* Clear DLAB in LCR to Access THR RBR IER */
898         UART_CLEAR_DLAB(uart);
899
900         UART_PUT_LCR(uart, lcr);
901
902         /* Enable UART */
903         UART_ENABLE_INTS(uart, ier);
904
905         val = UART_GET_GCTL(uart);
906         val |= UCEN;
907         UART_PUT_GCTL(uart, val);
908
909         /* Port speed changed, update the per-port timeout. */
910         uart_update_timeout(port, termios->c_cflag, baud);
911
912         spin_unlock_irqrestore(&uart->port.lock, flags);
913 }
914
915 static const char *bfin_serial_type(struct uart_port *port)
916 {
917         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
918
919         return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
920 }
921
922 /*
923  * Release the memory region(s) being used by 'port'.
924  */
925 static void bfin_serial_release_port(struct uart_port *port)
926 {
927 }
928
929 /*
930  * Request the memory region(s) being used by 'port'.
931  */
932 static int bfin_serial_request_port(struct uart_port *port)
933 {
934         return 0;
935 }
936
937 /*
938  * Configure/autoconfigure the port.
939  */
940 static void bfin_serial_config_port(struct uart_port *port, int flags)
941 {
942         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
943
944         if (flags & UART_CONFIG_TYPE &&
945             bfin_serial_request_port(&uart->port) == 0)
946                 uart->port.type = PORT_BFIN;
947 }
948
949 /*
950  * Verify the new serial_struct (for TIOCSSERIAL).
951  * The only change we allow are to the flags and type, and
952  * even then only between PORT_BFIN and PORT_UNKNOWN
953  */
954 static int
955 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
956 {
957         return 0;
958 }
959
960 /*
961  * Enable the IrDA function if tty->ldisc.num is N_IRDA.
962  * In other cases, disable IrDA function.
963  */
964 static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
965 {
966         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
967         unsigned short val;
968
969         switch (ld) {
970         case N_IRDA:
971                 val = UART_GET_GCTL(uart);
972                 val |= (IREN | RPOLC);
973                 UART_PUT_GCTL(uart, val);
974                 break;
975         default:
976                 val = UART_GET_GCTL(uart);
977                 val &= ~(IREN | RPOLC);
978                 UART_PUT_GCTL(uart, val);
979         }
980 }
981
982 static void bfin_serial_reset_irda(struct uart_port *port)
983 {
984         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
985         unsigned short val;
986
987         val = UART_GET_GCTL(uart);
988         val &= ~(IREN | RPOLC);
989         UART_PUT_GCTL(uart, val);
990         SSYNC();
991         val |= (IREN | RPOLC);
992         UART_PUT_GCTL(uart, val);
993         SSYNC();
994 }
995
996 #ifdef CONFIG_CONSOLE_POLL
997 /* Anomaly notes:
998  *  05000099 -  Because we only use THRE in poll_put and DR in poll_get,
999  *              losing other bits of UART_LSR is not a problem here.
1000  */
1001 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
1002 {
1003         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1004
1005         while (!(UART_GET_LSR(uart) & THRE))
1006                 cpu_relax();
1007
1008         UART_CLEAR_DLAB(uart);
1009         UART_PUT_CHAR(uart, (unsigned char)chr);
1010 }
1011
1012 static int bfin_serial_poll_get_char(struct uart_port *port)
1013 {
1014         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1015         unsigned char chr;
1016
1017         while (!(UART_GET_LSR(uart) & DR))
1018                 cpu_relax();
1019
1020         UART_CLEAR_DLAB(uart);
1021         chr = UART_GET_CHAR(uart);
1022
1023         return chr;
1024 }
1025 #endif
1026
1027 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1028         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1029 static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1030 {
1031         if (kgdboc_break_enabled) {
1032                 kgdboc_break_enabled = 0;
1033                 bfin_serial_shutdown(port);
1034         }
1035 }
1036
1037 static int bfin_kgdboc_port_startup(struct uart_port *port)
1038 {
1039         kgdboc_port_line = port->line;
1040         kgdboc_break_enabled = !bfin_serial_startup(port);
1041         return 0;
1042 }
1043 #endif
1044
1045 static struct uart_ops bfin_serial_pops = {
1046         .tx_empty       = bfin_serial_tx_empty,
1047         .set_mctrl      = bfin_serial_set_mctrl,
1048         .get_mctrl      = bfin_serial_get_mctrl,
1049         .stop_tx        = bfin_serial_stop_tx,
1050         .start_tx       = bfin_serial_start_tx,
1051         .stop_rx        = bfin_serial_stop_rx,
1052         .enable_ms      = bfin_serial_enable_ms,
1053         .break_ctl      = bfin_serial_break_ctl,
1054         .startup        = bfin_serial_startup,
1055         .shutdown       = bfin_serial_shutdown,
1056         .set_termios    = bfin_serial_set_termios,
1057         .set_ldisc      = bfin_serial_set_ldisc,
1058         .type           = bfin_serial_type,
1059         .release_port   = bfin_serial_release_port,
1060         .request_port   = bfin_serial_request_port,
1061         .config_port    = bfin_serial_config_port,
1062         .verify_port    = bfin_serial_verify_port,
1063 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1064         defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1065         .kgdboc_port_startup    = bfin_kgdboc_port_startup,
1066         .kgdboc_port_shutdown   = bfin_kgdboc_port_shutdown,
1067 #endif
1068 #ifdef CONFIG_CONSOLE_POLL
1069         .poll_put_char  = bfin_serial_poll_put_char,
1070         .poll_get_char  = bfin_serial_poll_get_char,
1071 #endif
1072 };
1073
1074 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1075 /*
1076  * If the port was already initialised (eg, by a boot loader),
1077  * try to determine the current setup.
1078  */
1079 static void __init
1080 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1081                            int *parity, int *bits)
1082 {
1083         unsigned short status;
1084
1085         status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1086         if (status == (ERBFI | ETBEI)) {
1087                 /* ok, the port was enabled */
1088                 u16 lcr, dlh, dll;
1089
1090                 lcr = UART_GET_LCR(uart);
1091
1092                 *parity = 'n';
1093                 if (lcr & PEN) {
1094                         if (lcr & EPS)
1095                                 *parity = 'e';
1096                         else
1097                                 *parity = 'o';
1098                 }
1099                 switch (lcr & 0x03) {
1100                 case 0:
1101                         *bits = 5;
1102                         break;
1103                 case 1:
1104                         *bits = 6;
1105                         break;
1106                 case 2:
1107                         *bits = 7;
1108                         break;
1109                 case 3:
1110                         *bits = 8;
1111                         break;
1112                 }
1113                 /* Set DLAB in LCR to Access DLL and DLH */
1114                 UART_SET_DLAB(uart);
1115
1116                 dll = UART_GET_DLL(uart);
1117                 dlh = UART_GET_DLH(uart);
1118
1119                 /* Clear DLAB in LCR to Access THR RBR IER */
1120                 UART_CLEAR_DLAB(uart);
1121
1122                 *baud = get_sclk() / (16*(dll | dlh << 8));
1123         }
1124         pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1125 }
1126
1127 static struct uart_driver bfin_serial_reg;
1128
1129 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1130 {
1131         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1132         while (!(UART_GET_LSR(uart) & THRE))
1133                 barrier();
1134         UART_PUT_CHAR(uart, ch);
1135 }
1136
1137 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1138                  defined (CONFIG_EARLY_PRINTK) */
1139
1140 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1141 #define CLASS_BFIN_CONSOLE      "bfin-console"
1142 /*
1143  * Interrupts are disabled on entering
1144  */
1145 static void
1146 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1147 {
1148         struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1149         unsigned long flags;
1150
1151         spin_lock_irqsave(&uart->port.lock, flags);
1152         uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1153         spin_unlock_irqrestore(&uart->port.lock, flags);
1154
1155 }
1156
1157 static int __init
1158 bfin_serial_console_setup(struct console *co, char *options)
1159 {
1160         struct bfin_serial_port *uart;
1161         int baud = 57600;
1162         int bits = 8;
1163         int parity = 'n';
1164 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1165         defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1166         int flow = 'r';
1167 # else
1168         int flow = 'n';
1169 # endif
1170
1171         /*
1172          * Check whether an invalid uart number has been specified, and
1173          * if so, search for the first available port that does have
1174          * console support.
1175          */
1176         if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1177                 return -ENODEV;
1178
1179         uart = bfin_serial_ports[co->index];
1180         if (!uart)
1181                 return -ENODEV;
1182
1183         if (options)
1184                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1185         else
1186                 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1187
1188         return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1189 }
1190
1191 static struct console bfin_serial_console = {
1192         .name           = BFIN_SERIAL_DEV_NAME,
1193         .write          = bfin_serial_console_write,
1194         .device         = uart_console_device,
1195         .setup          = bfin_serial_console_setup,
1196         .flags          = CON_PRINTBUFFER,
1197         .index          = -1,
1198         .data           = &bfin_serial_reg,
1199 };
1200 #define BFIN_SERIAL_CONSOLE     (&bfin_serial_console)
1201 #else
1202 #define BFIN_SERIAL_CONSOLE     NULL
1203 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1204
1205 #ifdef  CONFIG_EARLY_PRINTK
1206 static struct bfin_serial_port bfin_earlyprintk_port;
1207 #define CLASS_BFIN_EARLYPRINTK  "bfin-earlyprintk"
1208
1209 /*
1210  * Interrupts are disabled on entering
1211  */
1212 static void
1213 bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
1214 {
1215         unsigned long flags;
1216
1217         if (bfin_earlyprintk_port.port.line != co->index)
1218                 return;
1219
1220         spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1221         uart_console_write(&bfin_earlyprintk_port.port, s, count,
1222                 bfin_serial_console_putchar);
1223         spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1224 }
1225
1226 /*
1227  * This should have a .setup or .early_setup in it, but then things get called
1228  * without the command line options, and the baud rate gets messed up - so
1229  * don't let the common infrastructure play with things. (see calls to setup
1230  * & earlysetup in ./kernel/printk.c:register_console()
1231  */
1232 static struct __initdata console bfin_early_serial_console = {
1233         .name = "early_BFuart",
1234         .write = bfin_earlyprintk_console_write,
1235         .device = uart_console_device,
1236         .flags = CON_PRINTBUFFER,
1237         .index = -1,
1238         .data  = &bfin_serial_reg,
1239 };
1240 #endif
1241
1242 static struct uart_driver bfin_serial_reg = {
1243         .owner                  = THIS_MODULE,
1244         .driver_name            = DRIVER_NAME,
1245         .dev_name               = BFIN_SERIAL_DEV_NAME,
1246         .major                  = BFIN_SERIAL_MAJOR,
1247         .minor                  = BFIN_SERIAL_MINOR,
1248         .nr                     = BFIN_UART_NR_PORTS,
1249         .cons                   = BFIN_SERIAL_CONSOLE,
1250 };
1251
1252 static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1253 {
1254         struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1255
1256         return uart_suspend_port(&bfin_serial_reg, &uart->port);
1257 }
1258
1259 static int bfin_serial_resume(struct platform_device *pdev)
1260 {
1261         struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1262
1263         return uart_resume_port(&bfin_serial_reg, &uart->port);
1264 }
1265
1266 static int bfin_serial_probe(struct platform_device *pdev)
1267 {
1268         struct resource *res;
1269         struct bfin_serial_port *uart = NULL;
1270         int ret = 0;
1271
1272         if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1273                 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1274                 return -ENOENT;
1275         }
1276
1277         if (bfin_serial_ports[pdev->id] == NULL) {
1278
1279                 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1280                 if (!uart) {
1281                         dev_err(&pdev->dev,
1282                                 "fail to malloc bfin_serial_port\n");
1283                         return -ENOMEM;
1284                 }
1285                 bfin_serial_ports[pdev->id] = uart;
1286
1287 #ifdef CONFIG_EARLY_PRINTK
1288                 if (!(bfin_earlyprintk_port.port.membase
1289                         && bfin_earlyprintk_port.port.line == pdev->id)) {
1290                         /*
1291                          * If the peripheral PINs of current port is allocated
1292                          * in earlyprintk probe stage, don't do it again.
1293                          */
1294 #endif
1295                 ret = peripheral_request_list(
1296                         (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1297                 if (ret) {
1298                         dev_err(&pdev->dev,
1299                                 "fail to request bfin serial peripherals\n");
1300                         goto out_error_free_mem;
1301                 }
1302 #ifdef CONFIG_EARLY_PRINTK
1303                 }
1304 #endif
1305
1306                 spin_lock_init(&uart->port.lock);
1307                 uart->port.uartclk   = get_sclk();
1308                 uart->port.fifosize  = BFIN_UART_TX_FIFO_SIZE;
1309                 uart->port.ops       = &bfin_serial_pops;
1310                 uart->port.line      = pdev->id;
1311                 uart->port.iotype    = UPIO_MEM;
1312                 uart->port.flags     = UPF_BOOT_AUTOCONF;
1313
1314                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1315                 if (res == NULL) {
1316                         dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1317                         ret = -ENOENT;
1318                         goto out_error_free_peripherals;
1319                 }
1320
1321                 uart->port.membase = ioremap(res->start, resource_size(res));
1322                 if (!uart->port.membase) {
1323                         dev_err(&pdev->dev, "Cannot map uart IO\n");
1324                         ret = -ENXIO;
1325                         goto out_error_free_peripherals;
1326                 }
1327                 uart->port.mapbase = res->start;
1328
1329                 uart->tx_irq = platform_get_irq(pdev, 0);
1330                 if (uart->tx_irq < 0) {
1331                         dev_err(&pdev->dev, "No uart TX IRQ specified\n");
1332                         ret = -ENOENT;
1333                         goto out_error_unmap;
1334                 }
1335
1336                 uart->rx_irq = platform_get_irq(pdev, 1);
1337                 if (uart->rx_irq < 0) {
1338                         dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1339                         ret = -ENOENT;
1340                         goto out_error_unmap;
1341                 }
1342                 uart->port.irq = uart->rx_irq;
1343
1344                 uart->status_irq = platform_get_irq(pdev, 2);
1345                 if (uart->status_irq < 0) {
1346                         dev_err(&pdev->dev, "No uart status IRQ specified\n");
1347                         ret = -ENOENT;
1348                         goto out_error_unmap;
1349                 }
1350
1351 #ifdef CONFIG_SERIAL_BFIN_DMA
1352                 spin_lock_init(&uart->rx_lock);
1353                 uart->tx_done       = 1;
1354                 uart->tx_count      = 0;
1355
1356                 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1357                 if (res == NULL) {
1358                         dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1359                         ret = -ENOENT;
1360                         goto out_error_unmap;
1361                 }
1362                 uart->tx_dma_channel = res->start;
1363
1364                 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1365                 if (res == NULL) {
1366                         dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1367                         ret = -ENOENT;
1368                         goto out_error_unmap;
1369                 }
1370                 uart->rx_dma_channel = res->start;
1371
1372                 init_timer(&(uart->rx_dma_timer));
1373 #endif
1374
1375 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1376         defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1377                 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1378                 if (res == NULL)
1379                         uart->cts_pin = -1;
1380                 else {
1381                         uart->cts_pin = res->start;
1382                         uart->port.flags |= ASYNC_CTS_FLOW;
1383                 }
1384
1385                 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1386                 if (res == NULL)
1387                         uart->rts_pin = -1;
1388                 else
1389                         uart->rts_pin = res->start;
1390 #endif
1391         }
1392
1393 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1394         if (!is_early_platform_device(pdev)) {
1395 #endif
1396                 uart = bfin_serial_ports[pdev->id];
1397                 uart->port.dev = &pdev->dev;
1398                 dev_set_drvdata(&pdev->dev, uart);
1399                 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1400 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1401         }
1402 #endif
1403
1404         if (!ret)
1405                 return 0;
1406
1407         if (uart) {
1408 out_error_unmap:
1409                 iounmap(uart->port.membase);
1410 out_error_free_peripherals:
1411                 peripheral_free_list(
1412                         (unsigned short *)pdev->dev.platform_data);
1413 out_error_free_mem:
1414                 kfree(uart);
1415                 bfin_serial_ports[pdev->id] = NULL;
1416         }
1417
1418         return ret;
1419 }
1420
1421 static int __devexit bfin_serial_remove(struct platform_device *pdev)
1422 {
1423         struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1424
1425         dev_set_drvdata(&pdev->dev, NULL);
1426
1427         if (uart) {
1428                 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1429                 iounmap(uart->port.membase);
1430                 peripheral_free_list(
1431                         (unsigned short *)pdev->dev.platform_data);
1432                 kfree(uart);
1433                 bfin_serial_ports[pdev->id] = NULL;
1434         }
1435
1436         return 0;
1437 }
1438
1439 static struct platform_driver bfin_serial_driver = {
1440         .probe          = bfin_serial_probe,
1441         .remove         = __devexit_p(bfin_serial_remove),
1442         .suspend        = bfin_serial_suspend,
1443         .resume         = bfin_serial_resume,
1444         .driver         = {
1445                 .name   = DRIVER_NAME,
1446                 .owner  = THIS_MODULE,
1447         },
1448 };
1449
1450 #if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1451 static __initdata struct early_platform_driver early_bfin_serial_driver = {
1452         .class_str = CLASS_BFIN_CONSOLE,
1453         .pdrv = &bfin_serial_driver,
1454         .requested_id = EARLY_PLATFORM_ID_UNSET,
1455 };
1456
1457 static int __init bfin_serial_rs_console_init(void)
1458 {
1459         early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1460
1461         early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1462
1463         register_console(&bfin_serial_console);
1464
1465         return 0;
1466 }
1467 console_initcall(bfin_serial_rs_console_init);
1468 #endif
1469
1470 #ifdef CONFIG_EARLY_PRINTK
1471 /*
1472  * Memory can't be allocated dynamically during earlyprink init stage.
1473  * So, do individual probe for earlyprink with a static uart port variable.
1474  */
1475 static int bfin_earlyprintk_probe(struct platform_device *pdev)
1476 {
1477         struct resource *res;
1478         int ret;
1479
1480         if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1481                 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1482                 return -ENOENT;
1483         }
1484
1485         ret = peripheral_request_list(
1486                 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1487         if (ret) {
1488                 dev_err(&pdev->dev,
1489                                 "fail to request bfin serial peripherals\n");
1490                         return ret;
1491         }
1492
1493         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1494         if (res == NULL) {
1495                 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1496                 ret = -ENOENT;
1497                 goto out_error_free_peripherals;
1498         }
1499
1500         bfin_earlyprintk_port.port.membase = ioremap(res->start,
1501                                                      resource_size(res));
1502         if (!bfin_earlyprintk_port.port.membase) {
1503                 dev_err(&pdev->dev, "Cannot map uart IO\n");
1504                 ret = -ENXIO;
1505                 goto out_error_free_peripherals;
1506         }
1507         bfin_earlyprintk_port.port.mapbase = res->start;
1508         bfin_earlyprintk_port.port.line = pdev->id;
1509         bfin_earlyprintk_port.port.uartclk = get_sclk();
1510         bfin_earlyprintk_port.port.fifosize  = BFIN_UART_TX_FIFO_SIZE;
1511         spin_lock_init(&bfin_earlyprintk_port.port.lock);
1512
1513         return 0;
1514
1515 out_error_free_peripherals:
1516         peripheral_free_list(
1517                 (unsigned short *)pdev->dev.platform_data);
1518
1519         return ret;
1520 }
1521
1522 static struct platform_driver bfin_earlyprintk_driver = {
1523         .probe          = bfin_earlyprintk_probe,
1524         .driver         = {
1525                 .name   = DRIVER_NAME,
1526                 .owner  = THIS_MODULE,
1527         },
1528 };
1529
1530 static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1531         .class_str = CLASS_BFIN_EARLYPRINTK,
1532         .pdrv = &bfin_earlyprintk_driver,
1533         .requested_id = EARLY_PLATFORM_ID_UNSET,
1534 };
1535
1536 struct console __init *bfin_earlyserial_init(unsigned int port,
1537                                                 unsigned int cflag)
1538 {
1539         struct ktermios t;
1540         char port_name[20];
1541
1542         if (port < 0 || port >= BFIN_UART_NR_PORTS)
1543                 return NULL;
1544
1545         /*
1546          * Only probe resource of the given port in earlyprintk boot arg.
1547          * The expected port id should be indicated in port name string.
1548          */
1549         snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1550         early_platform_driver_register(&early_bfin_earlyprintk_driver,
1551                 port_name);
1552         early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1553
1554         if (!bfin_earlyprintk_port.port.membase)
1555                 return NULL;
1556
1557 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1558         /*
1559          * If we are using early serial, don't let the normal console rewind
1560          * log buffer, since that causes things to be printed multiple times
1561          */
1562         bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1563 #endif
1564
1565         bfin_early_serial_console.index = port;
1566         t.c_cflag = cflag;
1567         t.c_iflag = 0;
1568         t.c_oflag = 0;
1569         t.c_lflag = ICANON;
1570         t.c_line = port;
1571         bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1572
1573         return &bfin_early_serial_console;
1574 }
1575 #endif /* CONFIG_EARLY_PRINTK */
1576
1577 static int __init bfin_serial_init(void)
1578 {
1579         int ret;
1580
1581         pr_info("Blackfin serial driver\n");
1582
1583         ret = uart_register_driver(&bfin_serial_reg);
1584         if (ret) {
1585                 pr_err("failed to register %s:%d\n",
1586                         bfin_serial_reg.driver_name, ret);
1587         }
1588
1589         ret = platform_driver_register(&bfin_serial_driver);
1590         if (ret) {
1591                 pr_err("fail to register bfin uart\n");
1592                 uart_unregister_driver(&bfin_serial_reg);
1593         }
1594
1595         return ret;
1596 }
1597
1598 static void __exit bfin_serial_exit(void)
1599 {
1600         platform_driver_unregister(&bfin_serial_driver);
1601         uart_unregister_driver(&bfin_serial_reg);
1602 }
1603
1604
1605 module_init(bfin_serial_init);
1606 module_exit(bfin_serial_exit);
1607
1608 MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
1609 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1610 MODULE_LICENSE("GPL");
1611 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1612 MODULE_ALIAS("platform:bfin-uart");