blob: 26a3be7ced7d92df93616f2a50363970a93de67b [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 Zhangb06d2f22012-05-16 14:22:23 +08004 * Copyright 2006-2011 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>
Sonic Zhang57afb392009-09-09 10:46:19 +000038#include <asm/bfin_serial.h>
Bryan Wu194de562007-05-06 14:50:30 -070039
Mike Frysinger607c2682009-06-22 18:41:47 +010040#ifdef CONFIG_SERIAL_BFIN_MODULE
41# undef CONFIG_EARLY_PRINTK
42#endif
43
Mike Frysinger0271edd2009-08-06 15:20:05 -070044#ifdef CONFIG_SERIAL_BFIN_MODULE
45# undef CONFIG_EARLY_PRINTK
46#endif
47
Bryan Wu194de562007-05-06 14:50:30 -070048/* UART name and device definitions */
Sonic Zhang57afb392009-09-09 10:46:19 +000049#define BFIN_SERIAL_DEV_NAME "ttyBF"
Bryan Wu194de562007-05-06 14:50:30 -070050#define BFIN_SERIAL_MAJOR 204
51#define BFIN_SERIAL_MINOR 64
52
Sonic Zhang57afb392009-09-09 10:46:19 +000053static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
Mike Frysingerc9607ec2008-10-13 10:33:16 +010054
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000055#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
56 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
57
58# ifndef CONFIG_SERIAL_BFIN_PIO
59# error KGDB only support UART in PIO mode.
60# endif
61
62static int kgdboc_port_line;
63static int kgdboc_break_enabled;
64#endif
Bryan Wu194de562007-05-06 14:50:30 -070065/*
66 * Setup for console. Argument comes from the menuconfig
67 */
68#define DMA_RX_XCOUNT 512
69#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
70
Sonic Zhang0aef4562008-02-29 12:08:42 +080071#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070072
73#ifdef CONFIG_SERIAL_BFIN_DMA
74static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
75#else
Bryan Wu194de562007-05-06 14:50:30 -070076static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070077#endif
78
Graf Yang80d5c472009-01-02 13:40:22 +000079static void bfin_serial_reset_irda(struct uart_port *port);
80
Sonic Zhangd307d362009-04-07 16:52:26 +010081#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
82 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
83static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
84{
85 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
86 if (uart->cts_pin < 0)
87 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
88
89 /* CTS PIN is negative assertive. */
90 if (UART_GET_CTS(uart))
91 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
92 else
93 return TIOCM_DSR | TIOCM_CAR;
94}
95
96static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
97{
98 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
99 if (uart->rts_pin < 0)
100 return;
101
102 /* RTS PIN is negative assertive. */
103 if (mctrl & TIOCM_RTS)
104 UART_ENABLE_RTS(uart);
105 else
106 UART_DISABLE_RTS(uart);
107}
108
109/*
110 * Handle any change of modem status signal.
111 */
112static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
113{
114 struct bfin_serial_port *uart = dev_id;
Sonic Zhang64851632011-12-13 12:22:02 +0800115 unsigned int status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhangd307d362009-04-07 16:52:26 +0100116#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang64851632011-12-13 12:22:02 +0800117 struct tty_struct *tty = uart->port.state->port.tty;
118
Sonic Zhangd307d362009-04-07 16:52:26 +0100119 UART_CLEAR_SCTS(uart);
Sonic Zhang64851632011-12-13 12:22:02 +0800120 if (tty->hw_stopped) {
121 if (status) {
122 tty->hw_stopped = 0;
123 uart_write_wakeup(&uart->port);
124 }
125 } else {
126 if (!status)
127 tty->hw_stopped = 1;
128 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100129#endif
Sonic Zhangf5b69402011-12-05 18:13:11 +0800130 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Sonic Zhangd307d362009-04-07 16:52:26 +0100131
132 return IRQ_HANDLED;
133}
134#else
135static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
136{
137 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
138}
139
140static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
141{
142}
143#endif
144
Bryan Wu194de562007-05-06 14:50:30 -0700145/*
146 * interrupts are disabled on entry
147 */
148static void bfin_serial_stop_tx(struct uart_port *port)
149{
150 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000151#ifdef CONFIG_SERIAL_BFIN_DMA
Alan Coxebd2c8f2009-09-19 13:13:28 -0700152 struct circ_buf *xmit = &uart->port.state->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000153#endif
Bryan Wu194de562007-05-06 14:50:30 -0700154
Roy Huangf4d640c92007-07-12 16:43:46 +0800155 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800156 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800157
Bryan Wu194de562007-05-06 14:50:30 -0700158#ifdef CONFIG_SERIAL_BFIN_DMA
159 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800160 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
161 uart->port.icount.tx += uart->tx_count;
162 uart->tx_count = 0;
163 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700164#else
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800165#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Roy Huangf4d640c92007-07-12 16:43:46 +0800166 /* Clear TFI bit */
167 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700168#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800169 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800170#endif
Bryan Wu194de562007-05-06 14:50:30 -0700171}
172
173/*
174 * port is locked and interrupts are disabled
175 */
176static void bfin_serial_start_tx(struct uart_port *port)
177{
178 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700179 struct tty_struct *tty = uart->port.state->port.tty;
Graf Yang80d5c472009-01-02 13:40:22 +0000180
181 /*
182 * To avoid losting RX interrupt, we reset IR function
183 * before sending data.
184 */
Alan Coxadc8d742012-07-14 15:31:47 +0100185 if (tty->termios.c_line == N_IRDA)
Graf Yang80d5c472009-01-02 13:40:22 +0000186 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700187
188#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800189 if (uart->tx_done)
190 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700191#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800192 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800193 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800194#endif
Bryan Wu194de562007-05-06 14:50:30 -0700195}
196
197/*
198 * Interrupts are enabled
199 */
200static void bfin_serial_stop_rx(struct uart_port *port)
201{
202 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000203
Roy Huangf4d640c92007-07-12 16:43:46 +0800204 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700205}
206
207/*
208 * Set the modem control timer to fire immediately.
209 */
210static void bfin_serial_enable_ms(struct uart_port *port)
211{
212}
213
Sonic Zhang474f1a62007-06-29 16:35:17 +0800214
Mike Frysinger50e2e152008-04-25 03:03:03 +0800215#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800216# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
217# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
218#else
219# define UART_GET_ANOMALY_THRESHOLD(uart) 0
220# define UART_SET_ANOMALY_THRESHOLD(uart, v)
221#endif
222
Bryan Wu194de562007-05-06 14:50:30 -0700223#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700224static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
225{
Bryan Wu194de562007-05-06 14:50:30 -0700226 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800227 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700228
Sonic Zhang759eb042007-11-21 17:00:32 +0800229 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800230 UART_CLEAR_LSR(uart);
231
Sonic Zhangbb7e58f2011-07-19 18:09:24 +0800232 ch = UART_GET_CHAR(uart);
233 uart->port.icount.rx++;
Bryan Wu194de562007-05-06 14:50:30 -0700234
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000235#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
236 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhangcdc592d2010-01-19 06:13:12 -0500237 if (kgdb_connected && kgdboc_port_line == uart->port.line
238 && kgdboc_break_enabled)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000239 if (ch == 0x3) {/* Ctrl + C */
240 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800241 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800242 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000243
Jiri Slaby2e124b42013-01-03 15:53:06 +0100244 if (!uart->port.state)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000245 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800246#endif
Mike Frysinger50e2e152008-04-25 03:03:03 +0800247 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800248 /* The BF533 (and BF561) family of processors have a nice anomaly
249 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800250 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800251 * character comes across. Due to the nature of the flood, it is
252 * not possible to reliably catch bytes that are sent too quickly
253 * after this break. So application code talking to the Blackfin
254 * which sends a break signal must allow at least 1.5 character
255 * times after the end of the break for things to stabilize. This
256 * timeout was picked as it must absolutely be larger than 1
257 * character time +/- some percent. So 1.5 sounds good. All other
258 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800259 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800260 if (anomaly_start.tv_sec) {
261 struct timeval curr;
262 suseconds_t usecs;
263
264 if ((~ch & (~ch + 1)) & 0xff)
265 goto known_good_char;
266
267 do_gettimeofday(&curr);
268 if (curr.tv_sec - anomaly_start.tv_sec > 1)
269 goto known_good_char;
270
271 usecs = 0;
272 if (curr.tv_sec != anomaly_start.tv_sec)
273 usecs += USEC_PER_SEC;
274 usecs += curr.tv_usec - anomaly_start.tv_usec;
275
276 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
277 goto known_good_char;
278
279 if (ch)
280 anomaly_start.tv_sec = 0;
281 else
282 anomaly_start = curr;
283
284 return;
285
286 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000287 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800288 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800289 }
Bryan Wu194de562007-05-06 14:50:30 -0700290 }
Bryan Wu194de562007-05-06 14:50:30 -0700291
292 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800293 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800294 if (bfin_revid() < 5)
295 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700296 uart->port.icount.brk++;
297 if (uart_handle_break(&uart->port))
298 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800299 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800300 }
301 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700302 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800303 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700304 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800305 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700306 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800307
308 status &= uart->port.read_status_mask;
309
310 if (status & BI)
311 flg = TTY_BREAK;
312 else if (status & PE)
313 flg = TTY_PARITY;
314 else if (status & FE)
315 flg = TTY_FRAME;
316 else
Bryan Wu194de562007-05-06 14:50:30 -0700317 flg = TTY_NORMAL;
318
319 if (uart_handle_sysrq_char(&uart->port, ch))
320 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700321
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800322 uart_insert_char(&uart->port, status, OE, ch, flg);
323
324 ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100325 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700326}
327
328static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
329{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700330 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700331
Bryan Wu194de562007-05-06 14:50:30 -0700332 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800333#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100334 /* Clear TFI bit */
335 UART_PUT_LSR(uart, TFI);
336#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100337 /* Anomaly notes:
338 * 05000215 - we always clear ETBEI within last UART TX
339 * interrupt to end a string. It is always set
340 * when start a new tx.
341 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100342 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700343 return;
344 }
345
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800346 if (uart->port.x_char) {
347 UART_PUT_CHAR(uart, uart->port.x_char);
348 uart->port.icount.tx++;
349 uart->port.x_char = 0;
350 }
351
Sonic Zhang759eb042007-11-21 17:00:32 +0800352 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
353 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
354 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
355 uart->port.icount.tx++;
Sonic Zhang759eb042007-11-21 17:00:32 +0800356 }
Bryan Wu194de562007-05-06 14:50:30 -0700357
358 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
359 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700360}
361
Aubrey Li5c4e4722007-05-21 18:09:38 +0800362static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
363{
364 struct bfin_serial_port *uart = dev_id;
365
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800366 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800367 bfin_serial_rx_chars(uart);
Sonic Zhang759eb042007-11-21 17:00:32 +0800368
Aubrey Li5c4e4722007-05-21 18:09:38 +0800369 return IRQ_HANDLED;
370}
371
372static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700373{
374 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700375
Roy Huangf4d640c92007-07-12 16:43:46 +0800376 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800377 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800378 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700379 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800380
Bryan Wu194de562007-05-06 14:50:30 -0700381 return IRQ_HANDLED;
382}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800383#endif
Bryan Wu194de562007-05-06 14:50:30 -0700384
Bryan Wu194de562007-05-06 14:50:30 -0700385#ifdef CONFIG_SERIAL_BFIN_DMA
386static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
387{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700388 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700389
Bryan Wu194de562007-05-06 14:50:30 -0700390 uart->tx_done = 0;
391
Bryan Wu194de562007-05-06 14:50:30 -0700392 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800393 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700394 uart->tx_done = 1;
395 return;
396 }
397
Sonic Zhang1b733512007-12-21 16:45:12 +0800398 if (uart->port.x_char) {
399 UART_PUT_CHAR(uart, uart->port.x_char);
400 uart->port.icount.tx++;
401 uart->port.x_char = 0;
402 }
403
Bryan Wu194de562007-05-06 14:50:30 -0700404 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
405 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
406 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
407 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
408 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
409 set_dma_config(uart->tx_dma_channel,
410 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
411 INTR_ON_BUF,
412 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800413 DATA_SIZE_8,
414 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700415 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
416 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
417 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100418 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700419 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800420
Roy Huangf4d640c92007-07-12 16:43:46 +0800421 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700422}
423
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800424static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700425{
Bryan Wu194de562007-05-06 14:50:30 -0700426 int i, flg, status;
427
428 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800429 UART_CLEAR_LSR(uart);
430
Sonic Zhang56f5de82008-02-25 15:19:09 +0800431 uart->port.icount.rx +=
432 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
433 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700434
435 if (status & BI) {
436 uart->port.icount.brk++;
437 if (uart_handle_break(&uart->port))
438 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800439 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800440 }
441 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700442 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800443 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700444 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800445 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700446 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800447
448 status &= uart->port.read_status_mask;
449
450 if (status & BI)
451 flg = TTY_BREAK;
452 else if (status & PE)
453 flg = TTY_PARITY;
454 else if (status & FE)
455 flg = TTY_FRAME;
456 else
Bryan Wu194de562007-05-06 14:50:30 -0700457 flg = TTY_NORMAL;
458
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100459 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800460 if (i >= UART_XMIT_SIZE)
461 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100462 if (i == uart->rx_dma_buf.head)
463 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800464 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
465 uart_insert_char(&uart->port, status, OE,
466 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700467 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800468
469 dma_ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100470 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700471}
472
473void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
474{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100475 int x_pos, pos;
Steven Miao9642dbe2012-11-07 13:27:56 +0800476 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000477
Steven Miao9642dbe2012-11-07 13:27:56 +0800478 spin_lock_irqsave(&uart->rx_lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700479
Sonic Zhang8516c562009-06-11 13:38:16 +0100480 /* 2D DMA RX buffer ring is used. Because curr_y_count and
481 * curr_x_count can't be read as an atomic operation,
482 * curr_y_count should be read before curr_x_count. When
483 * curr_x_count is read, curr_y_count may already indicate
484 * next buffer line. But, the position calculated here is
485 * still indicate the old line. The wrong position data may
486 * be smaller than current buffer tail, which cause garbages
487 * are received if it is not prohibit.
488 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800489 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
490 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
491 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100492 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800493 uart->rx_dma_nrows = 0;
494 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700495 if (x_pos == DMA_RX_XCOUNT)
496 x_pos = 0;
497
498 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100499 /* Ignore receiving data if new position is in the same line of
500 * current buffer tail and small.
501 */
502 if (pos > uart->rx_dma_buf.tail ||
503 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800504 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700505 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800506 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700507 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800508
Steven Miao9642dbe2012-11-07 13:27:56 +0800509 spin_unlock_irqrestore(&uart->rx_lock, flags);
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 uart->port.icount.tx += uart->tx_count;
Sonic Zhang239c25b2012-05-16 14:22:24 +0800530 if (!(xmit->tail == 0 && xmit->head == 0)) {
Sonic Zhang60f4b002012-03-13 15:51:55 +0800531 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
Sonic Zhang1b733512007-12-21 16:45:12 +0800532
Sonic Zhang60f4b002012-03-13 15:51:55 +0800533 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
534 uart_write_wakeup(&uart->port);
535 }
Sonic Zhang56f5de82008-02-25 15:19:09 +0800536
Sonic Zhang1b733512007-12-21 16:45:12 +0800537 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700538 }
539
540 spin_unlock(&uart->port.lock);
541 return IRQ_HANDLED;
542}
543
544static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
545{
546 struct bfin_serial_port *uart = dev_id;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800547 unsigned int irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100548 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800549
Sonic Zhang0f66e502011-01-11 00:16:43 -0500550 spin_lock(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700551 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
552 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100553
554 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100555 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100556 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100557 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100558 uart->rx_dma_nrows = 0;
559
560 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
561 if (pos > uart->rx_dma_buf.tail ||
562 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
563 uart->rx_dma_buf.head = pos;
564 bfin_serial_dma_rx_chars(uart);
565 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
566 }
567
Sonic Zhang0f66e502011-01-11 00:16:43 -0500568 spin_unlock(&uart->rx_lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800569
Bryan Wu194de562007-05-06 14:50:30 -0700570 return IRQ_HANDLED;
571}
572#endif
573
574/*
575 * Return TIOCSER_TEMT when transmitter is not busy.
576 */
577static unsigned int bfin_serial_tx_empty(struct uart_port *port)
578{
579 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800580 unsigned int lsr;
Bryan Wu194de562007-05-06 14:50:30 -0700581
582 lsr = UART_GET_LSR(uart);
583 if (lsr & TEMT)
584 return TIOCSER_TEMT;
585 else
586 return 0;
587}
588
Bryan Wu194de562007-05-06 14:50:30 -0700589static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
590{
Mike Frysingercf686762007-06-11 16:12:49 +0800591 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang59bd2342012-05-16 14:22:26 +0800592 u32 lcr = UART_GET_LCR(uart);
Mike Frysingercf686762007-06-11 16:12:49 +0800593 if (break_state)
594 lcr |= SB;
595 else
596 lcr &= ~SB;
597 UART_PUT_LCR(uart, lcr);
598 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700599}
600
601static int bfin_serial_startup(struct uart_port *port)
602{
603 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
604
605#ifdef CONFIG_SERIAL_BFIN_DMA
606 dma_addr_t dma_handle;
607
608 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
609 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
610 return -EBUSY;
611 }
612
613 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
614 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
615 free_dma(uart->rx_dma_channel);
616 return -EBUSY;
617 }
618
619 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
620 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
621
622 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
623 uart->rx_dma_buf.head = 0;
624 uart->rx_dma_buf.tail = 0;
625 uart->rx_dma_nrows = 0;
626
627 set_dma_config(uart->rx_dma_channel,
628 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
629 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800630 DATA_SIZE_8,
631 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700632 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
633 set_dma_x_modify(uart->rx_dma_channel, 1);
634 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
635 set_dma_y_modify(uart->rx_dma_channel, 1);
636 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
637 enable_dma(uart->rx_dma_channel);
638
639 uart->rx_dma_timer.data = (unsigned long)(uart);
640 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
641 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
642 add_timer(&(uart->rx_dma_timer));
643#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100644# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000645 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
646 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
647 kgdboc_break_enabled = 0;
648 else {
649# endif
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800650 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700651 "BFIN_UART_RX", uart)) {
652 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
653 return -EBUSY;
654 }
655
656 if (request_irq
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800657 (uart->tx_irq, bfin_serial_tx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700658 "BFIN_UART_TX", uart)) {
659 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800660 free_irq(uart->rx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700661 return -EBUSY;
662 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100663
664# ifdef CONFIG_BF54x
665 {
Sonic Zhangb6100992010-10-27 04:16:47 -0400666 /*
667 * UART2 and UART3 on BF548 share interrupt PINs and DMA
668 * controllers with SPORT2 and SPORT3. UART rx and tx
669 * interrupts are generated in PIO mode only when configure
670 * their peripheral mapping registers properly, which means
671 * request corresponding DMA channels in PIO mode as well.
672 */
Sonic Zhangab2375f2008-10-13 10:33:51 +0100673 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
674
Sonic Zhang47918f02011-08-12 11:15:51 +0800675 switch (uart->rx_irq) {
Sonic Zhangab2375f2008-10-13 10:33:51 +0100676 case IRQ_UART3_RX:
677 uart_dma_ch_rx = CH_UART3_RX;
678 uart_dma_ch_tx = CH_UART3_TX;
679 break;
680 case IRQ_UART2_RX:
681 uart_dma_ch_rx = CH_UART2_RX;
682 uart_dma_ch_tx = CH_UART2_TX;
683 break;
684 default:
685 uart_dma_ch_rx = uart_dma_ch_tx = 0;
686 break;
687 };
688
689 if (uart_dma_ch_rx &&
690 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
691 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800692 free_irq(uart->rx_irq, uart);
693 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100694 return -EBUSY;
695 }
696 if (uart_dma_ch_tx &&
697 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
698 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
699 free_dma(uart_dma_ch_rx);
Sonic Zhang47918f02011-08-12 11:15:51 +0800700 free_irq(uart->rx_irq, uart);
701 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100702 return -EBUSY;
703 }
704 }
705# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100706# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000707 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
708 }
709# endif
Bryan Wu194de562007-05-06 14:50:30 -0700710#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100711
712#ifdef CONFIG_SERIAL_BFIN_CTSRTS
713 if (uart->cts_pin >= 0) {
714 if (request_irq(gpio_to_irq(uart->cts_pin),
715 bfin_serial_mctrl_cts_int,
716 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800717 0, "BFIN_UART_CTS", uart)) {
Sonic Zhang6f955702009-04-07 16:51:15 +0100718 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800719 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
Sonic Zhang6f955702009-04-07 16:51:15 +0100720 }
721 }
Sonic Zhang32b44562011-12-05 15:12:50 +0800722 if (uart->rts_pin >= 0) {
723 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
724 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
725 uart->rts_pin = -1;
726 } else
727 gpio_direction_output(uart->rts_pin, 0);
728 }
Sonic Zhang6f955702009-04-07 16:51:15 +0100729#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100730#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangb48dc712011-12-13 12:22:01 +0800731 if (uart->cts_pin >= 0) {
732 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
733 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
734 uart->cts_pin = -1;
735 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
736 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100737
Sonic Zhangb48dc712011-12-13 12:22:01 +0800738 /* CTS RTS PINs are negative assertive. */
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800739 UART_PUT_MCR(uart, UART_GET_MCR(uart) | ACTS);
Sonic Zhangb48dc712011-12-13 12:22:01 +0800740 UART_SET_IER(uart, EDSSI);
741 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100742#endif
743
Roy Huangf4d640c92007-07-12 16:43:46 +0800744 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700745 return 0;
746}
747
748static void bfin_serial_shutdown(struct uart_port *port)
749{
750 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
751
752#ifdef CONFIG_SERIAL_BFIN_DMA
753 disable_dma(uart->tx_dma_channel);
754 free_dma(uart->tx_dma_channel);
755 disable_dma(uart->rx_dma_channel);
756 free_dma(uart->rx_dma_channel);
757 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800758 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700759#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100760#ifdef CONFIG_BF54x
761 switch (uart->port.irq) {
762 case IRQ_UART3_RX:
763 free_dma(CH_UART3_RX);
764 free_dma(CH_UART3_TX);
765 break;
766 case IRQ_UART2_RX:
767 free_dma(CH_UART2_RX);
768 free_dma(CH_UART2_TX);
769 break;
770 default:
771 break;
772 };
773#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800774 free_irq(uart->rx_irq, uart);
775 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700776#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100777
Sonic Zhangd307d362009-04-07 16:52:26 +0100778#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100779 if (uart->cts_pin >= 0)
780 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800781 if (uart->rts_pin >= 0)
782 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100783#endif
784#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000785 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100786 free_irq(uart->status_irq, uart);
787#endif
Bryan Wu194de562007-05-06 14:50:30 -0700788}
789
790static void
791bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
792 struct ktermios *old)
793{
794 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
795 unsigned long flags;
796 unsigned int baud, quot;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800797 unsigned int ier, lcr = 0;
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800798 unsigned long timeout;
Bryan Wu194de562007-05-06 14:50:30 -0700799
800 switch (termios->c_cflag & CSIZE) {
801 case CS8:
802 lcr = WLS(8);
803 break;
804 case CS7:
805 lcr = WLS(7);
806 break;
807 case CS6:
808 lcr = WLS(6);
809 break;
810 case CS5:
811 lcr = WLS(5);
812 break;
813 default:
Masanari Iida46e99c42012-10-24 23:29:41 +0900814 printk(KERN_ERR "%s: word length not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700815 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700816 }
817
Sonic Zhang84507792009-06-11 13:50:20 +0100818 /* Anomaly notes:
819 * 05000231 - STOP bit is always set to 1 whatever the user is set.
820 */
821 if (termios->c_cflag & CSTOPB) {
822 if (ANOMALY_05000231)
823 printk(KERN_WARNING "STOP bits other than 1 is not "
824 "supported in case of anomaly 05000231.\n");
825 else
826 lcr |= STB;
827 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800828 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700829 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800830 if (!(termios->c_cflag & PARODD))
831 lcr |= EPS;
832 if (termios->c_cflag & CMSPAR)
833 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700834
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400835 spin_lock_irqsave(&uart->port.lock, flags);
836
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800837 port->read_status_mask = OE;
838 if (termios->c_iflag & INPCK)
839 port->read_status_mask |= (FE | PE);
840 if (termios->c_iflag & (BRKINT | PARMRK))
841 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700842
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800843 /*
844 * Characters to ignore
845 */
846 port->ignore_status_mask = 0;
847 if (termios->c_iflag & IGNPAR)
848 port->ignore_status_mask |= FE | PE;
849 if (termios->c_iflag & IGNBRK) {
850 port->ignore_status_mask |= BI;
851 /*
852 * If we're ignoring parity and break indicators,
853 * ignore overruns too (for real raw support).
854 */
855 if (termios->c_iflag & IGNPAR)
856 port->ignore_status_mask |= OE;
857 }
Bryan Wu194de562007-05-06 14:50:30 -0700858
859 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400860 quot = uart_get_divisor(port, baud);
861
862 /* If discipline is not IRDA, apply ANOMALY_05000230 */
863 if (termios->c_line != N_IRDA)
864 quot -= ANOMALY_05000230;
865
Mike Frysinger8851c712007-12-24 19:48:04 +0800866 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
867
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800868 /* Wait till the transfer buffer is empty */
869 timeout = jiffies + msecs_to_jiffies(10);
870 while (UART_GET_GCTL(uart) & UCEN && !(UART_GET_LSR(uart) & TEMT))
871 if (time_after(jiffies, timeout)) {
872 dev_warn(port->dev, "timeout waiting for TX buffer empty\n");
873 break;
874 }
875
Bryan Wu194de562007-05-06 14:50:30 -0700876 /* Disable UART */
877 ier = UART_GET_IER(uart);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800878 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) & ~UCEN);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800879 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700880
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800881 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +0800882 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700883
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800884 UART_PUT_CLK(uart, quot);
Bryan Wu194de562007-05-06 14:50:30 -0700885 SSYNC();
886
887 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800888 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700889
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800890 UART_PUT_LCR(uart, (UART_GET_LCR(uart) & ~LCR_MASK) | lcr);
Bryan Wu194de562007-05-06 14:50:30 -0700891
892 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800893 UART_ENABLE_INTS(uart, ier);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800894 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) | UCEN);
Bryan Wu194de562007-05-06 14:50:30 -0700895
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100896 /* Port speed changed, update the per-port timeout. */
897 uart_update_timeout(port, termios->c_cflag, baud);
898
Bryan Wu194de562007-05-06 14:50:30 -0700899 spin_unlock_irqrestore(&uart->port.lock, flags);
900}
901
902static const char *bfin_serial_type(struct uart_port *port)
903{
904 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
905
906 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
907}
908
909/*
910 * Release the memory region(s) being used by 'port'.
911 */
912static void bfin_serial_release_port(struct uart_port *port)
913{
914}
915
916/*
917 * Request the memory region(s) being used by 'port'.
918 */
919static int bfin_serial_request_port(struct uart_port *port)
920{
921 return 0;
922}
923
924/*
925 * Configure/autoconfigure the port.
926 */
927static void bfin_serial_config_port(struct uart_port *port, int flags)
928{
929 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
930
931 if (flags & UART_CONFIG_TYPE &&
932 bfin_serial_request_port(&uart->port) == 0)
933 uart->port.type = PORT_BFIN;
934}
935
936/*
937 * Verify the new serial_struct (for TIOCSSERIAL).
938 * The only change we allow are to the flags and type, and
939 * even then only between PORT_BFIN and PORT_UNKNOWN
940 */
941static int
942bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
943{
944 return 0;
945}
946
Graf Yang7d01b472008-02-29 11:31:08 +0800947/*
948 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
949 * In other cases, disable IrDA function.
950 */
Alan Coxd87d9b72010-06-01 22:52:53 +0200951static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
Graf Yang7d01b472008-02-29 11:31:08 +0800952{
Sonic Zhang57afb392009-09-09 10:46:19 +0000953 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800954 unsigned int val;
Graf Yang7d01b472008-02-29 11:31:08 +0800955
Alan Coxd87d9b72010-06-01 22:52:53 +0200956 switch (ld) {
Graf Yang7d01b472008-02-29 11:31:08 +0800957 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000958 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800959 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000960 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800961 break;
962 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000963 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800964 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000965 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800966 }
967}
968
Sonic Zhang6f955702009-04-07 16:51:15 +0100969static void bfin_serial_reset_irda(struct uart_port *port)
970{
Sonic Zhang57afb392009-09-09 10:46:19 +0000971 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800972 unsigned int val;
Sonic Zhang6f955702009-04-07 16:51:15 +0100973
Sonic Zhang57afb392009-09-09 10:46:19 +0000974 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800975 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000976 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100977 SSYNC();
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800978 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000979 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100980 SSYNC();
981}
982
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000983#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100984/* Anomaly notes:
985 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
986 * losing other bits of UART_LSR is not a problem here.
987 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000988static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
989{
990 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
991
992 while (!(UART_GET_LSR(uart) & THRE))
993 cpu_relax();
994
995 UART_CLEAR_DLAB(uart);
996 UART_PUT_CHAR(uart, (unsigned char)chr);
997}
998
999static int bfin_serial_poll_get_char(struct uart_port *port)
1000{
1001 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1002 unsigned char chr;
1003
1004 while (!(UART_GET_LSR(uart) & DR))
1005 cpu_relax();
1006
1007 UART_CLEAR_DLAB(uart);
1008 chr = UART_GET_CHAR(uart);
1009
1010 return chr;
1011}
1012#endif
1013
Bryan Wu194de562007-05-06 14:50:30 -07001014static struct uart_ops bfin_serial_pops = {
1015 .tx_empty = bfin_serial_tx_empty,
1016 .set_mctrl = bfin_serial_set_mctrl,
1017 .get_mctrl = bfin_serial_get_mctrl,
1018 .stop_tx = bfin_serial_stop_tx,
1019 .start_tx = bfin_serial_start_tx,
1020 .stop_rx = bfin_serial_stop_rx,
1021 .enable_ms = bfin_serial_enable_ms,
1022 .break_ctl = bfin_serial_break_ctl,
1023 .startup = bfin_serial_startup,
1024 .shutdown = bfin_serial_shutdown,
1025 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001026 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001027 .type = bfin_serial_type,
1028 .release_port = bfin_serial_release_port,
1029 .request_port = bfin_serial_request_port,
1030 .config_port = bfin_serial_config_port,
1031 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001032#ifdef CONFIG_CONSOLE_POLL
1033 .poll_put_char = bfin_serial_poll_put_char,
1034 .poll_get_char = bfin_serial_poll_get_char,
1035#endif
Bryan Wu194de562007-05-06 14:50:30 -07001036};
1037
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001038#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001039/*
1040 * If the port was already initialised (eg, by a boot loader),
1041 * try to determine the current setup.
1042 */
1043static void __init
1044bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1045 int *parity, int *bits)
1046{
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +08001047 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -07001048
1049 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1050 if (status == (ERBFI | ETBEI)) {
1051 /* ok, the port was enabled */
Sonic Zhang59bd2342012-05-16 14:22:26 +08001052 u32 lcr, clk;
Bryan Wu194de562007-05-06 14:50:30 -07001053
1054 lcr = UART_GET_LCR(uart);
1055
1056 *parity = 'n';
1057 if (lcr & PEN) {
1058 if (lcr & EPS)
1059 *parity = 'e';
1060 else
1061 *parity = 'o';
1062 }
Sonic Zhang59bd2342012-05-16 14:22:26 +08001063 *bits = ((lcr & WLS_MASK) >> WLS_OFFSET) + 5;
1064
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001065 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +08001066 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001067
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001068 clk = UART_GET_CLK(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001069
1070 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001071 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001072
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001073 *baud = get_sclk() / (16*clk);
Bryan Wu194de562007-05-06 14:50:30 -07001074 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001075 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001076}
Robin Getz0ae53642007-10-09 17:24:49 +08001077
Robin Getz0ae53642007-10-09 17:24:49 +08001078static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001079
Sonic Zhang57afb392009-09-09 10:46:19 +00001080static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1081{
1082 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1083 while (!(UART_GET_LSR(uart) & THRE))
1084 barrier();
1085 UART_PUT_CHAR(uart, ch);
1086}
1087
1088#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1089 defined (CONFIG_EARLY_PRINTK) */
1090
1091#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1092#define CLASS_BFIN_CONSOLE "bfin-console"
1093/*
1094 * Interrupts are disabled on entering
1095 */
1096static void
1097bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1098{
1099 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1100 unsigned long flags;
1101
1102 spin_lock_irqsave(&uart->port.lock, flags);
1103 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1104 spin_unlock_irqrestore(&uart->port.lock, flags);
1105
1106}
1107
Bryan Wu194de562007-05-06 14:50:30 -07001108static int __init
1109bfin_serial_console_setup(struct console *co, char *options)
1110{
1111 struct bfin_serial_port *uart;
1112 int baud = 57600;
1113 int bits = 8;
1114 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001115# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1116 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001117 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001118# else
Bryan Wu194de562007-05-06 14:50:30 -07001119 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001120# endif
Bryan Wu194de562007-05-06 14:50:30 -07001121
1122 /*
1123 * Check whether an invalid uart number has been specified, and
1124 * if so, search for the first available port that does have
1125 * console support.
1126 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001127 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1128 return -ENODEV;
1129
1130 uart = bfin_serial_ports[co->index];
1131 if (!uart)
1132 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001133
1134 if (options)
1135 uart_parse_options(options, &baud, &parity, &bits, &flow);
1136 else
1137 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1138
1139 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001140}
Robin Getz0ae53642007-10-09 17:24:49 +08001141
Bryan Wu194de562007-05-06 14:50:30 -07001142static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001143 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001144 .write = bfin_serial_console_write,
1145 .device = uart_console_device,
1146 .setup = bfin_serial_console_setup,
1147 .flags = CON_PRINTBUFFER,
1148 .index = -1,
1149 .data = &bfin_serial_reg,
1150};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001151#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001152#else
1153#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001154#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1155
Sonic Zhang57afb392009-09-09 10:46:19 +00001156#ifdef CONFIG_EARLY_PRINTK
1157static struct bfin_serial_port bfin_earlyprintk_port;
1158#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001159
Sonic Zhang57afb392009-09-09 10:46:19 +00001160/*
1161 * Interrupts are disabled on entering
1162 */
1163static void
1164bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001165{
Sonic Zhang57afb392009-09-09 10:46:19 +00001166 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001167
Sonic Zhang57afb392009-09-09 10:46:19 +00001168 if (bfin_earlyprintk_port.port.line != co->index)
1169 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001170
Sonic Zhang57afb392009-09-09 10:46:19 +00001171 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1172 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1173 bfin_serial_console_putchar);
1174 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001175}
1176
Robin Getz7de7c552009-06-11 13:38:57 +01001177/*
1178 * This should have a .setup or .early_setup in it, but then things get called
1179 * without the command line options, and the baud rate gets messed up - so
1180 * don't let the common infrastructure play with things. (see calls to setup
1181 * & earlysetup in ./kernel/printk.c:register_console()
1182 */
Mike Frysingerc1113402008-10-13 10:32:35 +01001183static struct __initdata console bfin_early_serial_console = {
Robin Getz0ae53642007-10-09 17:24:49 +08001184 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001185 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001186 .device = uart_console_device,
1187 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001188 .index = -1,
1189 .data = &bfin_serial_reg,
1190};
Sonic Zhang57afb392009-09-09 10:46:19 +00001191#endif
1192
1193static struct uart_driver bfin_serial_reg = {
1194 .owner = THIS_MODULE,
1195 .driver_name = DRIVER_NAME,
1196 .dev_name = BFIN_SERIAL_DEV_NAME,
1197 .major = BFIN_SERIAL_MAJOR,
1198 .minor = BFIN_SERIAL_MINOR,
1199 .nr = BFIN_UART_NR_PORTS,
1200 .cons = BFIN_SERIAL_CONSOLE,
1201};
1202
1203static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1204{
1205 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1206
1207 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1208}
1209
1210static int bfin_serial_resume(struct platform_device *pdev)
1211{
1212 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1213
1214 return uart_resume_port(&bfin_serial_reg, &uart->port);
1215}
1216
1217static int bfin_serial_probe(struct platform_device *pdev)
1218{
1219 struct resource *res;
1220 struct bfin_serial_port *uart = NULL;
1221 int ret = 0;
1222
1223 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1224 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1225 return -ENOENT;
1226 }
1227
1228 if (bfin_serial_ports[pdev->id] == NULL) {
1229
1230 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1231 if (!uart) {
1232 dev_err(&pdev->dev,
1233 "fail to malloc bfin_serial_port\n");
1234 return -ENOMEM;
1235 }
1236 bfin_serial_ports[pdev->id] = uart;
1237
1238#ifdef CONFIG_EARLY_PRINTK
1239 if (!(bfin_earlyprintk_port.port.membase
1240 && bfin_earlyprintk_port.port.line == pdev->id)) {
1241 /*
1242 * If the peripheral PINs of current port is allocated
1243 * in earlyprintk probe stage, don't do it again.
1244 */
1245#endif
1246 ret = peripheral_request_list(
1247 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1248 if (ret) {
1249 dev_err(&pdev->dev,
1250 "fail to request bfin serial peripherals\n");
1251 goto out_error_free_mem;
1252 }
1253#ifdef CONFIG_EARLY_PRINTK
1254 }
1255#endif
1256
1257 spin_lock_init(&uart->port.lock);
1258 uart->port.uartclk = get_sclk();
1259 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1260 uart->port.ops = &bfin_serial_pops;
1261 uart->port.line = pdev->id;
1262 uart->port.iotype = UPIO_MEM;
1263 uart->port.flags = UPF_BOOT_AUTOCONF;
1264
1265 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1266 if (res == NULL) {
1267 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1268 ret = -ENOENT;
1269 goto out_error_free_peripherals;
1270 }
1271
Joe Perches28f65c112011-06-09 09:13:32 -07001272 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001273 if (!uart->port.membase) {
1274 dev_err(&pdev->dev, "Cannot map uart IO\n");
1275 ret = -ENXIO;
1276 goto out_error_free_peripherals;
1277 }
1278 uart->port.mapbase = res->start;
1279
Sonic Zhang47918f02011-08-12 11:15:51 +08001280 uart->tx_irq = platform_get_irq(pdev, 0);
1281 if (uart->tx_irq < 0) {
1282 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001283 ret = -ENOENT;
1284 goto out_error_unmap;
1285 }
1286
Sonic Zhang47918f02011-08-12 11:15:51 +08001287 uart->rx_irq = platform_get_irq(pdev, 1);
1288 if (uart->rx_irq < 0) {
1289 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1290 ret = -ENOENT;
1291 goto out_error_unmap;
1292 }
1293 uart->port.irq = uart->rx_irq;
1294
1295 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001296 if (uart->status_irq < 0) {
1297 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1298 ret = -ENOENT;
1299 goto out_error_unmap;
1300 }
1301
1302#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001303 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001304 uart->tx_done = 1;
1305 uart->tx_count = 0;
1306
1307 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1308 if (res == NULL) {
1309 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1310 ret = -ENOENT;
1311 goto out_error_unmap;
1312 }
1313 uart->tx_dma_channel = res->start;
1314
1315 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1316 if (res == NULL) {
1317 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1318 ret = -ENOENT;
1319 goto out_error_unmap;
1320 }
1321 uart->rx_dma_channel = res->start;
1322
1323 init_timer(&(uart->rx_dma_timer));
1324#endif
1325
1326#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1327 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1328 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1329 if (res == NULL)
1330 uart->cts_pin = -1;
Sonic Zhangee948e32011-12-05 18:13:10 +08001331 else {
Sonic Zhang57afb392009-09-09 10:46:19 +00001332 uart->cts_pin = res->start;
Sonic Zhang64851632011-12-13 12:22:02 +08001333#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangee948e32011-12-05 18:13:10 +08001334 uart->port.flags |= ASYNC_CTS_FLOW;
Sonic Zhang64851632011-12-13 12:22:02 +08001335#endif
Sonic Zhangee948e32011-12-05 18:13:10 +08001336 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001337
1338 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1339 if (res == NULL)
1340 uart->rts_pin = -1;
1341 else
1342 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001343#endif
1344 }
1345
1346#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1347 if (!is_early_platform_device(pdev)) {
1348#endif
1349 uart = bfin_serial_ports[pdev->id];
1350 uart->port.dev = &pdev->dev;
1351 dev_set_drvdata(&pdev->dev, uart);
1352 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1353#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1354 }
1355#endif
1356
1357 if (!ret)
1358 return 0;
1359
1360 if (uart) {
1361out_error_unmap:
1362 iounmap(uart->port.membase);
1363out_error_free_peripherals:
1364 peripheral_free_list(
1365 (unsigned short *)pdev->dev.platform_data);
1366out_error_free_mem:
1367 kfree(uart);
1368 bfin_serial_ports[pdev->id] = NULL;
1369 }
1370
1371 return ret;
1372}
1373
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001374static int bfin_serial_remove(struct platform_device *pdev)
Sonic Zhang57afb392009-09-09 10:46:19 +00001375{
1376 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1377
1378 dev_set_drvdata(&pdev->dev, NULL);
1379
1380 if (uart) {
1381 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001382 iounmap(uart->port.membase);
1383 peripheral_free_list(
1384 (unsigned short *)pdev->dev.platform_data);
1385 kfree(uart);
1386 bfin_serial_ports[pdev->id] = NULL;
1387 }
1388
1389 return 0;
1390}
1391
1392static struct platform_driver bfin_serial_driver = {
1393 .probe = bfin_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001394 .remove = bfin_serial_remove,
Sonic Zhang57afb392009-09-09 10:46:19 +00001395 .suspend = bfin_serial_suspend,
1396 .resume = bfin_serial_resume,
1397 .driver = {
1398 .name = DRIVER_NAME,
1399 .owner = THIS_MODULE,
1400 },
1401};
1402
1403#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1404static __initdata struct early_platform_driver early_bfin_serial_driver = {
1405 .class_str = CLASS_BFIN_CONSOLE,
1406 .pdrv = &bfin_serial_driver,
1407 .requested_id = EARLY_PLATFORM_ID_UNSET,
1408};
1409
1410static int __init bfin_serial_rs_console_init(void)
1411{
1412 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1413
1414 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1415
1416 register_console(&bfin_serial_console);
1417
1418 return 0;
1419}
1420console_initcall(bfin_serial_rs_console_init);
1421#endif
1422
1423#ifdef CONFIG_EARLY_PRINTK
1424/*
1425 * Memory can't be allocated dynamically during earlyprink init stage.
1426 * So, do individual probe for earlyprink with a static uart port variable.
1427 */
1428static int bfin_earlyprintk_probe(struct platform_device *pdev)
1429{
1430 struct resource *res;
1431 int ret;
1432
1433 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1434 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1435 return -ENOENT;
1436 }
1437
1438 ret = peripheral_request_list(
1439 (unsigned short *)pdev->dev.platform_data, DRIVER_NAME);
1440 if (ret) {
1441 dev_err(&pdev->dev,
1442 "fail to request bfin serial peripherals\n");
1443 return ret;
1444 }
1445
1446 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1447 if (res == NULL) {
1448 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1449 ret = -ENOENT;
1450 goto out_error_free_peripherals;
1451 }
1452
1453 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c112011-06-09 09:13:32 -07001454 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001455 if (!bfin_earlyprintk_port.port.membase) {
1456 dev_err(&pdev->dev, "Cannot map uart IO\n");
1457 ret = -ENXIO;
1458 goto out_error_free_peripherals;
1459 }
1460 bfin_earlyprintk_port.port.mapbase = res->start;
1461 bfin_earlyprintk_port.port.line = pdev->id;
1462 bfin_earlyprintk_port.port.uartclk = get_sclk();
1463 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1464 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1465
1466 return 0;
1467
1468out_error_free_peripherals:
1469 peripheral_free_list(
1470 (unsigned short *)pdev->dev.platform_data);
1471
1472 return ret;
1473}
1474
1475static struct platform_driver bfin_earlyprintk_driver = {
1476 .probe = bfin_earlyprintk_probe,
1477 .driver = {
1478 .name = DRIVER_NAME,
1479 .owner = THIS_MODULE,
1480 },
1481};
1482
1483static __initdata struct early_platform_driver early_bfin_earlyprintk_driver = {
1484 .class_str = CLASS_BFIN_EARLYPRINTK,
1485 .pdrv = &bfin_earlyprintk_driver,
1486 .requested_id = EARLY_PLATFORM_ID_UNSET,
1487};
Robin Getz0ae53642007-10-09 17:24:49 +08001488
1489struct console __init *bfin_earlyserial_init(unsigned int port,
1490 unsigned int cflag)
1491{
Robin Getz0ae53642007-10-09 17:24:49 +08001492 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001493 char port_name[20];
1494
1495 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1496 return NULL;
1497
1498 /*
1499 * Only probe resource of the given port in earlyprintk boot arg.
1500 * The expected port id should be indicated in port name string.
1501 */
1502 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1503 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1504 port_name);
1505 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1506
1507 if (!bfin_earlyprintk_port.port.membase)
1508 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001509
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001510#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1511 /*
1512 * If we are using early serial, don't let the normal console rewind
1513 * log buffer, since that causes things to be printed multiple times
1514 */
1515 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1516#endif
1517
Robin Getz0ae53642007-10-09 17:24:49 +08001518 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001519 t.c_cflag = cflag;
1520 t.c_iflag = 0;
1521 t.c_oflag = 0;
1522 t.c_lflag = ICANON;
1523 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001524 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1525
Robin Getz0ae53642007-10-09 17:24:49 +08001526 return &bfin_early_serial_console;
1527}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001528#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001529
Bryan Wu194de562007-05-06 14:50:30 -07001530static int __init bfin_serial_init(void)
1531{
1532 int ret;
1533
Sonic Zhang57afb392009-09-09 10:46:19 +00001534 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001535
1536 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001537 if (ret) {
1538 pr_err("failed to register %s:%d\n",
1539 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001540 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001541
1542 ret = platform_driver_register(&bfin_serial_driver);
1543 if (ret) {
1544 pr_err("fail to register bfin uart\n");
1545 uart_unregister_driver(&bfin_serial_reg);
1546 }
1547
Bryan Wu194de562007-05-06 14:50:30 -07001548 return ret;
1549}
1550
1551static void __exit bfin_serial_exit(void)
1552{
1553 platform_driver_unregister(&bfin_serial_driver);
1554 uart_unregister_driver(&bfin_serial_reg);
1555}
1556
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001557
Bryan Wu194de562007-05-06 14:50:30 -07001558module_init(bfin_serial_init);
1559module_exit(bfin_serial_exit);
1560
Sonic Zhang57afb392009-09-09 10:46:19 +00001561MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001562MODULE_DESCRIPTION("Blackfin generic serial port driver");
1563MODULE_LICENSE("GPL");
1564MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001565MODULE_ALIAS("platform:bfin-uart");