blob: f9b5a72e261a517c47950c99b52b91d1f52fe029 [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 Zhang68a784c2009-01-02 13:40:38 +000084#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +080085 struct circ_buf *xmit = &uart->port.info->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +000086#endif
Bryan Wu194de562007-05-06 14:50:30 -070087
Roy Huangf4d640c2007-07-12 16:43:46 +080088 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080089 cpu_relax();
Roy Huangf4d640c2007-07-12 16:43:46 +080090
Bryan Wu194de562007-05-06 14:50:30 -070091#ifdef CONFIG_SERIAL_BFIN_DMA
92 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080093 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
94 uart->port.icount.tx += uart->tx_count;
95 uart->tx_count = 0;
96 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070097#else
Roy Huangf4d640c2007-07-12 16:43:46 +080098#ifdef CONFIG_BF54x
Roy Huangf4d640c2007-07-12 16:43:46 +080099 /* Clear TFI bit */
100 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700101#endif
Mike Frysinger89bf6dc2008-05-07 11:41:26 +0800102 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c2007-07-12 16:43:46 +0800103#endif
Bryan Wu194de562007-05-06 14:50:30 -0700104}
105
106/*
107 * port is locked and interrupts are disabled
108 */
109static void bfin_serial_start_tx(struct uart_port *port)
110{
111 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang80d5c472009-01-02 13:40:22 +0000112 struct tty_struct *tty = uart->port.info->port.tty;
113
114 /*
115 * To avoid losting RX interrupt, we reset IR function
116 * before sending data.
117 */
118 if (tty->termios->c_line == N_IRDA)
119 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700120
121#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800122 if (uart->tx_done)
123 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700124#else
Roy Huangf4d640c2007-07-12 16:43:46 +0800125 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800126 bfin_serial_tx_chars(uart);
Roy Huangf4d640c2007-07-12 16:43:46 +0800127#endif
Bryan Wu194de562007-05-06 14:50:30 -0700128}
129
130/*
131 * Interrupts are enabled
132 */
133static void bfin_serial_stop_rx(struct uart_port *port)
134{
135 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f02009-01-02 13:40:14 +0000136
Roy Huangf4d640c2007-07-12 16:43:46 +0800137 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700138}
139
140/*
141 * Set the modem control timer to fire immediately.
142 */
143static void bfin_serial_enable_ms(struct uart_port *port)
144{
145}
146
Sonic Zhang474f1a62007-06-29 16:35:17 +0800147
Mike Frysinger50e2e152008-04-25 03:03:03 +0800148#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800149# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
150# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
151#else
152# define UART_GET_ANOMALY_THRESHOLD(uart) 0
153# define UART_SET_ANOMALY_THRESHOLD(uart, v)
154#endif
155
Bryan Wu194de562007-05-06 14:50:30 -0700156#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700157static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
158{
Sonic Zhang52e15f02009-01-02 13:40:14 +0000159 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700160 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800161 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700162
Sonic Zhang759eb042007-11-21 17:00:32 +0800163 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800164 UART_CLEAR_LSR(uart);
165
166 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700167 uart->port.icount.rx++;
168
Sonic Zhang52e15f02009-01-02 13:40:14 +0000169#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
170 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
171 if (kgdb_connected && kgdboc_port_line == uart->port.line)
172 if (ch == 0x3) {/* Ctrl + C */
173 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800174 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800175 }
Sonic Zhang52e15f02009-01-02 13:40:14 +0000176
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100177 if (!uart->port.info || !uart->port.info->port.tty)
Sonic Zhang52e15f02009-01-02 13:40:14 +0000178 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800179#endif
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100180 tty = uart->port.info->port.tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800181
Mike Frysinger50e2e152008-04-25 03:03:03 +0800182 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800183 /* The BF533 (and BF561) family of processors have a nice anomaly
184 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800185 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800186 * character comes across. Due to the nature of the flood, it is
187 * not possible to reliably catch bytes that are sent too quickly
188 * after this break. So application code talking to the Blackfin
189 * which sends a break signal must allow at least 1.5 character
190 * times after the end of the break for things to stabilize. This
191 * timeout was picked as it must absolutely be larger than 1
192 * character time +/- some percent. So 1.5 sounds good. All other
193 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800194 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800195 if (anomaly_start.tv_sec) {
196 struct timeval curr;
197 suseconds_t usecs;
198
199 if ((~ch & (~ch + 1)) & 0xff)
200 goto known_good_char;
201
202 do_gettimeofday(&curr);
203 if (curr.tv_sec - anomaly_start.tv_sec > 1)
204 goto known_good_char;
205
206 usecs = 0;
207 if (curr.tv_sec != anomaly_start.tv_sec)
208 usecs += USEC_PER_SEC;
209 usecs += curr.tv_usec - anomaly_start.tv_usec;
210
211 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
212 goto known_good_char;
213
214 if (ch)
215 anomaly_start.tv_sec = 0;
216 else
217 anomaly_start = curr;
218
219 return;
220
221 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000222 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800223 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800224 }
Bryan Wu194de562007-05-06 14:50:30 -0700225 }
Bryan Wu194de562007-05-06 14:50:30 -0700226
227 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800228 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800229 if (bfin_revid() < 5)
230 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700231 uart->port.icount.brk++;
232 if (uart_handle_break(&uart->port))
233 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800234 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800235 }
236 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700237 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800238 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700239 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800240 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700241 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800242
243 status &= uart->port.read_status_mask;
244
245 if (status & BI)
246 flg = TTY_BREAK;
247 else if (status & PE)
248 flg = TTY_PARITY;
249 else if (status & FE)
250 flg = TTY_FRAME;
251 else
Bryan Wu194de562007-05-06 14:50:30 -0700252 flg = TTY_NORMAL;
253
254 if (uart_handle_sysrq_char(&uart->port, ch))
255 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700256
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800257 uart_insert_char(&uart->port, status, OE, ch, flg);
258
259 ignore_char:
260 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700261}
262
263static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
264{
265 struct circ_buf *xmit = &uart->port.info->xmit;
266
Bryan Wu194de562007-05-06 14:50:30 -0700267 /*
268 * Check the modem control lines before
269 * transmitting anything.
270 */
271 bfin_serial_mctrl_check(uart);
272
273 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea2008-10-13 10:33:33 +0100274#ifdef CONFIG_BF54x
275 /* Clear TFI bit */
276 UART_PUT_LSR(uart, TFI);
277#endif
278 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700279 return;
280 }
281
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800282 if (uart->port.x_char) {
283 UART_PUT_CHAR(uart, uart->port.x_char);
284 uart->port.icount.tx++;
285 uart->port.x_char = 0;
286 }
287
Sonic Zhang759eb042007-11-21 17:00:32 +0800288 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
289 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
290 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
291 uart->port.icount.tx++;
292 SSYNC();
293 }
Bryan Wu194de562007-05-06 14:50:30 -0700294
295 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
296 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700297}
298
Aubrey Li5c4e4722007-05-21 18:09:38 +0800299static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
300{
301 struct bfin_serial_port *uart = dev_id;
302
Roy Huangf4d640c2007-07-12 16:43:46 +0800303 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800304 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800305 bfin_serial_rx_chars(uart);
306 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800307
Aubrey Li5c4e4722007-05-21 18:09:38 +0800308 return IRQ_HANDLED;
309}
310
311static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700312{
313 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700314
Roy Huangf4d640c2007-07-12 16:43:46 +0800315 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800316 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800317 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700318 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800319
Bryan Wu194de562007-05-06 14:50:30 -0700320 return IRQ_HANDLED;
321}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800322#endif
Bryan Wu194de562007-05-06 14:50:30 -0700323
Bryan Wu194de562007-05-06 14:50:30 -0700324#ifdef CONFIG_SERIAL_BFIN_DMA
325static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
326{
327 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700328
Bryan Wu194de562007-05-06 14:50:30 -0700329 uart->tx_done = 0;
330
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800331 /*
332 * Check the modem control lines before
333 * transmitting anything.
334 */
335 bfin_serial_mctrl_check(uart);
336
Bryan Wu194de562007-05-06 14:50:30 -0700337 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800338 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700339 uart->tx_done = 1;
340 return;
341 }
342
Sonic Zhang1b733512007-12-21 16:45:12 +0800343 if (uart->port.x_char) {
344 UART_PUT_CHAR(uart, uart->port.x_char);
345 uart->port.icount.tx++;
346 uart->port.x_char = 0;
347 }
348
Bryan Wu194de562007-05-06 14:50:30 -0700349 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
350 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
351 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
352 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
353 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
354 set_dma_config(uart->tx_dma_channel,
355 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
356 INTR_ON_BUF,
357 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800358 DATA_SIZE_8,
359 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700360 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
361 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
362 set_dma_x_modify(uart->tx_dma_channel, 1);
363 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800364
Roy Huangf4d640c2007-07-12 16:43:46 +0800365 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700366}
367
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800368static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700369{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100370 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700371 int i, flg, status;
372
373 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800374 UART_CLEAR_LSR(uart);
375
Sonic Zhang56f5de82008-02-25 15:19:09 +0800376 uart->port.icount.rx +=
377 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
378 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700379
380 if (status & BI) {
381 uart->port.icount.brk++;
382 if (uart_handle_break(&uart->port))
383 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800384 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800385 }
386 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700387 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800388 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700389 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800390 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700391 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800392
393 status &= uart->port.read_status_mask;
394
395 if (status & BI)
396 flg = TTY_BREAK;
397 else if (status & PE)
398 flg = TTY_PARITY;
399 else if (status & FE)
400 flg = TTY_FRAME;
401 else
Bryan Wu194de562007-05-06 14:50:30 -0700402 flg = TTY_NORMAL;
403
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100404 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800405 if (i >= UART_XMIT_SIZE)
406 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100407 if (i == uart->rx_dma_buf.head)
408 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800409 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
410 uart_insert_char(&uart->port, status, OE,
411 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700412 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800413
414 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700415 tty_flip_buffer_push(tty);
416}
417
418void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
419{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100420 int x_pos, pos;
421 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000422
423 spin_lock_irqsave(&uart->port.lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700424
Sonic Zhang56f5de82008-02-25 15:19:09 +0800425 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
426 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
427 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
428 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
429 uart->rx_dma_nrows = 0;
430 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700431 if (x_pos == DMA_RX_XCOUNT)
432 x_pos = 0;
433
434 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800435 if (pos != uart->rx_dma_buf.tail) {
436 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700437 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800438 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700439 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800440
Sonic Zhang68a784c2009-01-02 13:40:38 +0000441 spin_unlock_irqrestore(&uart->port.lock, flags);
442
Sonic Zhang0a278422008-04-25 04:36:47 +0800443 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700444}
445
446static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
447{
448 struct bfin_serial_port *uart = dev_id;
449 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700450
451 spin_lock(&uart->port.lock);
452 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700453 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800454 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c2007-07-12 16:43:46 +0800455 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800456 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
457 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800458
Sonic Zhang56f5de82008-02-25 15:19:09 +0800459 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
460 uart_write_wakeup(&uart->port);
461
Sonic Zhang1b733512007-12-21 16:45:12 +0800462 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700463 }
464
465 spin_unlock(&uart->port.lock);
466 return IRQ_HANDLED;
467}
468
469static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
470{
471 struct bfin_serial_port *uart = dev_id;
472 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800473
Bryan Wu194de562007-05-06 14:50:30 -0700474 spin_lock(&uart->port.lock);
475 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
476 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000477 bfin_serial_dma_rx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700478 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800479
Bryan Wu194de562007-05-06 14:50:30 -0700480 return IRQ_HANDLED;
481}
482#endif
483
484/*
485 * Return TIOCSER_TEMT when transmitter is not busy.
486 */
487static unsigned int bfin_serial_tx_empty(struct uart_port *port)
488{
489 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
490 unsigned short lsr;
491
492 lsr = UART_GET_LSR(uart);
493 if (lsr & TEMT)
494 return TIOCSER_TEMT;
495 else
496 return 0;
497}
498
499static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
500{
501#ifdef CONFIG_SERIAL_BFIN_CTSRTS
502 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
503 if (uart->cts_pin < 0)
504 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
505
Sonic Zhang1feaa512008-06-03 12:19:45 +0800506 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700507 return TIOCM_DSR | TIOCM_CAR;
508 else
509#endif
510 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
511}
512
513static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
514{
515#ifdef CONFIG_SERIAL_BFIN_CTSRTS
516 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
517 if (uart->rts_pin < 0)
518 return;
519
520 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800521 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700522 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800523 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700524#endif
525}
526
527/*
528 * Handle any change of modem status signal since we were last called.
529 */
530static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
531{
532#ifdef CONFIG_SERIAL_BFIN_CTSRTS
533 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700534 struct uart_info *info = uart->port.info;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100535 struct tty_struct *tty = info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700536
537 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800538 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700539 if (!(status & TIOCM_CTS)) {
540 tty->hw_stopped = 1;
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800541 uart->cts_timer.data = (unsigned long)(uart);
542 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
543 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
544 add_timer(&(uart->cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700545 } else {
546 tty->hw_stopped = 0;
547 }
Bryan Wu194de562007-05-06 14:50:30 -0700548#endif
549}
550
551/*
552 * Interrupts are always disabled.
553 */
554static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
555{
Mike Frysingercf686762007-06-11 16:12:49 +0800556 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
557 u16 lcr = UART_GET_LCR(uart);
558 if (break_state)
559 lcr |= SB;
560 else
561 lcr &= ~SB;
562 UART_PUT_LCR(uart, lcr);
563 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700564}
565
566static int bfin_serial_startup(struct uart_port *port)
567{
568 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
569
570#ifdef CONFIG_SERIAL_BFIN_DMA
571 dma_addr_t dma_handle;
572
573 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
574 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
575 return -EBUSY;
576 }
577
578 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
579 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
580 free_dma(uart->rx_dma_channel);
581 return -EBUSY;
582 }
583
584 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
585 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
586
587 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
588 uart->rx_dma_buf.head = 0;
589 uart->rx_dma_buf.tail = 0;
590 uart->rx_dma_nrows = 0;
591
592 set_dma_config(uart->rx_dma_channel,
593 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
594 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800595 DATA_SIZE_8,
596 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700597 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
598 set_dma_x_modify(uart->rx_dma_channel, 1);
599 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
600 set_dma_y_modify(uart->rx_dma_channel, 1);
601 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
602 enable_dma(uart->rx_dma_channel);
603
604 uart->rx_dma_timer.data = (unsigned long)(uart);
605 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
606 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
607 add_timer(&(uart->rx_dma_timer));
608#else
Sonic Zhang52e15f02009-01-02 13:40:14 +0000609#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
610 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
611 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
612 kgdboc_break_enabled = 0;
613 else {
614# endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800615 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700616 "BFIN_UART_RX", uart)) {
617 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
618 return -EBUSY;
619 }
620
621 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800622 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700623 "BFIN_UART_TX", uart)) {
624 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
625 free_irq(uart->port.irq, uart);
626 return -EBUSY;
627 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100628
629# ifdef CONFIG_BF54x
630 {
631 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
632
633 switch (uart->port.irq) {
634 case IRQ_UART3_RX:
635 uart_dma_ch_rx = CH_UART3_RX;
636 uart_dma_ch_tx = CH_UART3_TX;
637 break;
638 case IRQ_UART2_RX:
639 uart_dma_ch_rx = CH_UART2_RX;
640 uart_dma_ch_tx = CH_UART2_TX;
641 break;
642 default:
643 uart_dma_ch_rx = uart_dma_ch_tx = 0;
644 break;
645 };
646
647 if (uart_dma_ch_rx &&
648 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
649 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
650 free_irq(uart->port.irq, uart);
651 free_irq(uart->port.irq + 1, uart);
652 return -EBUSY;
653 }
654 if (uart_dma_ch_tx &&
655 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
656 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
657 free_dma(uart_dma_ch_rx);
658 free_irq(uart->port.irq, uart);
659 free_irq(uart->port.irq + 1, uart);
660 return -EBUSY;
661 }
662 }
663# endif
Sonic Zhang52e15f02009-01-02 13:40:14 +0000664#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
665 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
666 }
667# endif
Bryan Wu194de562007-05-06 14:50:30 -0700668#endif
Roy Huangf4d640c2007-07-12 16:43:46 +0800669 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700670 return 0;
671}
672
673static void bfin_serial_shutdown(struct uart_port *port)
674{
675 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
676
677#ifdef CONFIG_SERIAL_BFIN_DMA
678 disable_dma(uart->tx_dma_channel);
679 free_dma(uart->tx_dma_channel);
680 disable_dma(uart->rx_dma_channel);
681 free_dma(uart->rx_dma_channel);
682 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800683 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700684#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100685#ifdef CONFIG_BF54x
686 switch (uart->port.irq) {
687 case IRQ_UART3_RX:
688 free_dma(CH_UART3_RX);
689 free_dma(CH_UART3_TX);
690 break;
691 case IRQ_UART2_RX:
692 free_dma(CH_UART2_RX);
693 free_dma(CH_UART2_TX);
694 break;
695 default:
696 break;
697 };
698#endif
Bryan Wu194de562007-05-06 14:50:30 -0700699 free_irq(uart->port.irq, uart);
700 free_irq(uart->port.irq+1, uart);
701#endif
702}
703
704static void
705bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
706 struct ktermios *old)
707{
708 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
709 unsigned long flags;
710 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800711 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700712
713 switch (termios->c_cflag & CSIZE) {
714 case CS8:
715 lcr = WLS(8);
716 break;
717 case CS7:
718 lcr = WLS(7);
719 break;
720 case CS6:
721 lcr = WLS(6);
722 break;
723 case CS5:
724 lcr = WLS(5);
725 break;
726 default:
727 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700728 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700729 }
730
731 if (termios->c_cflag & CSTOPB)
732 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800733 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700734 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800735 if (!(termios->c_cflag & PARODD))
736 lcr |= EPS;
737 if (termios->c_cflag & CMSPAR)
738 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700739
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800740 port->read_status_mask = OE;
741 if (termios->c_iflag & INPCK)
742 port->read_status_mask |= (FE | PE);
743 if (termios->c_iflag & (BRKINT | PARMRK))
744 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700745
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800746 /*
747 * Characters to ignore
748 */
749 port->ignore_status_mask = 0;
750 if (termios->c_iflag & IGNPAR)
751 port->ignore_status_mask |= FE | PE;
752 if (termios->c_iflag & IGNBRK) {
753 port->ignore_status_mask |= BI;
754 /*
755 * If we're ignoring parity and break indicators,
756 * ignore overruns too (for real raw support).
757 */
758 if (termios->c_iflag & IGNPAR)
759 port->ignore_status_mask |= OE;
760 }
Bryan Wu194de562007-05-06 14:50:30 -0700761
762 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangf4487102009-04-06 17:32:49 +0100763 quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
Bryan Wu194de562007-05-06 14:50:30 -0700764 spin_lock_irqsave(&uart->port.lock, flags);
765
Mike Frysinger8851c712007-12-24 19:48:04 +0800766 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
767
Bryan Wu194de562007-05-06 14:50:30 -0700768 /* Disable UART */
769 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800770 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700771
772 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800773 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700774
775 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700776 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
777 SSYNC();
778
779 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800780 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700781
782 UART_PUT_LCR(uart, lcr);
783
784 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800785 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700786
787 val = UART_GET_GCTL(uart);
788 val |= UCEN;
789 UART_PUT_GCTL(uart, val);
790
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100791 /* Port speed changed, update the per-port timeout. */
792 uart_update_timeout(port, termios->c_cflag, baud);
793
Bryan Wu194de562007-05-06 14:50:30 -0700794 spin_unlock_irqrestore(&uart->port.lock, flags);
795}
796
797static const char *bfin_serial_type(struct uart_port *port)
798{
799 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
800
801 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
802}
803
804/*
805 * Release the memory region(s) being used by 'port'.
806 */
807static void bfin_serial_release_port(struct uart_port *port)
808{
809}
810
811/*
812 * Request the memory region(s) being used by 'port'.
813 */
814static int bfin_serial_request_port(struct uart_port *port)
815{
816 return 0;
817}
818
819/*
820 * Configure/autoconfigure the port.
821 */
822static void bfin_serial_config_port(struct uart_port *port, int flags)
823{
824 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
825
826 if (flags & UART_CONFIG_TYPE &&
827 bfin_serial_request_port(&uart->port) == 0)
828 uart->port.type = PORT_BFIN;
829}
830
831/*
832 * Verify the new serial_struct (for TIOCSSERIAL).
833 * The only change we allow are to the flags and type, and
834 * even then only between PORT_BFIN and PORT_UNKNOWN
835 */
836static int
837bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
838{
839 return 0;
840}
841
Graf Yang7d01b472008-02-29 11:31:08 +0800842/*
843 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
844 * In other cases, disable IrDA function.
845 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800846static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800847{
Graf Yang3b8458a2008-06-07 15:36:33 +0800848 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800849 unsigned short val;
850
Takashi Iwaia88487c2008-07-16 21:54:42 +0100851 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800852 return;
853
Alan Coxb1cbefe2008-08-04 17:22:11 +0100854 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800855 case N_IRDA:
856 val = UART_GET_GCTL(&bfin_serial_ports[line]);
857 val |= (IREN | RPOLC);
858 UART_PUT_GCTL(&bfin_serial_ports[line], val);
859 break;
860 default:
861 val = UART_GET_GCTL(&bfin_serial_ports[line]);
862 val &= ~(IREN | RPOLC);
863 UART_PUT_GCTL(&bfin_serial_ports[line], val);
864 }
865}
866
Sonic Zhang52e15f02009-01-02 13:40:14 +0000867#ifdef CONFIG_CONSOLE_POLL
868static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
869{
870 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
871
872 while (!(UART_GET_LSR(uart) & THRE))
873 cpu_relax();
874
875 UART_CLEAR_DLAB(uart);
876 UART_PUT_CHAR(uart, (unsigned char)chr);
877}
878
879static int bfin_serial_poll_get_char(struct uart_port *port)
880{
881 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
882 unsigned char chr;
883
884 while (!(UART_GET_LSR(uart) & DR))
885 cpu_relax();
886
887 UART_CLEAR_DLAB(uart);
888 chr = UART_GET_CHAR(uart);
889
890 return chr;
891}
892#endif
893
894#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
895 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
896static void bfin_kgdboc_port_shutdown(struct uart_port *port)
897{
898 if (kgdboc_break_enabled) {
899 kgdboc_break_enabled = 0;
900 bfin_serial_shutdown(port);
901 }
902}
903
904static int bfin_kgdboc_port_startup(struct uart_port *port)
905{
906 kgdboc_port_line = port->line;
907 kgdboc_break_enabled = !bfin_serial_startup(port);
908 return 0;
909}
910#endif
911
Graf Yang80d5c472009-01-02 13:40:22 +0000912static void bfin_serial_reset_irda(struct uart_port *port)
913{
914 int line = port->line;
915 unsigned short val;
916
917 val = UART_GET_GCTL(&bfin_serial_ports[line]);
918 val &= ~(IREN | RPOLC);
919 UART_PUT_GCTL(&bfin_serial_ports[line], val);
920 SSYNC();
921 val |= (IREN | RPOLC);
922 UART_PUT_GCTL(&bfin_serial_ports[line], val);
923 SSYNC();
924}
925
Bryan Wu194de562007-05-06 14:50:30 -0700926static struct uart_ops bfin_serial_pops = {
927 .tx_empty = bfin_serial_tx_empty,
928 .set_mctrl = bfin_serial_set_mctrl,
929 .get_mctrl = bfin_serial_get_mctrl,
930 .stop_tx = bfin_serial_stop_tx,
931 .start_tx = bfin_serial_start_tx,
932 .stop_rx = bfin_serial_stop_rx,
933 .enable_ms = bfin_serial_enable_ms,
934 .break_ctl = bfin_serial_break_ctl,
935 .startup = bfin_serial_startup,
936 .shutdown = bfin_serial_shutdown,
937 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +0800938 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -0700939 .type = bfin_serial_type,
940 .release_port = bfin_serial_release_port,
941 .request_port = bfin_serial_request_port,
942 .config_port = bfin_serial_config_port,
943 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f02009-01-02 13:40:14 +0000944#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
945 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
946 .kgdboc_port_startup = bfin_kgdboc_port_startup,
947 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
948#endif
949#ifdef CONFIG_CONSOLE_POLL
950 .poll_put_char = bfin_serial_poll_put_char,
951 .poll_get_char = bfin_serial_poll_get_char,
952#endif
Bryan Wu194de562007-05-06 14:50:30 -0700953};
954
955static void __init bfin_serial_init_ports(void)
956{
957 static int first = 1;
958 int i;
959
960 if (!first)
961 return;
962 first = 0;
963
Mike Frysingerc9607ec2008-10-13 10:33:16 +0100964 for (i = 0; i < nr_active_ports; i++) {
Bryan Wu194de562007-05-06 14:50:30 -0700965 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100966 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -0700967 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
968 bfin_serial_ports[i].port.line = i;
969 bfin_serial_ports[i].port.iotype = UPIO_MEM;
970 bfin_serial_ports[i].port.membase =
971 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
972 bfin_serial_ports[i].port.mapbase =
973 bfin_serial_resource[i].uart_base_addr;
974 bfin_serial_ports[i].port.irq =
975 bfin_serial_resource[i].uart_irq;
976 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
977#ifdef CONFIG_SERIAL_BFIN_DMA
978 bfin_serial_ports[i].tx_done = 1;
979 bfin_serial_ports[i].tx_count = 0;
980 bfin_serial_ports[i].tx_dma_channel =
981 bfin_serial_resource[i].uart_tx_dma_channel;
982 bfin_serial_ports[i].rx_dma_channel =
983 bfin_serial_resource[i].uart_rx_dma_channel;
984 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700985#endif
986#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800987 init_timer(&(bfin_serial_ports[i].cts_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700988 bfin_serial_ports[i].cts_pin =
989 bfin_serial_resource[i].uart_cts_pin;
990 bfin_serial_ports[i].rts_pin =
991 bfin_serial_resource[i].uart_rts_pin;
992#endif
993 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700994 }
Roy Huangf4d640c2007-07-12 16:43:46 +0800995
Bryan Wu194de562007-05-06 14:50:30 -0700996}
997
Sonic Zhangb6efa1e2009-01-02 13:40:31 +0000998#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -0700999/*
1000 * If the port was already initialised (eg, by a boot loader),
1001 * try to determine the current setup.
1002 */
1003static void __init
1004bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1005 int *parity, int *bits)
1006{
1007 unsigned short status;
1008
1009 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1010 if (status == (ERBFI | ETBEI)) {
1011 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001012 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001013
1014 lcr = UART_GET_LCR(uart);
1015
1016 *parity = 'n';
1017 if (lcr & PEN) {
1018 if (lcr & EPS)
1019 *parity = 'e';
1020 else
1021 *parity = 'o';
1022 }
1023 switch (lcr & 0x03) {
1024 case 0: *bits = 5; break;
1025 case 1: *bits = 6; break;
1026 case 2: *bits = 7; break;
1027 case 3: *bits = 8; break;
1028 }
1029 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001030 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001031
1032 dll = UART_GET_DLL(uart);
1033 dlh = UART_GET_DLH(uart);
1034
1035 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001036 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001037
1038 *baud = get_sclk() / (16*(dll | dlh << 8));
1039 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001040 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001041}
Robin Getz0ae53642007-10-09 17:24:49 +08001042
Robin Getz0ae53642007-10-09 17:24:49 +08001043static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001044
1045static int __init
1046bfin_serial_console_setup(struct console *co, char *options)
1047{
1048 struct bfin_serial_port *uart;
1049 int baud = 57600;
1050 int bits = 8;
1051 int parity = 'n';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001052# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -07001053 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001054# else
Bryan Wu194de562007-05-06 14:50:30 -07001055 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001056# endif
Bryan Wu194de562007-05-06 14:50:30 -07001057
1058 /*
1059 * Check whether an invalid uart number has been specified, and
1060 * if so, search for the first available port that does have
1061 * console support.
1062 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001063 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -07001064 co->index = 0;
1065 uart = &bfin_serial_ports[co->index];
1066
1067 if (options)
1068 uart_parse_options(options, &baud, &parity, &bits, &flow);
1069 else
1070 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1071
1072 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001073}
1074#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1075 defined (CONFIG_EARLY_PRINTK) */
1076
1077#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1078static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1079{
1080 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1081 while (!(UART_GET_LSR(uart) & THRE))
1082 barrier();
1083 UART_PUT_CHAR(uart, ch);
1084 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -07001085}
1086
Robin Getz0ae53642007-10-09 17:24:49 +08001087/*
1088 * Interrupts are disabled on entering
1089 */
1090static void
1091bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1092{
1093 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
Mike Frysinger59e4e3e2009-04-06 17:32:28 +01001094 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001095
1096 spin_lock_irqsave(&uart->port.lock, flags);
1097 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1098 spin_unlock_irqrestore(&uart->port.lock, flags);
1099
1100}
1101
Bryan Wu194de562007-05-06 14:50:30 -07001102static struct console bfin_serial_console = {
1103 .name = BFIN_SERIAL_NAME,
1104 .write = bfin_serial_console_write,
1105 .device = uart_console_device,
1106 .setup = bfin_serial_console_setup,
1107 .flags = CON_PRINTBUFFER,
1108 .index = -1,
1109 .data = &bfin_serial_reg,
1110};
1111
1112static int __init bfin_serial_rs_console_init(void)
1113{
1114 bfin_serial_init_ports();
1115 register_console(&bfin_serial_console);
Sonic Zhang52e15f02009-01-02 13:40:14 +00001116
Bryan Wu194de562007-05-06 14:50:30 -07001117 return 0;
1118}
1119console_initcall(bfin_serial_rs_console_init);
1120
1121#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1122#else
1123#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001124#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1125
1126
1127#ifdef CONFIG_EARLY_PRINTK
1128static __init void early_serial_putc(struct uart_port *port, int ch)
1129{
1130 unsigned timeout = 0xffff;
1131 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1132
1133 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1134 cpu_relax();
1135 UART_PUT_CHAR(uart, ch);
1136}
1137
1138static __init void early_serial_write(struct console *con, const char *s,
1139 unsigned int n)
1140{
1141 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1142 unsigned int i;
1143
1144 for (i = 0; i < n; i++, s++) {
1145 if (*s == '\n')
1146 early_serial_putc(&uart->port, '\r');
1147 early_serial_putc(&uart->port, *s);
1148 }
1149}
1150
Mike Frysingerc1113402008-10-13 10:32:35 +01001151static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001152 .name = "early_BFuart",
1153 .write = early_serial_write,
1154 .device = uart_console_device,
1155 .flags = CON_PRINTBUFFER,
1156 .setup = bfin_serial_console_setup,
1157 .index = -1,
1158 .data = &bfin_serial_reg,
1159};
1160
1161struct console __init *bfin_earlyserial_init(unsigned int port,
1162 unsigned int cflag)
1163{
1164 struct bfin_serial_port *uart;
1165 struct ktermios t;
1166
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001167 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001168 port = 0;
1169 bfin_serial_init_ports();
1170 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001171 uart = &bfin_serial_ports[port];
1172 t.c_cflag = cflag;
1173 t.c_iflag = 0;
1174 t.c_oflag = 0;
1175 t.c_lflag = ICANON;
1176 t.c_line = port;
1177 bfin_serial_set_termios(&uart->port, &t, &t);
1178 return &bfin_early_serial_console;
1179}
1180
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001181#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001182
1183static struct uart_driver bfin_serial_reg = {
1184 .owner = THIS_MODULE,
1185 .driver_name = "bfin-uart",
1186 .dev_name = BFIN_SERIAL_NAME,
1187 .major = BFIN_SERIAL_MAJOR,
1188 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001189 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001190 .cons = BFIN_SERIAL_CONSOLE,
1191};
1192
1193static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1194{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001195 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001196
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001197 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001198 if (bfin_serial_ports[i].port.dev != &dev->dev)
1199 continue;
1200 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1201 }
Bryan Wu194de562007-05-06 14:50:30 -07001202
1203 return 0;
1204}
1205
1206static int bfin_serial_resume(struct platform_device *dev)
1207{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001208 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001209
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001210 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001211 if (bfin_serial_ports[i].port.dev != &dev->dev)
1212 continue;
1213 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1214 }
Bryan Wu194de562007-05-06 14:50:30 -07001215
1216 return 0;
1217}
1218
1219static int bfin_serial_probe(struct platform_device *dev)
1220{
1221 struct resource *res = dev->resource;
1222 int i;
1223
1224 for (i = 0; i < dev->num_resources; i++, res++)
1225 if (res->flags & IORESOURCE_MEM)
1226 break;
1227
1228 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001229 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001230 if (bfin_serial_ports[i].port.mapbase != res->start)
1231 continue;
1232 bfin_serial_ports[i].port.dev = &dev->dev;
1233 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001234 }
1235 }
1236
1237 return 0;
1238}
1239
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001240static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001241{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001242 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001243
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001244 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001245 if (bfin_serial_ports[i].port.dev != &dev->dev)
1246 continue;
1247 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1248 bfin_serial_ports[i].port.dev = NULL;
Bryan Wu194de562007-05-06 14:50:30 -07001249#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001250 gpio_free(bfin_serial_ports[i].cts_pin);
1251 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001252#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001253 }
Bryan Wu194de562007-05-06 14:50:30 -07001254
1255 return 0;
1256}
1257
1258static struct platform_driver bfin_serial_driver = {
1259 .probe = bfin_serial_probe,
1260 .remove = bfin_serial_remove,
1261 .suspend = bfin_serial_suspend,
1262 .resume = bfin_serial_resume,
1263 .driver = {
1264 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001265 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001266 },
1267};
1268
1269static int __init bfin_serial_init(void)
1270{
1271 int ret;
1272
1273 pr_info("Serial: Blackfin serial driver\n");
1274
1275 bfin_serial_init_ports();
1276
1277 ret = uart_register_driver(&bfin_serial_reg);
1278 if (ret == 0) {
1279 ret = platform_driver_register(&bfin_serial_driver);
1280 if (ret) {
1281 pr_debug("uart register failed\n");
1282 uart_unregister_driver(&bfin_serial_reg);
1283 }
1284 }
Bryan Wu194de562007-05-06 14:50:30 -07001285 return ret;
1286}
1287
1288static void __exit bfin_serial_exit(void)
1289{
1290 platform_driver_unregister(&bfin_serial_driver);
1291 uart_unregister_driver(&bfin_serial_reg);
1292}
1293
Sonic Zhang52e15f02009-01-02 13:40:14 +00001294
Bryan Wu194de562007-05-06 14:50:30 -07001295module_init(bfin_serial_init);
1296module_exit(bfin_serial_exit);
1297
1298MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1299MODULE_DESCRIPTION("Blackfin generic serial port driver");
1300MODULE_LICENSE("GPL");
1301MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001302MODULE_ALIAS("platform:bfin-uart");