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