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