blob: 45c32b23d736508f440f8c0f258fdb5a47380523 [file] [log] [blame]
Bryan Wu194de562007-05-06 14:50:30 -07001/*
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08002 * Blackfin On-Chip Serial Driver
Bryan Wu194de562007-05-06 14:50:30 -07003 *
Mike Frysingerd273e2012008-10-13 10:33:06 +01004 * Copyright 2006-2008 Analog Devices Inc.
Bryan Wu194de562007-05-06 14:50:30 -07005 *
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wu194de562007-05-06 14:50:30 -07007 *
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08008 * Licensed under the GPL-2 or later.
Bryan Wu194de562007-05-06 14:50:30 -07009 */
10
11#if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
12#define SUPPORT_SYSRQ
13#endif
14
15#include <linux/module.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/console.h>
19#include <linux/sysrq.h>
20#include <linux/platform_device.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial_core.h>
24
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000025#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
26 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhang474f1a62007-06-29 16:35:17 +080027#include <linux/kgdb.h>
28#include <asm/irq_regs.h>
29#endif
30
Bryan Wu194de562007-05-06 14:50:30 -070031#include <asm/gpio.h>
Bryan Wu639f6572008-08-27 10:51:02 +080032#include <mach/bfin_serial_5xx.h>
Bryan Wu194de562007-05-06 14:50:30 -070033
34#ifdef CONFIG_SERIAL_BFIN_DMA
35#include <linux/dma-mapping.h>
36#include <asm/io.h>
37#include <asm/irq.h>
38#include <asm/cacheflush.h>
39#endif
40
41/* UART name and device definitions */
42#define BFIN_SERIAL_NAME "ttyBF"
43#define BFIN_SERIAL_MAJOR 204
44#define BFIN_SERIAL_MINOR 64
45
Mike Frysingerc9607ec2008-10-13 10:33:16 +010046static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
47static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
48
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000049#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
50 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
51
52# ifndef CONFIG_SERIAL_BFIN_PIO
53# error KGDB only support UART in PIO mode.
54# endif
55
56static int kgdboc_port_line;
57static int kgdboc_break_enabled;
58#endif
Bryan Wu194de562007-05-06 14:50:30 -070059/*
60 * Setup for console. Argument comes from the menuconfig
61 */
62#define DMA_RX_XCOUNT 512
63#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
64
Sonic Zhang0aef4562008-02-29 12:08:42 +080065#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Sonic Zhangf30ac0c2008-06-19 17:46:39 +080066#define CTS_CHECK_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070067
68#ifdef CONFIG_SERIAL_BFIN_DMA
69static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
70#else
Bryan Wu194de562007-05-06 14:50:30 -070071static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070072#endif
73
74static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
75
Graf Yang80d5c472009-01-02 13:40:22 +000076static void bfin_serial_reset_irda(struct uart_port *port);
77
Bryan Wu194de562007-05-06 14:50:30 -070078/*
79 * interrupts are disabled on entry
80 */
81static void bfin_serial_stop_tx(struct uart_port *port)
82{
83 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +000084#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +080085 struct circ_buf *xmit = &uart->port.info->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +000086#endif
Bryan Wu194de562007-05-06 14:50:30 -070087
Roy Huangf4d640c92007-07-12 16:43:46 +080088 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080089 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +080090
Bryan Wu194de562007-05-06 14:50:30 -070091#ifdef CONFIG_SERIAL_BFIN_DMA
92 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080093 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
94 uart->port.icount.tx += uart->tx_count;
95 uart->tx_count = 0;
96 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070097#else
Roy Huangf4d640c92007-07-12 16:43:46 +080098#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +080099 /* Clear TFI bit */
100 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700101#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800102 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800103#endif
Bryan Wu194de562007-05-06 14:50:30 -0700104}
105
106/*
107 * port is locked and interrupts are disabled
108 */
109static void bfin_serial_start_tx(struct uart_port *port)
110{
111 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang80d5c472009-01-02 13:40:22 +0000112 struct tty_struct *tty = uart->port.info->port.tty;
113
114 /*
115 * To avoid losting RX interrupt, we reset IR function
116 * before sending data.
117 */
118 if (tty->termios->c_line == N_IRDA)
119 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700120
121#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800122 if (uart->tx_done)
123 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700124#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800125 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800126 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800127#endif
Bryan Wu194de562007-05-06 14:50:30 -0700128}
129
130/*
131 * Interrupts are enabled
132 */
133static void bfin_serial_stop_rx(struct uart_port *port)
134{
135 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000136
Roy Huangf4d640c92007-07-12 16:43:46 +0800137 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700138}
139
140/*
141 * Set the modem control timer to fire immediately.
142 */
143static void bfin_serial_enable_ms(struct uart_port *port)
144{
145}
146
Sonic Zhang474f1a62007-06-29 16:35:17 +0800147
Mike Frysinger50e2e152008-04-25 03:03:03 +0800148#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800149# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
150# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
151#else
152# define UART_GET_ANOMALY_THRESHOLD(uart) 0
153# define UART_SET_ANOMALY_THRESHOLD(uart, v)
154#endif
155
Bryan Wu194de562007-05-06 14:50:30 -0700156#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700157static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
158{
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000159 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700160 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800161 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700162
Sonic Zhang759eb042007-11-21 17:00:32 +0800163 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800164 UART_CLEAR_LSR(uart);
165
166 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700167 uart->port.icount.rx++;
168
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000169#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
170 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
171 if (kgdb_connected && kgdboc_port_line == uart->port.line)
172 if (ch == 0x3) {/* Ctrl + C */
173 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800174 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800175 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000176
177 if (!uart->port.info || !uart->port.info->tty)
178 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800179#endif
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000180 tty = uart->port.info->tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800181
Mike Frysinger50e2e152008-04-25 03:03:03 +0800182 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800183 /* The BF533 (and BF561) family of processors have a nice anomaly
184 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800185 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800186 * character comes across. Due to the nature of the flood, it is
187 * not possible to reliably catch bytes that are sent too quickly
188 * after this break. So application code talking to the Blackfin
189 * which sends a break signal must allow at least 1.5 character
190 * times after the end of the break for things to stabilize. This
191 * timeout was picked as it must absolutely be larger than 1
192 * character time +/- some percent. So 1.5 sounds good. All other
193 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800194 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800195 if (anomaly_start.tv_sec) {
196 struct timeval curr;
197 suseconds_t usecs;
198
199 if ((~ch & (~ch + 1)) & 0xff)
200 goto known_good_char;
201
202 do_gettimeofday(&curr);
203 if (curr.tv_sec - anomaly_start.tv_sec > 1)
204 goto known_good_char;
205
206 usecs = 0;
207 if (curr.tv_sec != anomaly_start.tv_sec)
208 usecs += USEC_PER_SEC;
209 usecs += curr.tv_usec - anomaly_start.tv_usec;
210
211 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
212 goto known_good_char;
213
214 if (ch)
215 anomaly_start.tv_sec = 0;
216 else
217 anomaly_start = curr;
218
219 return;
220
221 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000222 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800223 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800224 }
Bryan Wu194de562007-05-06 14:50:30 -0700225 }
Bryan Wu194de562007-05-06 14:50:30 -0700226
227 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800228 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800229 if (bfin_revid() < 5)
230 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700231 uart->port.icount.brk++;
232 if (uart_handle_break(&uart->port))
233 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800234 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800235 }
236 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700237 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800238 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700239 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800240 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700241 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800242
243 status &= uart->port.read_status_mask;
244
245 if (status & BI)
246 flg = TTY_BREAK;
247 else if (status & PE)
248 flg = TTY_PARITY;
249 else if (status & FE)
250 flg = TTY_FRAME;
251 else
Bryan Wu194de562007-05-06 14:50:30 -0700252 flg = TTY_NORMAL;
253
254 if (uart_handle_sysrq_char(&uart->port, ch))
255 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700256
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800257 uart_insert_char(&uart->port, status, OE, ch, flg);
258
259 ignore_char:
260 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700261}
262
263static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
264{
265 struct circ_buf *xmit = &uart->port.info->xmit;
266
Bryan Wu194de562007-05-06 14:50:30 -0700267 /*
268 * Check the modem control lines before
269 * transmitting anything.
270 */
271 bfin_serial_mctrl_check(uart);
272
273 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100274#ifdef CONFIG_BF54x
275 /* Clear TFI bit */
276 UART_PUT_LSR(uart, TFI);
277#endif
278 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700279 return;
280 }
281
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800282 if (uart->port.x_char) {
283 UART_PUT_CHAR(uart, uart->port.x_char);
284 uart->port.icount.tx++;
285 uart->port.x_char = 0;
286 }
287
Sonic Zhang759eb042007-11-21 17:00:32 +0800288 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
289 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
290 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
291 uart->port.icount.tx++;
292 SSYNC();
293 }
Bryan Wu194de562007-05-06 14:50:30 -0700294
295 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
296 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700297}
298
Aubrey Li5c4e4722007-05-21 18:09:38 +0800299static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
300{
301 struct bfin_serial_port *uart = dev_id;
302
Roy Huangf4d640c92007-07-12 16:43:46 +0800303 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800304 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800305 bfin_serial_rx_chars(uart);
306 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800307
Aubrey Li5c4e4722007-05-21 18:09:38 +0800308 return IRQ_HANDLED;
309}
310
311static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700312{
313 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700314
Roy Huangf4d640c92007-07-12 16:43:46 +0800315 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800316 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800317 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700318 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800319
Bryan Wu194de562007-05-06 14:50:30 -0700320 return IRQ_HANDLED;
321}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800322#endif
Bryan Wu194de562007-05-06 14:50:30 -0700323
Bryan Wu194de562007-05-06 14:50:30 -0700324#ifdef CONFIG_SERIAL_BFIN_DMA
325static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
326{
327 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700328
Bryan Wu194de562007-05-06 14:50:30 -0700329 uart->tx_done = 0;
330
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800331 /*
332 * Check the modem control lines before
333 * transmitting anything.
334 */
335 bfin_serial_mctrl_check(uart);
336
Bryan Wu194de562007-05-06 14:50:30 -0700337 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800338 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700339 uart->tx_done = 1;
340 return;
341 }
342
Sonic Zhang1b733512007-12-21 16:45:12 +0800343 if (uart->port.x_char) {
344 UART_PUT_CHAR(uart, uart->port.x_char);
345 uart->port.icount.tx++;
346 uart->port.x_char = 0;
347 }
348
Bryan Wu194de562007-05-06 14:50:30 -0700349 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
350 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
351 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
352 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
353 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
354 set_dma_config(uart->tx_dma_channel,
355 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
356 INTR_ON_BUF,
357 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800358 DATA_SIZE_8,
359 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700360 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
361 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
362 set_dma_x_modify(uart->tx_dma_channel, 1);
363 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800364
Roy Huangf4d640c92007-07-12 16:43:46 +0800365 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700366}
367
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800368static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700369{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100370 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700371 int i, flg, status;
372
373 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800374 UART_CLEAR_LSR(uart);
375
Sonic Zhang56f5de82008-02-25 15:19:09 +0800376 uart->port.icount.rx +=
377 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
378 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700379
380 if (status & BI) {
381 uart->port.icount.brk++;
382 if (uart_handle_break(&uart->port))
383 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800384 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800385 }
386 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700387 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800388 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700389 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800390 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700391 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800392
393 status &= uart->port.read_status_mask;
394
395 if (status & BI)
396 flg = TTY_BREAK;
397 else if (status & PE)
398 flg = TTY_PARITY;
399 else if (status & FE)
400 flg = TTY_FRAME;
401 else
Bryan Wu194de562007-05-06 14:50:30 -0700402 flg = TTY_NORMAL;
403
Sonic Zhang56f5de82008-02-25 15:19:09 +0800404 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
405 if (i >= UART_XMIT_SIZE)
406 i = 0;
407 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
408 uart_insert_char(&uart->port, status, OE,
409 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700410 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800411
412 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700413 tty_flip_buffer_push(tty);
414}
415
416void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
417{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100418 int x_pos, pos;
419 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000420
421 spin_lock_irqsave(&uart->port.lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700422
Sonic Zhang56f5de82008-02-25 15:19:09 +0800423 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
424 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
425 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
426 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
427 uart->rx_dma_nrows = 0;
428 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700429 if (x_pos == DMA_RX_XCOUNT)
430 x_pos = 0;
431
432 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800433 if (pos != uart->rx_dma_buf.tail) {
434 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700435 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800436 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700437 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800438
Sonic Zhang68a784c2009-01-02 13:40:38 +0000439 spin_unlock_irqrestore(&uart->port.lock, flags);
440
Sonic Zhang0a278422008-04-25 04:36:47 +0800441 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700442}
443
444static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
445{
446 struct bfin_serial_port *uart = dev_id;
447 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700448
449 spin_lock(&uart->port.lock);
450 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700451 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800452 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c92007-07-12 16:43:46 +0800453 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800454 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
455 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800456
Sonic Zhang56f5de82008-02-25 15:19:09 +0800457 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
458 uart_write_wakeup(&uart->port);
459
Sonic Zhang1b733512007-12-21 16:45:12 +0800460 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700461 }
462
463 spin_unlock(&uart->port.lock);
464 return IRQ_HANDLED;
465}
466
467static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
468{
469 struct bfin_serial_port *uart = dev_id;
470 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800471
Bryan Wu194de562007-05-06 14:50:30 -0700472 spin_lock(&uart->port.lock);
473 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
474 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000475 bfin_serial_dma_rx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700476 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800477
Bryan Wu194de562007-05-06 14:50:30 -0700478 return IRQ_HANDLED;
479}
480#endif
481
482/*
483 * Return TIOCSER_TEMT when transmitter is not busy.
484 */
485static unsigned int bfin_serial_tx_empty(struct uart_port *port)
486{
487 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
488 unsigned short lsr;
489
490 lsr = UART_GET_LSR(uart);
491 if (lsr & TEMT)
492 return TIOCSER_TEMT;
493 else
494 return 0;
495}
496
497static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
498{
499#ifdef CONFIG_SERIAL_BFIN_CTSRTS
500 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
501 if (uart->cts_pin < 0)
502 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
503
Sonic Zhang1feaa512008-06-03 12:19:45 +0800504 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700505 return TIOCM_DSR | TIOCM_CAR;
506 else
507#endif
508 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
509}
510
511static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
512{
513#ifdef CONFIG_SERIAL_BFIN_CTSRTS
514 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
515 if (uart->rts_pin < 0)
516 return;
517
518 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800519 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700520 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800521 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700522#endif
523}
524
525/*
526 * Handle any change of modem status signal since we were last called.
527 */
528static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
529{
530#ifdef CONFIG_SERIAL_BFIN_CTSRTS
531 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700532 struct uart_info *info = uart->port.info;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100533 struct tty_struct *tty = info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700534
535 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800536 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700537 if (!(status & TIOCM_CTS)) {
538 tty->hw_stopped = 1;
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800539 uart->cts_timer.data = (unsigned long)(uart);
540 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
541 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
542 add_timer(&(uart->cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700543 } else {
544 tty->hw_stopped = 0;
545 }
Bryan Wu194de562007-05-06 14:50:30 -0700546#endif
547}
548
549/*
550 * Interrupts are always disabled.
551 */
552static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
553{
Mike Frysingercf686762007-06-11 16:12:49 +0800554 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
555 u16 lcr = UART_GET_LCR(uart);
556 if (break_state)
557 lcr |= SB;
558 else
559 lcr &= ~SB;
560 UART_PUT_LCR(uart, lcr);
561 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700562}
563
564static int bfin_serial_startup(struct uart_port *port)
565{
566 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
567
568#ifdef CONFIG_SERIAL_BFIN_DMA
569 dma_addr_t dma_handle;
570
571 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
572 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
573 return -EBUSY;
574 }
575
576 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
577 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
578 free_dma(uart->rx_dma_channel);
579 return -EBUSY;
580 }
581
582 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
583 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
584
585 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
586 uart->rx_dma_buf.head = 0;
587 uart->rx_dma_buf.tail = 0;
588 uart->rx_dma_nrows = 0;
589
590 set_dma_config(uart->rx_dma_channel,
591 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
592 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800593 DATA_SIZE_8,
594 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700595 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
596 set_dma_x_modify(uart->rx_dma_channel, 1);
597 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
598 set_dma_y_modify(uart->rx_dma_channel, 1);
599 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
600 enable_dma(uart->rx_dma_channel);
601
602 uart->rx_dma_timer.data = (unsigned long)(uart);
603 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
604 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
605 add_timer(&(uart->rx_dma_timer));
606#else
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000607#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
608 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
609 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
610 kgdboc_break_enabled = 0;
611 else {
612# endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800613 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700614 "BFIN_UART_RX", uart)) {
615 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
616 return -EBUSY;
617 }
618
619 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800620 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700621 "BFIN_UART_TX", uart)) {
622 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
623 free_irq(uart->port.irq, uart);
624 return -EBUSY;
625 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100626
627# ifdef CONFIG_BF54x
628 {
629 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
630
631 switch (uart->port.irq) {
632 case IRQ_UART3_RX:
633 uart_dma_ch_rx = CH_UART3_RX;
634 uart_dma_ch_tx = CH_UART3_TX;
635 break;
636 case IRQ_UART2_RX:
637 uart_dma_ch_rx = CH_UART2_RX;
638 uart_dma_ch_tx = CH_UART2_TX;
639 break;
640 default:
641 uart_dma_ch_rx = uart_dma_ch_tx = 0;
642 break;
643 };
644
645 if (uart_dma_ch_rx &&
646 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
647 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
648 free_irq(uart->port.irq, uart);
649 free_irq(uart->port.irq + 1, uart);
650 return -EBUSY;
651 }
652 if (uart_dma_ch_tx &&
653 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
654 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
655 free_dma(uart_dma_ch_rx);
656 free_irq(uart->port.irq, uart);
657 free_irq(uart->port.irq + 1, uart);
658 return -EBUSY;
659 }
660 }
661# endif
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000662#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
663 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
664 }
665# endif
Bryan Wu194de562007-05-06 14:50:30 -0700666#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800667 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700668 return 0;
669}
670
671static void bfin_serial_shutdown(struct uart_port *port)
672{
673 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
674
675#ifdef CONFIG_SERIAL_BFIN_DMA
676 disable_dma(uart->tx_dma_channel);
677 free_dma(uart->tx_dma_channel);
678 disable_dma(uart->rx_dma_channel);
679 free_dma(uart->rx_dma_channel);
680 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800681 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700682#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100683#ifdef CONFIG_BF54x
684 switch (uart->port.irq) {
685 case IRQ_UART3_RX:
686 free_dma(CH_UART3_RX);
687 free_dma(CH_UART3_TX);
688 break;
689 case IRQ_UART2_RX:
690 free_dma(CH_UART2_RX);
691 free_dma(CH_UART2_TX);
692 break;
693 default:
694 break;
695 };
696#endif
Bryan Wu194de562007-05-06 14:50:30 -0700697 free_irq(uart->port.irq, uart);
698 free_irq(uart->port.irq+1, uart);
699#endif
700}
701
702static void
703bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
704 struct ktermios *old)
705{
706 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
707 unsigned long flags;
708 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800709 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700710
711 switch (termios->c_cflag & CSIZE) {
712 case CS8:
713 lcr = WLS(8);
714 break;
715 case CS7:
716 lcr = WLS(7);
717 break;
718 case CS6:
719 lcr = WLS(6);
720 break;
721 case CS5:
722 lcr = WLS(5);
723 break;
724 default:
725 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700726 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700727 }
728
729 if (termios->c_cflag & CSTOPB)
730 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800731 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700732 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800733 if (!(termios->c_cflag & PARODD))
734 lcr |= EPS;
735 if (termios->c_cflag & CMSPAR)
736 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700737
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800738 port->read_status_mask = OE;
739 if (termios->c_iflag & INPCK)
740 port->read_status_mask |= (FE | PE);
741 if (termios->c_iflag & (BRKINT | PARMRK))
742 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700743
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800744 /*
745 * Characters to ignore
746 */
747 port->ignore_status_mask = 0;
748 if (termios->c_iflag & IGNPAR)
749 port->ignore_status_mask |= FE | PE;
750 if (termios->c_iflag & IGNBRK) {
751 port->ignore_status_mask |= BI;
752 /*
753 * If we're ignoring parity and break indicators,
754 * ignore overruns too (for real raw support).
755 */
756 if (termios->c_iflag & IGNPAR)
757 port->ignore_status_mask |= OE;
758 }
Bryan Wu194de562007-05-06 14:50:30 -0700759
760 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
761 quot = uart_get_divisor(port, baud);
762 spin_lock_irqsave(&uart->port.lock, flags);
763
Mike Frysinger8851c712007-12-24 19:48:04 +0800764 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
765
Bryan Wu194de562007-05-06 14:50:30 -0700766 /* Disable UART */
767 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800768 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700769
770 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800771 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700772
773 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700774 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
775 SSYNC();
776
777 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800778 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700779
780 UART_PUT_LCR(uart, lcr);
781
782 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800783 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700784
785 val = UART_GET_GCTL(uart);
786 val |= UCEN;
787 UART_PUT_GCTL(uart, val);
788
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100789 /* Port speed changed, update the per-port timeout. */
790 uart_update_timeout(port, termios->c_cflag, baud);
791
Bryan Wu194de562007-05-06 14:50:30 -0700792 spin_unlock_irqrestore(&uart->port.lock, flags);
793}
794
795static const char *bfin_serial_type(struct uart_port *port)
796{
797 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
798
799 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
800}
801
802/*
803 * Release the memory region(s) being used by 'port'.
804 */
805static void bfin_serial_release_port(struct uart_port *port)
806{
807}
808
809/*
810 * Request the memory region(s) being used by 'port'.
811 */
812static int bfin_serial_request_port(struct uart_port *port)
813{
814 return 0;
815}
816
817/*
818 * Configure/autoconfigure the port.
819 */
820static void bfin_serial_config_port(struct uart_port *port, int flags)
821{
822 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
823
824 if (flags & UART_CONFIG_TYPE &&
825 bfin_serial_request_port(&uart->port) == 0)
826 uart->port.type = PORT_BFIN;
827}
828
829/*
830 * Verify the new serial_struct (for TIOCSSERIAL).
831 * The only change we allow are to the flags and type, and
832 * even then only between PORT_BFIN and PORT_UNKNOWN
833 */
834static int
835bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
836{
837 return 0;
838}
839
Graf Yang7d01b472008-02-29 11:31:08 +0800840/*
841 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
842 * In other cases, disable IrDA function.
843 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800844static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800845{
Graf Yang3b8458a2008-06-07 15:36:33 +0800846 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800847 unsigned short val;
848
Takashi Iwaia88487c2008-07-16 21:54:42 +0100849 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800850 return;
851
Alan Coxb1cbefe2008-08-04 17:22:11 +0100852 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800853 case N_IRDA:
854 val = UART_GET_GCTL(&bfin_serial_ports[line]);
855 val |= (IREN | RPOLC);
856 UART_PUT_GCTL(&bfin_serial_ports[line], val);
857 break;
858 default:
859 val = UART_GET_GCTL(&bfin_serial_ports[line]);
860 val &= ~(IREN | RPOLC);
861 UART_PUT_GCTL(&bfin_serial_ports[line], val);
862 }
863}
864
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000865#ifdef CONFIG_CONSOLE_POLL
866static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
867{
868 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
869
870 while (!(UART_GET_LSR(uart) & THRE))
871 cpu_relax();
872
873 UART_CLEAR_DLAB(uart);
874 UART_PUT_CHAR(uart, (unsigned char)chr);
875}
876
877static int bfin_serial_poll_get_char(struct uart_port *port)
878{
879 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
880 unsigned char chr;
881
882 while (!(UART_GET_LSR(uart) & DR))
883 cpu_relax();
884
885 UART_CLEAR_DLAB(uart);
886 chr = UART_GET_CHAR(uart);
887
888 return chr;
889}
890#endif
891
892#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
893 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
894static void bfin_kgdboc_port_shutdown(struct uart_port *port)
895{
896 if (kgdboc_break_enabled) {
897 kgdboc_break_enabled = 0;
898 bfin_serial_shutdown(port);
899 }
900}
901
902static int bfin_kgdboc_port_startup(struct uart_port *port)
903{
904 kgdboc_port_line = port->line;
905 kgdboc_break_enabled = !bfin_serial_startup(port);
906 return 0;
907}
908#endif
909
Graf Yang80d5c472009-01-02 13:40:22 +0000910static void bfin_serial_reset_irda(struct uart_port *port)
911{
912 int line = port->line;
913 unsigned short val;
914
915 val = UART_GET_GCTL(&bfin_serial_ports[line]);
916 val &= ~(IREN | RPOLC);
917 UART_PUT_GCTL(&bfin_serial_ports[line], val);
918 SSYNC();
919 val |= (IREN | RPOLC);
920 UART_PUT_GCTL(&bfin_serial_ports[line], val);
921 SSYNC();
922}
923
Bryan Wu194de562007-05-06 14:50:30 -0700924static struct uart_ops bfin_serial_pops = {
925 .tx_empty = bfin_serial_tx_empty,
926 .set_mctrl = bfin_serial_set_mctrl,
927 .get_mctrl = bfin_serial_get_mctrl,
928 .stop_tx = bfin_serial_stop_tx,
929 .start_tx = bfin_serial_start_tx,
930 .stop_rx = bfin_serial_stop_rx,
931 .enable_ms = bfin_serial_enable_ms,
932 .break_ctl = bfin_serial_break_ctl,
933 .startup = bfin_serial_startup,
934 .shutdown = bfin_serial_shutdown,
935 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +0800936 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -0700937 .type = bfin_serial_type,
938 .release_port = bfin_serial_release_port,
939 .request_port = bfin_serial_request_port,
940 .config_port = bfin_serial_config_port,
941 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000942#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
943 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
944 .kgdboc_port_startup = bfin_kgdboc_port_startup,
945 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
946#endif
947#ifdef CONFIG_CONSOLE_POLL
948 .poll_put_char = bfin_serial_poll_put_char,
949 .poll_get_char = bfin_serial_poll_get_char,
950#endif
Bryan Wu194de562007-05-06 14:50:30 -0700951};
952
953static void __init bfin_serial_init_ports(void)
954{
955 static int first = 1;
956 int i;
957
958 if (!first)
959 return;
960 first = 0;
961
Mike Frysingerc9607ec2008-10-13 10:33:16 +0100962 for (i = 0; i < nr_active_ports; i++) {
Bryan Wu194de562007-05-06 14:50:30 -0700963 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100964 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -0700965 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
966 bfin_serial_ports[i].port.line = i;
967 bfin_serial_ports[i].port.iotype = UPIO_MEM;
968 bfin_serial_ports[i].port.membase =
969 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
970 bfin_serial_ports[i].port.mapbase =
971 bfin_serial_resource[i].uart_base_addr;
972 bfin_serial_ports[i].port.irq =
973 bfin_serial_resource[i].uart_irq;
974 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
975#ifdef CONFIG_SERIAL_BFIN_DMA
976 bfin_serial_ports[i].tx_done = 1;
977 bfin_serial_ports[i].tx_count = 0;
978 bfin_serial_ports[i].tx_dma_channel =
979 bfin_serial_resource[i].uart_tx_dma_channel;
980 bfin_serial_ports[i].rx_dma_channel =
981 bfin_serial_resource[i].uart_rx_dma_channel;
982 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700983#endif
984#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800985 init_timer(&(bfin_serial_ports[i].cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700986 bfin_serial_ports[i].cts_pin =
987 bfin_serial_resource[i].uart_cts_pin;
988 bfin_serial_ports[i].rts_pin =
989 bfin_serial_resource[i].uart_rts_pin;
990#endif
991 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700992 }
Roy Huangf4d640c92007-07-12 16:43:46 +0800993
Bryan Wu194de562007-05-06 14:50:30 -0700994}
995
Sonic Zhangb6efa1e2009-01-02 13:40:31 +0000996#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -0700997/*
998 * If the port was already initialised (eg, by a boot loader),
999 * try to determine the current setup.
1000 */
1001static void __init
1002bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1003 int *parity, int *bits)
1004{
1005 unsigned short status;
1006
1007 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1008 if (status == (ERBFI | ETBEI)) {
1009 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001010 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001011
1012 lcr = UART_GET_LCR(uart);
1013
1014 *parity = 'n';
1015 if (lcr & PEN) {
1016 if (lcr & EPS)
1017 *parity = 'e';
1018 else
1019 *parity = 'o';
1020 }
1021 switch (lcr & 0x03) {
1022 case 0: *bits = 5; break;
1023 case 1: *bits = 6; break;
1024 case 2: *bits = 7; break;
1025 case 3: *bits = 8; break;
1026 }
1027 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001028 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001029
1030 dll = UART_GET_DLL(uart);
1031 dlh = UART_GET_DLH(uart);
1032
1033 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001034 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001035
1036 *baud = get_sclk() / (16*(dll | dlh << 8));
1037 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001038 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001039}
Robin Getz0ae53642007-10-09 17:24:49 +08001040
Robin Getz0ae53642007-10-09 17:24:49 +08001041static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001042
1043static int __init
1044bfin_serial_console_setup(struct console *co, char *options)
1045{
1046 struct bfin_serial_port *uart;
1047 int baud = 57600;
1048 int bits = 8;
1049 int parity = 'n';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001050# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -07001051 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001052# else
Bryan Wu194de562007-05-06 14:50:30 -07001053 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001054# endif
Bryan Wu194de562007-05-06 14:50:30 -07001055
1056 /*
1057 * Check whether an invalid uart number has been specified, and
1058 * if so, search for the first available port that does have
1059 * console support.
1060 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001061 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -07001062 co->index = 0;
1063 uart = &bfin_serial_ports[co->index];
1064
1065 if (options)
1066 uart_parse_options(options, &baud, &parity, &bits, &flow);
1067 else
1068 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1069
1070 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001071}
1072#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1073 defined (CONFIG_EARLY_PRINTK) */
1074
1075#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1076static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1077{
1078 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1079 while (!(UART_GET_LSR(uart) & THRE))
1080 barrier();
1081 UART_PUT_CHAR(uart, ch);
1082 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -07001083}
1084
Robin Getz0ae53642007-10-09 17:24:49 +08001085/*
1086 * Interrupts are disabled on entering
1087 */
1088static void
1089bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1090{
1091 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
Mike Frysinger59e4e3e2009-04-06 17:32:28 +01001092 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001093
1094 spin_lock_irqsave(&uart->port.lock, flags);
1095 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1096 spin_unlock_irqrestore(&uart->port.lock, flags);
1097
1098}
1099
Bryan Wu194de562007-05-06 14:50:30 -07001100static struct console bfin_serial_console = {
1101 .name = BFIN_SERIAL_NAME,
1102 .write = bfin_serial_console_write,
1103 .device = uart_console_device,
1104 .setup = bfin_serial_console_setup,
1105 .flags = CON_PRINTBUFFER,
1106 .index = -1,
1107 .data = &bfin_serial_reg,
1108};
1109
1110static int __init bfin_serial_rs_console_init(void)
1111{
1112 bfin_serial_init_ports();
1113 register_console(&bfin_serial_console);
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001114
Bryan Wu194de562007-05-06 14:50:30 -07001115 return 0;
1116}
1117console_initcall(bfin_serial_rs_console_init);
1118
1119#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1120#else
1121#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001122#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1123
1124
1125#ifdef CONFIG_EARLY_PRINTK
1126static __init void early_serial_putc(struct uart_port *port, int ch)
1127{
1128 unsigned timeout = 0xffff;
1129 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1130
1131 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1132 cpu_relax();
1133 UART_PUT_CHAR(uart, ch);
1134}
1135
1136static __init void early_serial_write(struct console *con, const char *s,
1137 unsigned int n)
1138{
1139 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1140 unsigned int i;
1141
1142 for (i = 0; i < n; i++, s++) {
1143 if (*s == '\n')
1144 early_serial_putc(&uart->port, '\r');
1145 early_serial_putc(&uart->port, *s);
1146 }
1147}
1148
Mike Frysingerc1113402008-10-13 10:32:35 +01001149static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001150 .name = "early_BFuart",
1151 .write = early_serial_write,
1152 .device = uart_console_device,
1153 .flags = CON_PRINTBUFFER,
1154 .setup = bfin_serial_console_setup,
1155 .index = -1,
1156 .data = &bfin_serial_reg,
1157};
1158
1159struct console __init *bfin_earlyserial_init(unsigned int port,
1160 unsigned int cflag)
1161{
1162 struct bfin_serial_port *uart;
1163 struct ktermios t;
1164
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001165 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001166 port = 0;
1167 bfin_serial_init_ports();
1168 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001169 uart = &bfin_serial_ports[port];
1170 t.c_cflag = cflag;
1171 t.c_iflag = 0;
1172 t.c_oflag = 0;
1173 t.c_lflag = ICANON;
1174 t.c_line = port;
1175 bfin_serial_set_termios(&uart->port, &t, &t);
1176 return &bfin_early_serial_console;
1177}
1178
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001179#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001180
1181static struct uart_driver bfin_serial_reg = {
1182 .owner = THIS_MODULE,
1183 .driver_name = "bfin-uart",
1184 .dev_name = BFIN_SERIAL_NAME,
1185 .major = BFIN_SERIAL_MAJOR,
1186 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001187 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001188 .cons = BFIN_SERIAL_CONSOLE,
1189};
1190
1191static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1192{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001193 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001194
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001195 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001196 if (bfin_serial_ports[i].port.dev != &dev->dev)
1197 continue;
1198 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1199 }
Bryan Wu194de562007-05-06 14:50:30 -07001200
1201 return 0;
1202}
1203
1204static int bfin_serial_resume(struct platform_device *dev)
1205{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001206 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001207
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001208 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001209 if (bfin_serial_ports[i].port.dev != &dev->dev)
1210 continue;
1211 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1212 }
Bryan Wu194de562007-05-06 14:50:30 -07001213
1214 return 0;
1215}
1216
1217static int bfin_serial_probe(struct platform_device *dev)
1218{
1219 struct resource *res = dev->resource;
1220 int i;
1221
1222 for (i = 0; i < dev->num_resources; i++, res++)
1223 if (res->flags & IORESOURCE_MEM)
1224 break;
1225
1226 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001227 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001228 if (bfin_serial_ports[i].port.mapbase != res->start)
1229 continue;
1230 bfin_serial_ports[i].port.dev = &dev->dev;
1231 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001232 }
1233 }
1234
1235 return 0;
1236}
1237
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001238static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001239{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001240 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001241
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001242 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001243 if (bfin_serial_ports[i].port.dev != &dev->dev)
1244 continue;
1245 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1246 bfin_serial_ports[i].port.dev = NULL;
Bryan Wu194de562007-05-06 14:50:30 -07001247#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001248 gpio_free(bfin_serial_ports[i].cts_pin);
1249 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001250#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001251 }
Bryan Wu194de562007-05-06 14:50:30 -07001252
1253 return 0;
1254}
1255
1256static struct platform_driver bfin_serial_driver = {
1257 .probe = bfin_serial_probe,
1258 .remove = bfin_serial_remove,
1259 .suspend = bfin_serial_suspend,
1260 .resume = bfin_serial_resume,
1261 .driver = {
1262 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001263 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001264 },
1265};
1266
1267static int __init bfin_serial_init(void)
1268{
1269 int ret;
1270
1271 pr_info("Serial: Blackfin serial driver\n");
1272
1273 bfin_serial_init_ports();
1274
1275 ret = uart_register_driver(&bfin_serial_reg);
1276 if (ret == 0) {
1277 ret = platform_driver_register(&bfin_serial_driver);
1278 if (ret) {
1279 pr_debug("uart register failed\n");
1280 uart_unregister_driver(&bfin_serial_reg);
1281 }
1282 }
Bryan Wu194de562007-05-06 14:50:30 -07001283 return ret;
1284}
1285
1286static void __exit bfin_serial_exit(void)
1287{
1288 platform_driver_unregister(&bfin_serial_driver);
1289 uart_unregister_driver(&bfin_serial_reg);
1290}
1291
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001292
Bryan Wu194de562007-05-06 14:50:30 -07001293module_init(bfin_serial_init);
1294module_exit(bfin_serial_exit);
1295
1296MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1297MODULE_DESCRIPTION("Blackfin generic serial port driver");
1298MODULE_LICENSE("GPL");
1299MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001300MODULE_ALIAS("platform:bfin-uart");