blob: 26953bfa6922f4ffbebc8da4bb7a2ac60f672695 [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 *
Sonic Zhang57afb392009-09-09 10:46:19 +00004 * Copyright 2006-2010 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
Sonic Zhang57afb392009-09-09 10:46:19 +000015#define DRIVER_NAME "bfin-uart"
16#define pr_fmt(fmt) DRIVER_NAME ": " fmt
17
Bryan Wu194de562007-05-06 14:50:30 -070018#include <linux/module.h>
19#include <linux/ioport.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Mike Frysinger599b7142010-01-19 06:13:13 -050021#include <linux/io.h>
Bryan Wu194de562007-05-06 14:50:30 -070022#include <linux/init.h>
23#include <linux/console.h>
24#include <linux/sysrq.h>
25#include <linux/platform_device.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial_core.h>
Sonic Zhang57afb392009-09-09 10:46:19 +000029#include <linux/gpio.h>
30#include <linux/irq.h>
31#include <linux/kgdb.h>
32#include <linux/slab.h>
Sonic Zhangb6100992010-10-27 04:16:47 -040033#include <linux/dma-mapping.h>
Bryan Wu194de562007-05-06 14:50:30 -070034
Sonic Zhang57afb392009-09-09 10:46:19 +000035#include <asm/portmux.h>
Bryan Wu194de562007-05-06 14:50:30 -070036#include <asm/cacheflush.h>
Sonic Zhang57afb392009-09-09 10:46:19 +000037#include <asm/dma.h>
38
39#define port_membase(uart) (((struct bfin_serial_port *)(uart))->port.membase)
40#define get_lsr_cache(uart) (((struct bfin_serial_port *)(uart))->lsr)
41#define put_lsr_cache(uart, v) (((struct bfin_serial_port *)(uart))->lsr = (v))
42#include <asm/bfin_serial.h>
Bryan Wu194de562007-05-06 14:50:30 -070043
Mike Frysinger607c2682009-06-22 18:41:47 +010044#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
Mike Frysinger0271edd2009-08-06 15:20:05 -070048#ifdef CONFIG_SERIAL_BFIN_MODULE
49# undef CONFIG_EARLY_PRINTK
50#endif
51
Bryan Wu194de562007-05-06 14:50:30 -070052/* UART name and device definitions */
Sonic Zhang57afb392009-09-09 10:46:19 +000053#define BFIN_SERIAL_DEV_NAME "ttyBF"
Bryan Wu194de562007-05-06 14:50:30 -070054#define BFIN_SERIAL_MAJOR 204
55#define BFIN_SERIAL_MINOR 64
56
Sonic Zhang57afb392009-09-09 10:46:19 +000057static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
Mike Frysingerc9607ec2008-10-13 10:33:16 +010058
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000059#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
60 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
61
62# ifndef CONFIG_SERIAL_BFIN_PIO
63# error KGDB only support UART in PIO mode.
64# endif
65
66static int kgdboc_port_line;
67static int kgdboc_break_enabled;
68#endif
Bryan Wu194de562007-05-06 14:50:30 -070069/*
70 * Setup for console. Argument comes from the menuconfig
71 */
72#define DMA_RX_XCOUNT 512
73#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
74
Sonic Zhang0aef4562008-02-29 12:08:42 +080075#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070076
77#ifdef CONFIG_SERIAL_BFIN_DMA
78static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
79#else
Bryan Wu194de562007-05-06 14:50:30 -070080static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070081#endif
82
Graf Yang80d5c472009-01-02 13:40:22 +000083static void bfin_serial_reset_irda(struct uart_port *port);
84
Sonic Zhangd307d362009-04-07 16:52:26 +010085#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
86 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
87static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
88{
89 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
90 if (uart->cts_pin < 0)
91 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
92
93 /* CTS PIN is negative assertive. */
94 if (UART_GET_CTS(uart))
95 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
96 else
97 return TIOCM_DSR | TIOCM_CAR;
98}
99
100static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
101{
102 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
103 if (uart->rts_pin < 0)
104 return;
105
106 /* RTS PIN is negative assertive. */
107 if (mctrl & TIOCM_RTS)
108 UART_ENABLE_RTS(uart);
109 else
110 UART_DISABLE_RTS(uart);
111}
112
113/*
114 * Handle any change of modem status signal.
115 */
116static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
117{
118 struct bfin_serial_port *uart = dev_id;
Sonic Zhang64851636d2011-12-13 12:22:02 +0800119 unsigned int status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhangd307d362009-04-07 16:52:26 +0100120#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang64851636d2011-12-13 12:22:02 +0800121 struct tty_struct *tty = uart->port.state->port.tty;
122
Sonic Zhangd307d362009-04-07 16:52:26 +0100123 UART_CLEAR_SCTS(uart);
Sonic Zhang64851636d2011-12-13 12:22:02 +0800124 if (tty->hw_stopped) {
125 if (status) {
126 tty->hw_stopped = 0;
127 uart_write_wakeup(&uart->port);
128 }
129 } else {
130 if (!status)
131 tty->hw_stopped = 1;
132 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100133#endif
Sonic Zhangf5b69402011-12-05 18:13:11 +0800134 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Sonic Zhangd307d362009-04-07 16:52:26 +0100135
136 return IRQ_HANDLED;
137}
138#else
139static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
140{
141 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
142}
143
144static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
145{
146}
147#endif
148
Bryan Wu194de562007-05-06 14:50:30 -0700149/*
150 * interrupts are disabled on entry
151 */
152static void bfin_serial_stop_tx(struct uart_port *port)
153{
154 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000155#ifdef CONFIG_SERIAL_BFIN_DMA
Alan Coxebd2c8f2009-09-19 13:13:28 -0700156 struct circ_buf *xmit = &uart->port.state->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000157#endif
Bryan Wu194de562007-05-06 14:50:30 -0700158
Roy Huangf4d640c92007-07-12 16:43:46 +0800159 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800160 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800161
Bryan Wu194de562007-05-06 14:50:30 -0700162#ifdef CONFIG_SERIAL_BFIN_DMA
163 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800164 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
165 uart->port.icount.tx += uart->tx_count;
166 uart->tx_count = 0;
167 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700168#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800169#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +0800170 /* Clear TFI bit */
171 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700172#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800173 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800174#endif
Bryan Wu194de562007-05-06 14:50:30 -0700175}
176
177/*
178 * port is locked and interrupts are disabled
179 */
180static void bfin_serial_start_tx(struct uart_port *port)
181{
182 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700183 struct tty_struct *tty = uart->port.state->port.tty;
Graf Yang80d5c472009-01-02 13:40:22 +0000184
185 /*
186 * To avoid losting RX interrupt, we reset IR function
187 * before sending data.
188 */
189 if (tty->termios->c_line == N_IRDA)
190 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700191
192#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800193 if (uart->tx_done)
194 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700195#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800196 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800197 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800198#endif
Bryan Wu194de562007-05-06 14:50:30 -0700199}
200
201/*
202 * Interrupts are enabled
203 */
204static void bfin_serial_stop_rx(struct uart_port *port)
205{
206 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000207
Roy Huangf4d640c92007-07-12 16:43:46 +0800208 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700209}
210
211/*
212 * Set the modem control timer to fire immediately.
213 */
214static void bfin_serial_enable_ms(struct uart_port *port)
215{
216}
217
Sonic Zhang474f1a62007-06-29 16:35:17 +0800218
Mike Frysinger50e2e152008-04-25 03:03:03 +0800219#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800220# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
221# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
222#else
223# define UART_GET_ANOMALY_THRESHOLD(uart) 0
224# define UART_SET_ANOMALY_THRESHOLD(uart, v)
225#endif
226
Bryan Wu194de562007-05-06 14:50:30 -0700227#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700228static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
229{
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000230 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700231 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800232 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700233
Sonic Zhang759eb042007-11-21 17:00:32 +0800234 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800235 UART_CLEAR_LSR(uart);
236
Sonic Zhangbb7e58f2011-07-19 18:09:24 +0800237 ch = UART_GET_CHAR(uart);
238 uart->port.icount.rx++;
Bryan Wu194de562007-05-06 14:50:30 -0700239
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000240#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
241 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhangcdc592d2010-01-19 06:13:12 -0500242 if (kgdb_connected && kgdboc_port_line == uart->port.line
243 && kgdboc_break_enabled)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000244 if (ch == 0x3) {/* Ctrl + C */
245 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800246 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800247 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000248
Alan Coxebd2c8f2009-09-19 13:13:28 -0700249 if (!uart->port.state || !uart->port.state->port.tty)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000250 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800251#endif
Alan Coxebd2c8f2009-09-19 13:13:28 -0700252 tty = uart->port.state->port.tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800253
Mike Frysinger50e2e152008-04-25 03:03:03 +0800254 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800255 /* The BF533 (and BF561) family of processors have a nice anomaly
256 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800257 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800258 * character comes across. Due to the nature of the flood, it is
259 * not possible to reliably catch bytes that are sent too quickly
260 * after this break. So application code talking to the Blackfin
261 * which sends a break signal must allow at least 1.5 character
262 * times after the end of the break for things to stabilize. This
263 * timeout was picked as it must absolutely be larger than 1
264 * character time +/- some percent. So 1.5 sounds good. All other
265 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800266 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800267 if (anomaly_start.tv_sec) {
268 struct timeval curr;
269 suseconds_t usecs;
270
271 if ((~ch & (~ch + 1)) & 0xff)
272 goto known_good_char;
273
274 do_gettimeofday(&curr);
275 if (curr.tv_sec - anomaly_start.tv_sec > 1)
276 goto known_good_char;
277
278 usecs = 0;
279 if (curr.tv_sec != anomaly_start.tv_sec)
280 usecs += USEC_PER_SEC;
281 usecs += curr.tv_usec - anomaly_start.tv_usec;
282
283 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
284 goto known_good_char;
285
286 if (ch)
287 anomaly_start.tv_sec = 0;
288 else
289 anomaly_start = curr;
290
291 return;
292
293 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000294 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800295 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800296 }
Bryan Wu194de562007-05-06 14:50:30 -0700297 }
Bryan Wu194de562007-05-06 14:50:30 -0700298
299 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800300 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800301 if (bfin_revid() < 5)
302 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700303 uart->port.icount.brk++;
304 if (uart_handle_break(&uart->port))
305 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800306 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800307 }
308 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700309 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800310 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700311 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800312 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700313 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800314
315 status &= uart->port.read_status_mask;
316
317 if (status & BI)
318 flg = TTY_BREAK;
319 else if (status & PE)
320 flg = TTY_PARITY;
321 else if (status & FE)
322 flg = TTY_FRAME;
323 else
Bryan Wu194de562007-05-06 14:50:30 -0700324 flg = TTY_NORMAL;
325
326 if (uart_handle_sysrq_char(&uart->port, ch))
327 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700328
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800329 uart_insert_char(&uart->port, status, OE, ch, flg);
330
331 ignore_char:
332 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700333}
334
335static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
336{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700337 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700338
Bryan Wu194de562007-05-06 14:50:30 -0700339 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100340#ifdef CONFIG_BF54x
341 /* Clear TFI bit */
342 UART_PUT_LSR(uart, TFI);
343#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100344 /* Anomaly notes:
345 * 05000215 - we always clear ETBEI within last UART TX
346 * interrupt to end a string. It is always set
347 * when start a new tx.
348 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100349 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700350 return;
351 }
352
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800353 if (uart->port.x_char) {
354 UART_PUT_CHAR(uart, uart->port.x_char);
355 uart->port.icount.tx++;
356 uart->port.x_char = 0;
357 }
358
Sonic Zhang759eb042007-11-21 17:00:32 +0800359 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
360 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
361 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
362 uart->port.icount.tx++;
Sonic Zhang759eb042007-11-21 17:00:32 +0800363 }
Bryan Wu194de562007-05-06 14:50:30 -0700364
365 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
366 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700367}
368
Aubrey Li5c4e4722007-05-21 18:09:38 +0800369static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
370{
371 struct bfin_serial_port *uart = dev_id;
372
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800373 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800374 bfin_serial_rx_chars(uart);
Sonic Zhang759eb042007-11-21 17:00:32 +0800375
Aubrey Li5c4e4722007-05-21 18:09:38 +0800376 return IRQ_HANDLED;
377}
378
379static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700380{
381 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700382
Roy Huangf4d640c92007-07-12 16:43:46 +0800383 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800384 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800385 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700386 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800387
Bryan Wu194de562007-05-06 14:50:30 -0700388 return IRQ_HANDLED;
389}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800390#endif
Bryan Wu194de562007-05-06 14:50:30 -0700391
Bryan Wu194de562007-05-06 14:50:30 -0700392#ifdef CONFIG_SERIAL_BFIN_DMA
393static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
394{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700395 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700396
Bryan Wu194de562007-05-06 14:50:30 -0700397 uart->tx_done = 0;
398
Bryan Wu194de562007-05-06 14:50:30 -0700399 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800400 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700401 uart->tx_done = 1;
402 return;
403 }
404
Sonic Zhang1b733512007-12-21 16:45:12 +0800405 if (uart->port.x_char) {
406 UART_PUT_CHAR(uart, uart->port.x_char);
407 uart->port.icount.tx++;
408 uart->port.x_char = 0;
409 }
410
Bryan Wu194de562007-05-06 14:50:30 -0700411 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
412 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
413 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
414 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
415 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
416 set_dma_config(uart->tx_dma_channel,
417 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
418 INTR_ON_BUF,
419 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800420 DATA_SIZE_8,
421 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700422 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
423 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
424 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100425 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700426 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800427
Roy Huangf4d640c92007-07-12 16:43:46 +0800428 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700429}
430
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800431static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700432{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700433 struct tty_struct *tty = uart->port.state->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700434 int i, flg, status;
435
436 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800437 UART_CLEAR_LSR(uart);
438
Sonic Zhang56f5de82008-02-25 15:19:09 +0800439 uart->port.icount.rx +=
440 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
441 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700442
443 if (status & BI) {
444 uart->port.icount.brk++;
445 if (uart_handle_break(&uart->port))
446 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800447 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800448 }
449 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700450 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800451 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700452 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800453 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700454 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800455
456 status &= uart->port.read_status_mask;
457
458 if (status & BI)
459 flg = TTY_BREAK;
460 else if (status & PE)
461 flg = TTY_PARITY;
462 else if (status & FE)
463 flg = TTY_FRAME;
464 else
Bryan Wu194de562007-05-06 14:50:30 -0700465 flg = TTY_NORMAL;
466
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100467 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800468 if (i >= UART_XMIT_SIZE)
469 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100470 if (i == uart->rx_dma_buf.head)
471 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800472 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
473 uart_insert_char(&uart->port, status, OE,
474 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700475 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800476
477 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700478 tty_flip_buffer_push(tty);
479}
480
481void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
482{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100483 int x_pos, pos;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000484
Sonic Zhang0f66e502011-01-11 00:16:43 -0500485 dma_disable_irq_nosync(uart->rx_dma_channel);
486 spin_lock_bh(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700487
Sonic Zhang8516c562009-06-11 13:38:16 +0100488 /* 2D DMA RX buffer ring is used. Because curr_y_count and
489 * curr_x_count can't be read as an atomic operation,
490 * curr_y_count should be read before curr_x_count. When
491 * curr_x_count is read, curr_y_count may already indicate
492 * next buffer line. But, the position calculated here is
493 * still indicate the old line. The wrong position data may
494 * be smaller than current buffer tail, which cause garbages
495 * are received if it is not prohibit.
496 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800497 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
498 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
499 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100500 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800501 uart->rx_dma_nrows = 0;
502 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700503 if (x_pos == DMA_RX_XCOUNT)
504 x_pos = 0;
505
506 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100507 /* Ignore receiving data if new position is in the same line of
508 * current buffer tail and small.
509 */
510 if (pos > uart->rx_dma_buf.tail ||
511 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800512 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700513 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800514 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700515 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800516
Sonic Zhang0f66e502011-01-11 00:16:43 -0500517 spin_unlock_bh(&uart->rx_lock);
Sonic Zhang2860b792009-06-11 13:51:33 +0100518 dma_enable_irq(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000519
Sonic Zhang0a278422008-04-25 04:36:47 +0800520 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700521}
522
523static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
524{
525 struct bfin_serial_port *uart = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700526 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700527
528 spin_lock(&uart->port.lock);
529 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700530 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800531 clear_dma_irqstat(uart->tx_dma_channel);
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100532 /* Anomaly notes:
533 * 05000215 - we always clear ETBEI within last UART TX
534 * interrupt to end a string. It is always set
535 * when start a new tx.
536 */
Roy Huangf4d640c92007-07-12 16:43:46 +0800537 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800538 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
539 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800540
Sonic Zhang56f5de82008-02-25 15:19:09 +0800541 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
542 uart_write_wakeup(&uart->port);
543
Sonic Zhang1b733512007-12-21 16:45:12 +0800544 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700545 }
546
547 spin_unlock(&uart->port.lock);
548 return IRQ_HANDLED;
549}
550
551static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
552{
553 struct bfin_serial_port *uart = dev_id;
554 unsigned short irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100555 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800556
Sonic Zhang0f66e502011-01-11 00:16:43 -0500557 spin_lock(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700558 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
559 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100560
561 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100562 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100563 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100564 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100565 uart->rx_dma_nrows = 0;
566
567 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
568 if (pos > uart->rx_dma_buf.tail ||
569 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
570 uart->rx_dma_buf.head = pos;
571 bfin_serial_dma_rx_chars(uart);
572 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
573 }
574
Sonic Zhang0f66e502011-01-11 00:16:43 -0500575 spin_unlock(&uart->rx_lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800576
Bryan Wu194de562007-05-06 14:50:30 -0700577 return IRQ_HANDLED;
578}
579#endif
580
581/*
582 * Return TIOCSER_TEMT when transmitter is not busy.
583 */
584static unsigned int bfin_serial_tx_empty(struct uart_port *port)
585{
586 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
587 unsigned short lsr;
588
589 lsr = UART_GET_LSR(uart);
590 if (lsr & TEMT)
591 return TIOCSER_TEMT;
592 else
593 return 0;
594}
595
Bryan Wu194de562007-05-06 14:50:30 -0700596static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
597{
Mike Frysingercf686762007-06-11 16:12:49 +0800598 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
599 u16 lcr = UART_GET_LCR(uart);
600 if (break_state)
601 lcr |= SB;
602 else
603 lcr &= ~SB;
604 UART_PUT_LCR(uart, lcr);
605 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700606}
607
608static int bfin_serial_startup(struct uart_port *port)
609{
610 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
611
612#ifdef CONFIG_SERIAL_BFIN_DMA
613 dma_addr_t dma_handle;
614
615 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
616 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
617 return -EBUSY;
618 }
619
620 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
621 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
622 free_dma(uart->rx_dma_channel);
623 return -EBUSY;
624 }
625
626 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
627 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
628
629 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
630 uart->rx_dma_buf.head = 0;
631 uart->rx_dma_buf.tail = 0;
632 uart->rx_dma_nrows = 0;
633
634 set_dma_config(uart->rx_dma_channel,
635 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
636 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800637 DATA_SIZE_8,
638 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700639 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
640 set_dma_x_modify(uart->rx_dma_channel, 1);
641 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
642 set_dma_y_modify(uart->rx_dma_channel, 1);
643 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
644 enable_dma(uart->rx_dma_channel);
645
646 uart->rx_dma_timer.data = (unsigned long)(uart);
647 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
648 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
649 add_timer(&(uart->rx_dma_timer));
650#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100651# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000652 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
653 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
654 kgdboc_break_enabled = 0;
655 else {
656# endif
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800657 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700658 "BFIN_UART_RX", uart)) {
659 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
660 return -EBUSY;
661 }
662
663 if (request_irq
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800664 (uart->tx_irq, bfin_serial_tx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700665 "BFIN_UART_TX", uart)) {
666 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800667 free_irq(uart->rx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700668 return -EBUSY;
669 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100670
671# ifdef CONFIG_BF54x
672 {
Sonic Zhangb6100992010-10-27 04:16:47 -0400673 /*
674 * UART2 and UART3 on BF548 share interrupt PINs and DMA
675 * controllers with SPORT2 and SPORT3. UART rx and tx
676 * interrupts are generated in PIO mode only when configure
677 * their peripheral mapping registers properly, which means
678 * request corresponding DMA channels in PIO mode as well.
679 */
Sonic Zhangab2375f2008-10-13 10:33:51 +0100680 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
681
Sonic Zhang47918f02011-08-12 11:15:51 +0800682 switch (uart->rx_irq) {
Sonic Zhangab2375f2008-10-13 10:33:51 +0100683 case IRQ_UART3_RX:
684 uart_dma_ch_rx = CH_UART3_RX;
685 uart_dma_ch_tx = CH_UART3_TX;
686 break;
687 case IRQ_UART2_RX:
688 uart_dma_ch_rx = CH_UART2_RX;
689 uart_dma_ch_tx = CH_UART2_TX;
690 break;
691 default:
692 uart_dma_ch_rx = uart_dma_ch_tx = 0;
693 break;
694 };
695
696 if (uart_dma_ch_rx &&
697 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
698 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800699 free_irq(uart->rx_irq, uart);
700 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100701 return -EBUSY;
702 }
703 if (uart_dma_ch_tx &&
704 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
705 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
706 free_dma(uart_dma_ch_rx);
Sonic Zhang47918f02011-08-12 11:15:51 +0800707 free_irq(uart->rx_irq, uart);
708 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100709 return -EBUSY;
710 }
711 }
712# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100713# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000714 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
715 }
716# endif
Bryan Wu194de562007-05-06 14:50:30 -0700717#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100718
719#ifdef CONFIG_SERIAL_BFIN_CTSRTS
720 if (uart->cts_pin >= 0) {
721 if (request_irq(gpio_to_irq(uart->cts_pin),
722 bfin_serial_mctrl_cts_int,
723 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800724 0, "BFIN_UART_CTS", uart)) {
Sonic Zhang6f955702009-04-07 16:51:15 +0100725 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800726 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
Sonic Zhang6f955702009-04-07 16:51:15 +0100727 }
728 }
Sonic Zhang32b44562011-12-05 15:12:50 +0800729 if (uart->rts_pin >= 0) {
730 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
731 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
732 uart->rts_pin = -1;
733 } else
734 gpio_direction_output(uart->rts_pin, 0);
735 }
Sonic Zhang6f955702009-04-07 16:51:15 +0100736#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100737#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangb48dc712011-12-13 12:22:01 +0800738 if (uart->cts_pin >= 0) {
739 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
740 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
741 uart->cts_pin = -1;
742 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
743 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100744
Sonic Zhangb48dc712011-12-13 12:22:01 +0800745 /* CTS RTS PINs are negative assertive. */
746 UART_PUT_MCR(uart, ACTS);
747 UART_SET_IER(uart, EDSSI);
748 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100749#endif
750
Roy Huangf4d640c92007-07-12 16:43:46 +0800751 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700752 return 0;
753}
754
755static void bfin_serial_shutdown(struct uart_port *port)
756{
757 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
758
759#ifdef CONFIG_SERIAL_BFIN_DMA
760 disable_dma(uart->tx_dma_channel);
761 free_dma(uart->tx_dma_channel);
762 disable_dma(uart->rx_dma_channel);
763 free_dma(uart->rx_dma_channel);
764 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800765 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700766#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100767#ifdef CONFIG_BF54x
768 switch (uart->port.irq) {
769 case IRQ_UART3_RX:
770 free_dma(CH_UART3_RX);
771 free_dma(CH_UART3_TX);
772 break;
773 case IRQ_UART2_RX:
774 free_dma(CH_UART2_RX);
775 free_dma(CH_UART2_TX);
776 break;
777 default:
778 break;
779 };
780#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800781 free_irq(uart->rx_irq, uart);
782 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700783#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100784
Sonic Zhangd307d362009-04-07 16:52:26 +0100785#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100786 if (uart->cts_pin >= 0)
787 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800788 if (uart->rts_pin >= 0)
789 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100790#endif
791#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000792 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100793 free_irq(uart->status_irq, uart);
794#endif
Bryan Wu194de562007-05-06 14:50:30 -0700795}
796
797static void
798bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
799 struct ktermios *old)
800{
801 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
802 unsigned long flags;
803 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800804 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700805
806 switch (termios->c_cflag & CSIZE) {
807 case CS8:
808 lcr = WLS(8);
809 break;
810 case CS7:
811 lcr = WLS(7);
812 break;
813 case CS6:
814 lcr = WLS(6);
815 break;
816 case CS5:
817 lcr = WLS(5);
818 break;
819 default:
820 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700821 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700822 }
823
Sonic Zhang84507792009-06-11 13:50:20 +0100824 /* Anomaly notes:
825 * 05000231 - STOP bit is always set to 1 whatever the user is set.
826 */
827 if (termios->c_cflag & CSTOPB) {
828 if (ANOMALY_05000231)
829 printk(KERN_WARNING "STOP bits other than 1 is not "
830 "supported in case of anomaly 05000231.\n");
831 else
832 lcr |= STB;
833 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800834 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700835 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800836 if (!(termios->c_cflag & PARODD))
837 lcr |= EPS;
838 if (termios->c_cflag & CMSPAR)
839 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700840
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400841 spin_lock_irqsave(&uart->port.lock, flags);
842
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800843 port->read_status_mask = OE;
844 if (termios->c_iflag & INPCK)
845 port->read_status_mask |= (FE | PE);
846 if (termios->c_iflag & (BRKINT | PARMRK))
847 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700848
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800849 /*
850 * Characters to ignore
851 */
852 port->ignore_status_mask = 0;
853 if (termios->c_iflag & IGNPAR)
854 port->ignore_status_mask |= FE | PE;
855 if (termios->c_iflag & IGNBRK) {
856 port->ignore_status_mask |= BI;
857 /*
858 * If we're ignoring parity and break indicators,
859 * ignore overruns too (for real raw support).
860 */
861 if (termios->c_iflag & IGNPAR)
862 port->ignore_status_mask |= OE;
863 }
Bryan Wu194de562007-05-06 14:50:30 -0700864
865 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400866 quot = uart_get_divisor(port, baud);
867
868 /* If discipline is not IRDA, apply ANOMALY_05000230 */
869 if (termios->c_line != N_IRDA)
870 quot -= ANOMALY_05000230;
871
Mike Frysinger8851c712007-12-24 19:48:04 +0800872 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
873
Bryan Wu194de562007-05-06 14:50:30 -0700874 /* Disable UART */
875 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800876 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700877
878 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800879 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700880
881 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700882 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
883 SSYNC();
884
885 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800886 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700887
888 UART_PUT_LCR(uart, lcr);
889
890 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800891 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700892
893 val = UART_GET_GCTL(uart);
894 val |= UCEN;
895 UART_PUT_GCTL(uart, val);
896
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100897 /* Port speed changed, update the per-port timeout. */
898 uart_update_timeout(port, termios->c_cflag, baud);
899
Bryan Wu194de562007-05-06 14:50:30 -0700900 spin_unlock_irqrestore(&uart->port.lock, flags);
901}
902
903static const char *bfin_serial_type(struct uart_port *port)
904{
905 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
906
907 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
908}
909
910/*
911 * Release the memory region(s) being used by 'port'.
912 */
913static void bfin_serial_release_port(struct uart_port *port)
914{
915}
916
917/*
918 * Request the memory region(s) being used by 'port'.
919 */
920static int bfin_serial_request_port(struct uart_port *port)
921{
922 return 0;
923}
924
925/*
926 * Configure/autoconfigure the port.
927 */
928static void bfin_serial_config_port(struct uart_port *port, int flags)
929{
930 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
931
932 if (flags & UART_CONFIG_TYPE &&
933 bfin_serial_request_port(&uart->port) == 0)
934 uart->port.type = PORT_BFIN;
935}
936
937/*
938 * Verify the new serial_struct (for TIOCSSERIAL).
939 * The only change we allow are to the flags and type, and
940 * even then only between PORT_BFIN and PORT_UNKNOWN
941 */
942static int
943bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
944{
945 return 0;
946}
947
Graf Yang7d01b472008-02-29 11:31:08 +0800948/*
949 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
950 * In other cases, disable IrDA function.
951 */
Alan Coxd87d9b72010-06-01 22:52:53 +0200952static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
Graf Yang7d01b472008-02-29 11:31:08 +0800953{
Sonic Zhang57afb392009-09-09 10:46:19 +0000954 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang7d01b472008-02-29 11:31:08 +0800955 unsigned short val;
956
Alan Coxd87d9b72010-06-01 22:52:53 +0200957 switch (ld) {
Graf Yang7d01b472008-02-29 11:31:08 +0800958 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000959 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800960 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000961 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800962 break;
963 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000964 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800965 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000966 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800967 }
968}
969
Sonic Zhang6f955702009-04-07 16:51:15 +0100970static void bfin_serial_reset_irda(struct uart_port *port)
971{
Sonic Zhang57afb392009-09-09 10:46:19 +0000972 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang6f955702009-04-07 16:51:15 +0100973 unsigned short val;
974
Sonic Zhang57afb392009-09-09 10:46:19 +0000975 val = UART_GET_GCTL(uart);
Sonic Zhang6f955702009-04-07 16:51:15 +0100976 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000977 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100978 SSYNC();
979 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000980 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100981 SSYNC();
982}
983
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000984#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100985/* Anomaly notes:
986 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
987 * losing other bits of UART_LSR is not a problem here.
988 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000989static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
990{
991 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
992
993 while (!(UART_GET_LSR(uart) & THRE))
994 cpu_relax();
995
996 UART_CLEAR_DLAB(uart);
997 UART_PUT_CHAR(uart, (unsigned char)chr);
998}
999
1000static int bfin_serial_poll_get_char(struct uart_port *port)
1001{
1002 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1003 unsigned char chr;
1004
1005 while (!(UART_GET_LSR(uart) & DR))
1006 cpu_relax();
1007
1008 UART_CLEAR_DLAB(uart);
1009 chr = UART_GET_CHAR(uart);
1010
1011 return chr;
1012}
1013#endif
1014
1015#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1016 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1017static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1018{
1019 if (kgdboc_break_enabled) {
1020 kgdboc_break_enabled = 0;
1021 bfin_serial_shutdown(port);
1022 }
1023}
1024
1025static int bfin_kgdboc_port_startup(struct uart_port *port)
1026{
1027 kgdboc_port_line = port->line;
1028 kgdboc_break_enabled = !bfin_serial_startup(port);
1029 return 0;
1030}
1031#endif
1032
Bryan Wu194de562007-05-06 14:50:30 -07001033static struct uart_ops bfin_serial_pops = {
1034 .tx_empty = bfin_serial_tx_empty,
1035 .set_mctrl = bfin_serial_set_mctrl,
1036 .get_mctrl = bfin_serial_get_mctrl,
1037 .stop_tx = bfin_serial_stop_tx,
1038 .start_tx = bfin_serial_start_tx,
1039 .stop_rx = bfin_serial_stop_rx,
1040 .enable_ms = bfin_serial_enable_ms,
1041 .break_ctl = bfin_serial_break_ctl,
1042 .startup = bfin_serial_startup,
1043 .shutdown = bfin_serial_shutdown,
1044 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001045 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001046 .type = bfin_serial_type,
1047 .release_port = bfin_serial_release_port,
1048 .request_port = bfin_serial_request_port,
1049 .config_port = bfin_serial_config_port,
1050 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001051#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1052 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1053 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1054 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1055#endif
1056#ifdef CONFIG_CONSOLE_POLL
1057 .poll_put_char = bfin_serial_poll_put_char,
1058 .poll_get_char = bfin_serial_poll_get_char,
1059#endif
Bryan Wu194de562007-05-06 14:50:30 -07001060};
1061
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001062#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001063/*
1064 * If the port was already initialised (eg, by a boot loader),
1065 * try to determine the current setup.
1066 */
1067static void __init
1068bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1069 int *parity, int *bits)
1070{
1071 unsigned short status;
1072
1073 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1074 if (status == (ERBFI | ETBEI)) {
1075 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001076 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001077
1078 lcr = UART_GET_LCR(uart);
1079
1080 *parity = 'n';
1081 if (lcr & PEN) {
1082 if (lcr & EPS)
1083 *parity = 'e';
1084 else
1085 *parity = 'o';
1086 }
1087 switch (lcr & 0x03) {
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001088 case 0:
1089 *bits = 5;
1090 break;
1091 case 1:
1092 *bits = 6;
1093 break;
1094 case 2:
1095 *bits = 7;
1096 break;
1097 case 3:
1098 *bits = 8;
1099 break;
Bryan Wu194de562007-05-06 14:50:30 -07001100 }
1101 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001102 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001103
1104 dll = UART_GET_DLL(uart);
1105 dlh = UART_GET_DLH(uart);
1106
1107 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001108 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001109
1110 *baud = get_sclk() / (16*(dll | dlh << 8));
1111 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001112 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001113}
Robin Getz0ae53642007-10-09 17:24:49 +08001114
Robin Getz0ae53642007-10-09 17:24:49 +08001115static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001116
Sonic Zhang57afb392009-09-09 10:46:19 +00001117static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1118{
1119 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1120 while (!(UART_GET_LSR(uart) & THRE))
1121 barrier();
1122 UART_PUT_CHAR(uart, ch);
1123}
1124
1125#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1126 defined (CONFIG_EARLY_PRINTK) */
1127
1128#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1129#define CLASS_BFIN_CONSOLE "bfin-console"
1130/*
1131 * Interrupts are disabled on entering
1132 */
1133static void
1134bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1135{
1136 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&uart->port.lock, flags);
1140 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1141 spin_unlock_irqrestore(&uart->port.lock, flags);
1142
1143}
1144
Bryan Wu194de562007-05-06 14:50:30 -07001145static int __init
1146bfin_serial_console_setup(struct console *co, char *options)
1147{
1148 struct bfin_serial_port *uart;
1149 int baud = 57600;
1150 int bits = 8;
1151 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001152# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1153 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001154 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001155# else
Bryan Wu194de562007-05-06 14:50:30 -07001156 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001157# endif
Bryan Wu194de562007-05-06 14:50:30 -07001158
1159 /*
1160 * Check whether an invalid uart number has been specified, and
1161 * if so, search for the first available port that does have
1162 * console support.
1163 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001164 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1165 return -ENODEV;
1166
1167 uart = bfin_serial_ports[co->index];
1168 if (!uart)
1169 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001170
1171 if (options)
1172 uart_parse_options(options, &baud, &parity, &bits, &flow);
1173 else
1174 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1175
1176 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001177}
Robin Getz0ae53642007-10-09 17:24:49 +08001178
Bryan Wu194de562007-05-06 14:50:30 -07001179static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001180 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001181 .write = bfin_serial_console_write,
1182 .device = uart_console_device,
1183 .setup = bfin_serial_console_setup,
1184 .flags = CON_PRINTBUFFER,
1185 .index = -1,
1186 .data = &bfin_serial_reg,
1187};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001188#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001189#else
1190#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001191#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1192
Sonic Zhang57afb392009-09-09 10:46:19 +00001193#ifdef CONFIG_EARLY_PRINTK
1194static struct bfin_serial_port bfin_earlyprintk_port;
1195#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001196
Sonic Zhang57afb392009-09-09 10:46:19 +00001197/*
1198 * Interrupts are disabled on entering
1199 */
1200static void
1201bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001202{
Sonic Zhang57afb392009-09-09 10:46:19 +00001203 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001204
Sonic Zhang57afb392009-09-09 10:46:19 +00001205 if (bfin_earlyprintk_port.port.line != co->index)
1206 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001207
Sonic Zhang57afb392009-09-09 10:46:19 +00001208 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1209 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1210 bfin_serial_console_putchar);
1211 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001212}
1213
Robin Getz7de7c552009-06-11 13:38:57 +01001214/*
1215 * This should have a .setup or .early_setup in it, but then things get called
1216 * without the command line options, and the baud rate gets messed up - so
1217 * don't let the common infrastructure play with things. (see calls to setup
1218 * & earlysetup in ./kernel/printk.c:register_console()
1219 */
Mike Frysingerc1113402008-10-13 10:32:35 +01001220static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001221 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001222 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001223 .device = uart_console_device,
1224 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001225 .index = -1,
1226 .data = &bfin_serial_reg,
1227};
Sonic Zhang57afb392009-09-09 10:46:19 +00001228#endif
1229
1230static struct uart_driver bfin_serial_reg = {
1231 .owner = THIS_MODULE,
1232 .driver_name = DRIVER_NAME,
1233 .dev_name = BFIN_SERIAL_DEV_NAME,
1234 .major = BFIN_SERIAL_MAJOR,
1235 .minor = BFIN_SERIAL_MINOR,
1236 .nr = BFIN_UART_NR_PORTS,
1237 .cons = BFIN_SERIAL_CONSOLE,
1238};
1239
1240static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1241{
1242 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1243
1244 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1245}
1246
1247static int bfin_serial_resume(struct platform_device *pdev)
1248{
1249 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1250
1251 return uart_resume_port(&bfin_serial_reg, &uart->port);
1252}
1253
1254static int bfin_serial_probe(struct platform_device *pdev)
1255{
1256 struct resource *res;
1257 struct bfin_serial_port *uart = NULL;
1258 int ret = 0;
1259
1260 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1261 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1262 return -ENOENT;
1263 }
1264
1265 if (bfin_serial_ports[pdev->id] == NULL) {
1266
1267 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1268 if (!uart) {
1269 dev_err(&pdev->dev,
1270 "fail to malloc bfin_serial_port\n");
1271 return -ENOMEM;
1272 }
1273 bfin_serial_ports[pdev->id] = uart;
1274
1275#ifdef CONFIG_EARLY_PRINTK
1276 if (!(bfin_earlyprintk_port.port.membase
1277 && bfin_earlyprintk_port.port.line == pdev->id)) {
1278 /*
1279 * If the peripheral PINs of current port is allocated
1280 * in earlyprintk probe stage, don't do it again.
1281 */
1282#endif
1283 ret = peripheral_request_list(
1284 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1285 if (ret) {
1286 dev_err(&pdev->dev,
1287 "fail to request bfin serial peripherals\n");
1288 goto out_error_free_mem;
1289 }
1290#ifdef CONFIG_EARLY_PRINTK
1291 }
1292#endif
1293
1294 spin_lock_init(&uart->port.lock);
1295 uart->port.uartclk = get_sclk();
1296 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1297 uart->port.ops = &bfin_serial_pops;
1298 uart->port.line = pdev->id;
1299 uart->port.iotype = UPIO_MEM;
1300 uart->port.flags = UPF_BOOT_AUTOCONF;
1301
1302 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1303 if (res == NULL) {
1304 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1305 ret = -ENOENT;
1306 goto out_error_free_peripherals;
1307 }
1308
Joe Perches28f65c112011-06-09 09:13:32 -07001309 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001310 if (!uart->port.membase) {
1311 dev_err(&pdev->dev, "Cannot map uart IO\n");
1312 ret = -ENXIO;
1313 goto out_error_free_peripherals;
1314 }
1315 uart->port.mapbase = res->start;
1316
Sonic Zhang47918f02011-08-12 11:15:51 +08001317 uart->tx_irq = platform_get_irq(pdev, 0);
1318 if (uart->tx_irq < 0) {
1319 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001320 ret = -ENOENT;
1321 goto out_error_unmap;
1322 }
1323
Sonic Zhang47918f02011-08-12 11:15:51 +08001324 uart->rx_irq = platform_get_irq(pdev, 1);
1325 if (uart->rx_irq < 0) {
1326 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1327 ret = -ENOENT;
1328 goto out_error_unmap;
1329 }
1330 uart->port.irq = uart->rx_irq;
1331
1332 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001333 if (uart->status_irq < 0) {
1334 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1335 ret = -ENOENT;
1336 goto out_error_unmap;
1337 }
1338
1339#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001340 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001341 uart->tx_done = 1;
1342 uart->tx_count = 0;
1343
1344 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1345 if (res == NULL) {
1346 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1347 ret = -ENOENT;
1348 goto out_error_unmap;
1349 }
1350 uart->tx_dma_channel = res->start;
1351
1352 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1353 if (res == NULL) {
1354 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1355 ret = -ENOENT;
1356 goto out_error_unmap;
1357 }
1358 uart->rx_dma_channel = res->start;
1359
1360 init_timer(&(uart->rx_dma_timer));
1361#endif
1362
1363#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1364 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1365 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1366 if (res == NULL)
1367 uart->cts_pin = -1;
Sonic Zhangee948e32011-12-05 18:13:10 +08001368 else {
Sonic Zhang57afb392009-09-09 10:46:19 +00001369 uart->cts_pin = res->start;
Sonic Zhang64851636d2011-12-13 12:22:02 +08001370#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangee948e32011-12-05 18:13:10 +08001371 uart->port.flags |= ASYNC_CTS_FLOW;
Sonic Zhang64851636d2011-12-13 12:22:02 +08001372#endif
Sonic Zhangee948e32011-12-05 18:13:10 +08001373 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001374
1375 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1376 if (res == NULL)
1377 uart->rts_pin = -1;
1378 else
1379 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001380#endif
1381 }
1382
1383#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1384 if (!is_early_platform_device(pdev)) {
1385#endif
1386 uart = bfin_serial_ports[pdev->id];
1387 uart->port.dev = &pdev->dev;
1388 dev_set_drvdata(&pdev->dev, uart);
1389 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1390#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1391 }
1392#endif
1393
1394 if (!ret)
1395 return 0;
1396
1397 if (uart) {
1398out_error_unmap:
1399 iounmap(uart->port.membase);
1400out_error_free_peripherals:
1401 peripheral_free_list(
1402 (unsigned short *)pdev->dev.platform_data);
1403out_error_free_mem:
1404 kfree(uart);
1405 bfin_serial_ports[pdev->id] = NULL;
1406 }
1407
1408 return ret;
1409}
1410
1411static int __devexit bfin_serial_remove(struct platform_device *pdev)
1412{
1413 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1414
1415 dev_set_drvdata(&pdev->dev, NULL);
1416
1417 if (uart) {
1418 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001419 iounmap(uart->port.membase);
1420 peripheral_free_list(
1421 (unsigned short *)pdev->dev.platform_data);
1422 kfree(uart);
1423 bfin_serial_ports[pdev->id] = NULL;
1424 }
1425
1426 return 0;
1427}
1428
1429static struct platform_driver bfin_serial_driver = {
1430 .probe = bfin_serial_probe,
1431 .remove = __devexit_p(bfin_serial_remove),
1432 .suspend = bfin_serial_suspend,
1433 .resume = bfin_serial_resume,
1434 .driver = {
1435 .name = DRIVER_NAME,
1436 .owner = THIS_MODULE,
1437 },
1438};
1439
1440#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1441static __initdata struct early_platform_driver early_bfin_serial_driver = {
1442 .class_str = CLASS_BFIN_CONSOLE,
1443 .pdrv = &bfin_serial_driver,
1444 .requested_id = EARLY_PLATFORM_ID_UNSET,
1445};
1446
1447static int __init bfin_serial_rs_console_init(void)
1448{
1449 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1450
1451 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1452
1453 register_console(&bfin_serial_console);
1454
1455 return 0;
1456}
1457console_initcall(bfin_serial_rs_console_init);
1458#endif
1459
1460#ifdef CONFIG_EARLY_PRINTK
1461/*
1462 * Memory can't be allocated dynamically during earlyprink init stage.
1463 * So, do individual probe for earlyprink with a static uart port variable.
1464 */
1465static int bfin_earlyprintk_probe(struct platform_device *pdev)
1466{
1467 struct resource *res;
1468 int ret;
1469
1470 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1471 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1472 return -ENOENT;
1473 }
1474
1475 ret = peripheral_request_list(
1476 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1477 if (ret) {
1478 dev_err(&pdev->dev,
1479 "fail to request bfin serial peripherals\n");
1480 return ret;
1481 }
1482
1483 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1484 if (res == NULL) {
1485 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1486 ret = -ENOENT;
1487 goto out_error_free_peripherals;
1488 }
1489
1490 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c112011-06-09 09:13:32 -07001491 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001492 if (!bfin_earlyprintk_port.port.membase) {
1493 dev_err(&pdev->dev, "Cannot map uart IO\n");
1494 ret = -ENXIO;
1495 goto out_error_free_peripherals;
1496 }
1497 bfin_earlyprintk_port.port.mapbase = res->start;
1498 bfin_earlyprintk_port.port.line = pdev->id;
1499 bfin_earlyprintk_port.port.uartclk = get_sclk();
1500 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1501 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1502
1503 return 0;
1504
1505out_error_free_peripherals:
1506 peripheral_free_list(
1507 (unsigned short *)pdev->dev.platform_data);
1508
1509 return ret;
1510}
1511
1512static struct platform_driver bfin_earlyprintk_driver = {
1513 .probe = bfin_earlyprintk_probe,
1514 .driver = {
1515 .name = DRIVER_NAME,
1516 .owner = THIS_MODULE,
1517 },
1518};
1519
1520static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1521 .class_str = CLASS_BFIN_EARLYPRINTK,
1522 .pdrv = &bfin_earlyprintk_driver,
1523 .requested_id = EARLY_PLATFORM_ID_UNSET,
1524};
Robin Getz0ae53642007-10-09 17:24:49 +08001525
1526struct console __init *bfin_earlyserial_init(unsigned int port,
1527 unsigned int cflag)
1528{
Robin Getz0ae53642007-10-09 17:24:49 +08001529 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001530 char port_name[20];
1531
1532 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1533 return NULL;
1534
1535 /*
1536 * Only probe resource of the given port in earlyprintk boot arg.
1537 * The expected port id should be indicated in port name string.
1538 */
1539 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1540 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1541 port_name);
1542 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1543
1544 if (!bfin_earlyprintk_port.port.membase)
1545 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001546
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001547#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1548 /*
1549 * If we are using early serial, don't let the normal console rewind
1550 * log buffer, since that causes things to be printed multiple times
1551 */
1552 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1553#endif
1554
Robin Getz0ae53642007-10-09 17:24:49 +08001555 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001556 t.c_cflag = cflag;
1557 t.c_iflag = 0;
1558 t.c_oflag = 0;
1559 t.c_lflag = ICANON;
1560 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001561 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1562
Robin Getz0ae53642007-10-09 17:24:49 +08001563 return &bfin_early_serial_console;
1564}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001565#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001566
Bryan Wu194de562007-05-06 14:50:30 -07001567static int __init bfin_serial_init(void)
1568{
1569 int ret;
1570
Sonic Zhang57afb392009-09-09 10:46:19 +00001571 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001572
1573 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001574 if (ret) {
1575 pr_err("failed to register %s:%d\n",
1576 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001577 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001578
1579 ret = platform_driver_register(&bfin_serial_driver);
1580 if (ret) {
1581 pr_err("fail to register bfin uart\n");
1582 uart_unregister_driver(&bfin_serial_reg);
1583 }
1584
Bryan Wu194de562007-05-06 14:50:30 -07001585 return ret;
1586}
1587
1588static void __exit bfin_serial_exit(void)
1589{
1590 platform_driver_unregister(&bfin_serial_driver);
1591 uart_unregister_driver(&bfin_serial_reg);
1592}
1593
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001594
Bryan Wu194de562007-05-06 14:50:30 -07001595module_init(bfin_serial_init);
1596module_exit(bfin_serial_exit);
1597
Sonic Zhang57afb392009-09-09 10:46:19 +00001598MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001599MODULE_DESCRIPTION("Blackfin generic serial port driver");
1600MODULE_LICENSE("GPL");
1601MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001602MODULE_ALIAS("platform:bfin-uart");