drivers/edac: i3000: 64bit build
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / serial / bfin_5xx.c
blobac2a3ef28d55435d3673a8909e01ee27c77ebd82
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>
6 * Created:
7 * Description: Driver for blackfin 5xx serial ports
9 * Modified:
10 * Copyright 2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
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.
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.
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
30 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31 #define SUPPORT_SYSRQ
32 #endif
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>
44 #ifdef CONFIG_KGDB_UART
45 #include <linux/kgdb.h>
46 #include <asm/irq_regs.h>
47 #endif
49 #include <asm/gpio.h>
50 #include <asm/mach/bfin_serial_5xx.h>
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
59 /* UART name and device definitions */
60 #define BFIN_SERIAL_NAME "ttyBF"
61 #define BFIN_SERIAL_MAJOR 204
62 #define BFIN_SERIAL_MINOR 64
65 * Setup for console. Argument comes from the menuconfig
67 #define DMA_RX_XCOUNT 512
68 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
70 #define DMA_RX_FLUSH_JIFFIES 5
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 static void local_put_char(struct bfin_serial_port *uart, char ch);
78 #endif
80 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
83 * interrupts are disabled on entry
85 static void bfin_serial_stop_tx(struct uart_port *port)
87 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
89 while (!(UART_GET_LSR(uart) & TEMT))
90 continue;
92 #ifdef CONFIG_SERIAL_BFIN_DMA
93 disable_dma(uart->tx_dma_channel);
94 #else
95 #ifdef CONFIG_BF54x
96 /* Waiting for Transmission Finished */
97 while (!(UART_GET_LSR(uart) & TFI))
98 continue;
99 /* Clear TFI bit */
100 UART_PUT_LSR(uart, TFI);
101 UART_CLEAR_IER(uart, ETBEI);
102 #else
103 unsigned short ier;
105 ier = UART_GET_IER(uart);
106 ier &= ~ETBEI;
107 UART_PUT_IER(uart, ier);
108 #endif
109 #endif
113 * port is locked and interrupts are disabled
115 static void bfin_serial_start_tx(struct uart_port *port)
117 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
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
135 * Interrupts are enabled
137 static void bfin_serial_stop_rx(struct uart_port *port)
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;
148 ier = UART_GET_IER(uart);
149 ier &= ~ERBFI;
150 UART_PUT_IER(uart, ier);
151 #endif
152 #ifdef CONFIG_KGDB_UART
154 #endif
158 * Set the modem control timer to fire immediately.
160 static void bfin_serial_enable_ms(struct uart_port *port)
164 #ifdef CONFIG_KGDB_UART
165 static int kgdb_entry_state;
167 void kgdb_put_debug_char(int chr)
169 struct bfin_serial_port *uart;
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];
176 while (!(UART_GET_LSR(uart) & THRE)) {
177 SSYNC();
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();
188 int kgdb_get_debug_char(void)
190 struct bfin_serial_port *uart;
191 unsigned char chr;
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];
198 while(!(UART_GET_LSR(uart) & DR)) {
199 SSYNC();
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();
208 return chr;
210 #endif
212 #ifdef CONFIG_SERIAL_BFIN_PIO
213 static void local_put_char(struct bfin_serial_port *uart, char ch)
215 unsigned short status;
216 int flags = 0;
218 spin_lock_irqsave(&uart->port.lock, flags);
220 do {
221 status = UART_GET_LSR(uart);
222 } while (!(status & THRE));
224 UART_PUT_CHAR(uart, ch);
225 SSYNC();
227 spin_unlock_irqrestore(&uart->port.lock, flags);
230 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
232 struct tty_struct *tty = uart->port.info->tty;
233 unsigned int status, ch, flg;
234 static int in_break = 0;
235 #ifdef CONFIG_KGDB_UART
236 struct pt_regs *regs = get_irq_regs();
237 #endif
239 status = UART_GET_LSR(uart);
240 ch = UART_GET_CHAR(uart);
241 uart->port.icount.rx++;
243 #ifdef CONFIG_KGDB_UART
244 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
245 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
246 kgdb_breakkey_pressed(regs);
247 return;
248 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
249 kgdb_entry_state = 1;
250 } else if (kgdb_entry_state == 1 && ch == 'q') {
251 kgdb_entry_state = 0;
252 kgdb_breakkey_pressed(regs);
253 return;
254 } else if (ch == 0x3) {/* Ctrl + C */
255 kgdb_entry_state = 0;
256 kgdb_breakkey_pressed(regs);
257 return;
258 } else {
259 kgdb_entry_state = 0;
262 #endif
264 if (ANOMALY_05000230) {
265 /* The BF533 family of processors have a nice misbehavior where
266 * they continuously generate characters for a "single" break.
267 * We have to basically ignore this flood until the "next" valid
268 * character comes across. All other Blackfin families operate
269 * properly though.
270 * Note: While Anomaly 05000230 does not directly address this,
271 * the changes that went in for it also fixed this issue.
273 if (in_break) {
274 if (ch != 0) {
275 in_break = 0;
276 ch = UART_GET_CHAR(uart);
277 if (bfin_revid() < 5)
278 return;
279 } else
280 return;
284 if (status & BI) {
285 if (ANOMALY_05000230)
286 in_break = 1;
287 uart->port.icount.brk++;
288 if (uart_handle_break(&uart->port))
289 goto ignore_char;
290 status &= ~(PE | FE);
292 if (status & PE)
293 uart->port.icount.parity++;
294 if (status & OE)
295 uart->port.icount.overrun++;
296 if (status & FE)
297 uart->port.icount.frame++;
299 status &= uart->port.read_status_mask;
301 if (status & BI)
302 flg = TTY_BREAK;
303 else if (status & PE)
304 flg = TTY_PARITY;
305 else if (status & FE)
306 flg = TTY_FRAME;
307 else
308 flg = TTY_NORMAL;
310 if (uart_handle_sysrq_char(&uart->port, ch))
311 goto ignore_char;
313 uart_insert_char(&uart->port, status, OE, ch, flg);
315 ignore_char:
316 tty_flip_buffer_push(tty);
319 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
321 struct circ_buf *xmit = &uart->port.info->xmit;
323 if (uart->port.x_char) {
324 UART_PUT_CHAR(uart, uart->port.x_char);
325 uart->port.icount.tx++;
326 uart->port.x_char = 0;
327 return;
330 * Check the modem control lines before
331 * transmitting anything.
333 bfin_serial_mctrl_check(uart);
335 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
336 bfin_serial_stop_tx(&uart->port);
337 return;
340 local_put_char(uart, xmit->buf[xmit->tail]);
341 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
342 uart->port.icount.tx++;
344 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
345 uart_write_wakeup(&uart->port);
347 if (uart_circ_empty(xmit))
348 bfin_serial_stop_tx(&uart->port);
351 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
353 struct bfin_serial_port *uart = dev_id;
355 #ifdef CONFIG_BF54x
356 unsigned short status;
357 spin_lock(&uart->port.lock);
358 status = UART_GET_LSR(uart);
359 while ((UART_GET_IER(uart) & ERBFI) && (status & DR)) {
360 bfin_serial_rx_chars(uart);
361 status = UART_GET_LSR(uart);
363 spin_unlock(&uart->port.lock);
364 #else
365 spin_lock(&uart->port.lock);
366 while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_RX_READY)
367 bfin_serial_rx_chars(uart);
368 spin_unlock(&uart->port.lock);
369 #endif
370 return IRQ_HANDLED;
373 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
375 struct bfin_serial_port *uart = dev_id;
377 #ifdef CONFIG_BF54x
378 unsigned short status;
379 spin_lock(&uart->port.lock);
380 status = UART_GET_LSR(uart);
381 while ((UART_GET_IER(uart) & ETBEI) && (status & THRE)) {
382 bfin_serial_tx_chars(uart);
383 status = UART_GET_LSR(uart);
385 spin_unlock(&uart->port.lock);
386 #else
387 spin_lock(&uart->port.lock);
388 while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_TX_READY)
389 bfin_serial_tx_chars(uart);
390 spin_unlock(&uart->port.lock);
391 #endif
392 return IRQ_HANDLED;
396 static void bfin_serial_do_work(struct work_struct *work)
398 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
400 bfin_serial_mctrl_check(uart);
402 #endif
404 #ifdef CONFIG_SERIAL_BFIN_DMA
405 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
407 struct circ_buf *xmit = &uart->port.info->xmit;
408 unsigned short ier;
409 int flags = 0;
411 if (!uart->tx_done)
412 return;
414 uart->tx_done = 0;
416 if (uart->port.x_char) {
417 UART_PUT_CHAR(uart, uart->port.x_char);
418 uart->port.icount.tx++;
419 uart->port.x_char = 0;
420 uart->tx_done = 1;
421 return;
424 * Check the modem control lines before
425 * transmitting anything.
427 bfin_serial_mctrl_check(uart);
429 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
430 bfin_serial_stop_tx(&uart->port);
431 uart->tx_done = 1;
432 return;
435 spin_lock_irqsave(&uart->port.lock, flags);
436 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
437 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
438 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
439 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
440 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
441 set_dma_config(uart->tx_dma_channel,
442 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
443 INTR_ON_BUF,
444 DIMENSION_LINEAR,
445 DATA_SIZE_8,
446 DMA_SYNC_RESTART));
447 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
448 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
449 set_dma_x_modify(uart->tx_dma_channel, 1);
450 enable_dma(uart->tx_dma_channel);
451 #ifdef CONFIG_BF54x
452 UART_SET_IER(uart, ETBEI);
453 #else
454 ier = UART_GET_IER(uart);
455 ier |= ETBEI;
456 UART_PUT_IER(uart, ier);
457 #endif
458 spin_unlock_irqrestore(&uart->port.lock, flags);
461 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
463 struct tty_struct *tty = uart->port.info->tty;
464 int i, flg, status;
466 status = UART_GET_LSR(uart);
467 uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
469 if (status & BI) {
470 uart->port.icount.brk++;
471 if (uart_handle_break(&uart->port))
472 goto dma_ignore_char;
473 status &= ~(PE | FE);
475 if (status & PE)
476 uart->port.icount.parity++;
477 if (status & OE)
478 uart->port.icount.overrun++;
479 if (status & FE)
480 uart->port.icount.frame++;
482 status &= uart->port.read_status_mask;
484 if (status & BI)
485 flg = TTY_BREAK;
486 else if (status & PE)
487 flg = TTY_PARITY;
488 else if (status & FE)
489 flg = TTY_FRAME;
490 else
491 flg = TTY_NORMAL;
493 for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
494 if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
495 goto dma_ignore_char;
496 uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
499 dma_ignore_char:
500 tty_flip_buffer_push(tty);
503 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
505 int x_pos, pos;
506 int flags = 0;
508 bfin_serial_dma_tx_chars(uart);
510 spin_lock_irqsave(&uart->port.lock, flags);
511 x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
512 if (x_pos == DMA_RX_XCOUNT)
513 x_pos = 0;
515 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
517 if (pos>uart->rx_dma_buf.tail) {
518 uart->rx_dma_buf.tail = pos;
519 bfin_serial_dma_rx_chars(uart);
520 uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
522 spin_unlock_irqrestore(&uart->port.lock, flags);
523 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
524 add_timer(&(uart->rx_dma_timer));
527 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
529 struct bfin_serial_port *uart = dev_id;
530 struct circ_buf *xmit = &uart->port.info->xmit;
531 unsigned short ier;
533 spin_lock(&uart->port.lock);
534 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
535 clear_dma_irqstat(uart->tx_dma_channel);
536 disable_dma(uart->tx_dma_channel);
537 #ifdef CONFIG_BF54x
538 UART_CLEAR_IER(uart, ETBEI);
539 #else
540 ier = UART_GET_IER(uart);
541 ier &= ~ETBEI;
542 UART_PUT_IER(uart, ier);
543 #endif
544 xmit->tail = (xmit->tail+uart->tx_count) &(UART_XMIT_SIZE -1);
545 uart->port.icount.tx+=uart->tx_count;
547 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
548 uart_write_wakeup(&uart->port);
550 if (uart_circ_empty(xmit))
551 bfin_serial_stop_tx(&uart->port);
552 uart->tx_done = 1;
555 spin_unlock(&uart->port.lock);
556 return IRQ_HANDLED;
559 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
561 struct bfin_serial_port *uart = dev_id;
562 unsigned short irqstat;
564 uart->rx_dma_nrows++;
565 if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
566 uart->rx_dma_nrows = 0;
567 uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
568 bfin_serial_dma_rx_chars(uart);
569 uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
571 spin_lock(&uart->port.lock);
572 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
573 clear_dma_irqstat(uart->rx_dma_channel);
575 spin_unlock(&uart->port.lock);
576 return IRQ_HANDLED;
578 #endif
581 * Return TIOCSER_TEMT when transmitter is not busy.
583 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
585 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
586 unsigned short lsr;
588 lsr = UART_GET_LSR(uart);
589 if (lsr & TEMT)
590 return TIOCSER_TEMT;
591 else
592 return 0;
595 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
597 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
598 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
599 if (uart->cts_pin < 0)
600 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
602 if (gpio_get_value(uart->cts_pin))
603 return TIOCM_DSR | TIOCM_CAR;
604 else
605 #endif
606 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
609 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
611 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
612 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
613 if (uart->rts_pin < 0)
614 return;
616 if (mctrl & TIOCM_RTS)
617 gpio_set_value(uart->rts_pin, 0);
618 else
619 gpio_set_value(uart->rts_pin, 1);
620 #endif
624 * Handle any change of modem status signal since we were last called.
626 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
628 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
629 unsigned int status;
630 # ifdef CONFIG_SERIAL_BFIN_DMA
631 struct uart_info *info = uart->port.info;
632 struct tty_struct *tty = info->tty;
634 status = bfin_serial_get_mctrl(&uart->port);
635 if (!(status & TIOCM_CTS)) {
636 tty->hw_stopped = 1;
637 } else {
638 tty->hw_stopped = 0;
640 # else
641 status = bfin_serial_get_mctrl(&uart->port);
642 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
643 if (!(status & TIOCM_CTS))
644 schedule_work(&uart->cts_workqueue);
645 # endif
646 #endif
650 * Interrupts are always disabled.
652 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
654 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
655 u16 lcr = UART_GET_LCR(uart);
656 if (break_state)
657 lcr |= SB;
658 else
659 lcr &= ~SB;
660 UART_PUT_LCR(uart, lcr);
661 SSYNC();
664 static int bfin_serial_startup(struct uart_port *port)
666 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
668 #ifdef CONFIG_SERIAL_BFIN_DMA
669 dma_addr_t dma_handle;
671 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
672 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
673 return -EBUSY;
676 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
677 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
678 free_dma(uart->rx_dma_channel);
679 return -EBUSY;
682 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
683 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
685 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
686 uart->rx_dma_buf.head = 0;
687 uart->rx_dma_buf.tail = 0;
688 uart->rx_dma_nrows = 0;
690 set_dma_config(uart->rx_dma_channel,
691 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
692 INTR_ON_ROW, DIMENSION_2D,
693 DATA_SIZE_8,
694 DMA_SYNC_RESTART));
695 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
696 set_dma_x_modify(uart->rx_dma_channel, 1);
697 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
698 set_dma_y_modify(uart->rx_dma_channel, 1);
699 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
700 enable_dma(uart->rx_dma_channel);
702 uart->rx_dma_timer.data = (unsigned long)(uart);
703 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
704 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
705 add_timer(&(uart->rx_dma_timer));
706 #else
707 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
708 "BFIN_UART_RX", uart)) {
709 # ifdef CONFIG_KGDB_UART
710 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
711 # endif
712 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
713 return -EBUSY;
714 # ifdef CONFIG_KGDB_UART
716 # endif
720 if (request_irq
721 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
722 "BFIN_UART_TX", uart)) {
723 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
724 free_irq(uart->port.irq, uart);
725 return -EBUSY;
727 #endif
728 #ifdef CONFIG_BF54x
729 UART_SET_IER(uart, ERBFI);
730 #else
731 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
732 #endif
733 return 0;
736 static void bfin_serial_shutdown(struct uart_port *port)
738 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
740 #ifdef CONFIG_SERIAL_BFIN_DMA
741 disable_dma(uart->tx_dma_channel);
742 free_dma(uart->tx_dma_channel);
743 disable_dma(uart->rx_dma_channel);
744 free_dma(uart->rx_dma_channel);
745 del_timer(&(uart->rx_dma_timer));
746 #else
747 #ifdef CONFIG_KGDB_UART
748 if (uart->port.line != CONFIG_KGDB_UART_PORT)
749 #endif
750 free_irq(uart->port.irq, uart);
751 free_irq(uart->port.irq+1, uart);
752 #endif
755 static void
756 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
757 struct ktermios *old)
759 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
760 unsigned long flags;
761 unsigned int baud, quot;
762 unsigned short val, ier, lsr, lcr = 0;
764 switch (termios->c_cflag & CSIZE) {
765 case CS8:
766 lcr = WLS(8);
767 break;
768 case CS7:
769 lcr = WLS(7);
770 break;
771 case CS6:
772 lcr = WLS(6);
773 break;
774 case CS5:
775 lcr = WLS(5);
776 break;
777 default:
778 printk(KERN_ERR "%s: word lengh not supported\n",
779 __FUNCTION__);
782 if (termios->c_cflag & CSTOPB)
783 lcr |= STB;
784 if (termios->c_cflag & PARENB)
785 lcr |= PEN;
786 if (!(termios->c_cflag & PARODD))
787 lcr |= EPS;
788 if (termios->c_cflag & CMSPAR)
789 lcr |= STP;
791 port->read_status_mask = OE;
792 if (termios->c_iflag & INPCK)
793 port->read_status_mask |= (FE | PE);
794 if (termios->c_iflag & (BRKINT | PARMRK))
795 port->read_status_mask |= BI;
798 * Characters to ignore
800 port->ignore_status_mask = 0;
801 if (termios->c_iflag & IGNPAR)
802 port->ignore_status_mask |= FE | PE;
803 if (termios->c_iflag & IGNBRK) {
804 port->ignore_status_mask |= BI;
806 * If we're ignoring parity and break indicators,
807 * ignore overruns too (for real raw support).
809 if (termios->c_iflag & IGNPAR)
810 port->ignore_status_mask |= OE;
813 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
814 quot = uart_get_divisor(port, baud);
815 spin_lock_irqsave(&uart->port.lock, flags);
817 do {
818 lsr = UART_GET_LSR(uart);
819 } while (!(lsr & TEMT));
821 /* Disable UART */
822 ier = UART_GET_IER(uart);
823 #ifdef CONFIG_BF54x
824 UART_CLEAR_IER(uart, 0xF);
825 #else
826 UART_PUT_IER(uart, 0);
827 #endif
829 #ifndef CONFIG_BF54x
830 /* Set DLAB in LCR to Access DLL and DLH */
831 val = UART_GET_LCR(uart);
832 val |= DLAB;
833 UART_PUT_LCR(uart, val);
834 SSYNC();
835 #endif
837 UART_PUT_DLL(uart, quot & 0xFF);
838 SSYNC();
839 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
840 SSYNC();
842 #ifndef CONFIG_BF54x
843 /* Clear DLAB in LCR to Access THR RBR IER */
844 val = UART_GET_LCR(uart);
845 val &= ~DLAB;
846 UART_PUT_LCR(uart, val);
847 SSYNC();
848 #endif
850 UART_PUT_LCR(uart, lcr);
852 /* Enable UART */
853 #ifdef CONFIG_BF54x
854 UART_SET_IER(uart, ier);
855 #else
856 UART_PUT_IER(uart, ier);
857 #endif
859 val = UART_GET_GCTL(uart);
860 val |= UCEN;
861 UART_PUT_GCTL(uart, val);
863 spin_unlock_irqrestore(&uart->port.lock, flags);
866 static const char *bfin_serial_type(struct uart_port *port)
868 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
870 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
874 * Release the memory region(s) being used by 'port'.
876 static void bfin_serial_release_port(struct uart_port *port)
881 * Request the memory region(s) being used by 'port'.
883 static int bfin_serial_request_port(struct uart_port *port)
885 return 0;
889 * Configure/autoconfigure the port.
891 static void bfin_serial_config_port(struct uart_port *port, int flags)
893 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
895 if (flags & UART_CONFIG_TYPE &&
896 bfin_serial_request_port(&uart->port) == 0)
897 uart->port.type = PORT_BFIN;
901 * Verify the new serial_struct (for TIOCSSERIAL).
902 * The only change we allow are to the flags and type, and
903 * even then only between PORT_BFIN and PORT_UNKNOWN
905 static int
906 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
908 return 0;
911 static struct uart_ops bfin_serial_pops = {
912 .tx_empty = bfin_serial_tx_empty,
913 .set_mctrl = bfin_serial_set_mctrl,
914 .get_mctrl = bfin_serial_get_mctrl,
915 .stop_tx = bfin_serial_stop_tx,
916 .start_tx = bfin_serial_start_tx,
917 .stop_rx = bfin_serial_stop_rx,
918 .enable_ms = bfin_serial_enable_ms,
919 .break_ctl = bfin_serial_break_ctl,
920 .startup = bfin_serial_startup,
921 .shutdown = bfin_serial_shutdown,
922 .set_termios = bfin_serial_set_termios,
923 .type = bfin_serial_type,
924 .release_port = bfin_serial_release_port,
925 .request_port = bfin_serial_request_port,
926 .config_port = bfin_serial_config_port,
927 .verify_port = bfin_serial_verify_port,
930 static void __init bfin_serial_init_ports(void)
932 static int first = 1;
933 int i;
935 if (!first)
936 return;
937 first = 0;
939 for (i = 0; i < nr_ports; i++) {
940 bfin_serial_ports[i].port.uartclk = get_sclk();
941 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
942 bfin_serial_ports[i].port.line = i;
943 bfin_serial_ports[i].port.iotype = UPIO_MEM;
944 bfin_serial_ports[i].port.membase =
945 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
946 bfin_serial_ports[i].port.mapbase =
947 bfin_serial_resource[i].uart_base_addr;
948 bfin_serial_ports[i].port.irq =
949 bfin_serial_resource[i].uart_irq;
950 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
951 #ifdef CONFIG_SERIAL_BFIN_DMA
952 bfin_serial_ports[i].tx_done = 1;
953 bfin_serial_ports[i].tx_count = 0;
954 bfin_serial_ports[i].tx_dma_channel =
955 bfin_serial_resource[i].uart_tx_dma_channel;
956 bfin_serial_ports[i].rx_dma_channel =
957 bfin_serial_resource[i].uart_rx_dma_channel;
958 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
959 #else
960 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
961 #endif
962 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
963 bfin_serial_ports[i].cts_pin =
964 bfin_serial_resource[i].uart_cts_pin;
965 bfin_serial_ports[i].rts_pin =
966 bfin_serial_resource[i].uart_rts_pin;
967 #endif
968 bfin_serial_hw_init(&bfin_serial_ports[i]);
973 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
975 * If the port was already initialised (eg, by a boot loader),
976 * try to determine the current setup.
978 static void __init
979 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
980 int *parity, int *bits)
982 unsigned short status;
984 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
985 if (status == (ERBFI | ETBEI)) {
986 /* ok, the port was enabled */
987 unsigned short lcr, val;
988 unsigned short dlh, dll;
990 lcr = UART_GET_LCR(uart);
992 *parity = 'n';
993 if (lcr & PEN) {
994 if (lcr & EPS)
995 *parity = 'e';
996 else
997 *parity = 'o';
999 switch (lcr & 0x03) {
1000 case 0: *bits = 5; break;
1001 case 1: *bits = 6; break;
1002 case 2: *bits = 7; break;
1003 case 3: *bits = 8; break;
1005 #ifndef CONFIG_BF54x
1006 /* Set DLAB in LCR to Access DLL and DLH */
1007 val = UART_GET_LCR(uart);
1008 val |= DLAB;
1009 UART_PUT_LCR(uart, val);
1010 #endif
1012 dll = UART_GET_DLL(uart);
1013 dlh = UART_GET_DLH(uart);
1015 #ifndef CONFIG_BF54x
1016 /* Clear DLAB in LCR to Access THR RBR IER */
1017 val = UART_GET_LCR(uart);
1018 val &= ~DLAB;
1019 UART_PUT_LCR(uart, val);
1020 #endif
1022 *baud = get_sclk() / (16*(dll | dlh << 8));
1024 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
1026 #endif
1028 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1029 static struct uart_driver bfin_serial_reg;
1031 static int __init
1032 bfin_serial_console_setup(struct console *co, char *options)
1034 struct bfin_serial_port *uart;
1035 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1036 int baud = 57600;
1037 int bits = 8;
1038 int parity = 'n';
1039 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1040 int flow = 'r';
1041 # else
1042 int flow = 'n';
1043 # endif
1044 # endif
1047 * Check whether an invalid uart number has been specified, and
1048 * if so, search for the first available port that does have
1049 * console support.
1051 if (co->index == -1 || co->index >= nr_ports)
1052 co->index = 0;
1053 uart = &bfin_serial_ports[co->index];
1055 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1056 if (options)
1057 uart_parse_options(options, &baud, &parity, &bits, &flow);
1058 else
1059 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1061 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1062 # else
1063 return 0;
1064 # endif
1066 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1067 defined (CONFIG_EARLY_PRINTK) */
1069 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1070 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1072 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1073 while (!(UART_GET_LSR(uart) & THRE))
1074 barrier();
1075 UART_PUT_CHAR(uart, ch);
1076 SSYNC();
1080 * Interrupts are disabled on entering
1082 static void
1083 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1085 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1086 int flags = 0;
1088 spin_lock_irqsave(&uart->port.lock, flags);
1089 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1090 spin_unlock_irqrestore(&uart->port.lock, flags);
1094 static struct console bfin_serial_console = {
1095 .name = BFIN_SERIAL_NAME,
1096 .write = bfin_serial_console_write,
1097 .device = uart_console_device,
1098 .setup = bfin_serial_console_setup,
1099 .flags = CON_PRINTBUFFER,
1100 .index = -1,
1101 .data = &bfin_serial_reg,
1104 static int __init bfin_serial_rs_console_init(void)
1106 bfin_serial_init_ports();
1107 register_console(&bfin_serial_console);
1108 #ifdef CONFIG_KGDB_UART
1109 kgdb_entry_state = 0;
1110 init_kgdb_uart();
1111 #endif
1112 return 0;
1114 console_initcall(bfin_serial_rs_console_init);
1116 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1117 #else
1118 #define BFIN_SERIAL_CONSOLE NULL
1119 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1122 #ifdef CONFIG_EARLY_PRINTK
1123 static __init void early_serial_putc(struct uart_port *port, int ch)
1125 unsigned timeout = 0xffff;
1126 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1128 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1129 cpu_relax();
1130 UART_PUT_CHAR(uart, ch);
1133 static __init void early_serial_write(struct console *con, const char *s,
1134 unsigned int n)
1136 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1137 unsigned int i;
1139 for (i = 0; i < n; i++, s++) {
1140 if (*s == '\n')
1141 early_serial_putc(&uart->port, '\r');
1142 early_serial_putc(&uart->port, *s);
1146 static struct __init console bfin_early_serial_console = {
1147 .name = "early_BFuart",
1148 .write = early_serial_write,
1149 .device = uart_console_device,
1150 .flags = CON_PRINTBUFFER,
1151 .setup = bfin_serial_console_setup,
1152 .index = -1,
1153 .data = &bfin_serial_reg,
1156 struct console __init *bfin_earlyserial_init(unsigned int port,
1157 unsigned int cflag)
1159 struct bfin_serial_port *uart;
1160 struct ktermios t;
1162 if (port == -1 || port >= nr_ports)
1163 port = 0;
1164 bfin_serial_init_ports();
1165 bfin_early_serial_console.index = port;
1166 uart = &bfin_serial_ports[port];
1167 t.c_cflag = cflag;
1168 t.c_iflag = 0;
1169 t.c_oflag = 0;
1170 t.c_lflag = ICANON;
1171 t.c_line = port;
1172 bfin_serial_set_termios(&uart->port, &t, &t);
1173 return &bfin_early_serial_console;
1176 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1178 static struct uart_driver bfin_serial_reg = {
1179 .owner = THIS_MODULE,
1180 .driver_name = "bfin-uart",
1181 .dev_name = BFIN_SERIAL_NAME,
1182 .major = BFIN_SERIAL_MAJOR,
1183 .minor = BFIN_SERIAL_MINOR,
1184 .nr = NR_PORTS,
1185 .cons = BFIN_SERIAL_CONSOLE,
1188 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1190 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1192 if (uart)
1193 uart_suspend_port(&bfin_serial_reg, &uart->port);
1195 return 0;
1198 static int bfin_serial_resume(struct platform_device *dev)
1200 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1202 if (uart)
1203 uart_resume_port(&bfin_serial_reg, &uart->port);
1205 return 0;
1208 static int bfin_serial_probe(struct platform_device *dev)
1210 struct resource *res = dev->resource;
1211 int i;
1213 for (i = 0; i < dev->num_resources; i++, res++)
1214 if (res->flags & IORESOURCE_MEM)
1215 break;
1217 if (i < dev->num_resources) {
1218 for (i = 0; i < nr_ports; i++, res++) {
1219 if (bfin_serial_ports[i].port.mapbase != res->start)
1220 continue;
1221 bfin_serial_ports[i].port.dev = &dev->dev;
1222 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1223 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1227 return 0;
1230 static int bfin_serial_remove(struct platform_device *pdev)
1232 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1235 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1236 gpio_free(uart->cts_pin);
1237 gpio_free(uart->rts_pin);
1238 #endif
1240 platform_set_drvdata(pdev, NULL);
1242 if (uart)
1243 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1245 return 0;
1248 static struct platform_driver bfin_serial_driver = {
1249 .probe = bfin_serial_probe,
1250 .remove = bfin_serial_remove,
1251 .suspend = bfin_serial_suspend,
1252 .resume = bfin_serial_resume,
1253 .driver = {
1254 .name = "bfin-uart",
1258 static int __init bfin_serial_init(void)
1260 int ret;
1261 #ifdef CONFIG_KGDB_UART
1262 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
1263 struct ktermios t;
1264 #endif
1266 pr_info("Serial: Blackfin serial driver\n");
1268 bfin_serial_init_ports();
1270 ret = uart_register_driver(&bfin_serial_reg);
1271 if (ret == 0) {
1272 ret = platform_driver_register(&bfin_serial_driver);
1273 if (ret) {
1274 pr_debug("uart register failed\n");
1275 uart_unregister_driver(&bfin_serial_reg);
1278 #ifdef CONFIG_KGDB_UART
1279 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
1280 request_irq(uart->port.irq, bfin_serial_rx_int,
1281 IRQF_DISABLED, "BFIN_UART_RX", uart);
1282 pr_info("Request irq for kgdb uart port\n");
1283 #ifdef CONFIG_BF54x
1284 UART_SET_IER(uart, ERBFI);
1285 #else
1286 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
1287 #endif
1288 SSYNC();
1289 t.c_cflag = CS8|B57600;
1290 t.c_iflag = 0;
1291 t.c_oflag = 0;
1292 t.c_lflag = ICANON;
1293 t.c_line = CONFIG_KGDB_UART_PORT;
1294 bfin_serial_set_termios(&uart->port, &t, &t);
1296 #endif
1297 return ret;
1300 static void __exit bfin_serial_exit(void)
1302 platform_driver_unregister(&bfin_serial_driver);
1303 uart_unregister_driver(&bfin_serial_reg);
1306 module_init(bfin_serial_init);
1307 module_exit(bfin_serial_exit);
1309 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1310 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1311 MODULE_LICENSE("GPL");
1312 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);