blob: 88449d36a3f751a73ee96c5102529240e395cdf3 [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 Zhang52e15f02009-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 Zhang52e15f02009-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 Zhang0711d852008-02-25 15:16:50 +080084 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -070085
Roy Huangf4d640c2007-07-12 16:43:46 +080086 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080087 cpu_relax();
Roy Huangf4d640c2007-07-12 16:43:46 +080088
Bryan Wu194de562007-05-06 14:50:30 -070089#ifdef CONFIG_SERIAL_BFIN_DMA
90 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080091 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
92 uart->port.icount.tx += uart->tx_count;
93 uart->tx_count = 0;
94 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070095#else
Roy Huangf4d640c2007-07-12 16:43:46 +080096#ifdef CONFIG_BF54x
Roy Huangf4d640c2007-07-12 16:43:46 +080097 /* Clear TFI bit */
98 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -070099#endif
Mike Frysinger89bf6dc2008-05-07 11:41:26 +0800100 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c2007-07-12 16:43:46 +0800101#endif
Bryan Wu194de562007-05-06 14:50:30 -0700102}
103
104/*
105 * port is locked and interrupts are disabled
106 */
107static void bfin_serial_start_tx(struct uart_port *port)
108{
109 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang80d5c472009-01-02 13:40:22 +0000110 struct tty_struct *tty = uart->port.info->port.tty;
111
112 /*
113 * To avoid losting RX interrupt, we reset IR function
114 * before sending data.
115 */
116 if (tty->termios->c_line == N_IRDA)
117 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700118
119#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800120 if (uart->tx_done)
121 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700122#else
Roy Huangf4d640c2007-07-12 16:43:46 +0800123 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800124 bfin_serial_tx_chars(uart);
Roy Huangf4d640c2007-07-12 16:43:46 +0800125#endif
Bryan Wu194de562007-05-06 14:50:30 -0700126}
127
128/*
129 * Interrupts are enabled
130 */
131static void bfin_serial_stop_rx(struct uart_port *port)
132{
133 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f02009-01-02 13:40:14 +0000134
Roy Huangf4d640c2007-07-12 16:43:46 +0800135 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700136}
137
138/*
139 * Set the modem control timer to fire immediately.
140 */
141static void bfin_serial_enable_ms(struct uart_port *port)
142{
143}
144
Sonic Zhang474f1a62007-06-29 16:35:17 +0800145
Mike Frysinger50e2e152008-04-25 03:03:03 +0800146#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800147# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
148# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
149#else
150# define UART_GET_ANOMALY_THRESHOLD(uart) 0
151# define UART_SET_ANOMALY_THRESHOLD(uart, v)
152#endif
153
Bryan Wu194de562007-05-06 14:50:30 -0700154#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700155static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
156{
Sonic Zhang52e15f02009-01-02 13:40:14 +0000157 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700158 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800159 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700160
Sonic Zhang759eb042007-11-21 17:00:32 +0800161 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800162 UART_CLEAR_LSR(uart);
163
164 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700165 uart->port.icount.rx++;
166
Sonic Zhang52e15f02009-01-02 13:40:14 +0000167#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
168 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
169 if (kgdb_connected && kgdboc_port_line == uart->port.line)
170 if (ch == 0x3) {/* Ctrl + C */
171 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800172 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800173 }
Sonic Zhang52e15f02009-01-02 13:40:14 +0000174
175 if (!uart->port.info || !uart->port.info->tty)
176 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800177#endif
Sonic Zhang52e15f02009-01-02 13:40:14 +0000178 tty = uart->port.info->tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800179
Mike Frysinger50e2e152008-04-25 03:03:03 +0800180 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800181 /* The BF533 (and BF561) family of processors have a nice anomaly
182 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800183 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800184 * character comes across. Due to the nature of the flood, it is
185 * not possible to reliably catch bytes that are sent too quickly
186 * after this break. So application code talking to the Blackfin
187 * which sends a break signal must allow at least 1.5 character
188 * times after the end of the break for things to stabilize. This
189 * timeout was picked as it must absolutely be larger than 1
190 * character time +/- some percent. So 1.5 sounds good. All other
191 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800192 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800193 if (anomaly_start.tv_sec) {
194 struct timeval curr;
195 suseconds_t usecs;
196
197 if ((~ch & (~ch + 1)) & 0xff)
198 goto known_good_char;
199
200 do_gettimeofday(&curr);
201 if (curr.tv_sec - anomaly_start.tv_sec > 1)
202 goto known_good_char;
203
204 usecs = 0;
205 if (curr.tv_sec != anomaly_start.tv_sec)
206 usecs += USEC_PER_SEC;
207 usecs += curr.tv_usec - anomaly_start.tv_usec;
208
209 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
210 goto known_good_char;
211
212 if (ch)
213 anomaly_start.tv_sec = 0;
214 else
215 anomaly_start = curr;
216
217 return;
218
219 known_good_char:
220 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800221 }
Bryan Wu194de562007-05-06 14:50:30 -0700222 }
Bryan Wu194de562007-05-06 14:50:30 -0700223
224 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800225 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800226 if (bfin_revid() < 5)
227 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700228 uart->port.icount.brk++;
229 if (uart_handle_break(&uart->port))
230 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800231 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800232 }
233 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700234 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800235 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700236 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800237 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700238 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800239
240 status &= uart->port.read_status_mask;
241
242 if (status & BI)
243 flg = TTY_BREAK;
244 else if (status & PE)
245 flg = TTY_PARITY;
246 else if (status & FE)
247 flg = TTY_FRAME;
248 else
Bryan Wu194de562007-05-06 14:50:30 -0700249 flg = TTY_NORMAL;
250
251 if (uart_handle_sysrq_char(&uart->port, ch))
252 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700253
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800254 uart_insert_char(&uart->port, status, OE, ch, flg);
255
256 ignore_char:
257 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700258}
259
260static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
261{
262 struct circ_buf *xmit = &uart->port.info->xmit;
263
Bryan Wu194de562007-05-06 14:50:30 -0700264 /*
265 * Check the modem control lines before
266 * transmitting anything.
267 */
268 bfin_serial_mctrl_check(uart);
269
270 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea2008-10-13 10:33:33 +0100271#ifdef CONFIG_BF54x
272 /* Clear TFI bit */
273 UART_PUT_LSR(uart, TFI);
274#endif
275 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700276 return;
277 }
278
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800279 if (uart->port.x_char) {
280 UART_PUT_CHAR(uart, uart->port.x_char);
281 uart->port.icount.tx++;
282 uart->port.x_char = 0;
283 }
284
Sonic Zhang759eb042007-11-21 17:00:32 +0800285 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
286 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
287 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
288 uart->port.icount.tx++;
289 SSYNC();
290 }
Bryan Wu194de562007-05-06 14:50:30 -0700291
292 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
293 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700294}
295
Aubrey Li5c4e4722007-05-21 18:09:38 +0800296static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
297{
298 struct bfin_serial_port *uart = dev_id;
299
Roy Huangf4d640c2007-07-12 16:43:46 +0800300 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800301 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800302 bfin_serial_rx_chars(uart);
303 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800304
Aubrey Li5c4e4722007-05-21 18:09:38 +0800305 return IRQ_HANDLED;
306}
307
308static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700309{
310 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700311
Roy Huangf4d640c2007-07-12 16:43:46 +0800312 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800313 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800314 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700315 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800316
Bryan Wu194de562007-05-06 14:50:30 -0700317 return IRQ_HANDLED;
318}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800319#endif
Bryan Wu194de562007-05-06 14:50:30 -0700320
Bryan Wu194de562007-05-06 14:50:30 -0700321#ifdef CONFIG_SERIAL_BFIN_DMA
322static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
323{
324 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700325
Bryan Wu194de562007-05-06 14:50:30 -0700326 uart->tx_done = 0;
327
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800328 /*
329 * Check the modem control lines before
330 * transmitting anything.
331 */
332 bfin_serial_mctrl_check(uart);
333
Bryan Wu194de562007-05-06 14:50:30 -0700334 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800335 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700336 uart->tx_done = 1;
337 return;
338 }
339
Sonic Zhang1b733512007-12-21 16:45:12 +0800340 if (uart->port.x_char) {
341 UART_PUT_CHAR(uart, uart->port.x_char);
342 uart->port.icount.tx++;
343 uart->port.x_char = 0;
344 }
345
Bryan Wu194de562007-05-06 14:50:30 -0700346 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
347 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
348 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
349 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
350 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
351 set_dma_config(uart->tx_dma_channel,
352 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
353 INTR_ON_BUF,
354 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800355 DATA_SIZE_8,
356 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700357 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
358 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
359 set_dma_x_modify(uart->tx_dma_channel, 1);
360 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800361
Roy Huangf4d640c2007-07-12 16:43:46 +0800362 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700363}
364
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800365static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700366{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100367 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700368 int i, flg, status;
369
370 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800371 UART_CLEAR_LSR(uart);
372
Sonic Zhang56f5de82008-02-25 15:19:09 +0800373 uart->port.icount.rx +=
374 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
375 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700376
377 if (status & BI) {
378 uart->port.icount.brk++;
379 if (uart_handle_break(&uart->port))
380 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800381 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800382 }
383 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700384 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800385 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700386 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800387 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700388 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800389
390 status &= uart->port.read_status_mask;
391
392 if (status & BI)
393 flg = TTY_BREAK;
394 else if (status & PE)
395 flg = TTY_PARITY;
396 else if (status & FE)
397 flg = TTY_FRAME;
398 else
Bryan Wu194de562007-05-06 14:50:30 -0700399 flg = TTY_NORMAL;
400
Sonic Zhang56f5de82008-02-25 15:19:09 +0800401 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
402 if (i >= UART_XMIT_SIZE)
403 i = 0;
404 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
405 uart_insert_char(&uart->port, status, OE,
406 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700407 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800408
409 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700410 tty_flip_buffer_push(tty);
411}
412
413void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
414{
415 int x_pos, pos;
Bryan Wu194de562007-05-06 14:50:30 -0700416
Sonic Zhang56f5de82008-02-25 15:19:09 +0800417 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
418 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
419 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
420 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
421 uart->rx_dma_nrows = 0;
422 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700423 if (x_pos == DMA_RX_XCOUNT)
424 x_pos = 0;
425
426 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800427 if (pos != uart->rx_dma_buf.tail) {
428 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700429 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800430 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700431 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800432
Sonic Zhang0a278422008-04-25 04:36:47 +0800433 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700434}
435
436static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
437{
438 struct bfin_serial_port *uart = dev_id;
439 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700440
441 spin_lock(&uart->port.lock);
442 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700443 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800444 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c2007-07-12 16:43:46 +0800445 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800446 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
447 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800448
Sonic Zhang56f5de82008-02-25 15:19:09 +0800449 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
450 uart_write_wakeup(&uart->port);
451
Sonic Zhang1b733512007-12-21 16:45:12 +0800452 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700453 }
454
455 spin_unlock(&uart->port.lock);
456 return IRQ_HANDLED;
457}
458
459static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
460{
461 struct bfin_serial_port *uart = dev_id;
462 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800463
Bryan Wu194de562007-05-06 14:50:30 -0700464 spin_lock(&uart->port.lock);
465 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
466 clear_dma_irqstat(uart->rx_dma_channel);
Bryan Wu194de562007-05-06 14:50:30 -0700467 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800468
Sonic Zhang0a278422008-04-25 04:36:47 +0800469 mod_timer(&(uart->rx_dma_timer), jiffies);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800470
Bryan Wu194de562007-05-06 14:50:30 -0700471 return IRQ_HANDLED;
472}
473#endif
474
475/*
476 * Return TIOCSER_TEMT when transmitter is not busy.
477 */
478static unsigned int bfin_serial_tx_empty(struct uart_port *port)
479{
480 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
481 unsigned short lsr;
482
483 lsr = UART_GET_LSR(uart);
484 if (lsr & TEMT)
485 return TIOCSER_TEMT;
486 else
487 return 0;
488}
489
490static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
491{
492#ifdef CONFIG_SERIAL_BFIN_CTSRTS
493 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
494 if (uart->cts_pin < 0)
495 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
496
Sonic Zhang1feaa512008-06-03 12:19:45 +0800497 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700498 return TIOCM_DSR | TIOCM_CAR;
499 else
500#endif
501 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
502}
503
504static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
505{
506#ifdef CONFIG_SERIAL_BFIN_CTSRTS
507 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
508 if (uart->rts_pin < 0)
509 return;
510
511 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800512 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700513 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800514 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700515#endif
516}
517
518/*
519 * Handle any change of modem status signal since we were last called.
520 */
521static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
522{
523#ifdef CONFIG_SERIAL_BFIN_CTSRTS
524 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700525 struct uart_info *info = uart->port.info;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100526 struct tty_struct *tty = info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700527
528 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800529 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700530 if (!(status & TIOCM_CTS)) {
531 tty->hw_stopped = 1;
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800532 uart->cts_timer.data = (unsigned long)(uart);
533 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
534 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
535 add_timer(&(uart->cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700536 } else {
537 tty->hw_stopped = 0;
538 }
Bryan Wu194de562007-05-06 14:50:30 -0700539#endif
540}
541
542/*
543 * Interrupts are always disabled.
544 */
545static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
546{
Mike Frysingercf686762007-06-11 16:12:49 +0800547 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
548 u16 lcr = UART_GET_LCR(uart);
549 if (break_state)
550 lcr |= SB;
551 else
552 lcr &= ~SB;
553 UART_PUT_LCR(uart, lcr);
554 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700555}
556
557static int bfin_serial_startup(struct uart_port *port)
558{
559 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
560
561#ifdef CONFIG_SERIAL_BFIN_DMA
562 dma_addr_t dma_handle;
563
564 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
565 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
566 return -EBUSY;
567 }
568
569 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
570 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
571 free_dma(uart->rx_dma_channel);
572 return -EBUSY;
573 }
574
575 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
576 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
577
578 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
579 uart->rx_dma_buf.head = 0;
580 uart->rx_dma_buf.tail = 0;
581 uart->rx_dma_nrows = 0;
582
583 set_dma_config(uart->rx_dma_channel,
584 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
585 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800586 DATA_SIZE_8,
587 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700588 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
589 set_dma_x_modify(uart->rx_dma_channel, 1);
590 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
591 set_dma_y_modify(uart->rx_dma_channel, 1);
592 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
593 enable_dma(uart->rx_dma_channel);
594
595 uart->rx_dma_timer.data = (unsigned long)(uart);
596 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
597 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
598 add_timer(&(uart->rx_dma_timer));
599#else
Sonic Zhang52e15f02009-01-02 13:40:14 +0000600#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
601 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
602 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
603 kgdboc_break_enabled = 0;
604 else {
605# endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800606 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700607 "BFIN_UART_RX", uart)) {
608 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
609 return -EBUSY;
610 }
611
612 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800613 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700614 "BFIN_UART_TX", uart)) {
615 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
616 free_irq(uart->port.irq, uart);
617 return -EBUSY;
618 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100619
620# ifdef CONFIG_BF54x
621 {
622 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
623
624 switch (uart->port.irq) {
625 case IRQ_UART3_RX:
626 uart_dma_ch_rx = CH_UART3_RX;
627 uart_dma_ch_tx = CH_UART3_TX;
628 break;
629 case IRQ_UART2_RX:
630 uart_dma_ch_rx = CH_UART2_RX;
631 uart_dma_ch_tx = CH_UART2_TX;
632 break;
633 default:
634 uart_dma_ch_rx = uart_dma_ch_tx = 0;
635 break;
636 };
637
638 if (uart_dma_ch_rx &&
639 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
640 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
641 free_irq(uart->port.irq, uart);
642 free_irq(uart->port.irq + 1, uart);
643 return -EBUSY;
644 }
645 if (uart_dma_ch_tx &&
646 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
647 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
648 free_dma(uart_dma_ch_rx);
649 free_irq(uart->port.irq, uart);
650 free_irq(uart->port.irq + 1, uart);
651 return -EBUSY;
652 }
653 }
654# endif
Sonic Zhang52e15f02009-01-02 13:40:14 +0000655#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
656 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
657 }
658# endif
Bryan Wu194de562007-05-06 14:50:30 -0700659#endif
Roy Huangf4d640c2007-07-12 16:43:46 +0800660 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700661 return 0;
662}
663
664static void bfin_serial_shutdown(struct uart_port *port)
665{
666 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
667
668#ifdef CONFIG_SERIAL_BFIN_DMA
669 disable_dma(uart->tx_dma_channel);
670 free_dma(uart->tx_dma_channel);
671 disable_dma(uart->rx_dma_channel);
672 free_dma(uart->rx_dma_channel);
673 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800674 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700675#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100676#ifdef CONFIG_BF54x
677 switch (uart->port.irq) {
678 case IRQ_UART3_RX:
679 free_dma(CH_UART3_RX);
680 free_dma(CH_UART3_TX);
681 break;
682 case IRQ_UART2_RX:
683 free_dma(CH_UART2_RX);
684 free_dma(CH_UART2_TX);
685 break;
686 default:
687 break;
688 };
689#endif
Bryan Wu194de562007-05-06 14:50:30 -0700690 free_irq(uart->port.irq, uart);
691 free_irq(uart->port.irq+1, uart);
692#endif
693}
694
695static void
696bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
697 struct ktermios *old)
698{
699 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
700 unsigned long flags;
701 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800702 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700703
704 switch (termios->c_cflag & CSIZE) {
705 case CS8:
706 lcr = WLS(8);
707 break;
708 case CS7:
709 lcr = WLS(7);
710 break;
711 case CS6:
712 lcr = WLS(6);
713 break;
714 case CS5:
715 lcr = WLS(5);
716 break;
717 default:
718 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700719 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700720 }
721
722 if (termios->c_cflag & CSTOPB)
723 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800724 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700725 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800726 if (!(termios->c_cflag & PARODD))
727 lcr |= EPS;
728 if (termios->c_cflag & CMSPAR)
729 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700730
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800731 port->read_status_mask = OE;
732 if (termios->c_iflag & INPCK)
733 port->read_status_mask |= (FE | PE);
734 if (termios->c_iflag & (BRKINT | PARMRK))
735 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700736
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800737 /*
738 * Characters to ignore
739 */
740 port->ignore_status_mask = 0;
741 if (termios->c_iflag & IGNPAR)
742 port->ignore_status_mask |= FE | PE;
743 if (termios->c_iflag & IGNBRK) {
744 port->ignore_status_mask |= BI;
745 /*
746 * If we're ignoring parity and break indicators,
747 * ignore overruns too (for real raw support).
748 */
749 if (termios->c_iflag & IGNPAR)
750 port->ignore_status_mask |= OE;
751 }
Bryan Wu194de562007-05-06 14:50:30 -0700752
753 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
754 quot = uart_get_divisor(port, baud);
755 spin_lock_irqsave(&uart->port.lock, flags);
756
Mike Frysinger8851c712007-12-24 19:48:04 +0800757 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
758
Bryan Wu194de562007-05-06 14:50:30 -0700759 /* Disable UART */
760 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800761 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700762
763 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800764 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700765
766 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700767 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
768 SSYNC();
769
770 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800771 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700772
773 UART_PUT_LCR(uart, lcr);
774
775 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800776 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700777
778 val = UART_GET_GCTL(uart);
779 val |= UCEN;
780 UART_PUT_GCTL(uart, val);
781
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100782 /* Port speed changed, update the per-port timeout. */
783 uart_update_timeout(port, termios->c_cflag, baud);
784
Bryan Wu194de562007-05-06 14:50:30 -0700785 spin_unlock_irqrestore(&uart->port.lock, flags);
786}
787
788static const char *bfin_serial_type(struct uart_port *port)
789{
790 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
791
792 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
793}
794
795/*
796 * Release the memory region(s) being used by 'port'.
797 */
798static void bfin_serial_release_port(struct uart_port *port)
799{
800}
801
802/*
803 * Request the memory region(s) being used by 'port'.
804 */
805static int bfin_serial_request_port(struct uart_port *port)
806{
807 return 0;
808}
809
810/*
811 * Configure/autoconfigure the port.
812 */
813static void bfin_serial_config_port(struct uart_port *port, int flags)
814{
815 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
816
817 if (flags & UART_CONFIG_TYPE &&
818 bfin_serial_request_port(&uart->port) == 0)
819 uart->port.type = PORT_BFIN;
820}
821
822/*
823 * Verify the new serial_struct (for TIOCSSERIAL).
824 * The only change we allow are to the flags and type, and
825 * even then only between PORT_BFIN and PORT_UNKNOWN
826 */
827static int
828bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
829{
830 return 0;
831}
832
Graf Yang7d01b472008-02-29 11:31:08 +0800833/*
834 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
835 * In other cases, disable IrDA function.
836 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800837static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800838{
Graf Yang3b8458a2008-06-07 15:36:33 +0800839 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800840 unsigned short val;
841
Takashi Iwaia88487c2008-07-16 21:54:42 +0100842 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800843 return;
844
Alan Coxb1cbefe2008-08-04 17:22:11 +0100845 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800846 case N_IRDA:
847 val = UART_GET_GCTL(&bfin_serial_ports[line]);
848 val |= (IREN | RPOLC);
849 UART_PUT_GCTL(&bfin_serial_ports[line], val);
850 break;
851 default:
852 val = UART_GET_GCTL(&bfin_serial_ports[line]);
853 val &= ~(IREN | RPOLC);
854 UART_PUT_GCTL(&bfin_serial_ports[line], val);
855 }
856}
857
Sonic Zhang52e15f02009-01-02 13:40:14 +0000858#ifdef CONFIG_CONSOLE_POLL
859static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
860{
861 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
862
863 while (!(UART_GET_LSR(uart) & THRE))
864 cpu_relax();
865
866 UART_CLEAR_DLAB(uart);
867 UART_PUT_CHAR(uart, (unsigned char)chr);
868}
869
870static int bfin_serial_poll_get_char(struct uart_port *port)
871{
872 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
873 unsigned char chr;
874
875 while (!(UART_GET_LSR(uart) & DR))
876 cpu_relax();
877
878 UART_CLEAR_DLAB(uart);
879 chr = UART_GET_CHAR(uart);
880
881 return chr;
882}
883#endif
884
885#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
886 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
887static void bfin_kgdboc_port_shutdown(struct uart_port *port)
888{
889 if (kgdboc_break_enabled) {
890 kgdboc_break_enabled = 0;
891 bfin_serial_shutdown(port);
892 }
893}
894
895static int bfin_kgdboc_port_startup(struct uart_port *port)
896{
897 kgdboc_port_line = port->line;
898 kgdboc_break_enabled = !bfin_serial_startup(port);
899 return 0;
900}
901#endif
902
Graf Yang80d5c472009-01-02 13:40:22 +0000903static void bfin_serial_reset_irda(struct uart_port *port)
904{
905 int line = port->line;
906 unsigned short val;
907
908 val = UART_GET_GCTL(&bfin_serial_ports[line]);
909 val &= ~(IREN | RPOLC);
910 UART_PUT_GCTL(&bfin_serial_ports[line], val);
911 SSYNC();
912 val |= (IREN | RPOLC);
913 UART_PUT_GCTL(&bfin_serial_ports[line], val);
914 SSYNC();
915}
916
Bryan Wu194de562007-05-06 14:50:30 -0700917static struct uart_ops bfin_serial_pops = {
918 .tx_empty = bfin_serial_tx_empty,
919 .set_mctrl = bfin_serial_set_mctrl,
920 .get_mctrl = bfin_serial_get_mctrl,
921 .stop_tx = bfin_serial_stop_tx,
922 .start_tx = bfin_serial_start_tx,
923 .stop_rx = bfin_serial_stop_rx,
924 .enable_ms = bfin_serial_enable_ms,
925 .break_ctl = bfin_serial_break_ctl,
926 .startup = bfin_serial_startup,
927 .shutdown = bfin_serial_shutdown,
928 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +0800929 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -0700930 .type = bfin_serial_type,
931 .release_port = bfin_serial_release_port,
932 .request_port = bfin_serial_request_port,
933 .config_port = bfin_serial_config_port,
934 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f02009-01-02 13:40:14 +0000935#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
936 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
937 .kgdboc_port_startup = bfin_kgdboc_port_startup,
938 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
939#endif
940#ifdef CONFIG_CONSOLE_POLL
941 .poll_put_char = bfin_serial_poll_put_char,
942 .poll_get_char = bfin_serial_poll_get_char,
943#endif
Bryan Wu194de562007-05-06 14:50:30 -0700944};
945
946static void __init bfin_serial_init_ports(void)
947{
948 static int first = 1;
949 int i;
950
951 if (!first)
952 return;
953 first = 0;
954
Mike Frysingerc9607ec2008-10-13 10:33:16 +0100955 for (i = 0; i < nr_active_ports; i++) {
Bryan Wu194de562007-05-06 14:50:30 -0700956 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100957 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -0700958 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
959 bfin_serial_ports[i].port.line = i;
960 bfin_serial_ports[i].port.iotype = UPIO_MEM;
961 bfin_serial_ports[i].port.membase =
962 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
963 bfin_serial_ports[i].port.mapbase =
964 bfin_serial_resource[i].uart_base_addr;
965 bfin_serial_ports[i].port.irq =
966 bfin_serial_resource[i].uart_irq;
967 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
968#ifdef CONFIG_SERIAL_BFIN_DMA
969 bfin_serial_ports[i].tx_done = 1;
970 bfin_serial_ports[i].tx_count = 0;
971 bfin_serial_ports[i].tx_dma_channel =
972 bfin_serial_resource[i].uart_tx_dma_channel;
973 bfin_serial_ports[i].rx_dma_channel =
974 bfin_serial_resource[i].uart_rx_dma_channel;
975 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700976#endif
977#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800978 init_timer(&(bfin_serial_ports[i].cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700979 bfin_serial_ports[i].cts_pin =
980 bfin_serial_resource[i].uart_cts_pin;
981 bfin_serial_ports[i].rts_pin =
982 bfin_serial_resource[i].uart_rts_pin;
983#endif
984 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700985 }
Roy Huangf4d640c2007-07-12 16:43:46 +0800986
Bryan Wu194de562007-05-06 14:50:30 -0700987}
988
Sonic Zhangb6efa1e2009-01-02 13:40:31 +0000989#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -0700990/*
991 * If the port was already initialised (eg, by a boot loader),
992 * try to determine the current setup.
993 */
994static void __init
995bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
996 int *parity, int *bits)
997{
998 unsigned short status;
999
1000 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1001 if (status == (ERBFI | ETBEI)) {
1002 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001003 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001004
1005 lcr = UART_GET_LCR(uart);
1006
1007 *parity = 'n';
1008 if (lcr & PEN) {
1009 if (lcr & EPS)
1010 *parity = 'e';
1011 else
1012 *parity = 'o';
1013 }
1014 switch (lcr & 0x03) {
1015 case 0: *bits = 5; break;
1016 case 1: *bits = 6; break;
1017 case 2: *bits = 7; break;
1018 case 3: *bits = 8; break;
1019 }
1020 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001021 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001022
1023 dll = UART_GET_DLL(uart);
1024 dlh = UART_GET_DLH(uart);
1025
1026 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001027 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001028
1029 *baud = get_sclk() / (16*(dll | dlh << 8));
1030 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001031 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001032}
Robin Getz0ae53642007-10-09 17:24:49 +08001033
Robin Getz0ae53642007-10-09 17:24:49 +08001034static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001035
1036static int __init
1037bfin_serial_console_setup(struct console *co, char *options)
1038{
1039 struct bfin_serial_port *uart;
1040 int baud = 57600;
1041 int bits = 8;
1042 int parity = 'n';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001043# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -07001044 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001045# else
Bryan Wu194de562007-05-06 14:50:30 -07001046 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001047# endif
Bryan Wu194de562007-05-06 14:50:30 -07001048
1049 /*
1050 * Check whether an invalid uart number has been specified, and
1051 * if so, search for the first available port that does have
1052 * console support.
1053 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001054 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -07001055 co->index = 0;
1056 uart = &bfin_serial_ports[co->index];
1057
1058 if (options)
1059 uart_parse_options(options, &baud, &parity, &bits, &flow);
1060 else
1061 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1062
1063 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001064}
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 Zhang52e15f02009-01-02 13:40:14 +00001107
Bryan Wu194de562007-05-06 14:50:30 -07001108 return 0;
1109}
1110console_initcall(bfin_serial_rs_console_init);
1111
1112#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1113#else
1114#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001115#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1116
1117
1118#ifdef CONFIG_EARLY_PRINTK
1119static __init void early_serial_putc(struct uart_port *port, int ch)
1120{
1121 unsigned timeout = 0xffff;
1122 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1123
1124 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1125 cpu_relax();
1126 UART_PUT_CHAR(uart, ch);
1127}
1128
1129static __init void early_serial_write(struct console *con, const char *s,
1130 unsigned int n)
1131{
1132 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1133 unsigned int i;
1134
1135 for (i = 0; i < n; i++, s++) {
1136 if (*s == '\n')
1137 early_serial_putc(&uart->port, '\r');
1138 early_serial_putc(&uart->port, *s);
1139 }
1140}
1141
Mike Frysingerc1113402008-10-13 10:32:35 +01001142static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001143 .name = "early_BFuart",
1144 .write = early_serial_write,
1145 .device = uart_console_device,
1146 .flags = CON_PRINTBUFFER,
1147 .setup = bfin_serial_console_setup,
1148 .index = -1,
1149 .data = &bfin_serial_reg,
1150};
1151
1152struct console __init *bfin_earlyserial_init(unsigned int port,
1153 unsigned int cflag)
1154{
1155 struct bfin_serial_port *uart;
1156 struct ktermios t;
1157
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001158 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001159 port = 0;
1160 bfin_serial_init_ports();
1161 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001162 uart = &bfin_serial_ports[port];
1163 t.c_cflag = cflag;
1164 t.c_iflag = 0;
1165 t.c_oflag = 0;
1166 t.c_lflag = ICANON;
1167 t.c_line = port;
1168 bfin_serial_set_termios(&uart->port, &t, &t);
1169 return &bfin_early_serial_console;
1170}
1171
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001172#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001173
1174static struct uart_driver bfin_serial_reg = {
1175 .owner = THIS_MODULE,
1176 .driver_name = "bfin-uart",
1177 .dev_name = BFIN_SERIAL_NAME,
1178 .major = BFIN_SERIAL_MAJOR,
1179 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001180 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001181 .cons = BFIN_SERIAL_CONSOLE,
1182};
1183
1184static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1185{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001186 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001187
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001188 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001189 if (bfin_serial_ports[i].port.dev != &dev->dev)
1190 continue;
1191 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1192 }
Bryan Wu194de562007-05-06 14:50:30 -07001193
1194 return 0;
1195}
1196
1197static int bfin_serial_resume(struct platform_device *dev)
1198{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001199 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001200
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001201 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001202 if (bfin_serial_ports[i].port.dev != &dev->dev)
1203 continue;
1204 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1205 }
Bryan Wu194de562007-05-06 14:50:30 -07001206
1207 return 0;
1208}
1209
1210static int bfin_serial_probe(struct platform_device *dev)
1211{
1212 struct resource *res = dev->resource;
1213 int i;
1214
1215 for (i = 0; i < dev->num_resources; i++, res++)
1216 if (res->flags & IORESOURCE_MEM)
1217 break;
1218
1219 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001220 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001221 if (bfin_serial_ports[i].port.mapbase != res->start)
1222 continue;
1223 bfin_serial_ports[i].port.dev = &dev->dev;
1224 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001225 }
1226 }
1227
1228 return 0;
1229}
1230
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001231static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001232{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001233 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001234
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001235 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001236 if (bfin_serial_ports[i].port.dev != &dev->dev)
1237 continue;
1238 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1239 bfin_serial_ports[i].port.dev = NULL;
Bryan Wu194de562007-05-06 14:50:30 -07001240#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001241 gpio_free(bfin_serial_ports[i].cts_pin);
1242 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001243#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001244 }
Bryan Wu194de562007-05-06 14:50:30 -07001245
1246 return 0;
1247}
1248
1249static struct platform_driver bfin_serial_driver = {
1250 .probe = bfin_serial_probe,
1251 .remove = bfin_serial_remove,
1252 .suspend = bfin_serial_suspend,
1253 .resume = bfin_serial_resume,
1254 .driver = {
1255 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001256 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001257 },
1258};
1259
1260static int __init bfin_serial_init(void)
1261{
1262 int ret;
1263
1264 pr_info("Serial: Blackfin serial driver\n");
1265
1266 bfin_serial_init_ports();
1267
1268 ret = uart_register_driver(&bfin_serial_reg);
1269 if (ret == 0) {
1270 ret = platform_driver_register(&bfin_serial_driver);
1271 if (ret) {
1272 pr_debug("uart register failed\n");
1273 uart_unregister_driver(&bfin_serial_reg);
1274 }
1275 }
Bryan Wu194de562007-05-06 14:50:30 -07001276 return ret;
1277}
1278
1279static void __exit bfin_serial_exit(void)
1280{
1281 platform_driver_unregister(&bfin_serial_driver);
1282 uart_unregister_driver(&bfin_serial_reg);
1283}
1284
Sonic Zhang52e15f02009-01-02 13:40:14 +00001285
Bryan Wu194de562007-05-06 14:50:30 -07001286module_init(bfin_serial_init);
1287module_exit(bfin_serial_exit);
1288
1289MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1290MODULE_DESCRIPTION("Blackfin generic serial port driver");
1291MODULE_LICENSE("GPL");
1292MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001293MODULE_ALIAS("platform:bfin-uart");