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