blob: dddc081568f176b12c397b66008811468fb01fa0 [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
Bryan Wu194de562007-05-06 14:50:30 -070044/* UART name and device definitions */
Sonic Zhang57afb392009-09-09 10:46:19 +000045#define BFIN_SERIAL_DEV_NAME "ttyBF"
Bryan Wu194de562007-05-06 14:50:30 -070046#define BFIN_SERIAL_MAJOR 204
47#define BFIN_SERIAL_MINOR 64
48
Sonic Zhang57afb392009-09-09 10:46:19 +000049static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
Mike Frysingerc9607ec2008-10-13 10:33:16 +010050
Sonic Zhang52e15f0e2009-01-02 13:40:14 +000051#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
52 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
53
54# ifndef CONFIG_SERIAL_BFIN_PIO
55# error KGDB only support UART in PIO mode.
56# endif
57
58static int kgdboc_port_line;
59static int kgdboc_break_enabled;
60#endif
Bryan Wu194de562007-05-06 14:50:30 -070061/*
62 * Setup for console. Argument comes from the menuconfig
63 */
64#define DMA_RX_XCOUNT 512
65#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
66
Sonic Zhang0aef4562008-02-29 12:08:42 +080067#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070068
69#ifdef CONFIG_SERIAL_BFIN_DMA
70static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
71#else
Bryan Wu194de562007-05-06 14:50:30 -070072static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070073#endif
74
Graf Yang80d5c472009-01-02 13:40:22 +000075static void bfin_serial_reset_irda(struct uart_port *port);
76
Sonic Zhangd307d362009-04-07 16:52:26 +010077#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
78 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
79static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
80{
81 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
82 if (uart->cts_pin < 0)
83 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
84
85 /* CTS PIN is negative assertive. */
86 if (UART_GET_CTS(uart))
87 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
88 else
89 return TIOCM_DSR | TIOCM_CAR;
90}
91
92static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
93{
94 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
95 if (uart->rts_pin < 0)
96 return;
97
98 /* RTS PIN is negative assertive. */
99 if (mctrl & TIOCM_RTS)
100 UART_ENABLE_RTS(uart);
101 else
102 UART_DISABLE_RTS(uart);
103}
104
105/*
106 * Handle any change of modem status signal.
107 */
108static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
109{
110 struct bfin_serial_port *uart = dev_id;
Sonic Zhang64851632011-12-13 12:22:02 +0800111 unsigned int status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhangd307d362009-04-07 16:52:26 +0100112#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang64851632011-12-13 12:22:02 +0800113 struct tty_struct *tty = uart->port.state->port.tty;
114
Sonic Zhangd307d362009-04-07 16:52:26 +0100115 UART_CLEAR_SCTS(uart);
Sonic Zhang64851632011-12-13 12:22:02 +0800116 if (tty->hw_stopped) {
117 if (status) {
118 tty->hw_stopped = 0;
119 uart_write_wakeup(&uart->port);
120 }
121 } else {
122 if (!status)
123 tty->hw_stopped = 1;
124 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100125#endif
Sonic Zhangf5b69402011-12-05 18:13:11 +0800126 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Sonic Zhangd307d362009-04-07 16:52:26 +0100127
128 return IRQ_HANDLED;
129}
130#else
131static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
132{
133 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
134}
135
136static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
137{
138}
139#endif
140
Bryan Wu194de562007-05-06 14:50:30 -0700141/*
142 * interrupts are disabled on entry
143 */
144static void bfin_serial_stop_tx(struct uart_port *port)
145{
146 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000147#ifdef CONFIG_SERIAL_BFIN_DMA
Alan Coxebd2c8f2009-09-19 13:13:28 -0700148 struct circ_buf *xmit = &uart->port.state->xmit;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000149#endif
Bryan Wu194de562007-05-06 14:50:30 -0700150
Roy Huangf4d640c92007-07-12 16:43:46 +0800151 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +0800152 cpu_relax();
Roy Huangf4d640c92007-07-12 16:43:46 +0800153
Bryan Wu194de562007-05-06 14:50:30 -0700154#ifdef CONFIG_SERIAL_BFIN_DMA
155 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800156 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
157 uart->port.icount.tx += uart->tx_count;
158 uart->tx_count = 0;
159 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -0700160#else
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800161#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Roy Huangf4d640c92007-07-12 16:43:46 +0800162 /* Clear TFI bit */
163 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -0700164#endif
Mike Frysinger89bf6dc52008-05-07 11:41:26 +0800165 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c92007-07-12 16:43:46 +0800166#endif
Bryan Wu194de562007-05-06 14:50:30 -0700167}
168
169/*
170 * port is locked and interrupts are disabled
171 */
172static void bfin_serial_start_tx(struct uart_port *port)
173{
174 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700175 struct tty_struct *tty = uart->port.state->port.tty;
Graf Yang80d5c472009-01-02 13:40:22 +0000176
177 /*
178 * To avoid losting RX interrupt, we reset IR function
179 * before sending data.
180 */
Alan Coxadc8d742012-07-14 15:31:47 +0100181 if (tty->termios.c_line == N_IRDA)
Graf Yang80d5c472009-01-02 13:40:22 +0000182 bfin_serial_reset_irda(port);
Bryan Wu194de562007-05-06 14:50:30 -0700183
184#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +0800185 if (uart->tx_done)
186 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700187#else
Roy Huangf4d640c92007-07-12 16:43:46 +0800188 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +0800189 bfin_serial_tx_chars(uart);
Roy Huangf4d640c92007-07-12 16:43:46 +0800190#endif
Bryan Wu194de562007-05-06 14:50:30 -0700191}
192
193/*
194 * Interrupts are enabled
195 */
196static void bfin_serial_stop_rx(struct uart_port *port)
197{
198 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000199
Roy Huangf4d640c92007-07-12 16:43:46 +0800200 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700201}
202
Mike Frysinger50e2e152008-04-25 03:03:03 +0800203#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800204# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
205# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
206#else
207# define UART_GET_ANOMALY_THRESHOLD(uart) 0
208# define UART_SET_ANOMALY_THRESHOLD(uart, v)
209#endif
210
Bryan Wu194de562007-05-06 14:50:30 -0700211#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700212static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
213{
Bryan Wu194de562007-05-06 14:50:30 -0700214 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800215 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700216
Sonic Zhang759eb042007-11-21 17:00:32 +0800217 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800218 UART_CLEAR_LSR(uart);
219
Sonic Zhangbb7e58f2011-07-19 18:09:24 +0800220 ch = UART_GET_CHAR(uart);
221 uart->port.icount.rx++;
Bryan Wu194de562007-05-06 14:50:30 -0700222
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000223#if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
224 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
Sonic Zhangcdc592d2010-01-19 06:13:12 -0500225 if (kgdb_connected && kgdboc_port_line == uart->port.line
226 && kgdboc_break_enabled)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000227 if (ch == 0x3) {/* Ctrl + C */
228 kgdb_breakpoint();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800229 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800230 }
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000231
Jiri Slaby2e124b42013-01-03 15:53:06 +0100232 if (!uart->port.state)
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000233 return;
Sonic Zhang474f1a62007-06-29 16:35:17 +0800234#endif
Mike Frysinger50e2e152008-04-25 03:03:03 +0800235 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800236 /* The BF533 (and BF561) family of processors have a nice anomaly
237 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800238 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800239 * character comes across. Due to the nature of the flood, it is
240 * not possible to reliably catch bytes that are sent too quickly
241 * after this break. So application code talking to the Blackfin
242 * which sends a break signal must allow at least 1.5 character
243 * times after the end of the break for things to stabilize. This
244 * timeout was picked as it must absolutely be larger than 1
245 * character time +/- some percent. So 1.5 sounds good. All other
246 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800247 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800248 if (anomaly_start.tv_sec) {
249 struct timeval curr;
250 suseconds_t usecs;
251
252 if ((~ch & (~ch + 1)) & 0xff)
253 goto known_good_char;
254
255 do_gettimeofday(&curr);
256 if (curr.tv_sec - anomaly_start.tv_sec > 1)
257 goto known_good_char;
258
259 usecs = 0;
260 if (curr.tv_sec != anomaly_start.tv_sec)
261 usecs += USEC_PER_SEC;
262 usecs += curr.tv_usec - anomaly_start.tv_usec;
263
264 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
265 goto known_good_char;
266
267 if (ch)
268 anomaly_start.tv_sec = 0;
269 else
270 anomaly_start = curr;
271
272 return;
273
274 known_good_char:
Sonic Zhange482a232009-01-02 13:40:45 +0000275 status &= ~BI;
Mike Frysinger8851c712007-12-24 19:48:04 +0800276 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800277 }
Bryan Wu194de562007-05-06 14:50:30 -0700278 }
Bryan Wu194de562007-05-06 14:50:30 -0700279
280 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800281 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800282 if (bfin_revid() < 5)
283 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700284 uart->port.icount.brk++;
285 if (uart_handle_break(&uart->port))
286 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800287 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800288 }
289 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700290 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800291 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700292 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800293 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700294 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800295
296 status &= uart->port.read_status_mask;
297
298 if (status & BI)
299 flg = TTY_BREAK;
300 else if (status & PE)
301 flg = TTY_PARITY;
302 else if (status & FE)
303 flg = TTY_FRAME;
304 else
Bryan Wu194de562007-05-06 14:50:30 -0700305 flg = TTY_NORMAL;
306
307 if (uart_handle_sysrq_char(&uart->port, ch))
308 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700309
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800310 uart_insert_char(&uart->port, status, OE, ch, flg);
311
312 ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100313 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700314}
315
316static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
317{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700318 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700319
Bryan Wu194de562007-05-06 14:50:30 -0700320 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800321#if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100322 /* Clear TFI bit */
323 UART_PUT_LSR(uart, TFI);
324#endif
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100325 /* Anomaly notes:
326 * 05000215 - we always clear ETBEI within last UART TX
327 * interrupt to end a string. It is always set
328 * when start a new tx.
329 */
Sonic Zhang5ffdeea22008-10-13 10:33:33 +0100330 UART_CLEAR_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700331 return;
332 }
333
Sonic Zhangf30ac0c2008-06-19 17:46:39 +0800334 if (uart->port.x_char) {
335 UART_PUT_CHAR(uart, uart->port.x_char);
336 uart->port.icount.tx++;
337 uart->port.x_char = 0;
338 }
339
Sonic Zhang759eb042007-11-21 17:00:32 +0800340 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
341 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
342 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
343 uart->port.icount.tx++;
Sonic Zhang759eb042007-11-21 17:00:32 +0800344 }
Bryan Wu194de562007-05-06 14:50:30 -0700345
346 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
347 uart_write_wakeup(&uart->port);
Bryan Wu194de562007-05-06 14:50:30 -0700348}
349
Aubrey Li5c4e4722007-05-21 18:09:38 +0800350static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
351{
352 struct bfin_serial_port *uart = dev_id;
353
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800354 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800355 bfin_serial_rx_chars(uart);
Sonic Zhang759eb042007-11-21 17:00:32 +0800356
Aubrey Li5c4e4722007-05-21 18:09:38 +0800357 return IRQ_HANDLED;
358}
359
360static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700361{
362 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700363
Roy Huangf4d640c92007-07-12 16:43:46 +0800364 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800365 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800366 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700367 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800368
Bryan Wu194de562007-05-06 14:50:30 -0700369 return IRQ_HANDLED;
370}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800371#endif
Bryan Wu194de562007-05-06 14:50:30 -0700372
Bryan Wu194de562007-05-06 14:50:30 -0700373#ifdef CONFIG_SERIAL_BFIN_DMA
374static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
375{
Alan Coxebd2c8f2009-09-19 13:13:28 -0700376 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700377
Bryan Wu194de562007-05-06 14:50:30 -0700378 uart->tx_done = 0;
379
Bryan Wu194de562007-05-06 14:50:30 -0700380 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800381 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700382 uart->tx_done = 1;
383 return;
384 }
385
Sonic Zhang1b733512007-12-21 16:45:12 +0800386 if (uart->port.x_char) {
387 UART_PUT_CHAR(uart, uart->port.x_char);
388 uart->port.icount.tx++;
389 uart->port.x_char = 0;
390 }
391
Bryan Wu194de562007-05-06 14:50:30 -0700392 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
393 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
394 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
395 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
396 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
397 set_dma_config(uart->tx_dma_channel,
398 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
399 INTR_ON_BUF,
400 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800401 DATA_SIZE_8,
402 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700403 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
404 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
405 set_dma_x_modify(uart->tx_dma_channel, 1);
Graf Yangf9d36da2009-06-11 13:42:17 +0100406 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700407 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800408
Roy Huangf4d640c92007-07-12 16:43:46 +0800409 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700410}
411
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800412static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700413{
Bryan Wu194de562007-05-06 14:50:30 -0700414 int i, flg, status;
415
416 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800417 UART_CLEAR_LSR(uart);
418
Sonic Zhang56f5de82008-02-25 15:19:09 +0800419 uart->port.icount.rx +=
420 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
421 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700422
423 if (status & BI) {
424 uart->port.icount.brk++;
425 if (uart_handle_break(&uart->port))
426 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800427 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800428 }
429 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700430 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800431 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700432 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800433 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700434 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800435
436 status &= uart->port.read_status_mask;
437
438 if (status & BI)
439 flg = TTY_BREAK;
440 else if (status & PE)
441 flg = TTY_PARITY;
442 else if (status & FE)
443 flg = TTY_FRAME;
444 else
Bryan Wu194de562007-05-06 14:50:30 -0700445 flg = TTY_NORMAL;
446
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100447 for (i = uart->rx_dma_buf.tail; ; i++) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800448 if (i >= UART_XMIT_SIZE)
449 i = 0;
Sonic Zhang8c4210e2009-04-06 17:32:42 +0100450 if (i == uart->rx_dma_buf.head)
451 break;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800452 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
453 uart_insert_char(&uart->port, status, OE,
454 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700455 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800456
457 dma_ignore_char:
Jiri Slaby2e124b42013-01-03 15:53:06 +0100458 tty_flip_buffer_push(&uart->port.state->port);
Bryan Wu194de562007-05-06 14:50:30 -0700459}
460
461void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
462{
Mike Frysinger59e4e3e2009-04-06 17:32:28 +0100463 int x_pos, pos;
Steven Miao9642dbe2012-11-07 13:27:56 +0800464 unsigned long flags;
Sonic Zhang68a784c2009-01-02 13:40:38 +0000465
Steven Miao9642dbe2012-11-07 13:27:56 +0800466 spin_lock_irqsave(&uart->rx_lock, flags);
Bryan Wu194de562007-05-06 14:50:30 -0700467
Sonic Zhang8516c562009-06-11 13:38:16 +0100468 /* 2D DMA RX buffer ring is used. Because curr_y_count and
469 * curr_x_count can't be read as an atomic operation,
470 * curr_y_count should be read before curr_x_count. When
471 * curr_x_count is read, curr_y_count may already indicate
472 * next buffer line. But, the position calculated here is
473 * still indicate the old line. The wrong position data may
474 * be smaller than current buffer tail, which cause garbages
475 * are received if it is not prohibit.
476 */
Sonic Zhang56f5de82008-02-25 15:19:09 +0800477 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
478 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
479 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100480 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang56f5de82008-02-25 15:19:09 +0800481 uart->rx_dma_nrows = 0;
482 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700483 if (x_pos == DMA_RX_XCOUNT)
484 x_pos = 0;
485
486 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang8516c562009-06-11 13:38:16 +0100487 /* Ignore receiving data if new position is in the same line of
488 * current buffer tail and small.
489 */
490 if (pos > uart->rx_dma_buf.tail ||
491 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
Sonic Zhang56f5de82008-02-25 15:19:09 +0800492 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700493 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800494 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700495 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800496
Steven Miao9642dbe2012-11-07 13:27:56 +0800497 spin_unlock_irqrestore(&uart->rx_lock, flags);
Sonic Zhang68a784c2009-01-02 13:40:38 +0000498
Sonic Zhang0a278422008-04-25 04:36:47 +0800499 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700500}
501
502static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
503{
504 struct bfin_serial_port *uart = dev_id;
Alan Coxebd2c8f2009-09-19 13:13:28 -0700505 struct circ_buf *xmit = &uart->port.state->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700506
507 spin_lock(&uart->port.lock);
508 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700509 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800510 clear_dma_irqstat(uart->tx_dma_channel);
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100511 /* Anomaly notes:
512 * 05000215 - we always clear ETBEI within last UART TX
513 * interrupt to end a string. It is always set
514 * when start a new tx.
515 */
Roy Huangf4d640c92007-07-12 16:43:46 +0800516 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800517 uart->port.icount.tx += uart->tx_count;
Sonic Zhang239c25b2012-05-16 14:22:24 +0800518 if (!(xmit->tail == 0 && xmit->head == 0)) {
Sonic Zhang60f4b002012-03-13 15:51:55 +0800519 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
Sonic Zhang1b733512007-12-21 16:45:12 +0800520
Sonic Zhang60f4b002012-03-13 15:51:55 +0800521 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
522 uart_write_wakeup(&uart->port);
523 }
Sonic Zhang56f5de82008-02-25 15:19:09 +0800524
Sonic Zhang1b733512007-12-21 16:45:12 +0800525 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700526 }
527
528 spin_unlock(&uart->port.lock);
529 return IRQ_HANDLED;
530}
531
532static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
533{
534 struct bfin_serial_port *uart = dev_id;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800535 unsigned int irqstat;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100536 int x_pos, pos;
Sonic Zhang0711d852008-02-25 15:16:50 +0800537
Sonic Zhang0f66e502011-01-11 00:16:43 -0500538 spin_lock(&uart->rx_lock);
Bryan Wu194de562007-05-06 14:50:30 -0700539 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
540 clear_dma_irqstat(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100541
542 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
Sonic Zhang35ff6932009-06-11 13:42:57 +0100543 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
Sonic Zhang8516c562009-06-11 13:38:16 +0100544 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
Sonic Zhang35ff6932009-06-11 13:42:57 +0100545 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
Sonic Zhang8516c562009-06-11 13:38:16 +0100546 uart->rx_dma_nrows = 0;
547
548 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
549 if (pos > uart->rx_dma_buf.tail ||
550 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
551 uart->rx_dma_buf.head = pos;
552 bfin_serial_dma_rx_chars(uart);
553 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
554 }
555
Sonic Zhang0f66e502011-01-11 00:16:43 -0500556 spin_unlock(&uart->rx_lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800557
Bryan Wu194de562007-05-06 14:50:30 -0700558 return IRQ_HANDLED;
559}
560#endif
561
562/*
563 * Return TIOCSER_TEMT when transmitter is not busy.
564 */
565static unsigned int bfin_serial_tx_empty(struct uart_port *port)
566{
567 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800568 unsigned int lsr;
Bryan Wu194de562007-05-06 14:50:30 -0700569
570 lsr = UART_GET_LSR(uart);
571 if (lsr & TEMT)
572 return TIOCSER_TEMT;
573 else
574 return 0;
575}
576
Bryan Wu194de562007-05-06 14:50:30 -0700577static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
578{
Mike Frysingercf686762007-06-11 16:12:49 +0800579 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang59bd2342012-05-16 14:22:26 +0800580 u32 lcr = UART_GET_LCR(uart);
Mike Frysingercf686762007-06-11 16:12:49 +0800581 if (break_state)
582 lcr |= SB;
583 else
584 lcr &= ~SB;
585 UART_PUT_LCR(uart, lcr);
586 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700587}
588
589static int bfin_serial_startup(struct uart_port *port)
590{
591 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
592
593#ifdef CONFIG_SERIAL_BFIN_DMA
594 dma_addr_t dma_handle;
595
596 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
597 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
598 return -EBUSY;
599 }
600
601 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
602 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
603 free_dma(uart->rx_dma_channel);
604 return -EBUSY;
605 }
606
607 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
608 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
609
610 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
611 uart->rx_dma_buf.head = 0;
612 uart->rx_dma_buf.tail = 0;
613 uart->rx_dma_nrows = 0;
614
615 set_dma_config(uart->rx_dma_channel,
616 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
617 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800618 DATA_SIZE_8,
619 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700620 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
621 set_dma_x_modify(uart->rx_dma_channel, 1);
622 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
623 set_dma_y_modify(uart->rx_dma_channel, 1);
624 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
625 enable_dma(uart->rx_dma_channel);
626
627 uart->rx_dma_timer.data = (unsigned long)(uart);
628 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
629 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
630 add_timer(&(uart->rx_dma_timer));
631#else
Sonic Zhang6f955702009-04-07 16:51:15 +0100632# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000633 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
634 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
635 kgdboc_break_enabled = 0;
636 else {
637# endif
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800638 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700639 "BFIN_UART_RX", uart)) {
640 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
641 return -EBUSY;
642 }
643
644 if (request_irq
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800645 (uart->tx_irq, bfin_serial_tx_int, 0,
Bryan Wu194de562007-05-06 14:50:30 -0700646 "BFIN_UART_TX", uart)) {
647 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800648 free_irq(uart->rx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700649 return -EBUSY;
650 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100651
652# ifdef CONFIG_BF54x
653 {
Sonic Zhangb6100992010-10-27 04:16:47 -0400654 /*
655 * UART2 and UART3 on BF548 share interrupt PINs and DMA
656 * controllers with SPORT2 and SPORT3. UART rx and tx
657 * interrupts are generated in PIO mode only when configure
658 * their peripheral mapping registers properly, which means
659 * request corresponding DMA channels in PIO mode as well.
660 */
Sonic Zhangab2375f2008-10-13 10:33:51 +0100661 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
662
Sonic Zhang47918f02011-08-12 11:15:51 +0800663 switch (uart->rx_irq) {
Sonic Zhangab2375f2008-10-13 10:33:51 +0100664 case IRQ_UART3_RX:
665 uart_dma_ch_rx = CH_UART3_RX;
666 uart_dma_ch_tx = CH_UART3_TX;
667 break;
668 case IRQ_UART2_RX:
669 uart_dma_ch_rx = CH_UART2_RX;
670 uart_dma_ch_tx = CH_UART2_TX;
671 break;
672 default:
673 uart_dma_ch_rx = uart_dma_ch_tx = 0;
674 break;
Joe Perchesfc8114722013-10-08 16:14:21 -0700675 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100676
677 if (uart_dma_ch_rx &&
678 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
679 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
Sonic Zhang47918f02011-08-12 11:15:51 +0800680 free_irq(uart->rx_irq, uart);
681 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100682 return -EBUSY;
683 }
684 if (uart_dma_ch_tx &&
685 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
686 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
687 free_dma(uart_dma_ch_rx);
Sonic Zhang47918f02011-08-12 11:15:51 +0800688 free_irq(uart->rx_irq, uart);
689 free_irq(uart->tx_irq, uart);
Sonic Zhangab2375f2008-10-13 10:33:51 +0100690 return -EBUSY;
691 }
692 }
693# endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100694# if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000695 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
696 }
697# endif
Bryan Wu194de562007-05-06 14:50:30 -0700698#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100699
700#ifdef CONFIG_SERIAL_BFIN_CTSRTS
701 if (uart->cts_pin >= 0) {
702 if (request_irq(gpio_to_irq(uart->cts_pin),
703 bfin_serial_mctrl_cts_int,
704 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
Yong Zhang9cfb5c02011-09-22 16:59:15 +0800705 0, "BFIN_UART_CTS", uart)) {
Sonic Zhang6f955702009-04-07 16:51:15 +0100706 uart->cts_pin = -1;
Joe Perchesa89f2462010-11-07 13:10:23 -0800707 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
Sonic Zhang6f955702009-04-07 16:51:15 +0100708 }
709 }
Sonic Zhang32b44562011-12-05 15:12:50 +0800710 if (uart->rts_pin >= 0) {
711 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
712 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
713 uart->rts_pin = -1;
714 } else
715 gpio_direction_output(uart->rts_pin, 0);
716 }
Sonic Zhang6f955702009-04-07 16:51:15 +0100717#endif
Sonic Zhangd307d362009-04-07 16:52:26 +0100718#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhangb48dc712011-12-13 12:22:01 +0800719 if (uart->cts_pin >= 0) {
720 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
Michael Opdenacker190c6cc2013-10-06 08:27:18 +0200721 0, "BFIN_UART_MODEM_STATUS", uart)) {
Sonic Zhangb48dc712011-12-13 12:22:01 +0800722 uart->cts_pin = -1;
723 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
724 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100725
Sonic Zhangb48dc712011-12-13 12:22:01 +0800726 /* CTS RTS PINs are negative assertive. */
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800727 UART_PUT_MCR(uart, UART_GET_MCR(uart) | ACTS);
Sonic Zhangb48dc712011-12-13 12:22:01 +0800728 UART_SET_IER(uart, EDSSI);
729 }
Sonic Zhangd307d362009-04-07 16:52:26 +0100730#endif
731
Roy Huangf4d640c92007-07-12 16:43:46 +0800732 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700733 return 0;
734}
735
736static void bfin_serial_shutdown(struct uart_port *port)
737{
738 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
739
740#ifdef CONFIG_SERIAL_BFIN_DMA
741 disable_dma(uart->tx_dma_channel);
742 free_dma(uart->tx_dma_channel);
743 disable_dma(uart->rx_dma_channel);
744 free_dma(uart->rx_dma_channel);
745 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800746 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700747#else
Sonic Zhangab2375f2008-10-13 10:33:51 +0100748#ifdef CONFIG_BF54x
749 switch (uart->port.irq) {
750 case IRQ_UART3_RX:
751 free_dma(CH_UART3_RX);
752 free_dma(CH_UART3_TX);
753 break;
754 case IRQ_UART2_RX:
755 free_dma(CH_UART2_RX);
756 free_dma(CH_UART2_TX);
757 break;
758 default:
759 break;
Joe Perchesfc8114722013-10-08 16:14:21 -0700760 }
Sonic Zhangab2375f2008-10-13 10:33:51 +0100761#endif
Sonic Zhang47918f02011-08-12 11:15:51 +0800762 free_irq(uart->rx_irq, uart);
763 free_irq(uart->tx_irq, uart);
Bryan Wu194de562007-05-06 14:50:30 -0700764#endif
Sonic Zhang6f955702009-04-07 16:51:15 +0100765
Sonic Zhangd307d362009-04-07 16:52:26 +0100766#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang6f955702009-04-07 16:51:15 +0100767 if (uart->cts_pin >= 0)
768 free_irq(gpio_to_irq(uart->cts_pin), uart);
Sonic Zhang32b44562011-12-05 15:12:50 +0800769 if (uart->rts_pin >= 0)
770 gpio_free(uart->rts_pin);
Sonic Zhangd307d362009-04-07 16:52:26 +0100771#endif
772#ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
Sonic Zhang57afb392009-09-09 10:46:19 +0000773 if (uart->cts_pin >= 0)
Sonic Zhangd307d362009-04-07 16:52:26 +0100774 free_irq(uart->status_irq, uart);
775#endif
Bryan Wu194de562007-05-06 14:50:30 -0700776}
777
778static void
779bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
780 struct ktermios *old)
781{
782 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
783 unsigned long flags;
784 unsigned int baud, quot;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800785 unsigned int ier, lcr = 0;
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800786 unsigned long timeout;
Bryan Wu194de562007-05-06 14:50:30 -0700787
788 switch (termios->c_cflag & CSIZE) {
789 case CS8:
790 lcr = WLS(8);
791 break;
792 case CS7:
793 lcr = WLS(7);
794 break;
795 case CS6:
796 lcr = WLS(6);
797 break;
798 case CS5:
799 lcr = WLS(5);
800 break;
801 default:
Masanari Iida46e99c42012-10-24 23:29:41 +0900802 printk(KERN_ERR "%s: word length not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700803 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700804 }
805
Sonic Zhang84507792009-06-11 13:50:20 +0100806 /* Anomaly notes:
807 * 05000231 - STOP bit is always set to 1 whatever the user is set.
808 */
809 if (termios->c_cflag & CSTOPB) {
810 if (ANOMALY_05000231)
811 printk(KERN_WARNING "STOP bits other than 1 is not "
812 "supported in case of anomaly 05000231.\n");
813 else
814 lcr |= STB;
815 }
Mike Frysinger19aa6382007-06-11 16:16:45 +0800816 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700817 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800818 if (!(termios->c_cflag & PARODD))
819 lcr |= EPS;
820 if (termios->c_cflag & CMSPAR)
821 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700822
Sonic Zhang5bb06b62010-10-27 04:16:50 -0400823 spin_lock_irqsave(&uart->port.lock, flags);
824
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800825 port->read_status_mask = OE;
826 if (termios->c_iflag & INPCK)
827 port->read_status_mask |= (FE | PE);
Peter Hurleyef8b9dd2014-06-16 08:10:41 -0400828 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800829 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700830
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800831 /*
832 * Characters to ignore
833 */
834 port->ignore_status_mask = 0;
835 if (termios->c_iflag & IGNPAR)
836 port->ignore_status_mask |= FE | PE;
837 if (termios->c_iflag & IGNBRK) {
838 port->ignore_status_mask |= BI;
839 /*
840 * If we're ignoring parity and break indicators,
841 * ignore overruns too (for real raw support).
842 */
843 if (termios->c_iflag & IGNPAR)
844 port->ignore_status_mask |= OE;
845 }
Bryan Wu194de562007-05-06 14:50:30 -0700846
847 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
Graf Yangca3e4422010-05-23 04:40:13 -0400848 quot = uart_get_divisor(port, baud);
849
850 /* If discipline is not IRDA, apply ANOMALY_05000230 */
851 if (termios->c_line != N_IRDA)
852 quot -= ANOMALY_05000230;
853
Mike Frysinger8851c712007-12-24 19:48:04 +0800854 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
855
Sonic Zhang1cd3f2d2012-11-19 14:40:56 +0800856 /* Wait till the transfer buffer is empty */
857 timeout = jiffies + msecs_to_jiffies(10);
858 while (UART_GET_GCTL(uart) & UCEN && !(UART_GET_LSR(uart) & TEMT))
859 if (time_after(jiffies, timeout)) {
860 dev_warn(port->dev, "timeout waiting for TX buffer empty\n");
861 break;
862 }
863
Bryan Wu194de562007-05-06 14:50:30 -0700864 /* Disable UART */
865 ier = UART_GET_IER(uart);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800866 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) & ~UCEN);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800867 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700868
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800869 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +0800870 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700871
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800872 UART_PUT_CLK(uart, quot);
Bryan Wu194de562007-05-06 14:50:30 -0700873 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
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800878 UART_PUT_LCR(uart, (UART_GET_LCR(uart) & ~LCR_MASK) | lcr);
Bryan Wu194de562007-05-06 14:50:30 -0700879
880 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800881 UART_ENABLE_INTS(uart, ier);
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800882 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) | UCEN);
Bryan Wu194de562007-05-06 14:50:30 -0700883
Graf Yangb3ef5ab2008-10-13 10:33:42 +0100884 /* Port speed changed, update the per-port timeout. */
885 uart_update_timeout(port, termios->c_cflag, baud);
886
Bryan Wu194de562007-05-06 14:50:30 -0700887 spin_unlock_irqrestore(&uart->port.lock, flags);
888}
889
890static const char *bfin_serial_type(struct uart_port *port)
891{
892 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
893
894 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
895}
896
897/*
898 * Release the memory region(s) being used by 'port'.
899 */
900static void bfin_serial_release_port(struct uart_port *port)
901{
902}
903
904/*
905 * Request the memory region(s) being used by 'port'.
906 */
907static int bfin_serial_request_port(struct uart_port *port)
908{
909 return 0;
910}
911
912/*
913 * Configure/autoconfigure the port.
914 */
915static void bfin_serial_config_port(struct uart_port *port, int flags)
916{
917 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
918
919 if (flags & UART_CONFIG_TYPE &&
920 bfin_serial_request_port(&uart->port) == 0)
921 uart->port.type = PORT_BFIN;
922}
923
924/*
925 * Verify the new serial_struct (for TIOCSSERIAL).
926 * The only change we allow are to the flags and type, and
927 * even then only between PORT_BFIN and PORT_UNKNOWN
928 */
929static int
930bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
931{
932 return 0;
933}
934
Graf Yang7d01b472008-02-29 11:31:08 +0800935/*
936 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
937 * In other cases, disable IrDA function.
938 */
Alan Coxd87d9b72010-06-01 22:52:53 +0200939static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
Graf Yang7d01b472008-02-29 11:31:08 +0800940{
Sonic Zhang57afb392009-09-09 10:46:19 +0000941 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800942 unsigned int val;
Graf Yang7d01b472008-02-29 11:31:08 +0800943
Alan Coxd87d9b72010-06-01 22:52:53 +0200944 switch (ld) {
Graf Yang7d01b472008-02-29 11:31:08 +0800945 case N_IRDA:
Sonic Zhang57afb392009-09-09 10:46:19 +0000946 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800947 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000948 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800949 break;
950 default:
Sonic Zhang57afb392009-09-09 10:46:19 +0000951 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800952 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000953 UART_PUT_GCTL(uart, val);
Graf Yang7d01b472008-02-29 11:31:08 +0800954 }
955}
956
Sonic Zhang6f955702009-04-07 16:51:15 +0100957static void bfin_serial_reset_irda(struct uart_port *port)
958{
Sonic Zhang57afb392009-09-09 10:46:19 +0000959 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +0800960 unsigned int val;
Sonic Zhang6f955702009-04-07 16:51:15 +0100961
Sonic Zhang57afb392009-09-09 10:46:19 +0000962 val = UART_GET_GCTL(uart);
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800963 val &= ~(UMOD_MASK | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000964 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100965 SSYNC();
Sonic Zhangb06d2f22012-05-16 14:22:23 +0800966 val |= (UMOD_IRDA | RPOLC);
Sonic Zhang57afb392009-09-09 10:46:19 +0000967 UART_PUT_GCTL(uart, val);
Sonic Zhang6f955702009-04-07 16:51:15 +0100968 SSYNC();
969}
970
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000971#ifdef CONFIG_CONSOLE_POLL
Sonic Zhang0efa4f22009-06-11 13:45:07 +0100972/* Anomaly notes:
973 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
974 * losing other bits of UART_LSR is not a problem here.
975 */
Sonic Zhang52e15f0e2009-01-02 13:40:14 +0000976static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
977{
978 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
979
980 while (!(UART_GET_LSR(uart) & THRE))
981 cpu_relax();
982
983 UART_CLEAR_DLAB(uart);
984 UART_PUT_CHAR(uart, (unsigned char)chr);
985}
986
987static int bfin_serial_poll_get_char(struct uart_port *port)
988{
989 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
990 unsigned char chr;
991
992 while (!(UART_GET_LSR(uart) & DR))
993 cpu_relax();
994
995 UART_CLEAR_DLAB(uart);
996 chr = UART_GET_CHAR(uart);
997
998 return chr;
999}
1000#endif
1001
Bryan Wu194de562007-05-06 14:50:30 -07001002static struct uart_ops bfin_serial_pops = {
1003 .tx_empty = bfin_serial_tx_empty,
1004 .set_mctrl = bfin_serial_set_mctrl,
1005 .get_mctrl = bfin_serial_get_mctrl,
1006 .stop_tx = bfin_serial_stop_tx,
1007 .start_tx = bfin_serial_start_tx,
1008 .stop_rx = bfin_serial_stop_rx,
Bryan Wu194de562007-05-06 14:50:30 -07001009 .break_ctl = bfin_serial_break_ctl,
1010 .startup = bfin_serial_startup,
1011 .shutdown = bfin_serial_shutdown,
1012 .set_termios = bfin_serial_set_termios,
Graf Yang3b8458a2008-06-07 15:36:33 +08001013 .set_ldisc = bfin_serial_set_ldisc,
Bryan Wu194de562007-05-06 14:50:30 -07001014 .type = bfin_serial_type,
1015 .release_port = bfin_serial_release_port,
1016 .request_port = bfin_serial_request_port,
1017 .config_port = bfin_serial_config_port,
1018 .verify_port = bfin_serial_verify_port,
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001019#ifdef CONFIG_CONSOLE_POLL
1020 .poll_put_char = bfin_serial_poll_put_char,
1021 .poll_get_char = bfin_serial_poll_get_char,
1022#endif
Bryan Wu194de562007-05-06 14:50:30 -07001023};
1024
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001025#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
Bryan Wu194de562007-05-06 14:50:30 -07001026/*
1027 * If the port was already initialised (eg, by a boot loader),
1028 * try to determine the current setup.
1029 */
1030static void __init
1031bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1032 int *parity, int *bits)
1033{
Sonic Zhang3c2d0ed2012-05-16 14:22:25 +08001034 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -07001035
1036 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1037 if (status == (ERBFI | ETBEI)) {
1038 /* ok, the port was enabled */
Sonic Zhang59bd2342012-05-16 14:22:26 +08001039 u32 lcr, clk;
Bryan Wu194de562007-05-06 14:50:30 -07001040
1041 lcr = UART_GET_LCR(uart);
1042
1043 *parity = 'n';
1044 if (lcr & PEN) {
1045 if (lcr & EPS)
1046 *parity = 'e';
1047 else
1048 *parity = 'o';
1049 }
Sonic Zhang59bd2342012-05-16 14:22:26 +08001050 *bits = ((lcr & WLS_MASK) >> WLS_OFFSET) + 5;
1051
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001052 /* Set DLAB in LCR to Access CLK */
Mike Frysinger45828b82008-05-07 11:41:26 +08001053 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001054
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001055 clk = UART_GET_CLK(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001056
1057 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +08001058 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -07001059
Sonic Zhangb06d2f22012-05-16 14:22:23 +08001060 *baud = get_sclk() / (16*clk);
Bryan Wu194de562007-05-06 14:50:30 -07001061 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -07001062 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -07001063}
Robin Getz0ae53642007-10-09 17:24:49 +08001064
Robin Getz0ae53642007-10-09 17:24:49 +08001065static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -07001066
Sonic Zhang57afb392009-09-09 10:46:19 +00001067static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1068{
1069 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1070 while (!(UART_GET_LSR(uart) & THRE))
1071 barrier();
1072 UART_PUT_CHAR(uart, ch);
1073}
1074
1075#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1076 defined (CONFIG_EARLY_PRINTK) */
1077
1078#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1079#define CLASS_BFIN_CONSOLE "bfin-console"
1080/*
1081 * Interrupts are disabled on entering
1082 */
1083static void
1084bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1085{
1086 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1087 unsigned long flags;
1088
1089 spin_lock_irqsave(&uart->port.lock, flags);
1090 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1091 spin_unlock_irqrestore(&uart->port.lock, flags);
1092
1093}
1094
Bryan Wu194de562007-05-06 14:50:30 -07001095static int __init
1096bfin_serial_console_setup(struct console *co, char *options)
1097{
1098 struct bfin_serial_port *uart;
1099 int baud = 57600;
1100 int bits = 8;
1101 int parity = 'n';
Sonic Zhangd307d362009-04-07 16:52:26 +01001102# if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1103 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
Bryan Wu194de562007-05-06 14:50:30 -07001104 int flow = 'r';
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001105# else
Bryan Wu194de562007-05-06 14:50:30 -07001106 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +08001107# endif
Bryan Wu194de562007-05-06 14:50:30 -07001108
1109 /*
1110 * Check whether an invalid uart number has been specified, and
1111 * if so, search for the first available port that does have
1112 * console support.
1113 */
Sonic Zhang57afb392009-09-09 10:46:19 +00001114 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1115 return -ENODEV;
1116
1117 uart = bfin_serial_ports[co->index];
1118 if (!uart)
1119 return -ENODEV;
Bryan Wu194de562007-05-06 14:50:30 -07001120
1121 if (options)
1122 uart_parse_options(options, &baud, &parity, &bits, &flow);
1123 else
1124 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1125
1126 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +08001127}
Robin Getz0ae53642007-10-09 17:24:49 +08001128
Bryan Wu194de562007-05-06 14:50:30 -07001129static struct console bfin_serial_console = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001130 .name = BFIN_SERIAL_DEV_NAME,
Bryan Wu194de562007-05-06 14:50:30 -07001131 .write = bfin_serial_console_write,
1132 .device = uart_console_device,
1133 .setup = bfin_serial_console_setup,
1134 .flags = CON_PRINTBUFFER,
1135 .index = -1,
1136 .data = &bfin_serial_reg,
1137};
Sonic Zhangbb7e58f2011-07-19 18:09:24 +08001138#define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
Bryan Wu194de562007-05-06 14:50:30 -07001139#else
1140#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001141#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1142
Sonic Zhang57afb392009-09-09 10:46:19 +00001143#ifdef CONFIG_EARLY_PRINTK
1144static struct bfin_serial_port bfin_earlyprintk_port;
1145#define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
Robin Getz0ae53642007-10-09 17:24:49 +08001146
Sonic Zhang57afb392009-09-09 10:46:19 +00001147/*
1148 * Interrupts are disabled on entering
1149 */
1150static void
1151bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
Robin Getz0ae53642007-10-09 17:24:49 +08001152{
Sonic Zhang57afb392009-09-09 10:46:19 +00001153 unsigned long flags;
Robin Getz0ae53642007-10-09 17:24:49 +08001154
Sonic Zhang57afb392009-09-09 10:46:19 +00001155 if (bfin_earlyprintk_port.port.line != co->index)
1156 return;
Robin Getz0ae53642007-10-09 17:24:49 +08001157
Sonic Zhang57afb392009-09-09 10:46:19 +00001158 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1159 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1160 bfin_serial_console_putchar);
1161 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
Robin Getz0ae53642007-10-09 17:24:49 +08001162}
1163
Robin Getz7de7c552009-06-11 13:38:57 +01001164/*
1165 * This should have a .setup or .early_setup in it, but then things get called
1166 * without the command line options, and the baud rate gets messed up - so
1167 * don't let the common infrastructure play with things. (see calls to setup
1168 * & earlysetup in ./kernel/printk.c:register_console()
1169 */
Sachin Kamat6734a832013-08-08 17:10:51 +05301170static struct console bfin_early_serial_console __initdata = {
Robin Getz0ae53642007-10-09 17:24:49 +08001171 .name = "early_BFuart",
Sonic Zhang57afb392009-09-09 10:46:19 +00001172 .write = bfin_earlyprintk_console_write,
Robin Getz0ae53642007-10-09 17:24:49 +08001173 .device = uart_console_device,
1174 .flags = CON_PRINTBUFFER,
Robin Getz0ae53642007-10-09 17:24:49 +08001175 .index = -1,
1176 .data = &bfin_serial_reg,
1177};
Sonic Zhang57afb392009-09-09 10:46:19 +00001178#endif
1179
1180static struct uart_driver bfin_serial_reg = {
1181 .owner = THIS_MODULE,
1182 .driver_name = DRIVER_NAME,
1183 .dev_name = BFIN_SERIAL_DEV_NAME,
1184 .major = BFIN_SERIAL_MAJOR,
1185 .minor = BFIN_SERIAL_MINOR,
1186 .nr = BFIN_UART_NR_PORTS,
1187 .cons = BFIN_SERIAL_CONSOLE,
1188};
1189
1190static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1191{
1192 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1193
1194 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1195}
1196
1197static int bfin_serial_resume(struct platform_device *pdev)
1198{
1199 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1200
1201 return uart_resume_port(&bfin_serial_reg, &uart->port);
1202}
1203
1204static int bfin_serial_probe(struct platform_device *pdev)
1205{
1206 struct resource *res;
1207 struct bfin_serial_port *uart = NULL;
1208 int ret = 0;
1209
1210 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1211 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1212 return -ENOENT;
1213 }
1214
1215 if (bfin_serial_ports[pdev->id] == NULL) {
1216
1217 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1218 if (!uart) {
1219 dev_err(&pdev->dev,
1220 "fail to malloc bfin_serial_port\n");
1221 return -ENOMEM;
1222 }
1223 bfin_serial_ports[pdev->id] = uart;
1224
1225#ifdef CONFIG_EARLY_PRINTK
1226 if (!(bfin_earlyprintk_port.port.membase
1227 && bfin_earlyprintk_port.port.line == pdev->id)) {
1228 /*
1229 * If the peripheral PINs of current port is allocated
1230 * in earlyprintk probe stage, don't do it again.
1231 */
1232#endif
1233 ret = peripheral_request_list(
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001234 dev_get_platdata(&pdev->dev),
Jingoo Han574de552013-07-30 17:06:57 +09001235 DRIVER_NAME);
Sonic Zhang57afb392009-09-09 10:46:19 +00001236 if (ret) {
1237 dev_err(&pdev->dev,
1238 "fail to request bfin serial peripherals\n");
1239 goto out_error_free_mem;
1240 }
1241#ifdef CONFIG_EARLY_PRINTK
1242 }
1243#endif
1244
1245 spin_lock_init(&uart->port.lock);
1246 uart->port.uartclk = get_sclk();
1247 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1248 uart->port.ops = &bfin_serial_pops;
1249 uart->port.line = pdev->id;
1250 uart->port.iotype = UPIO_MEM;
1251 uart->port.flags = UPF_BOOT_AUTOCONF;
1252
1253 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1254 if (res == NULL) {
1255 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1256 ret = -ENOENT;
1257 goto out_error_free_peripherals;
1258 }
1259
Joe Perches28f65c112011-06-09 09:13:32 -07001260 uart->port.membase = ioremap(res->start, resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001261 if (!uart->port.membase) {
1262 dev_err(&pdev->dev, "Cannot map uart IO\n");
1263 ret = -ENXIO;
1264 goto out_error_free_peripherals;
1265 }
1266 uart->port.mapbase = res->start;
1267
Sonic Zhang47918f02011-08-12 11:15:51 +08001268 uart->tx_irq = platform_get_irq(pdev, 0);
1269 if (uart->tx_irq < 0) {
1270 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
Sonic Zhang57afb392009-09-09 10:46:19 +00001271 ret = -ENOENT;
1272 goto out_error_unmap;
1273 }
1274
Sonic Zhang47918f02011-08-12 11:15:51 +08001275 uart->rx_irq = platform_get_irq(pdev, 1);
1276 if (uart->rx_irq < 0) {
1277 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1278 ret = -ENOENT;
1279 goto out_error_unmap;
1280 }
1281 uart->port.irq = uart->rx_irq;
1282
1283 uart->status_irq = platform_get_irq(pdev, 2);
Sonic Zhang57afb392009-09-09 10:46:19 +00001284 if (uart->status_irq < 0) {
1285 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1286 ret = -ENOENT;
1287 goto out_error_unmap;
1288 }
1289
1290#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0f66e502011-01-11 00:16:43 -05001291 spin_lock_init(&uart->rx_lock);
Sonic Zhang57afb392009-09-09 10:46:19 +00001292 uart->tx_done = 1;
1293 uart->tx_count = 0;
1294
1295 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1296 if (res == NULL) {
1297 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1298 ret = -ENOENT;
1299 goto out_error_unmap;
1300 }
1301 uart->tx_dma_channel = res->start;
1302
1303 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1304 if (res == NULL) {
1305 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1306 ret = -ENOENT;
1307 goto out_error_unmap;
1308 }
1309 uart->rx_dma_channel = res->start;
1310
1311 init_timer(&(uart->rx_dma_timer));
1312#endif
1313
1314#if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1315 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1316 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1317 if (res == NULL)
1318 uart->cts_pin = -1;
Sonic Zhangee948e32011-12-05 18:13:10 +08001319 else {
Sonic Zhang57afb392009-09-09 10:46:19 +00001320 uart->cts_pin = res->start;
Sonic Zhang64851632011-12-13 12:22:02 +08001321#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhangee948e32011-12-05 18:13:10 +08001322 uart->port.flags |= ASYNC_CTS_FLOW;
Sonic Zhang64851632011-12-13 12:22:02 +08001323#endif
Sonic Zhangee948e32011-12-05 18:13:10 +08001324 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001325
1326 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1327 if (res == NULL)
1328 uart->rts_pin = -1;
1329 else
1330 uart->rts_pin = res->start;
Sonic Zhang57afb392009-09-09 10:46:19 +00001331#endif
1332 }
1333
1334#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1335 if (!is_early_platform_device(pdev)) {
1336#endif
1337 uart = bfin_serial_ports[pdev->id];
1338 uart->port.dev = &pdev->dev;
1339 dev_set_drvdata(&pdev->dev, uart);
1340 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1341#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1342 }
1343#endif
1344
1345 if (!ret)
1346 return 0;
1347
1348 if (uart) {
1349out_error_unmap:
1350 iounmap(uart->port.membase);
1351out_error_free_peripherals:
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001352 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001353out_error_free_mem:
1354 kfree(uart);
1355 bfin_serial_ports[pdev->id] = NULL;
1356 }
1357
1358 return ret;
1359}
1360
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001361static int bfin_serial_remove(struct platform_device *pdev)
Sonic Zhang57afb392009-09-09 10:46:19 +00001362{
1363 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1364
1365 dev_set_drvdata(&pdev->dev, NULL);
1366
1367 if (uart) {
1368 uart_remove_one_port(&bfin_serial_reg, &uart->port);
Sonic Zhang57afb392009-09-09 10:46:19 +00001369 iounmap(uart->port.membase);
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001370 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001371 kfree(uart);
1372 bfin_serial_ports[pdev->id] = NULL;
1373 }
1374
1375 return 0;
1376}
1377
1378static struct platform_driver bfin_serial_driver = {
1379 .probe = bfin_serial_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -05001380 .remove = bfin_serial_remove,
Sonic Zhang57afb392009-09-09 10:46:19 +00001381 .suspend = bfin_serial_suspend,
1382 .resume = bfin_serial_resume,
1383 .driver = {
1384 .name = DRIVER_NAME,
1385 .owner = THIS_MODULE,
1386 },
1387};
1388
1389#if defined(CONFIG_SERIAL_BFIN_CONSOLE)
Sachin Kamat6734a832013-08-08 17:10:51 +05301390static struct early_platform_driver early_bfin_serial_driver __initdata = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001391 .class_str = CLASS_BFIN_CONSOLE,
1392 .pdrv = &bfin_serial_driver,
1393 .requested_id = EARLY_PLATFORM_ID_UNSET,
1394};
1395
1396static int __init bfin_serial_rs_console_init(void)
1397{
1398 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1399
1400 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1401
1402 register_console(&bfin_serial_console);
1403
1404 return 0;
1405}
1406console_initcall(bfin_serial_rs_console_init);
1407#endif
1408
1409#ifdef CONFIG_EARLY_PRINTK
1410/*
1411 * Memory can't be allocated dynamically during earlyprink init stage.
1412 * So, do individual probe for earlyprink with a static uart port variable.
1413 */
1414static int bfin_earlyprintk_probe(struct platform_device *pdev)
1415{
1416 struct resource *res;
1417 int ret;
1418
1419 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1420 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1421 return -ENOENT;
1422 }
1423
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001424 ret = peripheral_request_list(dev_get_platdata(&pdev->dev),
1425 DRIVER_NAME);
Sonic Zhang57afb392009-09-09 10:46:19 +00001426 if (ret) {
1427 dev_err(&pdev->dev,
1428 "fail to request bfin serial peripherals\n");
1429 return ret;
1430 }
1431
1432 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1433 if (res == NULL) {
1434 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1435 ret = -ENOENT;
1436 goto out_error_free_peripherals;
1437 }
1438
1439 bfin_earlyprintk_port.port.membase = ioremap(res->start,
Joe Perches28f65c112011-06-09 09:13:32 -07001440 resource_size(res));
Sonic Zhang57afb392009-09-09 10:46:19 +00001441 if (!bfin_earlyprintk_port.port.membase) {
1442 dev_err(&pdev->dev, "Cannot map uart IO\n");
1443 ret = -ENXIO;
1444 goto out_error_free_peripherals;
1445 }
1446 bfin_earlyprintk_port.port.mapbase = res->start;
1447 bfin_earlyprintk_port.port.line = pdev->id;
1448 bfin_earlyprintk_port.port.uartclk = get_sclk();
1449 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1450 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1451
1452 return 0;
1453
1454out_error_free_peripherals:
Jingoo Han0e03e3f2013-09-09 14:08:43 +09001455 peripheral_free_list(dev_get_platdata(&pdev->dev));
Sonic Zhang57afb392009-09-09 10:46:19 +00001456
1457 return ret;
1458}
1459
1460static struct platform_driver bfin_earlyprintk_driver = {
1461 .probe = bfin_earlyprintk_probe,
1462 .driver = {
1463 .name = DRIVER_NAME,
1464 .owner = THIS_MODULE,
1465 },
1466};
1467
Sachin Kamat6734a832013-08-08 17:10:51 +05301468static struct early_platform_driver early_bfin_earlyprintk_driver __initdata = {
Sonic Zhang57afb392009-09-09 10:46:19 +00001469 .class_str = CLASS_BFIN_EARLYPRINTK,
1470 .pdrv = &bfin_earlyprintk_driver,
1471 .requested_id = EARLY_PLATFORM_ID_UNSET,
1472};
Robin Getz0ae53642007-10-09 17:24:49 +08001473
1474struct console __init *bfin_earlyserial_init(unsigned int port,
1475 unsigned int cflag)
1476{
Robin Getz0ae53642007-10-09 17:24:49 +08001477 struct ktermios t;
Sonic Zhang57afb392009-09-09 10:46:19 +00001478 char port_name[20];
1479
1480 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1481 return NULL;
1482
1483 /*
1484 * Only probe resource of the given port in earlyprintk boot arg.
1485 * The expected port id should be indicated in port name string.
1486 */
1487 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1488 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1489 port_name);
1490 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1491
1492 if (!bfin_earlyprintk_port.port.membase)
1493 return NULL;
Robin Getz0ae53642007-10-09 17:24:49 +08001494
Sonic Zhang6d9e4492010-10-27 04:16:49 -04001495#ifdef CONFIG_SERIAL_BFIN_CONSOLE
1496 /*
1497 * If we are using early serial, don't let the normal console rewind
1498 * log buffer, since that causes things to be printed multiple times
1499 */
1500 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1501#endif
1502
Robin Getz0ae53642007-10-09 17:24:49 +08001503 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001504 t.c_cflag = cflag;
1505 t.c_iflag = 0;
1506 t.c_oflag = 0;
1507 t.c_lflag = ICANON;
1508 t.c_line = port;
Sonic Zhang57afb392009-09-09 10:46:19 +00001509 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1510
Robin Getz0ae53642007-10-09 17:24:49 +08001511 return &bfin_early_serial_console;
1512}
Sonic Zhangb6efa1e2009-01-02 13:40:31 +00001513#endif /* CONFIG_EARLY_PRINTK */
Bryan Wu194de562007-05-06 14:50:30 -07001514
Bryan Wu194de562007-05-06 14:50:30 -07001515static int __init bfin_serial_init(void)
1516{
1517 int ret;
1518
Sonic Zhang57afb392009-09-09 10:46:19 +00001519 pr_info("Blackfin serial driver\n");
Bryan Wu194de562007-05-06 14:50:30 -07001520
1521 ret = uart_register_driver(&bfin_serial_reg);
Sonic Zhang57afb392009-09-09 10:46:19 +00001522 if (ret) {
1523 pr_err("failed to register %s:%d\n",
1524 bfin_serial_reg.driver_name, ret);
Bryan Wu194de562007-05-06 14:50:30 -07001525 }
Sonic Zhang57afb392009-09-09 10:46:19 +00001526
1527 ret = platform_driver_register(&bfin_serial_driver);
1528 if (ret) {
1529 pr_err("fail to register bfin uart\n");
1530 uart_unregister_driver(&bfin_serial_reg);
1531 }
1532
Bryan Wu194de562007-05-06 14:50:30 -07001533 return ret;
1534}
1535
1536static void __exit bfin_serial_exit(void)
1537{
1538 platform_driver_unregister(&bfin_serial_driver);
1539 uart_unregister_driver(&bfin_serial_reg);
1540}
1541
Sonic Zhang52e15f0e2009-01-02 13:40:14 +00001542
Bryan Wu194de562007-05-06 14:50:30 -07001543module_init(bfin_serial_init);
1544module_exit(bfin_serial_exit);
1545
Sonic Zhang57afb392009-09-09 10:46:19 +00001546MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
Bryan Wu194de562007-05-06 14:50:30 -07001547MODULE_DESCRIPTION("Blackfin generic serial port driver");
1548MODULE_LICENSE("GPL");
1549MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001550MODULE_ALIAS("platform:bfin-uart");