blob: abac23741057142f6fc789e7abc6a17a0ff4cba1 [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 Zhang52e15f02009-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 Huangf4d640c2007-07-12 16:43:46 +0800150 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800151 cpu_relax();
Roy Huangf4d640c2007-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 Huangf4d640c2007-07-12 16:43:46 +0800160#ifdef CONFIG_BF54x
Roy Huangf4d640c2007-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 Frysinger89bf6dc2008-05-07 11:41:26 +0800164 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c2007-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 Huangf4d640c2007-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 Huangf4d640c2007-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 Zhang52e15f02009-01-02 13:40:14 +0000198
Roy Huangf4d640c2007-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 Zhang52e15f02009-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 Zhang52e15f02009-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 Zhang52e15f02009-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 Zhang52e15f02009-01-02 13:40:14 +0000239
Alan Coxebd2c8f2009-09-19 13:13:28 -0700240 if (!uart->port.state || !uart->port.state->port.tty)
Sonic Zhang52e15f02009-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 Zhang5ffdeea2008-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 Zhang5ffdeea2008-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 Huangf4d640c2007-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 Huangf4d640c2007-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 Huangf4d640c2007-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 Zhang52e15f02009-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 Zhang52e15f02009-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 Zhang57afb392009-09-09 10:46:19 +0000729 if (uart->cts_pin >= 0 && request_irq(uart->status_irq,
Sonic Zhangd307d362009-04-07 16:52:26 +0100730 bfin_serial_mctrl_cts_int,
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800731 0, "BFIN_UART_MODEM_STATUS", uart)) {
Sonic Zhang57afb392009-09-09 10:46:19 +0000732 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800733 pr_info("Unable to attach BlackFin UART Modem Status interrupt.\n");
Sonic Zhangd307d362009-04-07 16:52:26 +0100734 }
735
Sonic Zhangd307d362009-04-07 16:52:26 +0100736 /* CTS RTS PINs are negative assertive. */
737 UART_PUT_MCR(uart, ACTS);
738 UART_SET_IER(uart, EDSSI);
739#endif
740
Roy Huangf4d640c2007-07-12 16:43:46 +0800741 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700742 return 0;
743}
744
745static void bfin_serial_shutdown(struct uart_port *port)
746{
747 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
748
749#ifdef CONFIG_SERIAL_BFIN_DMA
750 disable_dma(uart->tx_dma_channel);
751 free_dma(uart->tx_dma_channel);
752 disable_dma(uart->rx_dma_channel);
753 free_dma(uart->rx_dma_channel);
754 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800755 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700756#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100757#ifdef CONFIG_BF54x
758 switch (uart->port.irq) {
759 case IRQ_UART3_RX:
760 free_dma(CH_UART3_RX);
761 free_dma(CH_UART3_TX);
762 break;
763 case IRQ_UART2_RX:
764 free_dma(CH_UART2_RX);
765 free_dma(CH_UART2_TX);
766 break;
767 default:
768 break;
769 };
770#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800771 free_irq(uart->rx_irq, uart);
772 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700773#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100774
Sonic Zhangd307d362009-04-07 16:52:26 +0100775#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100776 if (uart->cts_pin >= 0)
777 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800778 if (uart->rts_pin >= 0)
779 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100780#endif
781#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000782 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100783 free_irq(uart->status_irq, uart);
784#endif
Bryan Wu194de562007-05-06 14:50:30 -0700785}
786
787static void
788bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
789 struct ktermios *old)
790{
791 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
792 unsigned long flags;
793 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800794 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700795
796 switch (termios->c_cflag & CSIZE) {
797 case CS8:
798 lcr = WLS(8);
799 break;
800 case CS7:
801 lcr = WLS(7);
802 break;
803 case CS6:
804 lcr = WLS(6);
805 break;
806 case CS5:
807 lcr = WLS(5);
808 break;
809 default:
810 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700811 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700812 }
813
Sonic Zhang84507792009-06-11 13:50:20 +0100814 /* Anomaly notes:
815 * 05000231 - STOP bit is always set to 1 whatever the user is set.
816 */
817 if (termios->c_cflag & CSTOPB) {
818 if (ANOMALY_05000231)
819 printk(KERN_WARNING "STOP bits other than 1 is not "
820 "supported in case of anomaly 05000231.\n");
821 else
822 lcr |= STB;
823 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800824 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700825 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800826 if (!(termios->c_cflag & PARODD))
827 lcr |= EPS;
828 if (termios->c_cflag & CMSPAR)
829 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700830
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400831 spin_lock_irqsave(&uart->port.lock, flags);
832
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800833 port->read_status_mask = OE;
834 if (termios->c_iflag & INPCK)
835 port->read_status_mask |= (FE | PE);
836 if (termios->c_iflag & (BRKINT | PARMRK))
837 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700838
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800839 /*
840 * Characters to ignore
841 */
842 port->ignore_status_mask = 0;
843 if (termios->c_iflag & IGNPAR)
844 port->ignore_status_mask |= FE | PE;
845 if (termios->c_iflag & IGNBRK) {
846 port->ignore_status_mask |= BI;
847 /*
848 * If we're ignoring parity and break indicators,
849 * ignore overruns too (for real raw support).
850 */
851 if (termios->c_iflag & IGNPAR)
852 port->ignore_status_mask |= OE;
853 }
Bryan Wu194de562007-05-06 14:50:30 -0700854
855 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400856 quot = uart_get_divisor(port, baud);
857
858 /* If discipline is not IRDA, apply ANOMALY_05000230 */
859 if (termios->c_line != N_IRDA)
860 quot -= ANOMALY_05000230;
861
Mike Frysinger8851c712007-12-24 19:48:04 +0800862 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
863
Bryan Wu194de562007-05-06 14:50:30 -0700864 /* Disable UART */
865 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800866 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700867
868 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800869 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700870
871 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700872 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
873 SSYNC();
874
875 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800876 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700877
878 UART_PUT_LCR(uart, lcr);
879
880 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800881 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700882
883 val = UART_GET_GCTL(uart);
884 val |= UCEN;
885 UART_PUT_GCTL(uart, val);
886
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100887 /* Port speed changed, update the per-port timeout. */
888 uart_update_timeout(port, termios->c_cflag, baud);
889
Bryan Wu194de562007-05-06 14:50:30 -0700890 spin_unlock_irqrestore(&uart->port.lock, flags);
891}
892
893static const char *bfin_serial_type(struct uart_port *port)
894{
895 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
896
897 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
898}
899
900/*
901 * Release the memory region(s) being used by 'port'.
902 */
903static void bfin_serial_release_port(struct uart_port *port)
904{
905}
906
907/*
908 * Request the memory region(s) being used by 'port'.
909 */
910static int bfin_serial_request_port(struct uart_port *port)
911{
912 return 0;
913}
914
915/*
916 * Configure/autoconfigure the port.
917 */
918static void bfin_serial_config_port(struct uart_port *port, int flags)
919{
920 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
921
922 if (flags & UART_CONFIG_TYPE &&
923 bfin_serial_request_port(&uart->port) == 0)
924 uart->port.type = PORT_BFIN;
925}
926
927/*
928 * Verify the new serial_struct (for TIOCSSERIAL).
929 * The only change we allow are to the flags and type, and
930 * even then only between PORT_BFIN and PORT_UNKNOWN
931 */
932static int
933bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
934{
935 return 0;
936}
937
Graf Yang7d01b472008-02-29 11:31:08 +0800938/*
939 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
940 * In other cases, disable IrDA function.
941 */
Alan Coxd87d9b72010-06-01 22:52:53 +0200942static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
Graf Yang7d01b472008-02-29 11:31:08 +0800943{
Sonic Zhang57afb392009-09-09 10:46:19 +0000944 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Graf Yang7d01b472008-02-29 11:31:08 +0800945 unsigned short val;
946
Alan Coxd87d9b72010-06-01 22:52:53 +0200947 switch (ld) {
Graf Yang7d01b472008-02-29 11:31:08 +0800948 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000949 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800950 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000951 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800952 break;
953 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000954 val = UART_GET_GCTL(uart);
Graf Yang7d01b472008-02-29 11:31:08 +0800955 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000956 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800957 }
958}
959
Sonic Zhang6f955702009-04-07 16:51:15 +0100960static void bfin_serial_reset_irda(struct uart_port *port)
961{
Sonic Zhang57afb392009-09-09 10:46:19 +0000962 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang6f955702009-04-07 16:51:15 +0100963 unsigned short val;
964
Sonic Zhang57afb392009-09-09 10:46:19 +0000965 val = UART_GET_GCTL(uart);
Sonic Zhang6f955702009-04-07 16:51:15 +0100966 val &= ~(IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000967 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100968 SSYNC();
969 val |= (IREN | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000970 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100971 SSYNC();
972}
973
Sonic Zhang52e15f02009-01-02 13:40:14 +0000974#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100975/* Anomaly notes:
976 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
977 * losing other bits of UART_LSR is not a problem here.
978 */
Sonic Zhang52e15f02009-01-02 13:40:14 +0000979static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
980{
981 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
982
983 while (!(UART_GET_LSR(uart) & THRE))
984 cpu_relax();
985
986 UART_CLEAR_DLAB(uart);
987 UART_PUT_CHAR(uart, (unsigned char)chr);
988}
989
990static int bfin_serial_poll_get_char(struct uart_port *port)
991{
992 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
993 unsigned char chr;
994
995 while (!(UART_GET_LSR(uart) & DR))
996 cpu_relax();
997
998 UART_CLEAR_DLAB(uart);
999 chr = UART_GET_CHAR(uart);
1000
1001 return chr;
1002}
1003#endif
1004
1005#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1006 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1007static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1008{
1009 if (kgdboc_break_enabled) {
1010 kgdboc_break_enabled = 0;
1011 bfin_serial_shutdown(port);
1012 }
1013}
1014
1015static int bfin_kgdboc_port_startup(struct uart_port *port)
1016{
1017 kgdboc_port_line = port->line;
1018 kgdboc_break_enabled = !bfin_serial_startup(port);
1019 return 0;
1020}
1021#endif
1022
Bryan Wu194de562007-05-06 14:50:30 -07001023static struct uart_ops bfin_serial_pops = {
1024 .tx_empty = bfin_serial_tx_empty,
1025 .set_mctrl = bfin_serial_set_mctrl,
1026 .get_mctrl = bfin_serial_get_mctrl,
1027 .stop_tx = bfin_serial_stop_tx,
1028 .start_tx = bfin_serial_start_tx,
1029 .stop_rx = bfin_serial_stop_rx,
1030 .enable_ms = bfin_serial_enable_ms,
1031 .break_ctl = bfin_serial_break_ctl,
1032 .startup = bfin_serial_startup,
1033 .shutdown = bfin_serial_shutdown,
1034 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001035 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001036 .type = bfin_serial_type,
1037 .release_port = bfin_serial_release_port,
1038 .request_port = bfin_serial_request_port,
1039 .config_port = bfin_serial_config_port,
1040 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f02009-01-02 13:40:14 +00001041#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1042 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1043 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1044 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1045#endif
1046#ifdef CONFIG_CONSOLE_POLL
1047 .poll_put_char = bfin_serial_poll_put_char,
1048 .poll_get_char = bfin_serial_poll_get_char,
1049#endif
Bryan Wu194de562007-05-06 14:50:30 -07001050};
1051
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001052#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001053/*
1054 * If the port was already initialised (eg, by a boot loader),
1055 * try to determine the current setup.
1056 */
1057static void __init
1058bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1059 int *parity, int *bits)
1060{
1061 unsigned short status;
1062
1063 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1064 if (status == (ERBFI | ETBEI)) {
1065 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +08001066 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -07001067
1068 lcr = UART_GET_LCR(uart);
1069
1070 *parity = 'n';
1071 if (lcr & PEN) {
1072 if (lcr & EPS)
1073 *parity = 'e';
1074 else
1075 *parity = 'o';
1076 }
1077 switch (lcr & 0x03) {
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001078 case 0:
1079 *bits = 5;
1080 break;
1081 case 1:
1082 *bits = 6;
1083 break;
1084 case 2:
1085 *bits = 7;
1086 break;
1087 case 3:
1088 *bits = 8;
1089 break;
Bryan Wu194de562007-05-06 14:50:30 -07001090 }
1091 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +08001092 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001093
1094 dll = UART_GET_DLL(uart);
1095 dlh = UART_GET_DLH(uart);
1096
1097 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001098 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001099
1100 *baud = get_sclk() / (16*(dll | dlh << 8));
1101 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001102 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001103}
Robin Getz0ae53642007-10-09 17:24:49 +08001104
Robin Getz0ae53642007-10-09 17:24:49 +08001105static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001106
Sonic Zhang57afb392009-09-09 10:46:19 +00001107static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1108{
1109 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1110 while (!(UART_GET_LSR(uart) & THRE))
1111 barrier();
1112 UART_PUT_CHAR(uart, ch);
1113}
1114
1115#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1116 defined (CONFIG_EARLY_PRINTK) */
1117
1118#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1119#define CLASS_BFIN_CONSOLE "bfin-console"
1120/*
1121 * Interrupts are disabled on entering
1122 */
1123static void
1124bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1125{
1126 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1127 unsigned long flags;
1128
1129 spin_lock_irqsave(&uart->port.lock, flags);
1130 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1131 spin_unlock_irqrestore(&uart->port.lock, flags);
1132
1133}
1134
Bryan Wu194de562007-05-06 14:50:30 -07001135static int __init
1136bfin_serial_console_setup(struct console *co, char *options)
1137{
1138 struct bfin_serial_port *uart;
1139 int baud = 57600;
1140 int bits = 8;
1141 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001142# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1143 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001144 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001145# else
Bryan Wu194de562007-05-06 14:50:30 -07001146 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001147# endif
Bryan Wu194de562007-05-06 14:50:30 -07001148
1149 /*
1150 * Check whether an invalid uart number has been specified, and
1151 * if so, search for the first available port that does have
1152 * console support.
1153 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001154 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1155 return -ENODEV;
1156
1157 uart = bfin_serial_ports[co->index];
1158 if (!uart)
1159 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001160
1161 if (options)
1162 uart_parse_options(options, &baud, &parity, &bits, &flow);
1163 else
1164 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1165
1166 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001167}
Robin Getz0ae53642007-10-09 17:24:49 +08001168
Bryan Wu194de562007-05-06 14:50:30 -07001169static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001170 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001171 .write = bfin_serial_console_write,
1172 .device = uart_console_device,
1173 .setup = bfin_serial_console_setup,
1174 .flags = CON_PRINTBUFFER,
1175 .index = -1,
1176 .data = &bfin_serial_reg,
1177};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001178#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001179#else
1180#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001181#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1182
Sonic Zhang57afb392009-09-09 10:46:19 +00001183#ifdef CONFIG_EARLY_PRINTK
1184static struct bfin_serial_port bfin_earlyprintk_port;
1185#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001186
Sonic Zhang57afb392009-09-09 10:46:19 +00001187/*
1188 * Interrupts are disabled on entering
1189 */
1190static void
1191bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001192{
Sonic Zhang57afb392009-09-09 10:46:19 +00001193 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001194
Sonic Zhang57afb392009-09-09 10:46:19 +00001195 if (bfin_earlyprintk_port.port.line != co->index)
1196 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001197
Sonic Zhang57afb392009-09-09 10:46:19 +00001198 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1199 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1200 bfin_serial_console_putchar);
1201 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001202}
1203
Robin Getz7de7c552009-06-11 13:38:57 +01001204/*
1205 * This should have a .setup or .early_setup in it, but then things get called
1206 * without the command line options, and the baud rate gets messed up - so
1207 * don't let the common infrastructure play with things. (see calls to setup
1208 * & earlysetup in ./kernel/printk.c:register_console()
1209 */
Mike Frysingerc1113402008-10-13 10:32:35 +01001210static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001211 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001212 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001213 .device = uart_console_device,
1214 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001215 .index = -1,
1216 .data = &bfin_serial_reg,
1217};
Sonic Zhang57afb392009-09-09 10:46:19 +00001218#endif
1219
1220static struct uart_driver bfin_serial_reg = {
1221 .owner = THIS_MODULE,
1222 .driver_name = DRIVER_NAME,
1223 .dev_name = BFIN_SERIAL_DEV_NAME,
1224 .major = BFIN_SERIAL_MAJOR,
1225 .minor = BFIN_SERIAL_MINOR,
1226 .nr = BFIN_UART_NR_PORTS,
1227 .cons = BFIN_SERIAL_CONSOLE,
1228};
1229
1230static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1231{
1232 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1233
1234 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1235}
1236
1237static int bfin_serial_resume(struct platform_device *pdev)
1238{
1239 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1240
1241 return uart_resume_port(&bfin_serial_reg, &uart->port);
1242}
1243
1244static int bfin_serial_probe(struct platform_device *pdev)
1245{
1246 struct resource *res;
1247 struct bfin_serial_port *uart = NULL;
1248 int ret = 0;
1249
1250 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1251 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1252 return -ENOENT;
1253 }
1254
1255 if (bfin_serial_ports[pdev->id] == NULL) {
1256
1257 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1258 if (!uart) {
1259 dev_err(&pdev->dev,
1260 "fail to malloc bfin_serial_port\n");
1261 return -ENOMEM;
1262 }
1263 bfin_serial_ports[pdev->id] = uart;
1264
1265#ifdef CONFIG_EARLY_PRINTK
1266 if (!(bfin_earlyprintk_port.port.membase
1267 && bfin_earlyprintk_port.port.line == pdev->id)) {
1268 /*
1269 * If the peripheral PINs of current port is allocated
1270 * in earlyprintk probe stage, don't do it again.
1271 */
1272#endif
1273 ret = peripheral_request_list(
1274 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1275 if (ret) {
1276 dev_err(&pdev->dev,
1277 "fail to request bfin serial peripherals\n");
1278 goto out_error_free_mem;
1279 }
1280#ifdef CONFIG_EARLY_PRINTK
1281 }
1282#endif
1283
1284 spin_lock_init(&uart->port.lock);
1285 uart->port.uartclk = get_sclk();
1286 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1287 uart->port.ops = &bfin_serial_pops;
1288 uart->port.line = pdev->id;
1289 uart->port.iotype = UPIO_MEM;
1290 uart->port.flags = UPF_BOOT_AUTOCONF;
1291
1292 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1293 if (res == NULL) {
1294 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1295 ret = -ENOENT;
1296 goto out_error_free_peripherals;
1297 }
1298
Joe Perches28f65c12011-06-09 09:13:32 -07001299 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001300 if (!uart->port.membase) {
1301 dev_err(&pdev->dev, "Cannot map uart IO\n");
1302 ret = -ENXIO;
1303 goto out_error_free_peripherals;
1304 }
1305 uart->port.mapbase = res->start;
1306
Sonic Zhang47918f02011-08-12 11:15:51 +08001307 uart->tx_irq = platform_get_irq(pdev, 0);
1308 if (uart->tx_irq < 0) {
1309 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001310 ret = -ENOENT;
1311 goto out_error_unmap;
1312 }
1313
Sonic Zhang47918f02011-08-12 11:15:51 +08001314 uart->rx_irq = platform_get_irq(pdev, 1);
1315 if (uart->rx_irq < 0) {
1316 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1317 ret = -ENOENT;
1318 goto out_error_unmap;
1319 }
1320 uart->port.irq = uart->rx_irq;
1321
1322 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001323 if (uart->status_irq < 0) {
1324 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1325 ret = -ENOENT;
1326 goto out_error_unmap;
1327 }
1328
1329#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001330 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001331 uart->tx_done = 1;
1332 uart->tx_count = 0;
1333
1334 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1335 if (res == NULL) {
1336 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1337 ret = -ENOENT;
1338 goto out_error_unmap;
1339 }
1340 uart->tx_dma_channel = res->start;
1341
1342 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1343 if (res == NULL) {
1344 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1345 ret = -ENOENT;
1346 goto out_error_unmap;
1347 }
1348 uart->rx_dma_channel = res->start;
1349
1350 init_timer(&(uart->rx_dma_timer));
1351#endif
1352
1353#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1354 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1355 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1356 if (res == NULL)
1357 uart->cts_pin = -1;
Sonic Zhangee948e32011-12-05 18:13:10 +08001358 else {
Sonic Zhang57afb392009-09-09 10:46:19 +00001359 uart->cts_pin = res->start;
Sonic Zhangee948e32011-12-05 18:13:10 +08001360 uart->port.flags |= ASYNC_CTS_FLOW;
1361 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001362
1363 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1364 if (res == NULL)
1365 uart->rts_pin = -1;
1366 else
1367 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001368#endif
1369 }
1370
1371#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1372 if (!is_early_platform_device(pdev)) {
1373#endif
1374 uart = bfin_serial_ports[pdev->id];
1375 uart->port.dev = &pdev->dev;
1376 dev_set_drvdata(&pdev->dev, uart);
1377 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1378#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1379 }
1380#endif
1381
1382 if (!ret)
1383 return 0;
1384
1385 if (uart) {
1386out_error_unmap:
1387 iounmap(uart->port.membase);
1388out_error_free_peripherals:
1389 peripheral_free_list(
1390 (unsigned short *)pdev->dev.platform_data);
1391out_error_free_mem:
1392 kfree(uart);
1393 bfin_serial_ports[pdev->id] = NULL;
1394 }
1395
1396 return ret;
1397}
1398
1399static int __devexit bfin_serial_remove(struct platform_device *pdev)
1400{
1401 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1402
1403 dev_set_drvdata(&pdev->dev, NULL);
1404
1405 if (uart) {
1406 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001407 iounmap(uart->port.membase);
1408 peripheral_free_list(
1409 (unsigned short *)pdev->dev.platform_data);
1410 kfree(uart);
1411 bfin_serial_ports[pdev->id] = NULL;
1412 }
1413
1414 return 0;
1415}
1416
1417static struct platform_driver bfin_serial_driver = {
1418 .probe = bfin_serial_probe,
1419 .remove = __devexit_p(bfin_serial_remove),
1420 .suspend = bfin_serial_suspend,
1421 .resume = bfin_serial_resume,
1422 .driver = {
1423 .name = DRIVER_NAME,
1424 .owner = THIS_MODULE,
1425 },
1426};
1427
1428#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1429static __initdata struct early_platform_driver early_bfin_serial_driver = {
1430 .class_str = CLASS_BFIN_CONSOLE,
1431 .pdrv = &bfin_serial_driver,
1432 .requested_id = EARLY_PLATFORM_ID_UNSET,
1433};
1434
1435static int __init bfin_serial_rs_console_init(void)
1436{
1437 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1438
1439 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1440
1441 register_console(&bfin_serial_console);
1442
1443 return 0;
1444}
1445console_initcall(bfin_serial_rs_console_init);
1446#endif
1447
1448#ifdef CONFIG_EARLY_PRINTK
1449/*
1450 * Memory can't be allocated dynamically during earlyprink init stage.
1451 * So, do individual probe for earlyprink with a static uart port variable.
1452 */
1453static int bfin_earlyprintk_probe(struct platform_device *pdev)
1454{
1455 struct resource *res;
1456 int ret;
1457
1458 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1459 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1460 return -ENOENT;
1461 }
1462
1463 ret = peripheral_request_list(
1464 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1465 if (ret) {
1466 dev_err(&pdev->dev,
1467 "fail to request bfin serial peripherals\n");
1468 return ret;
1469 }
1470
1471 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1472 if (res == NULL) {
1473 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1474 ret = -ENOENT;
1475 goto out_error_free_peripherals;
1476 }
1477
1478 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c12011-06-09 09:13:32 -07001479 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001480 if (!bfin_earlyprintk_port.port.membase) {
1481 dev_err(&pdev->dev, "Cannot map uart IO\n");
1482 ret = -ENXIO;
1483 goto out_error_free_peripherals;
1484 }
1485 bfin_earlyprintk_port.port.mapbase = res->start;
1486 bfin_earlyprintk_port.port.line = pdev->id;
1487 bfin_earlyprintk_port.port.uartclk = get_sclk();
1488 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1489 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1490
1491 return 0;
1492
1493out_error_free_peripherals:
1494 peripheral_free_list(
1495 (unsigned short *)pdev->dev.platform_data);
1496
1497 return ret;
1498}
1499
1500static struct platform_driver bfin_earlyprintk_driver = {
1501 .probe = bfin_earlyprintk_probe,
1502 .driver = {
1503 .name = DRIVER_NAME,
1504 .owner = THIS_MODULE,
1505 },
1506};
1507
1508static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1509 .class_str = CLASS_BFIN_EARLYPRINTK,
1510 .pdrv = &bfin_earlyprintk_driver,
1511 .requested_id = EARLY_PLATFORM_ID_UNSET,
1512};
Robin Getz0ae53642007-10-09 17:24:49 +08001513
1514struct console __init *bfin_earlyserial_init(unsigned int port,
1515 unsigned int cflag)
1516{
Robin Getz0ae53642007-10-09 17:24:49 +08001517 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001518 char port_name[20];
1519
1520 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1521 return NULL;
1522
1523 /*
1524 * Only probe resource of the given port in earlyprintk boot arg.
1525 * The expected port id should be indicated in port name string.
1526 */
1527 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1528 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1529 port_name);
1530 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1531
1532 if (!bfin_earlyprintk_port.port.membase)
1533 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001534
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001535#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1536 /*
1537 * If we are using early serial, don't let the normal console rewind
1538 * log buffer, since that causes things to be printed multiple times
1539 */
1540 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1541#endif
1542
Robin Getz0ae53642007-10-09 17:24:49 +08001543 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001544 t.c_cflag = cflag;
1545 t.c_iflag = 0;
1546 t.c_oflag = 0;
1547 t.c_lflag = ICANON;
1548 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001549 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1550
Robin Getz0ae53642007-10-09 17:24:49 +08001551 return &bfin_early_serial_console;
1552}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001553#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001554
Bryan Wu194de562007-05-06 14:50:30 -07001555static int __init bfin_serial_init(void)
1556{
1557 int ret;
1558
Sonic Zhang57afb392009-09-09 10:46:19 +00001559 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001560
1561 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001562 if (ret) {
1563 pr_err("failed to register %s:%d\n",
1564 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001565 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001566
1567 ret = platform_driver_register(&bfin_serial_driver);
1568 if (ret) {
1569 pr_err("fail to register bfin uart\n");
1570 uart_unregister_driver(&bfin_serial_reg);
1571 }
1572
Bryan Wu194de562007-05-06 14:50:30 -07001573 return ret;
1574}
1575
1576static void __exit bfin_serial_exit(void)
1577{
1578 platform_driver_unregister(&bfin_serial_driver);
1579 uart_unregister_driver(&bfin_serial_reg);
1580}
1581
Sonic Zhang52e15f02009-01-02 13:40:14 +00001582
Bryan Wu194de562007-05-06 14:50:30 -07001583module_init(bfin_serial_init);
1584module_exit(bfin_serial_exit);
1585
Sonic Zhang57afb392009-09-09 10:46:19 +00001586MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001587MODULE_DESCRIPTION("Blackfin generic serial port driver");
1588MODULE_LICENSE("GPL");
1589MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001590MODULE_ALIAS("platform:bfin-uart");