[Blackfin] serial driver: Clean up UART DMA code.
[safe/jmp/linux-2.6] / drivers / serial / bfin_5xx.c
1 /*
2  * File:         drivers/serial/bfin_5xx.c
3  * Based on:     Based on drivers/serial/sa1100.c
4  * Author:       Aubrey Li <aubrey.li@analog.com>
5  *
6  * Created:
7  * Description:  Driver for blackfin 5xx serial ports
8  *
9  * Modified:
10  *               Copyright 2006 Analog Devices Inc.
11  *
12  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, see the file COPYING, or write
26  * to the Free Software Foundation, Inc.,
27  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28  */
29
30 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31 #define SUPPORT_SYSRQ
32 #endif
33
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/console.h>
38 #include <linux/sysrq.h>
39 #include <linux/platform_device.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/serial_core.h>
43
44 #ifdef CONFIG_KGDB_UART
45 #include <linux/kgdb.h>
46 #include <asm/irq_regs.h>
47 #endif
48
49 #include <asm/gpio.h>
50 #include <asm/mach/bfin_serial_5xx.h>
51
52 #ifdef CONFIG_SERIAL_BFIN_DMA
53 #include <linux/dma-mapping.h>
54 #include <asm/io.h>
55 #include <asm/irq.h>
56 #include <asm/cacheflush.h>
57 #endif
58
59 /* UART name and device definitions */
60 #define BFIN_SERIAL_NAME        "ttyBF"
61 #define BFIN_SERIAL_MAJOR       204
62 #define BFIN_SERIAL_MINOR       64
63
64 /*
65  * Setup for console. Argument comes from the menuconfig
66  */
67 #define DMA_RX_XCOUNT           512
68 #define DMA_RX_YCOUNT           (PAGE_SIZE / DMA_RX_XCOUNT)
69
70 #define DMA_RX_FLUSH_JIFFIES    5
71
72 #ifdef CONFIG_SERIAL_BFIN_DMA
73 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
74 #else
75 static void bfin_serial_do_work(struct work_struct *work);
76 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
77 #endif
78
79 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
80
81 /*
82  * interrupts are disabled on entry
83  */
84 static void bfin_serial_stop_tx(struct uart_port *port)
85 {
86         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
87 #if !defined(CONFIG_BF54x) && !defined(CONFIG_SERIAL_BFIN_DMA)
88         unsigned short ier;
89 #endif
90
91         while (!(UART_GET_LSR(uart) & TEMT))
92                 continue;
93
94 #ifdef CONFIG_SERIAL_BFIN_DMA
95         disable_dma(uart->tx_dma_channel);
96 #else
97 #ifdef CONFIG_BF54x
98         /* Waiting for Transmission Finished */
99         while (!(UART_GET_LSR(uart) & TFI))
100                 continue;
101         /* Clear TFI bit */
102         UART_PUT_LSR(uart, TFI);
103         UART_CLEAR_IER(uart, ETBEI);
104 #else
105         ier = UART_GET_IER(uart);
106         ier &= ~ETBEI;
107         UART_PUT_IER(uart, ier);
108 #endif
109 #endif
110 }
111
112 /*
113  * port is locked and interrupts are disabled
114  */
115 static void bfin_serial_start_tx(struct uart_port *port)
116 {
117         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
118
119 #ifdef CONFIG_SERIAL_BFIN_DMA
120         bfin_serial_dma_tx_chars(uart);
121 #else
122 #ifdef CONFIG_BF54x
123         UART_SET_IER(uart, ETBEI);
124 #else
125         unsigned short ier;
126         ier = UART_GET_IER(uart);
127         ier |= ETBEI;
128         UART_PUT_IER(uart, ier);
129 #endif
130         bfin_serial_tx_chars(uart);
131 #endif
132 }
133
134 /*
135  * Interrupts are enabled
136  */
137 static void bfin_serial_stop_rx(struct uart_port *port)
138 {
139         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
140 #ifdef  CONFIG_KGDB_UART
141         if (uart->port.line != CONFIG_KGDB_UART_PORT) {
142 #endif
143 #ifdef CONFIG_BF54x
144         UART_CLEAR_IER(uart, ERBFI);
145 #else
146         unsigned short ier;
147
148         ier = UART_GET_IER(uart);
149         ier &= ~ERBFI;
150         UART_PUT_IER(uart, ier);
151 #endif
152 #ifdef  CONFIG_KGDB_UART
153         }
154 #endif
155 }
156
157 /*
158  * Set the modem control timer to fire immediately.
159  */
160 static void bfin_serial_enable_ms(struct uart_port *port)
161 {
162 }
163
164 #ifdef CONFIG_KGDB_UART
165 static int kgdb_entry_state;
166
167 void kgdb_put_debug_char(int chr)
168 {
169         struct bfin_serial_port *uart;
170         
171         if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
172                 uart = &bfin_serial_ports[0];
173         else
174                 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
175         
176         while (!(UART_GET_LSR(uart) & THRE)) {
177                 SSYNC();
178         }
179
180 #ifndef CONFIG_BF54x
181         UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
182         SSYNC();
183 #endif
184         UART_PUT_CHAR(uart, (unsigned char)chr);
185         SSYNC();
186 }
187
188 int kgdb_get_debug_char(void)
189 {
190         struct bfin_serial_port *uart;
191         unsigned char chr;
192
193         if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
194                 uart = &bfin_serial_ports[0];
195         else
196                 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
197         
198         while(!(UART_GET_LSR(uart) & DR)) {
199                 SSYNC();
200         }
201 #ifndef CONFIG_BF54x
202         UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
203         SSYNC();
204 #endif
205         chr = UART_GET_CHAR(uart);
206         SSYNC();
207
208         return chr;
209 }
210 #endif
211
212 #ifdef CONFIG_SERIAL_BFIN_PIO
213 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
214 {
215         struct tty_struct *tty = uart->port.info->tty;
216         unsigned int status, ch, flg;
217         static int in_break = 0;
218 #ifdef CONFIG_KGDB_UART
219         struct pt_regs *regs = get_irq_regs();
220 #endif
221
222         ch = UART_GET_CHAR(uart);
223         status = UART_GET_LSR(uart);
224         uart->port.icount.rx++;
225
226 #ifdef CONFIG_KGDB_UART
227         if (uart->port.line == CONFIG_KGDB_UART_PORT) {
228                 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
229                         kgdb_breakkey_pressed(regs);
230                         return;
231                 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
232                         kgdb_entry_state = 1;
233                 } else if (kgdb_entry_state == 1 && ch == 'q') {
234                         kgdb_entry_state = 0;
235                         kgdb_breakkey_pressed(regs);
236                         return;
237                 } else if (ch == 0x3) {/* Ctrl + C */
238                         kgdb_entry_state = 0;
239                         kgdb_breakkey_pressed(regs);
240                         return;
241                 } else {
242                         kgdb_entry_state = 0;
243                 }
244         }
245 #endif
246
247         if (ANOMALY_05000230) {
248                 /* The BF533 family of processors have a nice misbehavior where
249                  * they continuously generate characters for a "single" break.
250                  * We have to basically ignore this flood until the "next" valid
251                  * character comes across.  All other Blackfin families operate
252                  * properly though.
253                  * Note: While Anomaly 05000230 does not directly address this,
254                  *       the changes that went in for it also fixed this issue.
255                  */
256                 if (in_break) {
257                         if (ch != 0) {
258                                 in_break = 0;
259                                 ch = UART_GET_CHAR(uart);
260                                 if (bfin_revid() < 5)
261                                         return;
262                         } else
263                                 return;
264                 }
265         }
266
267         if (status & BI) {
268                 if (ANOMALY_05000230)
269                         in_break = 1;
270                 uart->port.icount.brk++;
271                 if (uart_handle_break(&uart->port))
272                         goto ignore_char;
273                 status &= ~(PE | FE);
274         }
275         if (status & PE)
276                 uart->port.icount.parity++;
277         if (status & OE)
278                 uart->port.icount.overrun++;
279         if (status & FE)
280                 uart->port.icount.frame++;
281
282         status &= uart->port.read_status_mask;
283
284         if (status & BI)
285                 flg = TTY_BREAK;
286         else if (status & PE)
287                 flg = TTY_PARITY;
288         else if (status & FE)
289                 flg = TTY_FRAME;
290         else
291                 flg = TTY_NORMAL;
292
293         if (uart_handle_sysrq_char(&uart->port, ch))
294                 goto ignore_char;
295
296         uart_insert_char(&uart->port, status, OE, ch, flg);
297
298  ignore_char:
299         tty_flip_buffer_push(tty);
300 }
301
302 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
303 {
304         struct circ_buf *xmit = &uart->port.info->xmit;
305
306         if (uart->port.x_char) {
307                 UART_PUT_CHAR(uart, uart->port.x_char);
308                 uart->port.icount.tx++;
309                 uart->port.x_char = 0;
310         }
311         /*
312          * Check the modem control lines before
313          * transmitting anything.
314          */
315         bfin_serial_mctrl_check(uart);
316
317         if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
318                 bfin_serial_stop_tx(&uart->port);
319                 return;
320         }
321
322         while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
323                 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
324                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
325                 uart->port.icount.tx++;
326                 SSYNC();
327         }
328
329         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
330                 uart_write_wakeup(&uart->port);
331
332         if (uart_circ_empty(xmit))
333                 bfin_serial_stop_tx(&uart->port);
334 }
335
336 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
337 {
338         struct bfin_serial_port *uart = dev_id;
339
340         spin_lock(&uart->port.lock);
341         while ((UART_GET_IER(uart) & ERBFI) && (UART_GET_LSR(uart) & DR))
342                 bfin_serial_rx_chars(uart);
343         spin_unlock(&uart->port.lock);
344
345         return IRQ_HANDLED;
346 }
347
348 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
349 {
350         struct bfin_serial_port *uart = dev_id;
351
352         spin_lock(&uart->port.lock);
353         if ((UART_GET_IER(uart) & ETBEI) && (UART_GET_LSR(uart) & THRE))
354                 bfin_serial_tx_chars(uart);
355         spin_unlock(&uart->port.lock);
356
357         return IRQ_HANDLED;
358 }
359
360
361 static void bfin_serial_do_work(struct work_struct *work)
362 {
363         struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
364
365         bfin_serial_mctrl_check(uart);
366 }
367 #endif
368
369 #ifdef CONFIG_SERIAL_BFIN_DMA
370 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
371 {
372         struct circ_buf *xmit = &uart->port.info->xmit;
373         unsigned short ier;
374         int flags = 0;
375
376         if (!uart->tx_done)
377                 return;
378         uart->tx_done = 0;
379
380         if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
381                 bfin_serial_stop_tx(&uart->port);
382                 uart->tx_done = 1;
383                 return;
384         }
385
386         if (uart->port.x_char) {
387                 UART_PUT_CHAR(uart, uart->port.x_char);
388                 uart->port.icount.tx++;
389                 uart->port.x_char = 0;
390         }
391
392         /*
393          * Check the modem control lines before
394          * transmitting anything.
395          */
396         bfin_serial_mctrl_check(uart);
397
398         spin_lock_irqsave(&uart->port.lock, flags);
399         uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
400         if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
401                 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
402         blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
403                                         (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
404         set_dma_config(uart->tx_dma_channel,
405                 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
406                         INTR_ON_BUF,
407                         DIMENSION_LINEAR,
408                         DATA_SIZE_8,
409                         DMA_SYNC_RESTART));
410         set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
411         set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
412         set_dma_x_modify(uart->tx_dma_channel, 1);
413         enable_dma(uart->tx_dma_channel);
414 #ifdef CONFIG_BF54x
415         UART_SET_IER(uart, ETBEI);
416 #else
417         ier = UART_GET_IER(uart);
418         ier |= ETBEI;
419         UART_PUT_IER(uart, ier);
420 #endif
421         spin_unlock_irqrestore(&uart->port.lock, flags);
422 }
423
424 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
425 {
426         struct tty_struct *tty = uart->port.info->tty;
427         int i, flg, status;
428
429         status = UART_GET_LSR(uart);
430         uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
431
432         if (status & BI) {
433                 uart->port.icount.brk++;
434                 if (uart_handle_break(&uart->port))
435                         goto dma_ignore_char;
436                 status &= ~(PE | FE);
437         }
438         if (status & PE)
439                 uart->port.icount.parity++;
440         if (status & OE)
441                 uart->port.icount.overrun++;
442         if (status & FE)
443                 uart->port.icount.frame++;
444
445         status &= uart->port.read_status_mask;
446
447         if (status & BI)
448                 flg = TTY_BREAK;
449         else if (status & PE)
450                 flg = TTY_PARITY;
451         else if (status & FE)
452                 flg = TTY_FRAME;
453         else
454                 flg = TTY_NORMAL;
455
456         for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
457                 if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
458                         goto dma_ignore_char;
459                 uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
460         }
461
462  dma_ignore_char:
463         tty_flip_buffer_push(tty);
464 }
465
466 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
467 {
468         int x_pos, pos;
469         int flags = 0;
470
471         spin_lock_irqsave(&uart->port.lock, flags);
472         x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
473         if (x_pos == DMA_RX_XCOUNT)
474                 x_pos = 0;
475
476         pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
477
478         if (pos>uart->rx_dma_buf.tail) {
479                 uart->rx_dma_buf.tail = pos;
480                 bfin_serial_dma_rx_chars(uart);
481                 uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
482         }
483         spin_unlock_irqrestore(&uart->port.lock, flags);
484         uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
485         add_timer(&(uart->rx_dma_timer));
486 }
487
488 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
489 {
490         struct bfin_serial_port *uart = dev_id;
491         struct circ_buf *xmit = &uart->port.info->xmit;
492         unsigned short ier;
493
494         spin_lock(&uart->port.lock);
495         if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
496                 clear_dma_irqstat(uart->tx_dma_channel);
497                 disable_dma(uart->tx_dma_channel);
498 #ifdef CONFIG_BF54x
499                 UART_CLEAR_IER(uart, ETBEI);
500 #else
501                 ier = UART_GET_IER(uart);
502                 ier &= ~ETBEI;
503                 UART_PUT_IER(uart, ier);
504 #endif
505                 xmit->tail = (xmit->tail+uart->tx_count) &(UART_XMIT_SIZE -1);
506                 uart->port.icount.tx+=uart->tx_count;
507
508                 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
509                         uart_write_wakeup(&uart->port);
510
511                 uart->tx_done = 1;
512
513                 bfin_serial_dma_tx_chars(uart);
514         }
515
516         spin_unlock(&uart->port.lock);
517         return IRQ_HANDLED;
518 }
519
520 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
521 {
522         struct bfin_serial_port *uart = dev_id;
523         unsigned short irqstat;
524
525         uart->rx_dma_nrows++;
526         if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
527                 uart->rx_dma_nrows = 0;
528                 uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
529                 bfin_serial_dma_rx_chars(uart);
530                 uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
531         }
532         spin_lock(&uart->port.lock);
533         irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
534         clear_dma_irqstat(uart->rx_dma_channel);
535
536         spin_unlock(&uart->port.lock);
537         return IRQ_HANDLED;
538 }
539 #endif
540
541 /*
542  * Return TIOCSER_TEMT when transmitter is not busy.
543  */
544 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
545 {
546         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
547         unsigned short lsr;
548
549         lsr = UART_GET_LSR(uart);
550         if (lsr & TEMT)
551                 return TIOCSER_TEMT;
552         else
553                 return 0;
554 }
555
556 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
557 {
558 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
559         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
560         if (uart->cts_pin < 0)
561                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
562
563         if (gpio_get_value(uart->cts_pin))
564                 return TIOCM_DSR | TIOCM_CAR;
565         else
566 #endif
567                 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
568 }
569
570 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
571 {
572 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
573         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
574         if (uart->rts_pin < 0)
575                 return;
576
577         if (mctrl & TIOCM_RTS)
578                 gpio_set_value(uart->rts_pin, 0);
579         else
580                 gpio_set_value(uart->rts_pin, 1);
581 #endif
582 }
583
584 /*
585  * Handle any change of modem status signal since we were last called.
586  */
587 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
588 {
589 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
590         unsigned int status;
591 # ifdef CONFIG_SERIAL_BFIN_DMA
592         struct uart_info *info = uart->port.info;
593         struct tty_struct *tty = info->tty;
594
595         status = bfin_serial_get_mctrl(&uart->port);
596         if (!(status & TIOCM_CTS)) {
597                 tty->hw_stopped = 1;
598         } else {
599                 tty->hw_stopped = 0;
600         }
601 # else
602         status = bfin_serial_get_mctrl(&uart->port);
603         uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
604         if (!(status & TIOCM_CTS))
605                 schedule_work(&uart->cts_workqueue);
606 # endif
607 #endif
608 }
609
610 /*
611  * Interrupts are always disabled.
612  */
613 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
614 {
615         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
616         u16 lcr = UART_GET_LCR(uart);
617         if (break_state)
618                 lcr |= SB;
619         else
620                 lcr &= ~SB;
621         UART_PUT_LCR(uart, lcr);
622         SSYNC();
623 }
624
625 static int bfin_serial_startup(struct uart_port *port)
626 {
627         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
628
629 #ifdef CONFIG_SERIAL_BFIN_DMA
630         dma_addr_t dma_handle;
631
632         if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
633                 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
634                 return -EBUSY;
635         }
636
637         if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
638                 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
639                 free_dma(uart->rx_dma_channel);
640                 return -EBUSY;
641         }
642
643         set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
644         set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
645
646         uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
647         uart->rx_dma_buf.head = 0;
648         uart->rx_dma_buf.tail = 0;
649         uart->rx_dma_nrows = 0;
650
651         set_dma_config(uart->rx_dma_channel,
652                 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
653                                 INTR_ON_ROW, DIMENSION_2D,
654                                 DATA_SIZE_8,
655                                 DMA_SYNC_RESTART));
656         set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
657         set_dma_x_modify(uart->rx_dma_channel, 1);
658         set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
659         set_dma_y_modify(uart->rx_dma_channel, 1);
660         set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
661         enable_dma(uart->rx_dma_channel);
662
663         uart->rx_dma_timer.data = (unsigned long)(uart);
664         uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
665         uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
666         add_timer(&(uart->rx_dma_timer));
667 #else
668         if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
669              "BFIN_UART_RX", uart)) {
670 # ifdef CONFIG_KGDB_UART
671                 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
672 # endif
673                 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
674                 return -EBUSY;
675 # ifdef CONFIG_KGDB_UART
676                 }
677 # endif
678         }
679
680
681         if (request_irq
682             (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
683              "BFIN_UART_TX", uart)) {
684                 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
685                 free_irq(uart->port.irq, uart);
686                 return -EBUSY;
687         }
688 #endif
689 #ifdef CONFIG_BF54x
690         UART_SET_IER(uart, ERBFI);
691 #else
692         UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
693 #endif
694         return 0;
695 }
696
697 static void bfin_serial_shutdown(struct uart_port *port)
698 {
699         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
700
701 #ifdef CONFIG_SERIAL_BFIN_DMA
702         disable_dma(uart->tx_dma_channel);
703         free_dma(uart->tx_dma_channel);
704         disable_dma(uart->rx_dma_channel);
705         free_dma(uart->rx_dma_channel);
706         del_timer(&(uart->rx_dma_timer));
707 #else
708 #ifdef  CONFIG_KGDB_UART
709         if (uart->port.line != CONFIG_KGDB_UART_PORT)
710 #endif
711         free_irq(uart->port.irq, uart);
712         free_irq(uart->port.irq+1, uart);
713 #endif
714 }
715
716 static void
717 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
718                    struct ktermios *old)
719 {
720         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
721         unsigned long flags;
722         unsigned int baud, quot;
723         unsigned short val, ier, lsr, lcr = 0;
724
725         switch (termios->c_cflag & CSIZE) {
726         case CS8:
727                 lcr = WLS(8);
728                 break;
729         case CS7:
730                 lcr = WLS(7);
731                 break;
732         case CS6:
733                 lcr = WLS(6);
734                 break;
735         case CS5:
736                 lcr = WLS(5);
737                 break;
738         default:
739                 printk(KERN_ERR "%s: word lengh not supported\n",
740                         __FUNCTION__);
741         }
742
743         if (termios->c_cflag & CSTOPB)
744                 lcr |= STB;
745         if (termios->c_cflag & PARENB)
746                 lcr |= PEN;
747         if (!(termios->c_cflag & PARODD))
748                 lcr |= EPS;
749         if (termios->c_cflag & CMSPAR)
750                 lcr |= STP;
751
752         port->read_status_mask = OE;
753         if (termios->c_iflag & INPCK)
754                 port->read_status_mask |= (FE | PE);
755         if (termios->c_iflag & (BRKINT | PARMRK))
756                 port->read_status_mask |= BI;
757
758         /*
759          * Characters to ignore
760          */
761         port->ignore_status_mask = 0;
762         if (termios->c_iflag & IGNPAR)
763                 port->ignore_status_mask |= FE | PE;
764         if (termios->c_iflag & IGNBRK) {
765                 port->ignore_status_mask |= BI;
766                 /*
767                  * If we're ignoring parity and break indicators,
768                  * ignore overruns too (for real raw support).
769                  */
770                 if (termios->c_iflag & IGNPAR)
771                         port->ignore_status_mask |= OE;
772         }
773
774         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
775         quot = uart_get_divisor(port, baud);
776         spin_lock_irqsave(&uart->port.lock, flags);
777
778         do {
779                 lsr = UART_GET_LSR(uart);
780         } while (!(lsr & TEMT));
781
782         /* Disable UART */
783         ier = UART_GET_IER(uart);
784 #ifdef CONFIG_BF54x
785         UART_CLEAR_IER(uart, 0xF);
786 #else
787         UART_PUT_IER(uart, 0);
788 #endif
789
790 #ifndef CONFIG_BF54x
791         /* Set DLAB in LCR to Access DLL and DLH */
792         val = UART_GET_LCR(uart);
793         val |= DLAB;
794         UART_PUT_LCR(uart, val);
795         SSYNC();
796 #endif
797
798         UART_PUT_DLL(uart, quot & 0xFF);
799         SSYNC();
800         UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
801         SSYNC();
802
803 #ifndef CONFIG_BF54x
804         /* Clear DLAB in LCR to Access THR RBR IER */
805         val = UART_GET_LCR(uart);
806         val &= ~DLAB;
807         UART_PUT_LCR(uart, val);
808         SSYNC();
809 #endif
810
811         UART_PUT_LCR(uart, lcr);
812
813         /* Enable UART */
814 #ifdef CONFIG_BF54x
815         UART_SET_IER(uart, ier);
816 #else
817         UART_PUT_IER(uart, ier);
818 #endif
819
820         val = UART_GET_GCTL(uart);
821         val |= UCEN;
822         UART_PUT_GCTL(uart, val);
823
824         spin_unlock_irqrestore(&uart->port.lock, flags);
825 }
826
827 static const char *bfin_serial_type(struct uart_port *port)
828 {
829         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
830
831         return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
832 }
833
834 /*
835  * Release the memory region(s) being used by 'port'.
836  */
837 static void bfin_serial_release_port(struct uart_port *port)
838 {
839 }
840
841 /*
842  * Request the memory region(s) being used by 'port'.
843  */
844 static int bfin_serial_request_port(struct uart_port *port)
845 {
846         return 0;
847 }
848
849 /*
850  * Configure/autoconfigure the port.
851  */
852 static void bfin_serial_config_port(struct uart_port *port, int flags)
853 {
854         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
855
856         if (flags & UART_CONFIG_TYPE &&
857             bfin_serial_request_port(&uart->port) == 0)
858                 uart->port.type = PORT_BFIN;
859 }
860
861 /*
862  * Verify the new serial_struct (for TIOCSSERIAL).
863  * The only change we allow are to the flags and type, and
864  * even then only between PORT_BFIN and PORT_UNKNOWN
865  */
866 static int
867 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
868 {
869         return 0;
870 }
871
872 static struct uart_ops bfin_serial_pops = {
873         .tx_empty       = bfin_serial_tx_empty,
874         .set_mctrl      = bfin_serial_set_mctrl,
875         .get_mctrl      = bfin_serial_get_mctrl,
876         .stop_tx        = bfin_serial_stop_tx,
877         .start_tx       = bfin_serial_start_tx,
878         .stop_rx        = bfin_serial_stop_rx,
879         .enable_ms      = bfin_serial_enable_ms,
880         .break_ctl      = bfin_serial_break_ctl,
881         .startup        = bfin_serial_startup,
882         .shutdown       = bfin_serial_shutdown,
883         .set_termios    = bfin_serial_set_termios,
884         .type           = bfin_serial_type,
885         .release_port   = bfin_serial_release_port,
886         .request_port   = bfin_serial_request_port,
887         .config_port    = bfin_serial_config_port,
888         .verify_port    = bfin_serial_verify_port,
889 };
890
891 static void __init bfin_serial_init_ports(void)
892 {
893         static int first = 1;
894         int i;
895
896         if (!first)
897                 return;
898         first = 0;
899
900         for (i = 0; i < nr_ports; i++) {
901                 bfin_serial_ports[i].port.uartclk   = get_sclk();
902                 bfin_serial_ports[i].port.ops       = &bfin_serial_pops;
903                 bfin_serial_ports[i].port.line      = i;
904                 bfin_serial_ports[i].port.iotype    = UPIO_MEM;
905                 bfin_serial_ports[i].port.membase   =
906                         (void __iomem *)bfin_serial_resource[i].uart_base_addr;
907                 bfin_serial_ports[i].port.mapbase   =
908                         bfin_serial_resource[i].uart_base_addr;
909                 bfin_serial_ports[i].port.irq       =
910                         bfin_serial_resource[i].uart_irq;
911                 bfin_serial_ports[i].port.flags     = UPF_BOOT_AUTOCONF;
912 #ifdef CONFIG_SERIAL_BFIN_DMA
913                 bfin_serial_ports[i].tx_done        = 1;
914                 bfin_serial_ports[i].tx_count       = 0;
915                 bfin_serial_ports[i].tx_dma_channel =
916                         bfin_serial_resource[i].uart_tx_dma_channel;
917                 bfin_serial_ports[i].rx_dma_channel =
918                         bfin_serial_resource[i].uart_rx_dma_channel;
919                 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
920 #else
921                 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
922 #endif
923 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
924                 bfin_serial_ports[i].cts_pin        =
925                         bfin_serial_resource[i].uart_cts_pin;
926                 bfin_serial_ports[i].rts_pin        =
927                         bfin_serial_resource[i].uart_rts_pin;
928 #endif
929                 bfin_serial_hw_init(&bfin_serial_ports[i]);
930         }
931
932 }
933
934 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
935 /*
936  * If the port was already initialised (eg, by a boot loader),
937  * try to determine the current setup.
938  */
939 static void __init
940 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
941                            int *parity, int *bits)
942 {
943         unsigned short status;
944
945         status = UART_GET_IER(uart) & (ERBFI | ETBEI);
946         if (status == (ERBFI | ETBEI)) {
947                 /* ok, the port was enabled */
948                 unsigned short lcr, val;
949                 unsigned short dlh, dll;
950
951                 lcr = UART_GET_LCR(uart);
952
953                 *parity = 'n';
954                 if (lcr & PEN) {
955                         if (lcr & EPS)
956                                 *parity = 'e';
957                         else
958                                 *parity = 'o';
959                 }
960                 switch (lcr & 0x03) {
961                         case 0: *bits = 5; break;
962                         case 1: *bits = 6; break;
963                         case 2: *bits = 7; break;
964                         case 3: *bits = 8; break;
965                 }
966 #ifndef CONFIG_BF54x
967                 /* Set DLAB in LCR to Access DLL and DLH */
968                 val = UART_GET_LCR(uart);
969                 val |= DLAB;
970                 UART_PUT_LCR(uart, val);
971 #endif
972
973                 dll = UART_GET_DLL(uart);
974                 dlh = UART_GET_DLH(uart);
975
976 #ifndef CONFIG_BF54x
977                 /* Clear DLAB in LCR to Access THR RBR IER */
978                 val = UART_GET_LCR(uart);
979                 val &= ~DLAB;
980                 UART_PUT_LCR(uart, val);
981 #endif
982
983                 *baud = get_sclk() / (16*(dll | dlh << 8));
984         }
985         pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
986 }
987 #endif
988
989 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
990 static struct uart_driver bfin_serial_reg;
991
992 static int __init
993 bfin_serial_console_setup(struct console *co, char *options)
994 {
995         struct bfin_serial_port *uart;
996 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
997         int baud = 57600;
998         int bits = 8;
999         int parity = 'n';
1000 #  ifdef CONFIG_SERIAL_BFIN_CTSRTS
1001         int flow = 'r';
1002 #  else
1003         int flow = 'n';
1004 #  endif
1005 # endif
1006
1007         /*
1008          * Check whether an invalid uart number has been specified, and
1009          * if so, search for the first available port that does have
1010          * console support.
1011          */
1012         if (co->index == -1 || co->index >= nr_ports)
1013                 co->index = 0;
1014         uart = &bfin_serial_ports[co->index];
1015
1016 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1017         if (options)
1018                 uart_parse_options(options, &baud, &parity, &bits, &flow);
1019         else
1020                 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1021
1022         return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1023 # else
1024         return 0;
1025 # endif
1026 }
1027 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1028                                  defined (CONFIG_EARLY_PRINTK) */
1029
1030 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1031 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1032 {
1033         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1034         while (!(UART_GET_LSR(uart) & THRE))
1035                 barrier();
1036         UART_PUT_CHAR(uart, ch);
1037         SSYNC();
1038 }
1039
1040 /*
1041  * Interrupts are disabled on entering
1042  */
1043 static void
1044 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1045 {
1046         struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1047         int flags = 0;
1048
1049         spin_lock_irqsave(&uart->port.lock, flags);
1050         uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1051         spin_unlock_irqrestore(&uart->port.lock, flags);
1052
1053 }
1054
1055 static struct console bfin_serial_console = {
1056         .name           = BFIN_SERIAL_NAME,
1057         .write          = bfin_serial_console_write,
1058         .device         = uart_console_device,
1059         .setup          = bfin_serial_console_setup,
1060         .flags          = CON_PRINTBUFFER,
1061         .index          = -1,
1062         .data           = &bfin_serial_reg,
1063 };
1064
1065 static int __init bfin_serial_rs_console_init(void)
1066 {
1067         bfin_serial_init_ports();
1068         register_console(&bfin_serial_console);
1069 #ifdef CONFIG_KGDB_UART
1070         kgdb_entry_state = 0;
1071         init_kgdb_uart();
1072 #endif
1073         return 0;
1074 }
1075 console_initcall(bfin_serial_rs_console_init);
1076
1077 #define BFIN_SERIAL_CONSOLE     &bfin_serial_console
1078 #else
1079 #define BFIN_SERIAL_CONSOLE     NULL
1080 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1081
1082
1083 #ifdef CONFIG_EARLY_PRINTK
1084 static __init void early_serial_putc(struct uart_port *port, int ch)
1085 {
1086         unsigned timeout = 0xffff;
1087         struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1088
1089         while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1090                 cpu_relax();
1091         UART_PUT_CHAR(uart, ch);
1092 }
1093
1094 static __init void early_serial_write(struct console *con, const char *s,
1095                                         unsigned int n)
1096 {
1097         struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1098         unsigned int i;
1099
1100         for (i = 0; i < n; i++, s++) {
1101                 if (*s == '\n')
1102                         early_serial_putc(&uart->port, '\r');
1103                 early_serial_putc(&uart->port, *s);
1104         }
1105 }
1106
1107 static struct __init console bfin_early_serial_console = {
1108         .name = "early_BFuart",
1109         .write = early_serial_write,
1110         .device = uart_console_device,
1111         .flags = CON_PRINTBUFFER,
1112         .setup = bfin_serial_console_setup,
1113         .index = -1,
1114         .data  = &bfin_serial_reg,
1115 };
1116
1117 struct console __init *bfin_earlyserial_init(unsigned int port,
1118                                                 unsigned int cflag)
1119 {
1120         struct bfin_serial_port *uart;
1121         struct ktermios t;
1122
1123         if (port == -1 || port >= nr_ports)
1124                 port = 0;
1125         bfin_serial_init_ports();
1126         bfin_early_serial_console.index = port;
1127         uart = &bfin_serial_ports[port];
1128         t.c_cflag = cflag;
1129         t.c_iflag = 0;
1130         t.c_oflag = 0;
1131         t.c_lflag = ICANON;
1132         t.c_line = port;
1133         bfin_serial_set_termios(&uart->port, &t, &t);
1134         return &bfin_early_serial_console;
1135 }
1136
1137 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1138
1139 static struct uart_driver bfin_serial_reg = {
1140         .owner                  = THIS_MODULE,
1141         .driver_name            = "bfin-uart",
1142         .dev_name               = BFIN_SERIAL_NAME,
1143         .major                  = BFIN_SERIAL_MAJOR,
1144         .minor                  = BFIN_SERIAL_MINOR,
1145         .nr                     = NR_PORTS,
1146         .cons                   = BFIN_SERIAL_CONSOLE,
1147 };
1148
1149 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1150 {
1151         struct bfin_serial_port *uart = platform_get_drvdata(dev);
1152
1153         if (uart)
1154                 uart_suspend_port(&bfin_serial_reg, &uart->port);
1155
1156         return 0;
1157 }
1158
1159 static int bfin_serial_resume(struct platform_device *dev)
1160 {
1161         struct bfin_serial_port *uart = platform_get_drvdata(dev);
1162
1163         if (uart)
1164                 uart_resume_port(&bfin_serial_reg, &uart->port);
1165
1166         return 0;
1167 }
1168
1169 static int bfin_serial_probe(struct platform_device *dev)
1170 {
1171         struct resource *res = dev->resource;
1172         int i;
1173
1174         for (i = 0; i < dev->num_resources; i++, res++)
1175                 if (res->flags & IORESOURCE_MEM)
1176                         break;
1177
1178         if (i < dev->num_resources) {
1179                 for (i = 0; i < nr_ports; i++, res++) {
1180                         if (bfin_serial_ports[i].port.mapbase != res->start)
1181                                 continue;
1182                         bfin_serial_ports[i].port.dev = &dev->dev;
1183                         uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1184                         platform_set_drvdata(dev, &bfin_serial_ports[i]);
1185                 }
1186         }
1187
1188         return 0;
1189 }
1190
1191 static int bfin_serial_remove(struct platform_device *pdev)
1192 {
1193         struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1194
1195
1196 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1197         gpio_free(uart->cts_pin);
1198         gpio_free(uart->rts_pin);
1199 #endif
1200
1201         platform_set_drvdata(pdev, NULL);
1202
1203         if (uart)
1204                 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1205
1206         return 0;
1207 }
1208
1209 static struct platform_driver bfin_serial_driver = {
1210         .probe          = bfin_serial_probe,
1211         .remove         = bfin_serial_remove,
1212         .suspend        = bfin_serial_suspend,
1213         .resume         = bfin_serial_resume,
1214         .driver         = {
1215                 .name   = "bfin-uart",
1216         },
1217 };
1218
1219 static int __init bfin_serial_init(void)
1220 {
1221         int ret;
1222 #ifdef CONFIG_KGDB_UART
1223         struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
1224         struct ktermios t;
1225 #endif
1226
1227         pr_info("Serial: Blackfin serial driver\n");
1228
1229         bfin_serial_init_ports();
1230
1231         ret = uart_register_driver(&bfin_serial_reg);
1232         if (ret == 0) {
1233                 ret = platform_driver_register(&bfin_serial_driver);
1234                 if (ret) {
1235                         pr_debug("uart register failed\n");
1236                         uart_unregister_driver(&bfin_serial_reg);
1237                 }
1238         }
1239 #ifdef CONFIG_KGDB_UART
1240         if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
1241                 request_irq(uart->port.irq, bfin_serial_rx_int,
1242                         IRQF_DISABLED, "BFIN_UART_RX", uart);
1243                 pr_info("Request irq for kgdb uart port\n");
1244 #ifdef CONFIG_BF54x
1245                 UART_SET_IER(uart, ERBFI);
1246 #else
1247                 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
1248 #endif
1249                 SSYNC();
1250                 t.c_cflag = CS8|B57600;
1251                 t.c_iflag = 0;
1252                 t.c_oflag = 0;
1253                 t.c_lflag = ICANON;
1254                 t.c_line = CONFIG_KGDB_UART_PORT;
1255                 bfin_serial_set_termios(&uart->port, &t, &t);
1256         }
1257 #endif
1258         return ret;
1259 }
1260
1261 static void __exit bfin_serial_exit(void)
1262 {
1263         platform_driver_unregister(&bfin_serial_driver);
1264         uart_unregister_driver(&bfin_serial_reg);
1265 }
1266
1267 module_init(bfin_serial_init);
1268 module_exit(bfin_serial_exit);
1269
1270 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1271 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1272 MODULE_LICENSE("GPL");
1273 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);