blob: dedd9ab06d82669ed5f2a58a1f7125f115630c7e [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;
119 unsigned int status;
120
121 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhangd307d362009-04-07 16:52:26 +0100122#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangd307d362009-04-07 16:52:26 +0100123 UART_CLEAR_SCTS(uart);
Sonic Zhangd307d362009-04-07 16:52:26 +0100124#endif
Sonic Zhangf5b69402011-12-05 18:13:11 +0800125 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Sonic Zhangd307d362009-04-07 16:52:26 +0100126
127 return IRQ_HANDLED;
128}
129#else
130static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
131{
132 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
133}
134
135static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
136{
137}
138#endif
139
Bryan Wu194de562007-05-06 14:50:30 -0700140/*
141 * interrupts are disabled on entry
142 */
143static void bfin_serial_stop_tx(struct uart_port *port)
144{
145 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000146#ifdef CONFIG_SERIAL_BFIN_DMA
Alan Coxebd2c8f2009-09-19 13:13:28 -0700147 struct circ_buf *xmit = &uart->port.state->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000148#endif
Bryan Wu194de562007-05-06 14:50:30 -0700149
Roy Huangf4d640c92007-07-12 16:43:46 +0800150 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800151 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800152
Bryan Wu194de562007-05-06 14:50:30 -0700153#ifdef CONFIG_SERIAL_BFIN_DMA
154 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800155 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
156 uart->port.icount.tx += uart->tx_count;
157 uart->tx_count = 0;
158 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700159#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800160#ifdef CONFIG_BF54x
Roy Huangf4d640c92007-07-12 16:43:46 +0800161 /* Clear TFI bit */
162 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700163#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800164 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800165#endif
Bryan Wu194de562007-05-06 14:50:30 -0700166}
167
168/*
169 * port is locked and interrupts are disabled
170 */
171static void bfin_serial_start_tx(struct uart_port *port)
172{
173 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700174 struct tty_struct *tty = uart->port.state->port.tty;
Graf Yang80d5c472009-01-02 13:40:22 +0000175
176 /*
177 * To avoid losting RX interrupt, we reset IR function
178 * before sending data.
179 */
180 if (tty->termios->c_line == N_IRDA)
181 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700182
183#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800184 if (uart->tx_done)
185 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700186#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800187 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800188 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800189#endif
Bryan Wu194de562007-05-06 14:50:30 -0700190}
191
192/*
193 * Interrupts are enabled
194 */
195static void bfin_serial_stop_rx(struct uart_port *port)
196{
197 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000198
Roy Huangf4d640c92007-07-12 16:43:46 +0800199 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700200}
201
202/*
203 * Set the modem control timer to fire immediately.
204 */
205static void bfin_serial_enable_ms(struct uart_port *port)
206{
207}
208
Sonic Zhang474f1a62007-06-29 16:35:17 +0800209
Mike Frysinger50e2e152008-04-25 03:03:03 +0800210#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800211# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
212# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
213#else
214# define UART_GET_ANOMALY_THRESHOLD(uart) 0
215# define UART_SET_ANOMALY_THRESHOLD(uart, v)
216#endif
217
Bryan Wu194de562007-05-06 14:50:30 -0700218#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700219static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
220{
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000221 struct tty_struct *tty = NULL;
Bryan Wu194de562007-05-06 14:50:30 -0700222 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800223 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700224
Sonic Zhang759eb042007-11-21 17:00:32 +0800225 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800226 UART_CLEAR_LSR(uart);
227
Sonic Zhangbb7e58f2011-07-19 18:09:24 +0800228 ch = UART_GET_CHAR(uart);
229 uart->port.icount.rx++;
Bryan Wu194de562007-05-06 14:50:30 -0700230
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000231#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
232 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhangcdc592d2010-01-19 06:13:12 -0500233 if (kgdb_connected && kgdboc_port_line == uart->port.line
234 && kgdboc_break_enabled)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000235 if (ch == 0x3) {/* Ctrl + C */
236 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800237 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800238 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000239
Alan Coxebd2c8f2009-09-19 13:13:28 -0700240 if (!uart->port.state || !uart->port.state->port.tty)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000241 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800242#endif
Alan Coxebd2c8f2009-09-19 13:13:28 -0700243 tty = uart->port.state->port.tty;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800244
Mike Frysinger50e2e152008-04-25 03:03:03 +0800245 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800246 /* The BF533 (and BF561) family of processors have a nice anomaly
247 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800248 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800249 * character comes across. Due to the nature of the flood, it is
250 * not possible to reliably catch bytes that are sent too quickly
251 * after this break. So application code talking to the Blackfin
252 * which sends a break signal must allow at least 1.5 character
253 * times after the end of the break for things to stabilize. This
254 * timeout was picked as it must absolutely be larger than 1
255 * character time +/- some percent. So 1.5 sounds good. All other
256 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800257 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800258 if (anomaly_start.tv_sec) {
259 struct timeval curr;
260 suseconds_t usecs;
261
262 if ((~ch & (~ch + 1)) & 0xff)
263 goto known_good_char;
264
265 do_gettimeofday(&curr);
266 if (curr.tv_sec - anomaly_start.tv_sec > 1)
267 goto known_good_char;
268
269 usecs = 0;
270 if (curr.tv_sec != anomaly_start.tv_sec)
271 usecs += USEC_PER_SEC;
272 usecs += curr.tv_usec - anomaly_start.tv_usec;
273
274 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
275 goto known_good_char;
276
277 if (ch)
278 anomaly_start.tv_sec = 0;
279 else
280 anomaly_start = curr;
281
282 return;
283
284 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000285 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800286 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800287 }
Bryan Wu194de562007-05-06 14:50:30 -0700288 }
Bryan Wu194de562007-05-06 14:50:30 -0700289
290 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800291 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800292 if (bfin_revid() < 5)
293 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700294 uart->port.icount.brk++;
295 if (uart_handle_break(&uart->port))
296 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800297 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800298 }
299 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700300 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800301 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700302 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800303 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700304 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800305
306 status &= uart->port.read_status_mask;
307
308 if (status & BI)
309 flg = TTY_BREAK;
310 else if (status & PE)
311 flg = TTY_PARITY;
312 else if (status & FE)
313 flg = TTY_FRAME;
314 else
Bryan Wu194de562007-05-06 14:50:30 -0700315 flg = TTY_NORMAL;
316
317 if (uart_handle_sysrq_char(&uart->port, ch))
318 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700319
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800320 uart_insert_char(&uart->port, status, OE, ch, flg);
321
322 ignore_char:
323 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700324}
325
326static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
327{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700328 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700329
Bryan Wu194de562007-05-06 14:50:30 -0700330 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100331#ifdef CONFIG_BF54x
332 /* Clear TFI bit */
333 UART_PUT_LSR(uart, TFI);
334#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100335 /* Anomaly notes:
336 * 05000215 - we always clear ETBEI within last UART TX
337 * interrupt to end a string. It is always set
338 * when start a new tx.
339 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100340 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700341 return;
342 }
343
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800344 if (uart->port.x_char) {
345 UART_PUT_CHAR(uart, uart->port.x_char);
346 uart->port.icount.tx++;
347 uart->port.x_char = 0;
348 }
349
Sonic Zhang759eb042007-11-21 17:00:32 +0800350 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
351 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
352 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
353 uart->port.icount.tx++;
Sonic Zhang759eb042007-11-21 17:00:32 +0800354 }
Bryan Wu194de562007-05-06 14:50:30 -0700355
356 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
357 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700358}
359
Aubrey Li5c4e4722007-05-21 18:09:38 +0800360static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
361{
362 struct bfin_serial_port *uart = dev_id;
363
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800364 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800365 bfin_serial_rx_chars(uart);
Sonic Zhang759eb042007-11-21 17:00:32 +0800366
Aubrey Li5c4e4722007-05-21 18:09:38 +0800367 return IRQ_HANDLED;
368}
369
370static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700371{
372 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700373
Roy Huangf4d640c92007-07-12 16:43:46 +0800374 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800375 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800376 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700377 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800378
Bryan Wu194de562007-05-06 14:50:30 -0700379 return IRQ_HANDLED;
380}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800381#endif
Bryan Wu194de562007-05-06 14:50:30 -0700382
Bryan Wu194de562007-05-06 14:50:30 -0700383#ifdef CONFIG_SERIAL_BFIN_DMA
384static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
385{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700386 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700387
Bryan Wu194de562007-05-06 14:50:30 -0700388 uart->tx_done = 0;
389
Bryan Wu194de562007-05-06 14:50:30 -0700390 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800391 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700392 uart->tx_done = 1;
393 return;
394 }
395
Sonic Zhang1b733512007-12-21 16:45:12 +0800396 if (uart->port.x_char) {
397 UART_PUT_CHAR(uart, uart->port.x_char);
398 uart->port.icount.tx++;
399 uart->port.x_char = 0;
400 }
401
Bryan Wu194de562007-05-06 14:50:30 -0700402 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
403 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
404 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
405 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
406 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
407 set_dma_config(uart->tx_dma_channel,
408 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
409 INTR_ON_BUF,
410 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800411 DATA_SIZE_8,
412 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700413 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
414 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
415 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100416 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700417 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800418
Roy Huangf4d640c92007-07-12 16:43:46 +0800419 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700420}
421
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800422static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700423{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700424 struct tty_struct *tty = uart->port.state->port.tty;
Bryan Wu194de562007-05-06 14:50:30 -0700425 int i, flg, status;
426
427 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800428 UART_CLEAR_LSR(uart);
429
Sonic Zhang56f5de82008-02-25 15:19:09 +0800430 uart->port.icount.rx +=
431 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
432 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700433
434 if (status & BI) {
435 uart->port.icount.brk++;
436 if (uart_handle_break(&uart->port))
437 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800438 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800439 }
440 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700441 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800442 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700443 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800444 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700445 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800446
447 status &= uart->port.read_status_mask;
448
449 if (status & BI)
450 flg = TTY_BREAK;
451 else if (status & PE)
452 flg = TTY_PARITY;
453 else if (status & FE)
454 flg = TTY_FRAME;
455 else
Bryan Wu194de562007-05-06 14:50:30 -0700456 flg = TTY_NORMAL;
457
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100458 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800459 if (i >= UART_XMIT_SIZE)
460 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100461 if (i == uart->rx_dma_buf.head)
462 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800463 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
464 uart_insert_char(&uart->port, status, OE,
465 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700466 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800467
468 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700469 tty_flip_buffer_push(tty);
470}
471
472void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
473{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100474 int x_pos, pos;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000475
Sonic Zhang0f66e502011-01-11 00:16:43 -0500476 dma_disable_irq_nosync(uart->rx_dma_channel);
477 spin_lock_bh(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700478
Sonic Zhang8516c562009-06-11 13:38:16 +0100479 /* 2D DMA RX buffer ring is used. Because curr_y_count and
480 * curr_x_count can't be read as an atomic operation,
481 * curr_y_count should be read before curr_x_count. When
482 * curr_x_count is read, curr_y_count may already indicate
483 * next buffer line. But, the position calculated here is
484 * still indicate the old line. The wrong position data may
485 * be smaller than current buffer tail, which cause garbages
486 * are received if it is not prohibit.
487 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800488 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
489 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
490 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100491 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800492 uart->rx_dma_nrows = 0;
493 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700494 if (x_pos == DMA_RX_XCOUNT)
495 x_pos = 0;
496
497 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100498 /* Ignore receiving data if new position is in the same line of
499 * current buffer tail and small.
500 */
501 if (pos > uart->rx_dma_buf.tail ||
502 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800503 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700504 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800505 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700506 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800507
Sonic Zhang0f66e502011-01-11 00:16:43 -0500508 spin_unlock_bh(&uart->rx_lock);
Sonic Zhang2860b792009-06-11 13:51:33 +0100509 dma_enable_irq(uart->rx_dma_channel);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000510
Sonic Zhang0a278422008-04-25 04:36:47 +0800511 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700512}
513
514static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
515{
516 struct bfin_serial_port *uart = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700517 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700518
519 spin_lock(&uart->port.lock);
520 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700521 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800522 clear_dma_irqstat(uart->tx_dma_channel);
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100523 /* Anomaly notes:
524 * 05000215 - we always clear ETBEI within last UART TX
525 * interrupt to end a string. It is always set
526 * when start a new tx.
527 */
Roy Huangf4d640c92007-07-12 16:43:46 +0800528 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800529 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
530 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800531
Sonic Zhang56f5de82008-02-25 15:19:09 +0800532 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
533 uart_write_wakeup(&uart->port);
534
Sonic Zhang1b733512007-12-21 16:45:12 +0800535 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700536 }
537
538 spin_unlock(&uart->port.lock);
539 return IRQ_HANDLED;
540}
541
542static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
543{
544 struct bfin_serial_port *uart = dev_id;
545 unsigned short irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100546 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800547
Sonic Zhang0f66e502011-01-11 00:16:43 -0500548 spin_lock(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700549 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
550 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100551
552 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100553 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100554 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100555 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100556 uart->rx_dma_nrows = 0;
557
558 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
559 if (pos > uart->rx_dma_buf.tail ||
560 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
561 uart->rx_dma_buf.head = pos;
562 bfin_serial_dma_rx_chars(uart);
563 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
564 }
565
Sonic Zhang0f66e502011-01-11 00:16:43 -0500566 spin_unlock(&uart->rx_lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800567
Bryan Wu194de562007-05-06 14:50:30 -0700568 return IRQ_HANDLED;
569}
570#endif
571
572/*
573 * Return TIOCSER_TEMT when transmitter is not busy.
574 */
575static unsigned int bfin_serial_tx_empty(struct uart_port *port)
576{
577 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
578 unsigned short lsr;
579
580 lsr = UART_GET_LSR(uart);
581 if (lsr & TEMT)
582 return TIOCSER_TEMT;
583 else
584 return 0;
585}
586
Bryan Wu194de562007-05-06 14:50:30 -0700587static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
588{
Mike Frysingercf686762007-06-11 16:12:49 +0800589 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
590 u16 lcr = UART_GET_LCR(uart);
591 if (break_state)
592 lcr |= SB;
593 else
594 lcr &= ~SB;
595 UART_PUT_LCR(uart, lcr);
596 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700597}
598
599static int bfin_serial_startup(struct uart_port *port)
600{
601 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
602
603#ifdef CONFIG_SERIAL_BFIN_DMA
604 dma_addr_t dma_handle;
605
606 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
607 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
608 return -EBUSY;
609 }
610
611 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
612 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
613 free_dma(uart->rx_dma_channel);
614 return -EBUSY;
615 }
616
617 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
618 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
619
620 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
621 uart->rx_dma_buf.head = 0;
622 uart->rx_dma_buf.tail = 0;
623 uart->rx_dma_nrows = 0;
624
625 set_dma_config(uart->rx_dma_channel,
626 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
627 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800628 DATA_SIZE_8,
629 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700630 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
631 set_dma_x_modify(uart->rx_dma_channel, 1);
632 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
633 set_dma_y_modify(uart->rx_dma_channel, 1);
634 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
635 enable_dma(uart->rx_dma_channel);
636
637 uart->rx_dma_timer.data = (unsigned long)(uart);
638 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
639 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
640 add_timer(&(uart->rx_dma_timer));
641#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100642# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000643 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
644 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
645 kgdboc_break_enabled = 0;
646 else {
647# endif
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800648 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700649 "BFIN_UART_RX", uart)) {
650 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
651 return -EBUSY;
652 }
653
654 if (request_irq
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800655 (uart->tx_irq, bfin_serial_tx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700656 "BFIN_UART_TX", uart)) {
657 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800658 free_irq(uart->rx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700659 return -EBUSY;
660 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100661
662# ifdef CONFIG_BF54x
663 {
Sonic Zhangb6100992010-10-27 04:16:47 -0400664 /*
665 * UART2 and UART3 on BF548 share interrupt PINs and DMA
666 * controllers with SPORT2 and SPORT3. UART rx and tx
667 * interrupts are generated in PIO mode only when configure
668 * their peripheral mapping registers properly, which means
669 * request corresponding DMA channels in PIO mode as well.
670 */
Sonic Zhangab2375f2008-10-13 10:33:51 +0100671 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
672
Sonic Zhang47918f02011-08-12 11:15:51 +0800673 switch (uart->rx_irq) {
Sonic Zhangab2375f2008-10-13 10:33:51 +0100674 case IRQ_UART3_RX:
675 uart_dma_ch_rx = CH_UART3_RX;
676 uart_dma_ch_tx = CH_UART3_TX;
677 break;
678 case IRQ_UART2_RX:
679 uart_dma_ch_rx = CH_UART2_RX;
680 uart_dma_ch_tx = CH_UART2_TX;
681 break;
682 default:
683 uart_dma_ch_rx = uart_dma_ch_tx = 0;
684 break;
685 };
686
687 if (uart_dma_ch_rx &&
688 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
689 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800690 free_irq(uart->rx_irq, uart);
691 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100692 return -EBUSY;
693 }
694 if (uart_dma_ch_tx &&
695 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
696 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
697 free_dma(uart_dma_ch_rx);
Sonic Zhang47918f02011-08-12 11:15:51 +0800698 free_irq(uart->rx_irq, uart);
699 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100700 return -EBUSY;
701 }
702 }
703# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100704# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000705 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
706 }
707# endif
Bryan Wu194de562007-05-06 14:50:30 -0700708#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100709
710#ifdef CONFIG_SERIAL_BFIN_CTSRTS
711 if (uart->cts_pin >= 0) {
712 if (request_irq(gpio_to_irq(uart->cts_pin),
713 bfin_serial_mctrl_cts_int,
714 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800715 0, "BFIN_UART_CTS", uart)) {
Sonic Zhang6f955702009-04-07 16:51:15 +0100716 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800717 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
Sonic Zhang6f955702009-04-07 16:51:15 +0100718 }
719 }
Sonic Zhang32b44562011-12-05 15:12:50 +0800720 if (uart->rts_pin >= 0) {
721 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
722 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
723 uart->rts_pin = -1;
724 } else
725 gpio_direction_output(uart->rts_pin, 0);
726 }
Sonic Zhang6f955702009-04-07 16:51:15 +0100727#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100728#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangb48dc712011-12-13 12:22:01 +0800729 if (uart->cts_pin >= 0) {
730 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
731 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
732 uart->cts_pin = -1;
733 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
734 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100735
Sonic Zhangb48dc712011-12-13 12:22:01 +0800736 /* CTS RTS PINs are negative assertive. */
737 UART_PUT_MCR(uart, ACTS);
738 UART_SET_IER(uart, EDSSI);
739 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100740#endif
741
Roy Huangf4d640c92007-07-12 16:43:46 +0800742 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700743 return 0;
744}
745
746static void bfin_serial_shutdown(struct uart_port *port)
747{
748 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
749
750#ifdef CONFIG_SERIAL_BFIN_DMA
751 disable_dma(uart->tx_dma_channel);
752 free_dma(uart->tx_dma_channel);
753 disable_dma(uart->rx_dma_channel);
754 free_dma(uart->rx_dma_channel);
755 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800756 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700757#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100758#ifdef CONFIG_BF54x
759 switch (uart->port.irq) {
760 case IRQ_UART3_RX:
761 free_dma(CH_UART3_RX);
762 free_dma(CH_UART3_TX);
763 break;
764 case IRQ_UART2_RX:
765 free_dma(CH_UART2_RX);
766 free_dma(CH_UART2_TX);
767 break;
768 default:
769 break;
770 };
771#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800772 free_irq(uart->rx_irq, uart);
773 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700774#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100775
Sonic Zhangd307d362009-04-07 16:52:26 +0100776#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100777 if (uart->cts_pin >= 0)
778 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800779 if (uart->rts_pin >= 0)
780 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100781#endif
782#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000783 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100784 free_irq(uart->status_irq, uart);
785#endif
Bryan Wu194de562007-05-06 14:50:30 -0700786}
787
788static void
789bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
790 struct ktermios *old)
791{
792 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
793 unsigned long flags;
794 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800795 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700796
797 switch (termios->c_cflag & CSIZE) {
798 case CS8:
799 lcr = WLS(8);
800 break;
801 case CS7:
802 lcr = WLS(7);
803 break;
804 case CS6:
805 lcr = WLS(6);
806 break;
807 case CS5:
808 lcr = WLS(5);
809 break;
810 default:
811 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700812 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700813 }
814
Sonic Zhang84507792009-06-11 13:50:20 +0100815 /* Anomaly notes:
816 * 05000231 - STOP bit is always set to 1 whatever the user is set.
817 */
818 if (termios->c_cflag & CSTOPB) {
819 if (ANOMALY_05000231)
820 printk(KERN_WARNING "STOP bits other than 1 is not "
821 "supported in case of anomaly 05000231.\n");
822 else
823 lcr |= STB;
824 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800825 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700826 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800827 if (!(termios->c_cflag & PARODD))
828 lcr |= EPS;
829 if (termios->c_cflag & CMSPAR)
830 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700831
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400832 spin_lock_irqsave(&uart->port.lock, flags);
833
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800834 port->read_status_mask = OE;
835 if (termios->c_iflag & INPCK)
836 port->read_status_mask |= (FE | PE);
837 if (termios->c_iflag & (BRKINT | PARMRK))
838 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700839
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800840 /*
841 * Characters to ignore
842 */
843 port->ignore_status_mask = 0;
844 if (termios->c_iflag & IGNPAR)
845 port->ignore_status_mask |= FE | PE;
846 if (termios->c_iflag & IGNBRK) {
847 port->ignore_status_mask |= BI;
848 /*
849 * If we're ignoring parity and break indicators,
850 * ignore overruns too (for real raw support).
851 */
852 if (termios->c_iflag & IGNPAR)
853 port->ignore_status_mask |= OE;
854 }
Bryan Wu194de562007-05-06 14:50:30 -0700855
856 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400857 quot = uart_get_divisor(port, baud);
858
859 /* If discipline is not IRDA, apply ANOMALY_05000230 */
860 if (termios->c_line != N_IRDA)
861 quot -= ANOMALY_05000230;
862
Mike Frysinger8851c712007-12-24 19:48:04 +0800863 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
864
Bryan Wu194de562007-05-06 14:50:30 -0700865 /* Disable UART */
866 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800867 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700868
869 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800870 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700871
872 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700873 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
874 SSYNC();
875
876 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800877 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700878
879 UART_PUT_LCR(uart, lcr);
880
881 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800882 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700883
884 val = UART_GET_GCTL(uart);
885 val |= UCEN;
886 UART_PUT_GCTL(uart, val);
887
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100888 /* Port speed changed, update the per-port timeout. */
889 uart_update_timeout(port, termios->c_cflag, baud);
890
Bryan Wu194de562007-05-06 14:50:30 -0700891 spin_unlock_irqrestore(&uart->port.lock, flags);
892}
893
894static const char *bfin_serial_type(struct uart_port *port)
895{
896 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
897
898 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
899}
900
901/*
902 * Release the memory region(s) being used by 'port'.
903 */
904static void bfin_serial_release_port(struct uart_port *port)
905{
906}
907
908/*
909 * Request the memory region(s) being used by 'port'.
910 */
911static int bfin_serial_request_port(struct uart_port *port)
912{
913 return 0;
914}
915
916/*
917 * Configure/autoconfigure the port.
918 */
919static void bfin_serial_config_port(struct uart_port *port, int flags)
920{
921 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
922
923 if (flags & UART_CONFIG_TYPE &&
924 bfin_serial_request_port(&uart->port) == 0)
925 uart->port.type = PORT_BFIN;
926}
927
928/*
929 * Verify the new serial_struct (for TIOCSSERIAL).
930 * The only change we allow are to the flags and type, and
931 * even then only between PORT_BFIN and PORT_UNKNOWN
932 */
933static int
934bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
935{
936 return 0;
937}
938
Graf Yang7d01b472008-02-29 11:31:08 +0800939/*
940 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
941 * In other cases, disable IrDA function.
942 */
Alan Coxd87d9b72010-06-01 22:52:53 +0200943static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
Graf Yang7d01b472008-02-29 11:31:08 +0800944{
Sonic Zhang57afb392009-09-09 10:46:19 +0000945 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang7d01b472008-02-29 11:31:08 +0800946 unsigned short val;
947
Alan Coxd87d9b72010-06-01 22:52:53 +0200948 switch (ld) {
Graf Yang7d01b472008-02-29 11:31:08 +0800949 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000950 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800951 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000952 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800953 break;
954 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000955 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800956 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000957 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800958 }
959}
960
Sonic Zhang6f955702009-04-07 16:51:15 +0100961static void bfin_serial_reset_irda(struct uart_port *port)
962{
Sonic Zhang57afb392009-09-09 10:46:19 +0000963 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang6f955702009-04-07 16:51:15 +0100964 unsigned short val;
965
Sonic Zhang57afb392009-09-09 10:46:19 +0000966 val = UART_GET_GCTL(uart);
Sonic Zhang6f955702009-04-07 16:51:15 +0100967 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000968 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100969 SSYNC();
970 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000971 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100972 SSYNC();
973}
974
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000975#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100976/* Anomaly notes:
977 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
978 * losing other bits of UART_LSR is not a problem here.
979 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000980static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
981{
982 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
983
984 while (!(UART_GET_LSR(uart) & THRE))
985 cpu_relax();
986
987 UART_CLEAR_DLAB(uart);
988 UART_PUT_CHAR(uart, (unsigned char)chr);
989}
990
991static int bfin_serial_poll_get_char(struct uart_port *port)
992{
993 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
994 unsigned char chr;
995
996 while (!(UART_GET_LSR(uart) & DR))
997 cpu_relax();
998
999 UART_CLEAR_DLAB(uart);
1000 chr = UART_GET_CHAR(uart);
1001
1002 return chr;
1003}
1004#endif
1005
1006#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1007 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1008static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1009{
1010 if (kgdboc_break_enabled) {
1011 kgdboc_break_enabled = 0;
1012 bfin_serial_shutdown(port);
1013 }
1014}
1015
1016static int bfin_kgdboc_port_startup(struct uart_port *port)
1017{
1018 kgdboc_port_line = port->line;
1019 kgdboc_break_enabled = !bfin_serial_startup(port);
1020 return 0;
1021}
1022#endif
1023
Bryan Wu194de562007-05-06 14:50:30 -07001024static struct uart_ops bfin_serial_pops = {
1025 .tx_empty = bfin_serial_tx_empty,
1026 .set_mctrl = bfin_serial_set_mctrl,
1027 .get_mctrl = bfin_serial_get_mctrl,
1028 .stop_tx = bfin_serial_stop_tx,
1029 .start_tx = bfin_serial_start_tx,
1030 .stop_rx = bfin_serial_stop_rx,
1031 .enable_ms = bfin_serial_enable_ms,
1032 .break_ctl = bfin_serial_break_ctl,
1033 .startup = bfin_serial_startup,
1034 .shutdown = bfin_serial_shutdown,
1035 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001036 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001037 .type = bfin_serial_type,
1038 .release_port = bfin_serial_release_port,
1039 .request_port = bfin_serial_request_port,
1040 .config_port = bfin_serial_config_port,
1041 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001042#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1043 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1044 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1045 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1046#endif
1047#ifdef CONFIG_CONSOLE_POLL
1048 .poll_put_char = bfin_serial_poll_put_char,
1049 .poll_get_char = bfin_serial_poll_get_char,
1050#endif
Bryan Wu194de562007-05-06 14:50:30 -07001051};
1052
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001053#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001054/*
1055 * If the port was already initialised (eg, by a boot loader),
1056 * try to determine the current setup.
1057 */
1058static void __init
1059bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1060 int *parity, int *bits)
1061{
1062 unsigned short status;
1063
1064 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1065 if (status == (ERBFI | ETBEI)) {
1066 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001067 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001068
1069 lcr = UART_GET_LCR(uart);
1070
1071 *parity = 'n';
1072 if (lcr & PEN) {
1073 if (lcr & EPS)
1074 *parity = 'e';
1075 else
1076 *parity = 'o';
1077 }
1078 switch (lcr & 0x03) {
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001079 case 0:
1080 *bits = 5;
1081 break;
1082 case 1:
1083 *bits = 6;
1084 break;
1085 case 2:
1086 *bits = 7;
1087 break;
1088 case 3:
1089 *bits = 8;
1090 break;
Bryan Wu194de562007-05-06 14:50:30 -07001091 }
1092 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001093 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001094
1095 dll = UART_GET_DLL(uart);
1096 dlh = UART_GET_DLH(uart);
1097
1098 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001099 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001100
1101 *baud = get_sclk() / (16*(dll | dlh << 8));
1102 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001103 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001104}
Robin Getz0ae53642007-10-09 17:24:49 +08001105
Robin Getz0ae53642007-10-09 17:24:49 +08001106static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001107
Sonic Zhang57afb392009-09-09 10:46:19 +00001108static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1109{
1110 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1111 while (!(UART_GET_LSR(uart) & THRE))
1112 barrier();
1113 UART_PUT_CHAR(uart, ch);
1114}
1115
1116#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1117 defined (CONFIG_EARLY_PRINTK) */
1118
1119#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1120#define CLASS_BFIN_CONSOLE "bfin-console"
1121/*
1122 * Interrupts are disabled on entering
1123 */
1124static void
1125bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1126{
1127 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1128 unsigned long flags;
1129
1130 spin_lock_irqsave(&uart->port.lock, flags);
1131 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1132 spin_unlock_irqrestore(&uart->port.lock, flags);
1133
1134}
1135
Bryan Wu194de562007-05-06 14:50:30 -07001136static int __init
1137bfin_serial_console_setup(struct console *co, char *options)
1138{
1139 struct bfin_serial_port *uart;
1140 int baud = 57600;
1141 int bits = 8;
1142 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001143# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1144 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001145 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001146# else
Bryan Wu194de562007-05-06 14:50:30 -07001147 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001148# endif
Bryan Wu194de562007-05-06 14:50:30 -07001149
1150 /*
1151 * Check whether an invalid uart number has been specified, and
1152 * if so, search for the first available port that does have
1153 * console support.
1154 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001155 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1156 return -ENODEV;
1157
1158 uart = bfin_serial_ports[co->index];
1159 if (!uart)
1160 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001161
1162 if (options)
1163 uart_parse_options(options, &baud, &parity, &bits, &flow);
1164 else
1165 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1166
1167 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001168}
Robin Getz0ae53642007-10-09 17:24:49 +08001169
Bryan Wu194de562007-05-06 14:50:30 -07001170static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001171 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001172 .write = bfin_serial_console_write,
1173 .device = uart_console_device,
1174 .setup = bfin_serial_console_setup,
1175 .flags = CON_PRINTBUFFER,
1176 .index = -1,
1177 .data = &bfin_serial_reg,
1178};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001179#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001180#else
1181#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001182#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1183
Sonic Zhang57afb392009-09-09 10:46:19 +00001184#ifdef CONFIG_EARLY_PRINTK
1185static struct bfin_serial_port bfin_earlyprintk_port;
1186#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001187
Sonic Zhang57afb392009-09-09 10:46:19 +00001188/*
1189 * Interrupts are disabled on entering
1190 */
1191static void
1192bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001193{
Sonic Zhang57afb392009-09-09 10:46:19 +00001194 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001195
Sonic Zhang57afb392009-09-09 10:46:19 +00001196 if (bfin_earlyprintk_port.port.line != co->index)
1197 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001198
Sonic Zhang57afb392009-09-09 10:46:19 +00001199 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1200 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1201 bfin_serial_console_putchar);
1202 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001203}
1204
Robin Getz7de7c552009-06-11 13:38:57 +01001205/*
1206 * This should have a .setup or .early_setup in it, but then things get called
1207 * without the command line options, and the baud rate gets messed up - so
1208 * don't let the common infrastructure play with things. (see calls to setup
1209 * & earlysetup in ./kernel/printk.c:register_console()
1210 */
Mike Frysingerc1113402008-10-13 10:32:35 +01001211static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001212 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001213 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001214 .device = uart_console_device,
1215 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001216 .index = -1,
1217 .data = &bfin_serial_reg,
1218};
Sonic Zhang57afb392009-09-09 10:46:19 +00001219#endif
1220
1221static struct uart_driver bfin_serial_reg = {
1222 .owner = THIS_MODULE,
1223 .driver_name = DRIVER_NAME,
1224 .dev_name = BFIN_SERIAL_DEV_NAME,
1225 .major = BFIN_SERIAL_MAJOR,
1226 .minor = BFIN_SERIAL_MINOR,
1227 .nr = BFIN_UART_NR_PORTS,
1228 .cons = BFIN_SERIAL_CONSOLE,
1229};
1230
1231static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1232{
1233 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1234
1235 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1236}
1237
1238static int bfin_serial_resume(struct platform_device *pdev)
1239{
1240 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1241
1242 return uart_resume_port(&bfin_serial_reg, &uart->port);
1243}
1244
1245static int bfin_serial_probe(struct platform_device *pdev)
1246{
1247 struct resource *res;
1248 struct bfin_serial_port *uart = NULL;
1249 int ret = 0;
1250
1251 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1252 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1253 return -ENOENT;
1254 }
1255
1256 if (bfin_serial_ports[pdev->id] == NULL) {
1257
1258 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1259 if (!uart) {
1260 dev_err(&pdev->dev,
1261 "fail to malloc bfin_serial_port\n");
1262 return -ENOMEM;
1263 }
1264 bfin_serial_ports[pdev->id] = uart;
1265
1266#ifdef CONFIG_EARLY_PRINTK
1267 if (!(bfin_earlyprintk_port.port.membase
1268 && bfin_earlyprintk_port.port.line == pdev->id)) {
1269 /*
1270 * If the peripheral PINs of current port is allocated
1271 * in earlyprintk probe stage, don't do it again.
1272 */
1273#endif
1274 ret = peripheral_request_list(
1275 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1276 if (ret) {
1277 dev_err(&pdev->dev,
1278 "fail to request bfin serial peripherals\n");
1279 goto out_error_free_mem;
1280 }
1281#ifdef CONFIG_EARLY_PRINTK
1282 }
1283#endif
1284
1285 spin_lock_init(&uart->port.lock);
1286 uart->port.uartclk = get_sclk();
1287 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1288 uart->port.ops = &bfin_serial_pops;
1289 uart->port.line = pdev->id;
1290 uart->port.iotype = UPIO_MEM;
1291 uart->port.flags = UPF_BOOT_AUTOCONF;
1292
1293 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1294 if (res == NULL) {
1295 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1296 ret = -ENOENT;
1297 goto out_error_free_peripherals;
1298 }
1299
Joe Perches28f65c112011-06-09 09:13:32 -07001300 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001301 if (!uart->port.membase) {
1302 dev_err(&pdev->dev, "Cannot map uart IO\n");
1303 ret = -ENXIO;
1304 goto out_error_free_peripherals;
1305 }
1306 uart->port.mapbase = res->start;
1307
Sonic Zhang47918f02011-08-12 11:15:51 +08001308 uart->tx_irq = platform_get_irq(pdev, 0);
1309 if (uart->tx_irq < 0) {
1310 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001311 ret = -ENOENT;
1312 goto out_error_unmap;
1313 }
1314
Sonic Zhang47918f02011-08-12 11:15:51 +08001315 uart->rx_irq = platform_get_irq(pdev, 1);
1316 if (uart->rx_irq < 0) {
1317 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1318 ret = -ENOENT;
1319 goto out_error_unmap;
1320 }
1321 uart->port.irq = uart->rx_irq;
1322
1323 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001324 if (uart->status_irq < 0) {
1325 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1326 ret = -ENOENT;
1327 goto out_error_unmap;
1328 }
1329
1330#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001331 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001332 uart->tx_done = 1;
1333 uart->tx_count = 0;
1334
1335 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1336 if (res == NULL) {
1337 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1338 ret = -ENOENT;
1339 goto out_error_unmap;
1340 }
1341 uart->tx_dma_channel = res->start;
1342
1343 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1344 if (res == NULL) {
1345 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1346 ret = -ENOENT;
1347 goto out_error_unmap;
1348 }
1349 uart->rx_dma_channel = res->start;
1350
1351 init_timer(&(uart->rx_dma_timer));
1352#endif
1353
1354#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1355 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1356 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1357 if (res == NULL)
1358 uart->cts_pin = -1;
Sonic Zhangee948e32011-12-05 18:13:10 +08001359 else {
Sonic Zhang57afb392009-09-09 10:46:19 +00001360 uart->cts_pin = res->start;
Sonic Zhangee948e32011-12-05 18:13:10 +08001361 uart->port.flags |= ASYNC_CTS_FLOW;
1362 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001363
1364 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1365 if (res == NULL)
1366 uart->rts_pin = -1;
1367 else
1368 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001369#endif
1370 }
1371
1372#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1373 if (!is_early_platform_device(pdev)) {
1374#endif
1375 uart = bfin_serial_ports[pdev->id];
1376 uart->port.dev = &pdev->dev;
1377 dev_set_drvdata(&pdev->dev, uart);
1378 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1379#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1380 }
1381#endif
1382
1383 if (!ret)
1384 return 0;
1385
1386 if (uart) {
1387out_error_unmap:
1388 iounmap(uart->port.membase);
1389out_error_free_peripherals:
1390 peripheral_free_list(
1391 (unsigned short *)pdev->dev.platform_data);
1392out_error_free_mem:
1393 kfree(uart);
1394 bfin_serial_ports[pdev->id] = NULL;
1395 }
1396
1397 return ret;
1398}
1399
1400static int __devexit bfin_serial_remove(struct platform_device *pdev)
1401{
1402 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1403
1404 dev_set_drvdata(&pdev->dev, NULL);
1405
1406 if (uart) {
1407 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001408 iounmap(uart->port.membase);
1409 peripheral_free_list(
1410 (unsigned short *)pdev->dev.platform_data);
1411 kfree(uart);
1412 bfin_serial_ports[pdev->id] = NULL;
1413 }
1414
1415 return 0;
1416}
1417
1418static struct platform_driver bfin_serial_driver = {
1419 .probe = bfin_serial_probe,
1420 .remove = __devexit_p(bfin_serial_remove),
1421 .suspend = bfin_serial_suspend,
1422 .resume = bfin_serial_resume,
1423 .driver = {
1424 .name = DRIVER_NAME,
1425 .owner = THIS_MODULE,
1426 },
1427};
1428
1429#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1430static __initdata struct early_platform_driver early_bfin_serial_driver = {
1431 .class_str = CLASS_BFIN_CONSOLE,
1432 .pdrv = &bfin_serial_driver,
1433 .requested_id = EARLY_PLATFORM_ID_UNSET,
1434};
1435
1436static int __init bfin_serial_rs_console_init(void)
1437{
1438 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1439
1440 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1441
1442 register_console(&bfin_serial_console);
1443
1444 return 0;
1445}
1446console_initcall(bfin_serial_rs_console_init);
1447#endif
1448
1449#ifdef CONFIG_EARLY_PRINTK
1450/*
1451 * Memory can't be allocated dynamically during earlyprink init stage.
1452 * So, do individual probe for earlyprink with a static uart port variable.
1453 */
1454static int bfin_earlyprintk_probe(struct platform_device *pdev)
1455{
1456 struct resource *res;
1457 int ret;
1458
1459 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1460 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1461 return -ENOENT;
1462 }
1463
1464 ret = peripheral_request_list(
1465 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1466 if (ret) {
1467 dev_err(&pdev->dev,
1468 "fail to request bfin serial peripherals\n");
1469 return ret;
1470 }
1471
1472 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1473 if (res == NULL) {
1474 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1475 ret = -ENOENT;
1476 goto out_error_free_peripherals;
1477 }
1478
1479 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c112011-06-09 09:13:32 -07001480 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001481 if (!bfin_earlyprintk_port.port.membase) {
1482 dev_err(&pdev->dev, "Cannot map uart IO\n");
1483 ret = -ENXIO;
1484 goto out_error_free_peripherals;
1485 }
1486 bfin_earlyprintk_port.port.mapbase = res->start;
1487 bfin_earlyprintk_port.port.line = pdev->id;
1488 bfin_earlyprintk_port.port.uartclk = get_sclk();
1489 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1490 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1491
1492 return 0;
1493
1494out_error_free_peripherals:
1495 peripheral_free_list(
1496 (unsigned short *)pdev->dev.platform_data);
1497
1498 return ret;
1499}
1500
1501static struct platform_driver bfin_earlyprintk_driver = {
1502 .probe = bfin_earlyprintk_probe,
1503 .driver = {
1504 .name = DRIVER_NAME,
1505 .owner = THIS_MODULE,
1506 },
1507};
1508
1509static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1510 .class_str = CLASS_BFIN_EARLYPRINTK,
1511 .pdrv = &bfin_earlyprintk_driver,
1512 .requested_id = EARLY_PLATFORM_ID_UNSET,
1513};
Robin Getz0ae53642007-10-09 17:24:49 +08001514
1515struct console __init *bfin_earlyserial_init(unsigned int port,
1516 unsigned int cflag)
1517{
Robin Getz0ae53642007-10-09 17:24:49 +08001518 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001519 char port_name[20];
1520
1521 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1522 return NULL;
1523
1524 /*
1525 * Only probe resource of the given port in earlyprintk boot arg.
1526 * The expected port id should be indicated in port name string.
1527 */
1528 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1529 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1530 port_name);
1531 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1532
1533 if (!bfin_earlyprintk_port.port.membase)
1534 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001535
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001536#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1537 /*
1538 * If we are using early serial, don't let the normal console rewind
1539 * log buffer, since that causes things to be printed multiple times
1540 */
1541 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1542#endif
1543
Robin Getz0ae53642007-10-09 17:24:49 +08001544 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001545 t.c_cflag = cflag;
1546 t.c_iflag = 0;
1547 t.c_oflag = 0;
1548 t.c_lflag = ICANON;
1549 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001550 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1551
Robin Getz0ae53642007-10-09 17:24:49 +08001552 return &bfin_early_serial_console;
1553}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001554#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001555
Bryan Wu194de562007-05-06 14:50:30 -07001556static int __init bfin_serial_init(void)
1557{
1558 int ret;
1559
Sonic Zhang57afb392009-09-09 10:46:19 +00001560 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001561
1562 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001563 if (ret) {
1564 pr_err("failed to register %s:%d\n",
1565 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001566 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001567
1568 ret = platform_driver_register(&bfin_serial_driver);
1569 if (ret) {
1570 pr_err("fail to register bfin uart\n");
1571 uart_unregister_driver(&bfin_serial_reg);
1572 }
1573
Bryan Wu194de562007-05-06 14:50:30 -07001574 return ret;
1575}
1576
1577static void __exit bfin_serial_exit(void)
1578{
1579 platform_driver_unregister(&bfin_serial_driver);
1580 uart_unregister_driver(&bfin_serial_reg);
1581}
1582
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001583
Bryan Wu194de562007-05-06 14:50:30 -07001584module_init(bfin_serial_init);
1585module_exit(bfin_serial_exit);
1586
Sonic Zhang57afb392009-09-09 10:46:19 +00001587MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001588MODULE_DESCRIPTION("Blackfin generic serial port driver");
1589MODULE_LICENSE("GPL");
1590MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001591MODULE_ALIAS("platform:bfin-uart");