blob: b4a7650af696f7bb80834e6e1994c669d0a399c8 [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
Mike Frysinger607c2682009-06-22 18:41:47 +010041#ifdef CONFIG_SERIAL_BFIN_MODULE
42# undef CONFIG_EARLY_PRINTK
43#endif
44
Bryan Wu194de562007-05-06 14:50:30 -070045/* UART name and device definitions */
46#define BFIN_SERIAL_NAME "ttyBF"
47#define BFIN_SERIAL_MAJOR 204
48#define BFIN_SERIAL_MINOR 64
49
Mike Frysingerc9607ec2008-10-13 10:33:16 +010050static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
51static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
52
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000053#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
54 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
55
56# ifndef CONFIG_SERIAL_BFIN_PIO
57# error KGDB only support UART in PIO mode.
58# endif
59
60static int kgdboc_port_line;
61static int kgdboc_break_enabled;
62#endif
Bryan Wu194de562007-05-06 14:50:30 -070063/*
64 * Setup for console. Argument comes from the menuconfig
65 */
66#define DMA_RX_XCOUNT 512
67#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
68
Sonic Zhang0aef4562008-02-29 12:08:42 +080069#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070070
71#ifdef CONFIG_SERIAL_BFIN_DMA
72static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
73#else
Bryan Wu194de562007-05-06 14:50:30 -070074static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070075#endif
76
Graf Yang80d5c472009-01-02 13:40:22 +000077static void bfin_serial_reset_irda(struct uart_port *port);
78
Sonic Zhangd307d362009-04-07 16:52:26 +010079#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
80 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
81static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
82{
83 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
84 if (uart->cts_pin < 0)
85 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
86
87 /* CTS PIN is negative assertive. */
88 if (UART_GET_CTS(uart))
89 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
90 else
91 return TIOCM_DSR | TIOCM_CAR;
92}
93
94static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
95{
96 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
97 if (uart->rts_pin < 0)
98 return;
99
100 /* RTS PIN is negative assertive. */
101 if (mctrl & TIOCM_RTS)
102 UART_ENABLE_RTS(uart);
103 else
104 UART_DISABLE_RTS(uart);
105}
106
107/*
108 * Handle any change of modem status signal.
109 */
110static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
111{
112 struct bfin_serial_port *uart = dev_id;
113 unsigned int status;
114
115 status = bfin_serial_get_mctrl(&uart->port);
116 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
117#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
118 uart->scts = 1;
119 UART_CLEAR_SCTS(uart);
120 UART_CLEAR_IER(uart, EDSSI);
121#endif
122
123 return IRQ_HANDLED;
124}
125#else
126static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
127{
128 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
129}
130
131static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
132{
133}
134#endif
135
Bryan Wu194de562007-05-06 14:50:30 -0700136/*
137 * interrupts are disabled on entry
138 */
139static void bfin_serial_stop_tx(struct uart_port *port)
140{
141 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000142#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800143 struct circ_buf *xmit = &uart->port.info->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000144#endif
Bryan Wu194de562007-05-06 14:50:30 -0700145
Roy Huangf4d640c92007-07-12 16:43:46 +0800146 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800147 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800148
Bryan Wu194de562007-05-06 14:50:30 -0700149#ifdef CONFIG_SERIAL_BFIN_DMA
150 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800151 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
152 uart->port.icount.tx += uart->tx_count;
153 uart->tx_count = 0;
154 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700155#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800156#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +0800157 /* Clear TFI bit */
158 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700159#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800160 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800161#endif
Bryan Wu194de562007-05-06 14:50:30 -0700162}
163
164/*
165 * port is locked and interrupts are disabled
166 */
167static void bfin_serial_start_tx(struct uart_port *port)
168{
169 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang80d5c472009-01-02 13:40:22 +0000170 struct tty_struct *tty = uart->port.info->port.tty;
171
Sonic Zhangd307d362009-04-07 16:52:26 +0100172#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Roel Kluindaba0282009-04-21 12:24:58 -0700173 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
Sonic Zhangd307d362009-04-07 16:52:26 +0100174 uart->scts = 0;
175 uart_handle_cts_change(&uart->port, uart->scts);
176 }
177#endif
178
Graf Yang80d5c472009-01-02 13:40:22 +0000179 /*
180 * To avoid losting RX interrupt, we reset IR function
181 * before sending data.
182 */
183 if (tty->termios->c_line == N_IRDA)
184 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700185
186#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800187 if (uart->tx_done)
188 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700189#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800190 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800191 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800192#endif
Bryan Wu194de562007-05-06 14:50:30 -0700193}
194
195/*
196 * Interrupts are enabled
197 */
198static void bfin_serial_stop_rx(struct uart_port *port)
199{
200 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000201
Roy Huangf4d640c92007-07-12 16:43:46 +0800202 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700203}
204
205/*
206 * Set the modem control timer to fire immediately.
207 */
208static void bfin_serial_enable_ms(struct uart_port *port)
209{
210}
211
Sonic Zhang474f1a62007-06-29 16:35:17 +0800212
Mike Frysinger50e2e152008-04-25 03:03:03 +0800213#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800214# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
215# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
216#else
217# define UART_GET_ANOMALY_THRESHOLD(uart) 0
218# define UART_SET_ANOMALY_THRESHOLD(uart, v)
219#endif
220
Bryan Wu194de562007-05-06 14:50:30 -0700221#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700222static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
223{
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000224 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700225 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800226 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700227
Sonic Zhang759eb042007-11-21 17:00:32 +0800228 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800229 UART_CLEAR_LSR(uart);
230
231 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700232 uart->port.icount.rx++;
233
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000234#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
235 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
236 if (kgdb_connected && kgdboc_port_line == uart->port.line)
237 if (ch == 0x3) {/* Ctrl + C */
238 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800239 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800240 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000241
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100242 if (!uart->port.info || !uart->port.info->port.tty)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000243 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800244#endif
Sonic Zhangdf04baf2009-04-06 17:32:35 +0100245 tty = uart->port.info->port.tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800246
Mike Frysinger50e2e152008-04-25 03:03:03 +0800247 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800248 /* The BF533 (and BF561) family of processors have a nice anomaly
249 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800250 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800251 * character comes across. Due to the nature of the flood, it is
252 * not possible to reliably catch bytes that are sent too quickly
253 * after this break. So application code talking to the Blackfin
254 * which sends a break signal must allow at least 1.5 character
255 * times after the end of the break for things to stabilize. This
256 * timeout was picked as it must absolutely be larger than 1
257 * character time +/- some percent. So 1.5 sounds good. All other
258 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800259 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800260 if (anomaly_start.tv_sec) {
261 struct timeval curr;
262 suseconds_t usecs;
263
264 if ((~ch & (~ch + 1)) & 0xff)
265 goto known_good_char;
266
267 do_gettimeofday(&curr);
268 if (curr.tv_sec - anomaly_start.tv_sec > 1)
269 goto known_good_char;
270
271 usecs = 0;
272 if (curr.tv_sec != anomaly_start.tv_sec)
273 usecs += USEC_PER_SEC;
274 usecs += curr.tv_usec - anomaly_start.tv_usec;
275
276 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
277 goto known_good_char;
278
279 if (ch)
280 anomaly_start.tv_sec = 0;
281 else
282 anomaly_start = curr;
283
284 return;
285
286 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000287 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800288 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800289 }
Bryan Wu194de562007-05-06 14:50:30 -0700290 }
Bryan Wu194de562007-05-06 14:50:30 -0700291
292 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800293 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800294 if (bfin_revid() < 5)
295 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700296 uart->port.icount.brk++;
297 if (uart_handle_break(&uart->port))
298 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800299 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800300 }
301 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700302 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800303 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700304 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800305 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700306 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800307
308 status &= uart->port.read_status_mask;
309
310 if (status & BI)
311 flg = TTY_BREAK;
312 else if (status & PE)
313 flg = TTY_PARITY;
314 else if (status & FE)
315 flg = TTY_FRAME;
316 else
Bryan Wu194de562007-05-06 14:50:30 -0700317 flg = TTY_NORMAL;
318
319 if (uart_handle_sysrq_char(&uart->port, ch))
320 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700321
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800322 uart_insert_char(&uart->port, status, OE, ch, flg);
323
324 ignore_char:
325 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700326}
327
328static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
329{
330 struct circ_buf *xmit = &uart->port.info->xmit;
331
Bryan Wu194de562007-05-06 14:50:30 -0700332 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100333#ifdef CONFIG_BF54x
334 /* Clear TFI bit */
335 UART_PUT_LSR(uart, TFI);
336#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100337 /* Anomaly notes:
338 * 05000215 - we always clear ETBEI within last UART TX
339 * interrupt to end a string. It is always set
340 * when start a new tx.
341 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100342 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700343 return;
344 }
345
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800346 if (uart->port.x_char) {
347 UART_PUT_CHAR(uart, uart->port.x_char);
348 uart->port.icount.tx++;
349 uart->port.x_char = 0;
350 }
351
Sonic Zhang759eb042007-11-21 17:00:32 +0800352 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
353 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
354 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
355 uart->port.icount.tx++;
356 SSYNC();
357 }
Bryan Wu194de562007-05-06 14:50:30 -0700358
359 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
360 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700361}
362
Aubrey Li5c4e4722007-05-21 18:09:38 +0800363static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
364{
365 struct bfin_serial_port *uart = dev_id;
366
Roy Huangf4d640c92007-07-12 16:43:46 +0800367 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800368 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800369 bfin_serial_rx_chars(uart);
370 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800371
Aubrey Li5c4e4722007-05-21 18:09:38 +0800372 return IRQ_HANDLED;
373}
374
375static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700376{
377 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700378
Sonic Zhangd307d362009-04-07 16:52:26 +0100379#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Roel Kluindaba0282009-04-21 12:24:58 -0700380 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
Sonic Zhangd307d362009-04-07 16:52:26 +0100381 uart->scts = 0;
382 uart_handle_cts_change(&uart->port, uart->scts);
383 }
384#endif
Roy Huangf4d640c92007-07-12 16:43:46 +0800385 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800386 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800387 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700388 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800389
Bryan Wu194de562007-05-06 14:50:30 -0700390 return IRQ_HANDLED;
391}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800392#endif
Bryan Wu194de562007-05-06 14:50:30 -0700393
Bryan Wu194de562007-05-06 14:50:30 -0700394#ifdef CONFIG_SERIAL_BFIN_DMA
395static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
396{
397 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700398
Bryan Wu194de562007-05-06 14:50:30 -0700399 uart->tx_done = 0;
400
Bryan Wu194de562007-05-06 14:50:30 -0700401 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800402 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700403 uart->tx_done = 1;
404 return;
405 }
406
Sonic Zhang1b733512007-12-21 16:45:12 +0800407 if (uart->port.x_char) {
408 UART_PUT_CHAR(uart, uart->port.x_char);
409 uart->port.icount.tx++;
410 uart->port.x_char = 0;
411 }
412
Bryan Wu194de562007-05-06 14:50:30 -0700413 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
414 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
415 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
416 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
417 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
418 set_dma_config(uart->tx_dma_channel,
419 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
420 INTR_ON_BUF,
421 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800422 DATA_SIZE_8,
423 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700424 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
425 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
426 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100427 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700428 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800429
Roy Huangf4d640c92007-07-12 16:43:46 +0800430 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700431}
432
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800433static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700434{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100435 struct tty_struct *tty = uart->port.info->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700436 int i, flg, status;
437
438 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800439 UART_CLEAR_LSR(uart);
440
Sonic Zhang56f5de82008-02-25 15:19:09 +0800441 uart->port.icount.rx +=
442 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
443 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700444
445 if (status & BI) {
446 uart->port.icount.brk++;
447 if (uart_handle_break(&uart->port))
448 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800449 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800450 }
451 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700452 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800453 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700454 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800455 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700456 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800457
458 status &= uart->port.read_status_mask;
459
460 if (status & BI)
461 flg = TTY_BREAK;
462 else if (status & PE)
463 flg = TTY_PARITY;
464 else if (status & FE)
465 flg = TTY_FRAME;
466 else
Bryan Wu194de562007-05-06 14:50:30 -0700467 flg = TTY_NORMAL;
468
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100469 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800470 if (i >= UART_XMIT_SIZE)
471 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100472 if (i == uart->rx_dma_buf.head)
473 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800474 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
475 uart_insert_char(&uart->port, status, OE,
476 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700477 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800478
479 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700480 tty_flip_buffer_push(tty);
481}
482
483void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
484{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100485 int x_pos, pos;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000486
Sonic Zhang2860b792009-06-11 13:51:33 +0100487 dma_disable_irq(uart->rx_dma_channel);
488 spin_lock_bh(&uart->port.lock);
Bryan Wu194de562007-05-06 14:50:30 -0700489
Sonic Zhang8516c562009-06-11 13:38:16 +0100490 /* 2D DMA RX buffer ring is used. Because curr_y_count and
491 * curr_x_count can't be read as an atomic operation,
492 * curr_y_count should be read before curr_x_count. When
493 * curr_x_count is read, curr_y_count may already indicate
494 * next buffer line. But, the position calculated here is
495 * still indicate the old line. The wrong position data may
496 * be smaller than current buffer tail, which cause garbages
497 * are received if it is not prohibit.
498 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800499 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
500 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
501 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100502 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800503 uart->rx_dma_nrows = 0;
504 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700505 if (x_pos == DMA_RX_XCOUNT)
506 x_pos = 0;
507
508 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100509 /* Ignore receiving data if new position is in the same line of
510 * current buffer tail and small.
511 */
512 if (pos > uart->rx_dma_buf.tail ||
513 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800514 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700515 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800516 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700517 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800518
Sonic Zhang2860b792009-06-11 13:51:33 +0100519 spin_unlock_bh(&uart->port.lock);
520 dma_enable_irq(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000521
Sonic Zhang0a278422008-04-25 04:36:47 +0800522 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700523}
524
525static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
526{
527 struct bfin_serial_port *uart = dev_id;
528 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700529
Sonic Zhangd307d362009-04-07 16:52:26 +0100530#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Roel Kluindaba0282009-04-21 12:24:58 -0700531 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
Sonic Zhangd307d362009-04-07 16:52:26 +0100532 uart->scts = 0;
533 uart_handle_cts_change(&uart->port, uart->scts);
534 }
535#endif
536
Bryan Wu194de562007-05-06 14:50:30 -0700537 spin_lock(&uart->port.lock);
538 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700539 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800540 clear_dma_irqstat(uart->tx_dma_channel);
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100541 /* Anomaly notes:
542 * 05000215 - we always clear ETBEI within last UART TX
543 * interrupt to end a string. It is always set
544 * when start a new tx.
545 */
Roy Huangf4d640c92007-07-12 16:43:46 +0800546 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800547 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
548 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800549
Sonic Zhang56f5de82008-02-25 15:19:09 +0800550 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
551 uart_write_wakeup(&uart->port);
552
Sonic Zhang1b733512007-12-21 16:45:12 +0800553 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700554 }
555
556 spin_unlock(&uart->port.lock);
557 return IRQ_HANDLED;
558}
559
560static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
561{
562 struct bfin_serial_port *uart = dev_id;
563 unsigned short irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100564 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800565
Bryan Wu194de562007-05-06 14:50:30 -0700566 spin_lock(&uart->port.lock);
567 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
568 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100569
570 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100571 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100572 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100573 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100574 uart->rx_dma_nrows = 0;
575
576 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
577 if (pos > uart->rx_dma_buf.tail ||
578 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
579 uart->rx_dma_buf.head = pos;
580 bfin_serial_dma_rx_chars(uart);
581 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
582 }
583
Bryan Wu194de562007-05-06 14:50:30 -0700584 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800585
Bryan Wu194de562007-05-06 14:50:30 -0700586 return IRQ_HANDLED;
587}
588#endif
589
590/*
591 * Return TIOCSER_TEMT when transmitter is not busy.
592 */
593static unsigned int bfin_serial_tx_empty(struct uart_port *port)
594{
595 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
596 unsigned short lsr;
597
598 lsr = UART_GET_LSR(uart);
599 if (lsr & TEMT)
600 return TIOCSER_TEMT;
601 else
602 return 0;
603}
604
Bryan Wu194de562007-05-06 14:50:30 -0700605static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
606{
Mike Frysingercf686762007-06-11 16:12:49 +0800607 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
608 u16 lcr = UART_GET_LCR(uart);
609 if (break_state)
610 lcr |= SB;
611 else
612 lcr &= ~SB;
613 UART_PUT_LCR(uart, lcr);
614 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700615}
616
617static int bfin_serial_startup(struct uart_port *port)
618{
619 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
620
621#ifdef CONFIG_SERIAL_BFIN_DMA
622 dma_addr_t dma_handle;
623
624 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
625 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
626 return -EBUSY;
627 }
628
629 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
630 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
631 free_dma(uart->rx_dma_channel);
632 return -EBUSY;
633 }
634
635 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
636 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
637
638 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
639 uart->rx_dma_buf.head = 0;
640 uart->rx_dma_buf.tail = 0;
641 uart->rx_dma_nrows = 0;
642
643 set_dma_config(uart->rx_dma_channel,
644 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
645 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800646 DATA_SIZE_8,
647 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700648 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
649 set_dma_x_modify(uart->rx_dma_channel, 1);
650 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
651 set_dma_y_modify(uart->rx_dma_channel, 1);
652 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
653 enable_dma(uart->rx_dma_channel);
654
655 uart->rx_dma_timer.data = (unsigned long)(uart);
656 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
657 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
658 add_timer(&(uart->rx_dma_timer));
659#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100660# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000661 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
662 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
663 kgdboc_break_enabled = 0;
664 else {
665# endif
Sonic Zhanga359cca2007-10-10 16:47:58 +0800666 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700667 "BFIN_UART_RX", uart)) {
668 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
669 return -EBUSY;
670 }
671
672 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800673 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700674 "BFIN_UART_TX", uart)) {
675 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
676 free_irq(uart->port.irq, uart);
677 return -EBUSY;
678 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100679
680# ifdef CONFIG_BF54x
681 {
682 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
683
684 switch (uart->port.irq) {
685 case IRQ_UART3_RX:
686 uart_dma_ch_rx = CH_UART3_RX;
687 uart_dma_ch_tx = CH_UART3_TX;
688 break;
689 case IRQ_UART2_RX:
690 uart_dma_ch_rx = CH_UART2_RX;
691 uart_dma_ch_tx = CH_UART2_TX;
692 break;
693 default:
694 uart_dma_ch_rx = uart_dma_ch_tx = 0;
695 break;
696 };
697
698 if (uart_dma_ch_rx &&
699 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
700 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
701 free_irq(uart->port.irq, uart);
702 free_irq(uart->port.irq + 1, uart);
703 return -EBUSY;
704 }
705 if (uart_dma_ch_tx &&
706 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
707 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
708 free_dma(uart_dma_ch_rx);
709 free_irq(uart->port.irq, uart);
710 free_irq(uart->port.irq + 1, uart);
711 return -EBUSY;
712 }
713 }
714# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100715# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000716 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
717 }
718# endif
Bryan Wu194de562007-05-06 14:50:30 -0700719#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100720
721#ifdef CONFIG_SERIAL_BFIN_CTSRTS
722 if (uart->cts_pin >= 0) {
723 if (request_irq(gpio_to_irq(uart->cts_pin),
724 bfin_serial_mctrl_cts_int,
725 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
726 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
727 uart->cts_pin = -1;
728 pr_info("Unable to attach BlackFin UART CTS interrupt.\
729 So, disable it.\n");
730 }
731 }
732 if (uart->rts_pin >= 0) {
733 gpio_request(uart->rts_pin, DRIVER_NAME);
734 gpio_direction_output(uart->rts_pin, 0);
735 }
736#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100737#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
738 if (request_irq(uart->status_irq,
739 bfin_serial_mctrl_cts_int,
740 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
741 pr_info("Unable to attach BlackFin UART Modem \
742 Status interrupt.\n");
743 }
744
745 if (uart->cts_pin >= 0) {
746 gpio_request(uart->cts_pin, DRIVER_NAME);
747 gpio_direction_output(uart->cts_pin, 1);
748 }
749 if (uart->rts_pin >= 0) {
750 gpio_request(uart->rts_pin, DRIVER_NAME);
751 gpio_direction_output(uart->rts_pin, 0);
752 }
753
754 /* CTS RTS PINs are negative assertive. */
755 UART_PUT_MCR(uart, ACTS);
756 UART_SET_IER(uart, EDSSI);
757#endif
758
Roy Huangf4d640c92007-07-12 16:43:46 +0800759 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700760 return 0;
761}
762
763static void bfin_serial_shutdown(struct uart_port *port)
764{
765 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
766
767#ifdef CONFIG_SERIAL_BFIN_DMA
768 disable_dma(uart->tx_dma_channel);
769 free_dma(uart->tx_dma_channel);
770 disable_dma(uart->rx_dma_channel);
771 free_dma(uart->rx_dma_channel);
772 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800773 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700774#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100775#ifdef CONFIG_BF54x
776 switch (uart->port.irq) {
777 case IRQ_UART3_RX:
778 free_dma(CH_UART3_RX);
779 free_dma(CH_UART3_TX);
780 break;
781 case IRQ_UART2_RX:
782 free_dma(CH_UART2_RX);
783 free_dma(CH_UART2_TX);
784 break;
785 default:
786 break;
787 };
788#endif
Bryan Wu194de562007-05-06 14:50:30 -0700789 free_irq(uart->port.irq, uart);
790 free_irq(uart->port.irq+1, uart);
791#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100792
Sonic Zhangd307d362009-04-07 16:52:26 +0100793#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100794 if (uart->cts_pin >= 0)
795 free_irq(gpio_to_irq(uart->cts_pin), uart);
796 if (uart->rts_pin >= 0)
797 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100798#endif
799#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
800 if (uart->cts_pin >= 0)
801 gpio_free(uart->cts_pin);
802 if (uart->rts_pin >= 0)
803 gpio_free(uart->rts_pin);
804 if (UART_GET_IER(uart) && EDSSI)
805 free_irq(uart->status_irq, uart);
806#endif
Bryan Wu194de562007-05-06 14:50:30 -0700807}
808
809static void
810bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
811 struct ktermios *old)
812{
813 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
814 unsigned long flags;
815 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800816 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700817
818 switch (termios->c_cflag & CSIZE) {
819 case CS8:
820 lcr = WLS(8);
821 break;
822 case CS7:
823 lcr = WLS(7);
824 break;
825 case CS6:
826 lcr = WLS(6);
827 break;
828 case CS5:
829 lcr = WLS(5);
830 break;
831 default:
832 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700833 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700834 }
835
Sonic Zhang84507792009-06-11 13:50:20 +0100836 /* Anomaly notes:
837 * 05000231 - STOP bit is always set to 1 whatever the user is set.
838 */
839 if (termios->c_cflag & CSTOPB) {
840 if (ANOMALY_05000231)
841 printk(KERN_WARNING "STOP bits other than 1 is not "
842 "supported in case of anomaly 05000231.\n");
843 else
844 lcr |= STB;
845 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800846 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700847 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800848 if (!(termios->c_cflag & PARODD))
849 lcr |= EPS;
850 if (termios->c_cflag & CMSPAR)
851 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700852
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800853 port->read_status_mask = OE;
854 if (termios->c_iflag & INPCK)
855 port->read_status_mask |= (FE | PE);
856 if (termios->c_iflag & (BRKINT | PARMRK))
857 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700858
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800859 /*
860 * Characters to ignore
861 */
862 port->ignore_status_mask = 0;
863 if (termios->c_iflag & IGNPAR)
864 port->ignore_status_mask |= FE | PE;
865 if (termios->c_iflag & IGNBRK) {
866 port->ignore_status_mask |= BI;
867 /*
868 * If we're ignoring parity and break indicators,
869 * ignore overruns too (for real raw support).
870 */
871 if (termios->c_iflag & IGNPAR)
872 port->ignore_status_mask |= OE;
873 }
Bryan Wu194de562007-05-06 14:50:30 -0700874
875 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangf4487102009-04-06 17:32:49 +0100876 quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
Bryan Wu194de562007-05-06 14:50:30 -0700877 spin_lock_irqsave(&uart->port.lock, flags);
878
Mike Frysinger8851c712007-12-24 19:48:04 +0800879 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
880
Bryan Wu194de562007-05-06 14:50:30 -0700881 /* Disable UART */
882 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800883 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700884
885 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800886 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700887
888 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700889 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
890 SSYNC();
891
892 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800893 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700894
895 UART_PUT_LCR(uart, lcr);
896
897 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800898 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700899
900 val = UART_GET_GCTL(uart);
901 val |= UCEN;
902 UART_PUT_GCTL(uart, val);
903
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100904 /* Port speed changed, update the per-port timeout. */
905 uart_update_timeout(port, termios->c_cflag, baud);
906
Bryan Wu194de562007-05-06 14:50:30 -0700907 spin_unlock_irqrestore(&uart->port.lock, flags);
908}
909
910static const char *bfin_serial_type(struct uart_port *port)
911{
912 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
913
914 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
915}
916
917/*
918 * Release the memory region(s) being used by 'port'.
919 */
920static void bfin_serial_release_port(struct uart_port *port)
921{
922}
923
924/*
925 * Request the memory region(s) being used by 'port'.
926 */
927static int bfin_serial_request_port(struct uart_port *port)
928{
929 return 0;
930}
931
932/*
933 * Configure/autoconfigure the port.
934 */
935static void bfin_serial_config_port(struct uart_port *port, int flags)
936{
937 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
938
939 if (flags & UART_CONFIG_TYPE &&
940 bfin_serial_request_port(&uart->port) == 0)
941 uart->port.type = PORT_BFIN;
942}
943
944/*
945 * Verify the new serial_struct (for TIOCSSERIAL).
946 * The only change we allow are to the flags and type, and
947 * even then only between PORT_BFIN and PORT_UNKNOWN
948 */
949static int
950bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
951{
952 return 0;
953}
954
Graf Yang7d01b472008-02-29 11:31:08 +0800955/*
956 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
957 * In other cases, disable IrDA function.
958 */
Graf Yang3b8458a2008-06-07 15:36:33 +0800959static void bfin_serial_set_ldisc(struct uart_port *port)
Graf Yang7d01b472008-02-29 11:31:08 +0800960{
Graf Yang3b8458a2008-06-07 15:36:33 +0800961 int line = port->line;
Graf Yang7d01b472008-02-29 11:31:08 +0800962 unsigned short val;
963
Takashi Iwaia88487c2008-07-16 21:54:42 +0100964 if (line >= port->info->port.tty->driver->num)
Graf Yang7d01b472008-02-29 11:31:08 +0800965 return;
966
Alan Coxb1cbefe2008-08-04 17:22:11 +0100967 switch (port->info->port.tty->termios->c_line) {
Graf Yang7d01b472008-02-29 11:31:08 +0800968 case N_IRDA:
969 val = UART_GET_GCTL(&bfin_serial_ports[line]);
970 val |= (IREN | RPOLC);
971 UART_PUT_GCTL(&bfin_serial_ports[line], val);
972 break;
973 default:
974 val = UART_GET_GCTL(&bfin_serial_ports[line]);
975 val &= ~(IREN | RPOLC);
976 UART_PUT_GCTL(&bfin_serial_ports[line], val);
977 }
978}
979
Sonic Zhang6f955702009-04-07 16:51:15 +0100980static void bfin_serial_reset_irda(struct uart_port *port)
981{
982 int line = port->line;
983 unsigned short val;
984
985 val = UART_GET_GCTL(&bfin_serial_ports[line]);
986 val &= ~(IREN | RPOLC);
987 UART_PUT_GCTL(&bfin_serial_ports[line], val);
988 SSYNC();
989 val |= (IREN | RPOLC);
990 UART_PUT_GCTL(&bfin_serial_ports[line], val);
991 SSYNC();
992}
993
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000994#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100995/* Anomaly notes:
996 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
997 * losing other bits of UART_LSR is not a problem here.
998 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000999static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
1000{
1001 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1002
1003 while (!(UART_GET_LSR(uart) & THRE))
1004 cpu_relax();
1005
1006 UART_CLEAR_DLAB(uart);
1007 UART_PUT_CHAR(uart, (unsigned char)chr);
1008}
1009
1010static int bfin_serial_poll_get_char(struct uart_port *port)
1011{
1012 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1013 unsigned char chr;
1014
1015 while (!(UART_GET_LSR(uart) & DR))
1016 cpu_relax();
1017
1018 UART_CLEAR_DLAB(uart);
1019 chr = UART_GET_CHAR(uart);
1020
1021 return chr;
1022}
1023#endif
1024
1025#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1026 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1027static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1028{
1029 if (kgdboc_break_enabled) {
1030 kgdboc_break_enabled = 0;
1031 bfin_serial_shutdown(port);
1032 }
1033}
1034
1035static int bfin_kgdboc_port_startup(struct uart_port *port)
1036{
1037 kgdboc_port_line = port->line;
1038 kgdboc_break_enabled = !bfin_serial_startup(port);
1039 return 0;
1040}
1041#endif
1042
Bryan Wu194de562007-05-06 14:50:30 -07001043static struct uart_ops bfin_serial_pops = {
1044 .tx_empty = bfin_serial_tx_empty,
1045 .set_mctrl = bfin_serial_set_mctrl,
1046 .get_mctrl = bfin_serial_get_mctrl,
1047 .stop_tx = bfin_serial_stop_tx,
1048 .start_tx = bfin_serial_start_tx,
1049 .stop_rx = bfin_serial_stop_rx,
1050 .enable_ms = bfin_serial_enable_ms,
1051 .break_ctl = bfin_serial_break_ctl,
1052 .startup = bfin_serial_startup,
1053 .shutdown = bfin_serial_shutdown,
1054 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001055 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001056 .type = bfin_serial_type,
1057 .release_port = bfin_serial_release_port,
1058 .request_port = bfin_serial_request_port,
1059 .config_port = bfin_serial_config_port,
1060 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001061#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1062 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1063 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1064 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1065#endif
1066#ifdef CONFIG_CONSOLE_POLL
1067 .poll_put_char = bfin_serial_poll_put_char,
1068 .poll_get_char = bfin_serial_poll_get_char,
1069#endif
Bryan Wu194de562007-05-06 14:50:30 -07001070};
1071
Sonic Zhang6f955702009-04-07 16:51:15 +01001072static void __init bfin_serial_hw_init(void)
1073{
1074#ifdef CONFIG_SERIAL_BFIN_UART0
1075 peripheral_request(P_UART0_TX, DRIVER_NAME);
1076 peripheral_request(P_UART0_RX, DRIVER_NAME);
1077#endif
1078
1079#ifdef CONFIG_SERIAL_BFIN_UART1
1080 peripheral_request(P_UART1_TX, DRIVER_NAME);
1081 peripheral_request(P_UART1_RX, DRIVER_NAME);
1082
1083# if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
1084 peripheral_request(P_UART1_RTS, DRIVER_NAME);
1085 peripheral_request(P_UART1_CTS, DRIVER_NAME);
1086# endif
1087#endif
1088
1089#ifdef CONFIG_SERIAL_BFIN_UART2
1090 peripheral_request(P_UART2_TX, DRIVER_NAME);
1091 peripheral_request(P_UART2_RX, DRIVER_NAME);
1092#endif
1093
1094#ifdef CONFIG_SERIAL_BFIN_UART3
1095 peripheral_request(P_UART3_TX, DRIVER_NAME);
1096 peripheral_request(P_UART3_RX, DRIVER_NAME);
1097
1098# if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
1099 peripheral_request(P_UART3_RTS, DRIVER_NAME);
1100 peripheral_request(P_UART3_CTS, DRIVER_NAME);
1101# endif
1102#endif
1103}
1104
Bryan Wu194de562007-05-06 14:50:30 -07001105static void __init bfin_serial_init_ports(void)
1106{
1107 static int first = 1;
1108 int i;
1109
1110 if (!first)
1111 return;
1112 first = 0;
1113
Sonic Zhang6f955702009-04-07 16:51:15 +01001114 bfin_serial_hw_init();
1115
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001116 for (i = 0; i < nr_active_ports; i++) {
Mike Frysinger9c529a32009-06-22 18:37:24 +01001117 spin_lock_init(&bfin_serial_ports[i].port.lock);
Bryan Wu194de562007-05-06 14:50:30 -07001118 bfin_serial_ports[i].port.uartclk = get_sclk();
Graf Yangb3ef5ab2008-10-13 10:33:42 +01001119 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
Bryan Wu194de562007-05-06 14:50:30 -07001120 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
1121 bfin_serial_ports[i].port.line = i;
1122 bfin_serial_ports[i].port.iotype = UPIO_MEM;
1123 bfin_serial_ports[i].port.membase =
1124 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
1125 bfin_serial_ports[i].port.mapbase =
1126 bfin_serial_resource[i].uart_base_addr;
1127 bfin_serial_ports[i].port.irq =
1128 bfin_serial_resource[i].uart_irq;
Sonic Zhangd307d362009-04-07 16:52:26 +01001129 bfin_serial_ports[i].status_irq =
1130 bfin_serial_resource[i].uart_status_irq;
Bryan Wu194de562007-05-06 14:50:30 -07001131 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
1132#ifdef CONFIG_SERIAL_BFIN_DMA
1133 bfin_serial_ports[i].tx_done = 1;
1134 bfin_serial_ports[i].tx_count = 0;
1135 bfin_serial_ports[i].tx_dma_channel =
1136 bfin_serial_resource[i].uart_tx_dma_channel;
1137 bfin_serial_ports[i].rx_dma_channel =
1138 bfin_serial_resource[i].uart_rx_dma_channel;
1139 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -07001140#endif
Sonic Zhangd307d362009-04-07 16:52:26 +01001141#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1142 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001143 bfin_serial_ports[i].cts_pin =
1144 bfin_serial_resource[i].uart_cts_pin;
1145 bfin_serial_ports[i].rts_pin =
1146 bfin_serial_resource[i].uart_rts_pin;
1147#endif
Bryan Wu194de562007-05-06 14:50:30 -07001148 }
1149}
1150
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001151#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001152/*
1153 * If the port was already initialised (eg, by a boot loader),
1154 * try to determine the current setup.
1155 */
1156static void __init
1157bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1158 int *parity, int *bits)
1159{
1160 unsigned short status;
1161
1162 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1163 if (status == (ERBFI | ETBEI)) {
1164 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001165 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001166
1167 lcr = UART_GET_LCR(uart);
1168
1169 *parity = 'n';
1170 if (lcr & PEN) {
1171 if (lcr & EPS)
1172 *parity = 'e';
1173 else
1174 *parity = 'o';
1175 }
1176 switch (lcr & 0x03) {
1177 case 0: *bits = 5; break;
1178 case 1: *bits = 6; break;
1179 case 2: *bits = 7; break;
1180 case 3: *bits = 8; break;
1181 }
1182 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001183 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001184
1185 dll = UART_GET_DLL(uart);
1186 dlh = UART_GET_DLH(uart);
1187
1188 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001189 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001190
1191 *baud = get_sclk() / (16*(dll | dlh << 8));
1192 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001193 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001194}
Robin Getz0ae53642007-10-09 17:24:49 +08001195
Robin Getz0ae53642007-10-09 17:24:49 +08001196static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001197
1198static int __init
1199bfin_serial_console_setup(struct console *co, char *options)
1200{
1201 struct bfin_serial_port *uart;
1202 int baud = 57600;
1203 int bits = 8;
1204 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001205# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1206 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001207 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001208# else
Bryan Wu194de562007-05-06 14:50:30 -07001209 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001210# endif
Bryan Wu194de562007-05-06 14:50:30 -07001211
1212 /*
1213 * Check whether an invalid uart number has been specified, and
1214 * if so, search for the first available port that does have
1215 * console support.
1216 */
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001217 if (co->index == -1 || co->index >= nr_active_ports)
Bryan Wu194de562007-05-06 14:50:30 -07001218 co->index = 0;
1219 uart = &bfin_serial_ports[co->index];
1220
1221 if (options)
1222 uart_parse_options(options, &baud, &parity, &bits, &flow);
1223 else
1224 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1225
1226 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001227}
1228#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1229 defined (CONFIG_EARLY_PRINTK) */
1230
1231#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1232static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1233{
1234 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1235 while (!(UART_GET_LSR(uart) & THRE))
1236 barrier();
1237 UART_PUT_CHAR(uart, ch);
1238 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -07001239}
1240
Robin Getz0ae53642007-10-09 17:24:49 +08001241/*
1242 * Interrupts are disabled on entering
1243 */
1244static void
1245bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1246{
1247 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
Mike Frysinger59e4e3e2009-04-06 17:32:28 +01001248 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001249
1250 spin_lock_irqsave(&uart->port.lock, flags);
1251 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1252 spin_unlock_irqrestore(&uart->port.lock, flags);
1253
1254}
1255
Bryan Wu194de562007-05-06 14:50:30 -07001256static struct console bfin_serial_console = {
1257 .name = BFIN_SERIAL_NAME,
1258 .write = bfin_serial_console_write,
1259 .device = uart_console_device,
1260 .setup = bfin_serial_console_setup,
1261 .flags = CON_PRINTBUFFER,
1262 .index = -1,
1263 .data = &bfin_serial_reg,
1264};
1265
1266static int __init bfin_serial_rs_console_init(void)
1267{
1268 bfin_serial_init_ports();
1269 register_console(&bfin_serial_console);
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001270
Bryan Wu194de562007-05-06 14:50:30 -07001271 return 0;
1272}
1273console_initcall(bfin_serial_rs_console_init);
1274
1275#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1276#else
1277#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001278#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1279
1280
1281#ifdef CONFIG_EARLY_PRINTK
1282static __init void early_serial_putc(struct uart_port *port, int ch)
1283{
1284 unsigned timeout = 0xffff;
1285 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1286
1287 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1288 cpu_relax();
1289 UART_PUT_CHAR(uart, ch);
1290}
1291
1292static __init void early_serial_write(struct console *con, const char *s,
1293 unsigned int n)
1294{
1295 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1296 unsigned int i;
1297
1298 for (i = 0; i < n; i++, s++) {
1299 if (*s == '\n')
1300 early_serial_putc(&uart->port, '\r');
1301 early_serial_putc(&uart->port, *s);
1302 }
1303}
1304
Robin Getz7de7c552009-06-11 13:38:57 +01001305/*
1306 * This should have a .setup or .early_setup in it, but then things get called
1307 * without the command line options, and the baud rate gets messed up - so
1308 * don't let the common infrastructure play with things. (see calls to setup
1309 * & earlysetup in ./kernel/printk.c:register_console()
1310 */
Mike Frysingerc1113402008-10-13 10:32:35 +01001311static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001312 .name = "early_BFuart",
1313 .write = early_serial_write,
1314 .device = uart_console_device,
1315 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001316 .index = -1,
1317 .data = &bfin_serial_reg,
1318};
1319
1320struct console __init *bfin_earlyserial_init(unsigned int port,
1321 unsigned int cflag)
1322{
1323 struct bfin_serial_port *uart;
1324 struct ktermios t;
1325
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001326 if (port == -1 || port >= nr_active_ports)
Robin Getz0ae53642007-10-09 17:24:49 +08001327 port = 0;
1328 bfin_serial_init_ports();
1329 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001330 uart = &bfin_serial_ports[port];
1331 t.c_cflag = cflag;
1332 t.c_iflag = 0;
1333 t.c_oflag = 0;
1334 t.c_lflag = ICANON;
1335 t.c_line = port;
1336 bfin_serial_set_termios(&uart->port, &t, &t);
1337 return &bfin_early_serial_console;
1338}
1339
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001340#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001341
1342static struct uart_driver bfin_serial_reg = {
1343 .owner = THIS_MODULE,
1344 .driver_name = "bfin-uart",
1345 .dev_name = BFIN_SERIAL_NAME,
1346 .major = BFIN_SERIAL_MAJOR,
1347 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001348 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001349 .cons = BFIN_SERIAL_CONSOLE,
1350};
1351
1352static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1353{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001354 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001355
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001356 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001357 if (bfin_serial_ports[i].port.dev != &dev->dev)
1358 continue;
1359 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1360 }
Bryan Wu194de562007-05-06 14:50:30 -07001361
1362 return 0;
1363}
1364
1365static int bfin_serial_resume(struct platform_device *dev)
1366{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001367 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001368
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001369 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001370 if (bfin_serial_ports[i].port.dev != &dev->dev)
1371 continue;
1372 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1373 }
Bryan Wu194de562007-05-06 14:50:30 -07001374
1375 return 0;
1376}
1377
1378static int bfin_serial_probe(struct platform_device *dev)
1379{
1380 struct resource *res = dev->resource;
1381 int i;
1382
1383 for (i = 0; i < dev->num_resources; i++, res++)
1384 if (res->flags & IORESOURCE_MEM)
1385 break;
1386
1387 if (i < dev->num_resources) {
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001388 for (i = 0; i < nr_active_ports; i++, res++) {
Bryan Wu194de562007-05-06 14:50:30 -07001389 if (bfin_serial_ports[i].port.mapbase != res->start)
1390 continue;
1391 bfin_serial_ports[i].port.dev = &dev->dev;
1392 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
Bryan Wu194de562007-05-06 14:50:30 -07001393 }
1394 }
1395
1396 return 0;
1397}
1398
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001399static int bfin_serial_remove(struct platform_device *dev)
Bryan Wu194de562007-05-06 14:50:30 -07001400{
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001401 int i;
Bryan Wu194de562007-05-06 14:50:30 -07001402
Mike Frysingerc9607ec2008-10-13 10:33:16 +01001403 for (i = 0; i < nr_active_ports; i++) {
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001404 if (bfin_serial_ports[i].port.dev != &dev->dev)
1405 continue;
1406 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1407 bfin_serial_ports[i].port.dev = NULL;
Sonic Zhangd307d362009-04-07 16:52:26 +01001408#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1409 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001410 gpio_free(bfin_serial_ports[i].cts_pin);
1411 gpio_free(bfin_serial_ports[i].rts_pin);
Bryan Wu194de562007-05-06 14:50:30 -07001412#endif
Sonic Zhangccfbc3e2008-10-13 10:32:44 +01001413 }
Bryan Wu194de562007-05-06 14:50:30 -07001414
1415 return 0;
1416}
1417
1418static struct platform_driver bfin_serial_driver = {
1419 .probe = bfin_serial_probe,
1420 .remove = bfin_serial_remove,
1421 .suspend = bfin_serial_suspend,
1422 .resume = bfin_serial_resume,
1423 .driver = {
1424 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001425 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001426 },
1427};
1428
1429static int __init bfin_serial_init(void)
1430{
1431 int ret;
1432
1433 pr_info("Serial: Blackfin serial driver\n");
1434
1435 bfin_serial_init_ports();
1436
1437 ret = uart_register_driver(&bfin_serial_reg);
1438 if (ret == 0) {
1439 ret = platform_driver_register(&bfin_serial_driver);
1440 if (ret) {
1441 pr_debug("uart register failed\n");
1442 uart_unregister_driver(&bfin_serial_reg);
1443 }
1444 }
Bryan Wu194de562007-05-06 14:50:30 -07001445 return ret;
1446}
1447
1448static void __exit bfin_serial_exit(void)
1449{
1450 platform_driver_unregister(&bfin_serial_driver);
1451 uart_unregister_driver(&bfin_serial_reg);
1452}
1453
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001454
Bryan Wu194de562007-05-06 14:50:30 -07001455module_init(bfin_serial_init);
1456module_exit(bfin_serial_exit);
1457
1458MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1459MODULE_DESCRIPTION("Blackfin generic serial port driver");
1460MODULE_LICENSE("GPL");
1461MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001462MODULE_ALIAS("platform:bfin-uart");