blob: fbbddb9e73f6a2bcafd7a75f1b58add88aa0488a [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)
Bryan Wu194de562007-05-06 14:50:30 -070066
67#ifdef CONFIG_SERIAL_BFIN_DMA
68static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
69#else
Bryan Wu194de562007-05-06 14:50:30 -070070static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070071#endif
72
Graf Yang80d5c472009-01-02 13:40:22 +000073static void bfin_serial_reset_irda(struct uart_port *port);
74
Bryan Wu194de562007-05-06 14:50:30 -070075/*
76 * interrupts are disabled on entry
77 */
78static void bfin_serial_stop_tx(struct uart_port *port)
79{
80 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +000081#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +080082 struct circ_buf *xmit = &uart->port.info->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +000083#endif
Bryan Wu194de562007-05-06 14:50:30 -070084
Roy Huangf4d640c92007-07-12 16:43:46 +080085 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080086 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +080087
Bryan Wu194de562007-05-06 14:50:30 -070088#ifdef CONFIG_SERIAL_BFIN_DMA
89 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080090 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
91 uart->port.icount.tx += uart->tx_count;
92 uart->tx_count = 0;
93 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070094#else
Roy Huangf4d640c92007-07-12 16:43:46 +080095#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +080096 /* Clear TFI bit */
97 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -070098#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +080099 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800100#endif
Bryan Wu194de562007-05-06 14:50:30 -0700101}
102
103/*
104 * port is locked and interrupts are disabled
105 */
106static void bfin_serial_start_tx(struct uart_port *port)
107{
108 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang80d5c472009-01-02 13:40:22 +0000109 struct tty_struct *tty = uart->port.info->port.tty;
110
111 /*
112 * To avoid losting RX interrupt, we reset IR function
113 * before sending data.
114 */
115 if (tty->termios->c_line == N_IRDA)
116 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700117
118#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800119 if (uart->tx_done)
120 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700121#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800122 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800123 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800124#endif
Bryan Wu194de562007-05-06 14:50:30 -0700125}
126
127/*
128 * Interrupts are enabled
129 */
130static void bfin_serial_stop_rx(struct uart_port *port)
131{
132 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000133
Roy Huangf4d640c92007-07-12 16:43:46 +0800134 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700135}
136
137/*
138 * Set the modem control timer to fire immediately.
139 */
140static void bfin_serial_enable_ms(struct uart_port *port)
141{
142}
143
Sonic Zhang474f1a62007-06-29 16:35:17 +0800144
Mike Frysinger50e2e152008-04-25 03:03:03 +0800145#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800146# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
147# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
148#else
149# define UART_GET_ANOMALY_THRESHOLD(uart) 0
150# define UART_SET_ANOMALY_THRESHOLD(uart, v)
151#endif
152
Bryan Wu194de562007-05-06 14:50:30 -0700153#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700154static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
155{
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000156 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700157 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800158 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700159
Sonic Zhang759eb042007-11-21 17:00:32 +0800160 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800161 UART_CLEAR_LSR(uart);
162
163 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700164 uart->port.icount.rx++;
165
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000166#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
167 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
168 if (kgdb_connected && kgdboc_port_line == uart->port.line)
169 if (ch == 0x3) {/* Ctrl + C */
170 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800171 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800172 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000173
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100174 if (!uart->port.info || !uart->port.info->port.tty)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000175 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800176#endif
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100177 tty = uart->port.info->port.tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800178
Mike Frysinger50e2e152008-04-25 03:03:03 +0800179 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800180 /* The BF533 (and BF561) family of processors have a nice anomaly
181 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800182 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800183 * character comes across. Due to the nature of the flood, it is
184 * not possible to reliably catch bytes that are sent too quickly
185 * after this break. So application code talking to the Blackfin
186 * which sends a break signal must allow at least 1.5 character
187 * times after the end of the break for things to stabilize. This
188 * timeout was picked as it must absolutely be larger than 1
189 * character time +/- some percent. So 1.5 sounds good. All other
190 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800191 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800192 if (anomaly_start.tv_sec) {
193 struct timeval curr;
194 suseconds_t usecs;
195
196 if ((~ch & (~ch + 1)) & 0xff)
197 goto known_good_char;
198
199 do_gettimeofday(&curr);
200 if (curr.tv_sec - anomaly_start.tv_sec > 1)
201 goto known_good_char;
202
203 usecs = 0;
204 if (curr.tv_sec != anomaly_start.tv_sec)
205 usecs += USEC_PER_SEC;
206 usecs += curr.tv_usec - anomaly_start.tv_usec;
207
208 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
209 goto known_good_char;
210
211 if (ch)
212 anomaly_start.tv_sec = 0;
213 else
214 anomaly_start = curr;
215
216 return;
217
218 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000219 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800220 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 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100265#ifdef CONFIG_BF54x
266 /* Clear TFI bit */
267 UART_PUT_LSR(uart, TFI);
268#endif
269 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700270 return;
271 }
272
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800273 if (uart->port.x_char) {
274 UART_PUT_CHAR(uart, uart->port.x_char);
275 uart->port.icount.tx++;
276 uart->port.x_char = 0;
277 }
278
Sonic Zhang759eb042007-11-21 17:00:32 +0800279 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
280 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
281 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
282 uart->port.icount.tx++;
283 SSYNC();
284 }
Bryan Wu194de562007-05-06 14:50:30 -0700285
286 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
287 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700288}
289
Aubrey Li5c4e4722007-05-21 18:09:38 +0800290static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
291{
292 struct bfin_serial_port *uart = dev_id;
293
Roy Huangf4d640c92007-07-12 16:43:46 +0800294 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800295 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800296 bfin_serial_rx_chars(uart);
297 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800298
Aubrey Li5c4e4722007-05-21 18:09:38 +0800299 return IRQ_HANDLED;
300}
301
302static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700303{
304 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700305
Roy Huangf4d640c92007-07-12 16:43:46 +0800306 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800307 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800308 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700309 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800310
Bryan Wu194de562007-05-06 14:50:30 -0700311 return IRQ_HANDLED;
312}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800313#endif
Bryan Wu194de562007-05-06 14:50:30 -0700314
Bryan Wu194de562007-05-06 14:50:30 -0700315#ifdef CONFIG_SERIAL_BFIN_DMA
316static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
317{
318 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700319
Bryan Wu194de562007-05-06 14:50:30 -0700320 uart->tx_done = 0;
321
Bryan Wu194de562007-05-06 14:50:30 -0700322 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800323 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700324 uart->tx_done = 1;
325 return;
326 }
327
Sonic Zhang1b733512007-12-21 16:45:12 +0800328 if (uart->port.x_char) {
329 UART_PUT_CHAR(uart, uart->port.x_char);
330 uart->port.icount.tx++;
331 uart->port.x_char = 0;
332 }
333
Bryan Wu194de562007-05-06 14:50:30 -0700334 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
335 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
336 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
337 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
338 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
339 set_dma_config(uart->tx_dma_channel,
340 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
341 INTR_ON_BUF,
342 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800343 DATA_SIZE_8,
344 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700345 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
346 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
347 set_dma_x_modify(uart->tx_dma_channel, 1);
348 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800349
Roy Huangf4d640c92007-07-12 16:43:46 +0800350 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700351}
352
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800353static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700354{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100355 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700356 int i, flg, status;
357
358 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800359 UART_CLEAR_LSR(uart);
360
Sonic Zhang56f5de82008-02-25 15:19:09 +0800361 uart->port.icount.rx +=
362 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
363 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700364
365 if (status & BI) {
366 uart->port.icount.brk++;
367 if (uart_handle_break(&uart->port))
368 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800369 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800370 }
371 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700372 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800373 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700374 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800375 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700376 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800377
378 status &= uart->port.read_status_mask;
379
380 if (status & BI)
381 flg = TTY_BREAK;
382 else if (status & PE)
383 flg = TTY_PARITY;
384 else if (status & FE)
385 flg = TTY_FRAME;
386 else
Bryan Wu194de562007-05-06 14:50:30 -0700387 flg = TTY_NORMAL;
388
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100389 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800390 if (i >= UART_XMIT_SIZE)
391 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100392 if (i == uart->rx_dma_buf.head)
393 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800394 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
395 uart_insert_char(&uart->port, status, OE,
396 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700397 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800398
399 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700400 tty_flip_buffer_push(tty);
401}
402
403void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
404{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100405 int x_pos, pos;
406 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000407
408 spin_lock_irqsave(&uart->port.lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700409
Sonic Zhang56f5de82008-02-25 15:19:09 +0800410 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
411 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
412 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
413 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
414 uart->rx_dma_nrows = 0;
415 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700416 if (x_pos == DMA_RX_XCOUNT)
417 x_pos = 0;
418
419 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800420 if (pos != uart->rx_dma_buf.tail) {
421 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700422 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800423 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700424 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800425
Sonic Zhang68a784c2009-01-02 13:40:38 +0000426 spin_unlock_irqrestore(&uart->port.lock, flags);
427
Sonic Zhang0a278422008-04-25 04:36:47 +0800428 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700429}
430
431static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
432{
433 struct bfin_serial_port *uart = dev_id;
434 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700435
436 spin_lock(&uart->port.lock);
437 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700438 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800439 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c92007-07-12 16:43:46 +0800440 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800441 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
442 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800443
Sonic Zhang56f5de82008-02-25 15:19:09 +0800444 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
445 uart_write_wakeup(&uart->port);
446
Sonic Zhang1b733512007-12-21 16:45:12 +0800447 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700448 }
449
450 spin_unlock(&uart->port.lock);
451 return IRQ_HANDLED;
452}
453
454static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
455{
456 struct bfin_serial_port *uart = dev_id;
457 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800458
Bryan Wu194de562007-05-06 14:50:30 -0700459 spin_lock(&uart->port.lock);
460 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
461 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000462 bfin_serial_dma_rx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700463 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800464
Bryan Wu194de562007-05-06 14:50:30 -0700465 return IRQ_HANDLED;
466}
467#endif
468
469/*
470 * Return TIOCSER_TEMT when transmitter is not busy.
471 */
472static unsigned int bfin_serial_tx_empty(struct uart_port *port)
473{
474 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
475 unsigned short lsr;
476
477 lsr = UART_GET_LSR(uart);
478 if (lsr & TEMT)
479 return TIOCSER_TEMT;
480 else
481 return 0;
482}
483
484static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
485{
486#ifdef CONFIG_SERIAL_BFIN_CTSRTS
487 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
488 if (uart->cts_pin < 0)
489 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
490
Sonic Zhang1feaa512008-06-03 12:19:45 +0800491 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700492 return TIOCM_DSR | TIOCM_CAR;
493 else
494#endif
495 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
496}
497
498static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
499{
500#ifdef CONFIG_SERIAL_BFIN_CTSRTS
501 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
502 if (uart->rts_pin < 0)
503 return;
504
505 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800506 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700507 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800508 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700509#endif
510}
511
Bryan Wu194de562007-05-06 14:50:30 -0700512#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100513/*
514 * Handle any change of modem status signal.
515 */
516static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
517{
518 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700519 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700520
521 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800522 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Sonic Zhang6f955702009-04-07 16:51:15 +0100523
524 return IRQ_HANDLED;
Bryan Wu194de562007-05-06 14:50:30 -0700525}
Sonic Zhang6f955702009-04-07 16:51:15 +0100526#endif
Bryan Wu194de562007-05-06 14:50:30 -0700527
528/*
529 * Interrupts are always disabled.
530 */
531static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
532{
Mike Frysingercf686762007-06-11 16:12:49 +0800533 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
534 u16 lcr = UART_GET_LCR(uart);
535 if (break_state)
536 lcr |= SB;
537 else
538 lcr &= ~SB;
539 UART_PUT_LCR(uart, lcr);
540 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700541}
542
543static int bfin_serial_startup(struct uart_port *port)
544{
545 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
546
547#ifdef CONFIG_SERIAL_BFIN_DMA
548 dma_addr_t dma_handle;
549
550 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
551 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
552 return -EBUSY;
553 }
554
555 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
556 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
557 free_dma(uart->rx_dma_channel);
558 return -EBUSY;
559 }
560
561 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
562 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
563
564 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
565 uart->rx_dma_buf.head = 0;
566 uart->rx_dma_buf.tail = 0;
567 uart->rx_dma_nrows = 0;
568
569 set_dma_config(uart->rx_dma_channel,
570 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
571 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800572 DATA_SIZE_8,
573 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700574 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
575 set_dma_x_modify(uart->rx_dma_channel, 1);
576 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
577 set_dma_y_modify(uart->rx_dma_channel, 1);
578 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
579 enable_dma(uart->rx_dma_channel);
580
581 uart->rx_dma_timer.data = (unsigned long)(uart);
582 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
583 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
584 add_timer(&(uart->rx_dma_timer));
585#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100586# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000587 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
588 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
589 kgdboc_break_enabled = 0;
590 else {
591# endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800592 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700593 "BFIN_UART_RX", uart)) {
594 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
595 return -EBUSY;
596 }
597
598 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800599 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700600 "BFIN_UART_TX", uart)) {
601 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
602 free_irq(uart->port.irq, uart);
603 return -EBUSY;
604 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100605
606# ifdef CONFIG_BF54x
607 {
608 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
609
610 switch (uart->port.irq) {
611 case IRQ_UART3_RX:
612 uart_dma_ch_rx = CH_UART3_RX;
613 uart_dma_ch_tx = CH_UART3_TX;
614 break;
615 case IRQ_UART2_RX:
616 uart_dma_ch_rx = CH_UART2_RX;
617 uart_dma_ch_tx = CH_UART2_TX;
618 break;
619 default:
620 uart_dma_ch_rx = uart_dma_ch_tx = 0;
621 break;
622 };
623
624 if (uart_dma_ch_rx &&
625 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
626 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
627 free_irq(uart->port.irq, uart);
628 free_irq(uart->port.irq + 1, uart);
629 return -EBUSY;
630 }
631 if (uart_dma_ch_tx &&
632 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
633 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
634 free_dma(uart_dma_ch_rx);
635 free_irq(uart->port.irq, uart);
636 free_irq(uart->port.irq + 1, uart);
637 return -EBUSY;
638 }
639 }
640# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100641# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000642 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
643 }
644# endif
Bryan Wu194de562007-05-06 14:50:30 -0700645#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100646
647#ifdef CONFIG_SERIAL_BFIN_CTSRTS
648 if (uart->cts_pin >= 0) {
649 if (request_irq(gpio_to_irq(uart->cts_pin),
650 bfin_serial_mctrl_cts_int,
651 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
652 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
653 uart->cts_pin = -1;
654 pr_info("Unable to attach BlackFin UART CTS interrupt.\
655 So, disable it.\n");
656 }
657 }
658 if (uart->rts_pin >= 0) {
659 gpio_request(uart->rts_pin, DRIVER_NAME);
660 gpio_direction_output(uart->rts_pin, 0);
661 }
662#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800663 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700664 return 0;
665}
666
667static void bfin_serial_shutdown(struct uart_port *port)
668{
669 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
670
671#ifdef CONFIG_SERIAL_BFIN_DMA
672 disable_dma(uart->tx_dma_channel);
673 free_dma(uart->tx_dma_channel);
674 disable_dma(uart->rx_dma_channel);
675 free_dma(uart->rx_dma_channel);
676 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800677 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700678#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100679#ifdef CONFIG_BF54x
680 switch (uart->port.irq) {
681 case IRQ_UART3_RX:
682 free_dma(CH_UART3_RX);
683 free_dma(CH_UART3_TX);
684 break;
685 case IRQ_UART2_RX:
686 free_dma(CH_UART2_RX);
687 free_dma(CH_UART2_TX);
688 break;
689 default:
690 break;
691 };
692#endif
Bryan Wu194de562007-05-06 14:50:30 -0700693 free_irq(uart->port.irq, uart);
694 free_irq(uart->port.irq+1, uart);
695#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100696
697# ifdef CONFIG_SERIAL_BFIN_CTSRTS
698 if (uart->cts_pin >= 0)
699 free_irq(gpio_to_irq(uart->cts_pin), uart);
700 if (uart->rts_pin >= 0)
701 gpio_free(uart->rts_pin);
702# endif
Bryan Wu194de562007-05-06 14:50:30 -0700703}
704
705static void
706bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
707 struct ktermios *old)
708{
709 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
710 unsigned long flags;
711 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800712 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700713
714 switch (termios->c_cflag & CSIZE) {
715 case CS8:
716 lcr = WLS(8);
717 break;
718 case CS7:
719 lcr = WLS(7);
720 break;
721 case CS6:
722 lcr = WLS(6);
723 break;
724 case CS5:
725 lcr = WLS(5);
726 break;
727 default:
728 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700729 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700730 }
731
732 if (termios->c_cflag & CSTOPB)
733 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800734 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700735 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800736 if (!(termios->c_cflag & PARODD))
737 lcr |= EPS;
738 if (termios->c_cflag & CMSPAR)
739 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700740
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800741 port->read_status_mask = OE;
742 if (termios->c_iflag & INPCK)
743 port->read_status_mask |= (FE | PE);
744 if (termios->c_iflag & (BRKINT | PARMRK))
745 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700746
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800747 /*
748 * Characters to ignore
749 */
750 port->ignore_status_mask = 0;
751 if (termios->c_iflag & IGNPAR)
752 port->ignore_status_mask |= FE | PE;
753 if (termios->c_iflag & IGNBRK) {
754 port->ignore_status_mask |= BI;
755 /*
756 * If we're ignoring parity and break indicators,
757 * ignore overruns too (for real raw support).
758 */
759 if (termios->c_iflag & IGNPAR)
760 port->ignore_status_mask |= OE;
761 }
Bryan Wu194de562007-05-06 14:50:30 -0700762
763 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangf4487102009-04-06 17:32:49 +0100764 quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
Bryan Wu194de562007-05-06 14:50:30 -0700765 spin_lock_irqsave(&uart->port.lock, flags);
766
Mike Frysinger8851c712007-12-24 19:48:04 +0800767 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
768
Bryan Wu194de562007-05-06 14:50:30 -0700769 /* Disable UART */
770 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800771 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700772
773 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800774 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700775
776 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700777 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
778 SSYNC();
779
780 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800781 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700782
783 UART_PUT_LCR(uart, lcr);
784
785 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800786 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700787
788 val = UART_GET_GCTL(uart);
789 val |= UCEN;
790 UART_PUT_GCTL(uart, val);
791
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100792 /* Port speed changed, update the per-port timeout. */
793 uart_update_timeout(port, termios->c_cflag, baud);
794
Bryan Wu194de562007-05-06 14:50:30 -0700795 spin_unlock_irqrestore(&uart->port.lock, flags);
796}
797
798static const char *bfin_serial_type(struct uart_port *port)
799{
800 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
801
802 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
803}
804
805/*
806 * Release the memory region(s) being used by 'port'.
807 */
808static void bfin_serial_release_port(struct uart_port *port)
809{
810}
811
812/*
813 * Request the memory region(s) being used by 'port'.
814 */
815static int bfin_serial_request_port(struct uart_port *port)
816{
817 return 0;
818}
819
820/*
821 * Configure/autoconfigure the port.
822 */
823static void bfin_serial_config_port(struct uart_port *port, int flags)
824{
825 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
826
827 if (flags & UART_CONFIG_TYPE &&
828 bfin_serial_request_port(&uart->port) == 0)
829 uart->port.type = PORT_BFIN;
830}
831
832/*
833 * Verify the new serial_struct (for TIOCSSERIAL).
834 * The only change we allow are to the flags and type, and
835 * even then only between PORT_BFIN and PORT_UNKNOWN
836 */
837static int
838bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
839{
840 return 0;
841}
842
Graf Yang7d01b472008-02-29 11:31:08 +0800843/*
844 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
845 * In other cases, disable IrDA function.
846 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800847static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800848{
Graf Yang3b8458a2008-06-07 15:36:33 +0800849 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800850 unsigned short val;
851
Takashi Iwaia88487c2008-07-16 21:54:42 +0100852 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800853 return;
854
Alan Coxb1cbefe2008-08-04 17:22:11 +0100855 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800856 case N_IRDA:
857 val = UART_GET_GCTL(&bfin_serial_ports[line]);
858 val |= (IREN | RPOLC);
859 UART_PUT_GCTL(&bfin_serial_ports[line], val);
860 break;
861 default:
862 val = UART_GET_GCTL(&bfin_serial_ports[line]);
863 val &= ~(IREN | RPOLC);
864 UART_PUT_GCTL(&bfin_serial_ports[line], val);
865 }
866}
867
Sonic Zhang6f955702009-04-07 16:51:15 +0100868static void bfin_serial_reset_irda(struct uart_port *port)
869{
870 int line = port->line;
871 unsigned short val;
872
873 val = UART_GET_GCTL(&bfin_serial_ports[line]);
874 val &= ~(IREN | RPOLC);
875 UART_PUT_GCTL(&bfin_serial_ports[line], val);
876 SSYNC();
877 val |= (IREN | RPOLC);
878 UART_PUT_GCTL(&bfin_serial_ports[line], val);
879 SSYNC();
880}
881
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000882#ifdef CONFIG_CONSOLE_POLL
883static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
884{
885 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
886
887 while (!(UART_GET_LSR(uart) & THRE))
888 cpu_relax();
889
890 UART_CLEAR_DLAB(uart);
891 UART_PUT_CHAR(uart, (unsigned char)chr);
892}
893
894static int bfin_serial_poll_get_char(struct uart_port *port)
895{
896 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
897 unsigned char chr;
898
899 while (!(UART_GET_LSR(uart) & DR))
900 cpu_relax();
901
902 UART_CLEAR_DLAB(uart);
903 chr = UART_GET_CHAR(uart);
904
905 return chr;
906}
907#endif
908
909#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
910 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
911static void bfin_kgdboc_port_shutdown(struct uart_port *port)
912{
913 if (kgdboc_break_enabled) {
914 kgdboc_break_enabled = 0;
915 bfin_serial_shutdown(port);
916 }
917}
918
919static int bfin_kgdboc_port_startup(struct uart_port *port)
920{
921 kgdboc_port_line = port->line;
922 kgdboc_break_enabled = !bfin_serial_startup(port);
923 return 0;
924}
925#endif
926
Bryan Wu194de562007-05-06 14:50:30 -0700927static struct uart_ops bfin_serial_pops = {
928 .tx_empty = bfin_serial_tx_empty,
929 .set_mctrl = bfin_serial_set_mctrl,
930 .get_mctrl = bfin_serial_get_mctrl,
931 .stop_tx = bfin_serial_stop_tx,
932 .start_tx = bfin_serial_start_tx,
933 .stop_rx = bfin_serial_stop_rx,
934 .enable_ms = bfin_serial_enable_ms,
935 .break_ctl = bfin_serial_break_ctl,
936 .startup = bfin_serial_startup,
937 .shutdown = bfin_serial_shutdown,
938 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +0800939 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -0700940 .type = bfin_serial_type,
941 .release_port = bfin_serial_release_port,
942 .request_port = bfin_serial_request_port,
943 .config_port = bfin_serial_config_port,
944 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000945#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
946 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
947 .kgdboc_port_startup = bfin_kgdboc_port_startup,
948 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
949#endif
950#ifdef CONFIG_CONSOLE_POLL
951 .poll_put_char = bfin_serial_poll_put_char,
952 .poll_get_char = bfin_serial_poll_get_char,
953#endif
Bryan Wu194de562007-05-06 14:50:30 -0700954};
955
Sonic Zhang6f955702009-04-07 16:51:15 +0100956static void __init bfin_serial_hw_init(void)
957{
958#ifdef CONFIG_SERIAL_BFIN_UART0
959 peripheral_request(P_UART0_TX, DRIVER_NAME);
960 peripheral_request(P_UART0_RX, DRIVER_NAME);
961#endif
962
963#ifdef CONFIG_SERIAL_BFIN_UART1
964 peripheral_request(P_UART1_TX, DRIVER_NAME);
965 peripheral_request(P_UART1_RX, DRIVER_NAME);
966
967# if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
968 peripheral_request(P_UART1_RTS, DRIVER_NAME);
969 peripheral_request(P_UART1_CTS, DRIVER_NAME);
970# endif
971#endif
972
973#ifdef CONFIG_SERIAL_BFIN_UART2
974 peripheral_request(P_UART2_TX, DRIVER_NAME);
975 peripheral_request(P_UART2_RX, DRIVER_NAME);
976#endif
977
978#ifdef CONFIG_SERIAL_BFIN_UART3
979 peripheral_request(P_UART3_TX, DRIVER_NAME);
980 peripheral_request(P_UART3_RX, DRIVER_NAME);
981
982# if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
983 peripheral_request(P_UART3_RTS, DRIVER_NAME);
984 peripheral_request(P_UART3_CTS, DRIVER_NAME);
985# endif
986#endif
987}
988
Bryan Wu194de562007-05-06 14:50:30 -0700989static void __init bfin_serial_init_ports(void)
990{
991 static int first = 1;
992 int i;
993
994 if (!first)
995 return;
996 first = 0;
997
Sonic Zhang6f955702009-04-07 16:51:15 +0100998 bfin_serial_hw_init();
999
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001000 for (i = 0; i < nr_active_ports; i++) {
Bryan Wu194de562007-05-06 14:50:30 -07001001 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +01001002 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -07001003 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
1004 bfin_serial_ports[i].port.line = i;
1005 bfin_serial_ports[i].port.iotype = UPIO_MEM;
1006 bfin_serial_ports[i].port.membase =
1007 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
1008 bfin_serial_ports[i].port.mapbase =
1009 bfin_serial_resource[i].uart_base_addr;
1010 bfin_serial_ports[i].port.irq =
1011 bfin_serial_resource[i].uart_irq;
1012 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
1013#ifdef CONFIG_SERIAL_BFIN_DMA
1014 bfin_serial_ports[i].tx_done = 1;
1015 bfin_serial_ports[i].tx_count = 0;
1016 bfin_serial_ports[i].tx_dma_channel =
1017 bfin_serial_resource[i].uart_tx_dma_channel;
1018 bfin_serial_ports[i].rx_dma_channel =
1019 bfin_serial_resource[i].uart_rx_dma_channel;
1020 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -07001021#endif
1022#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1023 bfin_serial_ports[i].cts_pin =
1024 bfin_serial_resource[i].uart_cts_pin;
1025 bfin_serial_ports[i].rts_pin =
1026 bfin_serial_resource[i].uart_rts_pin;
1027#endif
Bryan Wu194de562007-05-06 14:50:30 -07001028 }
1029}
1030
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001031#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001032/*
1033 * If the port was already initialised (eg, by a boot loader),
1034 * try to determine the current setup.
1035 */
1036static void __init
1037bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1038 int *parity, int *bits)
1039{
1040 unsigned short status;
1041
1042 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1043 if (status == (ERBFI | ETBEI)) {
1044 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001045 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001046
1047 lcr = UART_GET_LCR(uart);
1048
1049 *parity = 'n';
1050 if (lcr & PEN) {
1051 if (lcr & EPS)
1052 *parity = 'e';
1053 else
1054 *parity = 'o';
1055 }
1056 switch (lcr & 0x03) {
1057 case 0: *bits = 5; break;
1058 case 1: *bits = 6; break;
1059 case 2: *bits = 7; break;
1060 case 3: *bits = 8; break;
1061 }
1062 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001063 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001064
1065 dll = UART_GET_DLL(uart);
1066 dlh = UART_GET_DLH(uart);
1067
1068 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001069 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001070
1071 *baud = get_sclk() / (16*(dll | dlh << 8));
1072 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001073 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001074}
Robin Getz0ae53642007-10-09 17:24:49 +08001075
Robin Getz0ae53642007-10-09 17:24:49 +08001076static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001077
1078static int __init
1079bfin_serial_console_setup(struct console *co, char *options)
1080{
1081 struct bfin_serial_port *uart;
1082 int baud = 57600;
1083 int bits = 8;
1084 int parity = 'n';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001085# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -07001086 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001087# else
Bryan Wu194de562007-05-06 14:50:30 -07001088 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001089# endif
Bryan Wu194de562007-05-06 14:50:30 -07001090
1091 /*
1092 * Check whether an invalid uart number has been specified, and
1093 * if so, search for the first available port that does have
1094 * console support.
1095 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001096 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -07001097 co->index = 0;
1098 uart = &bfin_serial_ports[co->index];
1099
1100 if (options)
1101 uart_parse_options(options, &baud, &parity, &bits, &flow);
1102 else
1103 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1104
1105 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001106}
1107#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1108 defined (CONFIG_EARLY_PRINTK) */
1109
1110#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1111static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1112{
1113 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1114 while (!(UART_GET_LSR(uart) & THRE))
1115 barrier();
1116 UART_PUT_CHAR(uart, ch);
1117 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -07001118}
1119
Robin Getz0ae53642007-10-09 17:24:49 +08001120/*
1121 * Interrupts are disabled on entering
1122 */
1123static void
1124bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1125{
1126 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
Mike Frysinger59e4e3e2009-04-06 17:32:28 +01001127 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001128
1129 spin_lock_irqsave(&uart->port.lock, flags);
1130 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1131 spin_unlock_irqrestore(&uart->port.lock, flags);
1132
1133}
1134
Bryan Wu194de562007-05-06 14:50:30 -07001135static struct console bfin_serial_console = {
1136 .name = BFIN_SERIAL_NAME,
1137 .write = bfin_serial_console_write,
1138 .device = uart_console_device,
1139 .setup = bfin_serial_console_setup,
1140 .flags = CON_PRINTBUFFER,
1141 .index = -1,
1142 .data = &bfin_serial_reg,
1143};
1144
1145static int __init bfin_serial_rs_console_init(void)
1146{
1147 bfin_serial_init_ports();
1148 register_console(&bfin_serial_console);
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001149
Bryan Wu194de562007-05-06 14:50:30 -07001150 return 0;
1151}
1152console_initcall(bfin_serial_rs_console_init);
1153
1154#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1155#else
1156#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001157#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1158
1159
1160#ifdef CONFIG_EARLY_PRINTK
1161static __init void early_serial_putc(struct uart_port *port, int ch)
1162{
1163 unsigned timeout = 0xffff;
1164 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1165
1166 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1167 cpu_relax();
1168 UART_PUT_CHAR(uart, ch);
1169}
1170
1171static __init void early_serial_write(struct console *con, const char *s,
1172 unsigned int n)
1173{
1174 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1175 unsigned int i;
1176
1177 for (i = 0; i < n; i++, s++) {
1178 if (*s == '\n')
1179 early_serial_putc(&uart->port, '\r');
1180 early_serial_putc(&uart->port, *s);
1181 }
1182}
1183
Mike Frysingerc1113402008-10-13 10:32:35 +01001184static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001185 .name = "early_BFuart",
1186 .write = early_serial_write,
1187 .device = uart_console_device,
1188 .flags = CON_PRINTBUFFER,
1189 .setup = bfin_serial_console_setup,
1190 .index = -1,
1191 .data = &bfin_serial_reg,
1192};
1193
1194struct console __init *bfin_earlyserial_init(unsigned int port,
1195 unsigned int cflag)
1196{
1197 struct bfin_serial_port *uart;
1198 struct ktermios t;
1199
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001200 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001201 port = 0;
1202 bfin_serial_init_ports();
1203 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001204 uart = &bfin_serial_ports[port];
1205 t.c_cflag = cflag;
1206 t.c_iflag = 0;
1207 t.c_oflag = 0;
1208 t.c_lflag = ICANON;
1209 t.c_line = port;
1210 bfin_serial_set_termios(&uart->port, &t, &t);
1211 return &bfin_early_serial_console;
1212}
1213
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001214#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001215
1216static struct uart_driver bfin_serial_reg = {
1217 .owner = THIS_MODULE,
1218 .driver_name = "bfin-uart",
1219 .dev_name = BFIN_SERIAL_NAME,
1220 .major = BFIN_SERIAL_MAJOR,
1221 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001222 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001223 .cons = BFIN_SERIAL_CONSOLE,
1224};
1225
1226static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1227{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001228 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001229
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001230 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001231 if (bfin_serial_ports[i].port.dev != &dev->dev)
1232 continue;
1233 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1234 }
Bryan Wu194de562007-05-06 14:50:30 -07001235
1236 return 0;
1237}
1238
1239static int bfin_serial_resume(struct platform_device *dev)
1240{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001241 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001242
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001243 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001244 if (bfin_serial_ports[i].port.dev != &dev->dev)
1245 continue;
1246 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1247 }
Bryan Wu194de562007-05-06 14:50:30 -07001248
1249 return 0;
1250}
1251
1252static int bfin_serial_probe(struct platform_device *dev)
1253{
1254 struct resource *res = dev->resource;
1255 int i;
1256
1257 for (i = 0; i < dev->num_resources; i++, res++)
1258 if (res->flags & IORESOURCE_MEM)
1259 break;
1260
1261 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001262 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001263 if (bfin_serial_ports[i].port.mapbase != res->start)
1264 continue;
1265 bfin_serial_ports[i].port.dev = &dev->dev;
1266 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001267 }
1268 }
1269
1270 return 0;
1271}
1272
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001273static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001274{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001275 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001276
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001277 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001278 if (bfin_serial_ports[i].port.dev != &dev->dev)
1279 continue;
1280 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1281 bfin_serial_ports[i].port.dev = NULL;
Bryan Wu194de562007-05-06 14:50:30 -07001282#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001283 gpio_free(bfin_serial_ports[i].cts_pin);
1284 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001285#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001286 }
Bryan Wu194de562007-05-06 14:50:30 -07001287
1288 return 0;
1289}
1290
1291static struct platform_driver bfin_serial_driver = {
1292 .probe = bfin_serial_probe,
1293 .remove = bfin_serial_remove,
1294 .suspend = bfin_serial_suspend,
1295 .resume = bfin_serial_resume,
1296 .driver = {
1297 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001298 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001299 },
1300};
1301
1302static int __init bfin_serial_init(void)
1303{
1304 int ret;
1305
1306 pr_info("Serial: Blackfin serial driver\n");
1307
1308 bfin_serial_init_ports();
1309
1310 ret = uart_register_driver(&bfin_serial_reg);
1311 if (ret == 0) {
1312 ret = platform_driver_register(&bfin_serial_driver);
1313 if (ret) {
1314 pr_debug("uart register failed\n");
1315 uart_unregister_driver(&bfin_serial_reg);
1316 }
1317 }
Bryan Wu194de562007-05-06 14:50:30 -07001318 return ret;
1319}
1320
1321static void __exit bfin_serial_exit(void)
1322{
1323 platform_driver_unregister(&bfin_serial_driver);
1324 uart_unregister_driver(&bfin_serial_reg);
1325}
1326
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001327
Bryan Wu194de562007-05-06 14:50:30 -07001328module_init(bfin_serial_init);
1329module_exit(bfin_serial_exit);
1330
1331MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1332MODULE_DESCRIPTION("Blackfin generic serial port driver");
1333MODULE_LICENSE("GPL");
1334MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001335MODULE_ALIAS("platform:bfin-uart");