blob: 636b6876c6fb5359a328dbaa0441bd57058a6490 [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 *
Mike Frysinger1ba7a3e2008-01-11 15:56:26 +08004 * Copyright 2006-2007 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
15#include <linux/module.h>
16#include <linux/ioport.h>
17#include <linux/init.h>
18#include <linux/console.h>
19#include <linux/sysrq.h>
20#include <linux/platform_device.h>
21#include <linux/tty.h>
22#include <linux/tty_flip.h>
23#include <linux/serial_core.h>
24
Sonic Zhang474f1a62007-06-29 16:35:17 +080025#ifdef CONFIG_KGDB_UART
26#include <linux/kgdb.h>
27#include <asm/irq_regs.h>
28#endif
29
Bryan Wu194de562007-05-06 14:50:30 -070030#include <asm/gpio.h>
31#include <asm/mach/bfin_serial_5xx.h>
32
33#ifdef CONFIG_SERIAL_BFIN_DMA
34#include <linux/dma-mapping.h>
35#include <asm/io.h>
36#include <asm/irq.h>
37#include <asm/cacheflush.h>
38#endif
39
40/* UART name and device definitions */
41#define BFIN_SERIAL_NAME "ttyBF"
42#define BFIN_SERIAL_MAJOR 204
43#define BFIN_SERIAL_MINOR 64
44
45/*
46 * Setup for console. Argument comes from the menuconfig
47 */
48#define DMA_RX_XCOUNT 512
49#define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
50
Sonic Zhang0aef4562008-02-29 12:08:42 +080051#define DMA_RX_FLUSH_JIFFIES (HZ / 50)
Bryan Wu194de562007-05-06 14:50:30 -070052
53#ifdef CONFIG_SERIAL_BFIN_DMA
54static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
55#else
Bryan Wu194de562007-05-06 14:50:30 -070056static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
Bryan Wu194de562007-05-06 14:50:30 -070057#endif
58
59static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
60
61/*
62 * interrupts are disabled on entry
63 */
64static void bfin_serial_stop_tx(struct uart_port *port)
65{
66 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Sonic Zhang0711d852008-02-25 15:16:50 +080067 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -070068
Roy Huangf4d640c2007-07-12 16:43:46 +080069 while (!(UART_GET_LSR(uart) & TEMT))
Sonic Zhang0711d852008-02-25 15:16:50 +080070 cpu_relax();
Roy Huangf4d640c2007-07-12 16:43:46 +080071
Bryan Wu194de562007-05-06 14:50:30 -070072#ifdef CONFIG_SERIAL_BFIN_DMA
73 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +080074 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
75 uart->port.icount.tx += uart->tx_count;
76 uart->tx_count = 0;
77 uart->tx_done = 1;
Bryan Wu194de562007-05-06 14:50:30 -070078#else
Roy Huangf4d640c2007-07-12 16:43:46 +080079#ifdef CONFIG_BF54x
Roy Huangf4d640c2007-07-12 16:43:46 +080080 /* Clear TFI bit */
81 UART_PUT_LSR(uart, TFI);
Bryan Wu194de562007-05-06 14:50:30 -070082#endif
Mike Frysinger89bf6dc2008-05-07 11:41:26 +080083 UART_CLEAR_IER(uart, ETBEI);
Roy Huangf4d640c2007-07-12 16:43:46 +080084#endif
Bryan Wu194de562007-05-06 14:50:30 -070085}
86
87/*
88 * port is locked and interrupts are disabled
89 */
90static void bfin_serial_start_tx(struct uart_port *port)
91{
92 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
93
94#ifdef CONFIG_SERIAL_BFIN_DMA
Sonic Zhang0711d852008-02-25 15:16:50 +080095 if (uart->tx_done)
96 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -070097#else
Roy Huangf4d640c2007-07-12 16:43:46 +080098 UART_SET_IER(uart, ETBEI);
Sonic Zhanga359cca2007-10-10 16:47:58 +080099 bfin_serial_tx_chars(uart);
Roy Huangf4d640c2007-07-12 16:43:46 +0800100#endif
Bryan Wu194de562007-05-06 14:50:30 -0700101}
102
103/*
104 * Interrupts are enabled
105 */
106static void bfin_serial_stop_rx(struct uart_port *port)
107{
108 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
Mike Frysinger89bf6dc2008-05-07 11:41:26 +0800109#ifdef CONFIG_KGDB_UART
110 if (uart->port.line != CONFIG_KGDB_UART_PORT)
Sonic Zhanga359cca2007-10-10 16:47:58 +0800111#endif
Roy Huangf4d640c2007-07-12 16:43:46 +0800112 UART_CLEAR_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700113}
114
115/*
116 * Set the modem control timer to fire immediately.
117 */
118static void bfin_serial_enable_ms(struct uart_port *port)
119{
120}
121
Sonic Zhang474f1a62007-06-29 16:35:17 +0800122#ifdef CONFIG_KGDB_UART
123static int kgdb_entry_state;
124
125void kgdb_put_debug_char(int chr)
126{
127 struct bfin_serial_port *uart;
128
Graf Yang2ade9722008-04-25 02:55:49 +0800129 if (CONFIG_KGDB_UART_PORT < 0
130 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800131 uart = &bfin_serial_ports[0];
132 else
133 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
134
135 while (!(UART_GET_LSR(uart) & THRE)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800136 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800137 }
Sonic Zhanga359cca2007-10-10 16:47:58 +0800138
Mike Frysinger45828b82008-05-07 11:41:26 +0800139 UART_CLEAR_DLAB(uart);
Sonic Zhang474f1a62007-06-29 16:35:17 +0800140 UART_PUT_CHAR(uart, (unsigned char)chr);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800141 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800142}
143
144int kgdb_get_debug_char(void)
145{
146 struct bfin_serial_port *uart;
147 unsigned char chr;
148
Graf Yang2ade9722008-04-25 02:55:49 +0800149 if (CONFIG_KGDB_UART_PORT < 0
150 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
Sonic Zhang474f1a62007-06-29 16:35:17 +0800151 uart = &bfin_serial_ports[0];
152 else
153 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
154
155 while(!(UART_GET_LSR(uart) & DR)) {
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800156 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800157 }
Mike Frysinger45828b82008-05-07 11:41:26 +0800158 UART_CLEAR_DLAB(uart);
Sonic Zhang474f1a62007-06-29 16:35:17 +0800159 chr = UART_GET_CHAR(uart);
Mike Frysingerd5148ff2007-07-25 11:57:42 +0800160 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800161
162 return chr;
163}
164#endif
165
Mike Frysinger50e2e152008-04-25 03:03:03 +0800166#if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
Mike Frysinger8851c712007-12-24 19:48:04 +0800167# define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
168# define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
169#else
170# define UART_GET_ANOMALY_THRESHOLD(uart) 0
171# define UART_SET_ANOMALY_THRESHOLD(uart, v)
172#endif
173
Bryan Wu194de562007-05-06 14:50:30 -0700174#ifdef CONFIG_SERIAL_BFIN_PIO
Bryan Wu194de562007-05-06 14:50:30 -0700175static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
176{
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800177 struct tty_struct *tty = uart->port.info->tty;
Bryan Wu194de562007-05-06 14:50:30 -0700178 unsigned int status, ch, flg;
Mike Frysinger8851c712007-12-24 19:48:04 +0800179 static struct timeval anomaly_start = { .tv_sec = 0 };
Bryan Wu194de562007-05-06 14:50:30 -0700180
Sonic Zhang759eb042007-11-21 17:00:32 +0800181 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800182 UART_CLEAR_LSR(uart);
183
184 ch = UART_GET_CHAR(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700185 uart->port.icount.rx++;
186
Sonic Zhang474f1a62007-06-29 16:35:17 +0800187#ifdef CONFIG_KGDB_UART
188 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
Mike Frysinger89bf6dc2008-05-07 11:41:26 +0800189 struct pt_regs *regs = get_irq_regs();
Sonic Zhang474f1a62007-06-29 16:35:17 +0800190 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
191 kgdb_breakkey_pressed(regs);
192 return;
193 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
194 kgdb_entry_state = 1;
195 } else if (kgdb_entry_state == 1 && ch == 'q') {
196 kgdb_entry_state = 0;
197 kgdb_breakkey_pressed(regs);
198 return;
199 } else if (ch == 0x3) {/* Ctrl + C */
200 kgdb_entry_state = 0;
201 kgdb_breakkey_pressed(regs);
202 return;
203 } else {
204 kgdb_entry_state = 0;
205 }
206 }
207#endif
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800208
Mike Frysinger50e2e152008-04-25 03:03:03 +0800209 if (ANOMALY_05000363) {
Mike Frysinger8851c712007-12-24 19:48:04 +0800210 /* The BF533 (and BF561) family of processors have a nice anomaly
211 * where they continuously generate characters for a "single" break.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800212 * We have to basically ignore this flood until the "next" valid
Mike Frysinger8851c712007-12-24 19:48:04 +0800213 * character comes across. Due to the nature of the flood, it is
214 * not possible to reliably catch bytes that are sent too quickly
215 * after this break. So application code talking to the Blackfin
216 * which sends a break signal must allow at least 1.5 character
217 * times after the end of the break for things to stabilize. This
218 * timeout was picked as it must absolutely be larger than 1
219 * character time +/- some percent. So 1.5 sounds good. All other
220 * Blackfin families operate properly. Woo.
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800221 */
Mike Frysinger8851c712007-12-24 19:48:04 +0800222 if (anomaly_start.tv_sec) {
223 struct timeval curr;
224 suseconds_t usecs;
225
226 if ((~ch & (~ch + 1)) & 0xff)
227 goto known_good_char;
228
229 do_gettimeofday(&curr);
230 if (curr.tv_sec - anomaly_start.tv_sec > 1)
231 goto known_good_char;
232
233 usecs = 0;
234 if (curr.tv_sec != anomaly_start.tv_sec)
235 usecs += USEC_PER_SEC;
236 usecs += curr.tv_usec - anomaly_start.tv_usec;
237
238 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
239 goto known_good_char;
240
241 if (ch)
242 anomaly_start.tv_sec = 0;
243 else
244 anomaly_start = curr;
245
246 return;
247
248 known_good_char:
249 anomaly_start.tv_sec = 0;
Mike Frysingerbbf275f2007-08-05 16:48:08 +0800250 }
Bryan Wu194de562007-05-06 14:50:30 -0700251 }
Bryan Wu194de562007-05-06 14:50:30 -0700252
253 if (status & BI) {
Mike Frysinger50e2e152008-04-25 03:03:03 +0800254 if (ANOMALY_05000363)
Mike Frysinger8851c712007-12-24 19:48:04 +0800255 if (bfin_revid() < 5)
256 do_gettimeofday(&anomaly_start);
Bryan Wu194de562007-05-06 14:50:30 -0700257 uart->port.icount.brk++;
258 if (uart_handle_break(&uart->port))
259 goto ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800260 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800261 }
262 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700263 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800264 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700265 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800266 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700267 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800268
269 status &= uart->port.read_status_mask;
270
271 if (status & BI)
272 flg = TTY_BREAK;
273 else if (status & PE)
274 flg = TTY_PARITY;
275 else if (status & FE)
276 flg = TTY_FRAME;
277 else
Bryan Wu194de562007-05-06 14:50:30 -0700278 flg = TTY_NORMAL;
279
280 if (uart_handle_sysrq_char(&uart->port, ch))
281 goto ignore_char;
Bryan Wu194de562007-05-06 14:50:30 -0700282
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800283 uart_insert_char(&uart->port, status, OE, ch, flg);
284
285 ignore_char:
286 tty_flip_buffer_push(tty);
Bryan Wu194de562007-05-06 14:50:30 -0700287}
288
289static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
290{
291 struct circ_buf *xmit = &uart->port.info->xmit;
292
293 if (uart->port.x_char) {
294 UART_PUT_CHAR(uart, uart->port.x_char);
295 uart->port.icount.tx++;
296 uart->port.x_char = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700297 }
298 /*
299 * Check the modem control lines before
300 * transmitting anything.
301 */
302 bfin_serial_mctrl_check(uart);
303
304 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
305 bfin_serial_stop_tx(&uart->port);
306 return;
307 }
308
Sonic Zhang759eb042007-11-21 17:00:32 +0800309 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
310 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
311 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
312 uart->port.icount.tx++;
313 SSYNC();
314 }
Bryan Wu194de562007-05-06 14:50:30 -0700315
316 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
317 uart_write_wakeup(&uart->port);
318
319 if (uart_circ_empty(xmit))
320 bfin_serial_stop_tx(&uart->port);
321}
322
Aubrey Li5c4e4722007-05-21 18:09:38 +0800323static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
324{
325 struct bfin_serial_port *uart = dev_id;
326
Roy Huangf4d640c2007-07-12 16:43:46 +0800327 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800328 while (UART_GET_LSR(uart) & DR)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800329 bfin_serial_rx_chars(uart);
330 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800331
Aubrey Li5c4e4722007-05-21 18:09:38 +0800332 return IRQ_HANDLED;
333}
334
335static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
Bryan Wu194de562007-05-06 14:50:30 -0700336{
337 struct bfin_serial_port *uart = dev_id;
Bryan Wu194de562007-05-06 14:50:30 -0700338
Roy Huangf4d640c2007-07-12 16:43:46 +0800339 spin_lock(&uart->port.lock);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800340 if (UART_GET_LSR(uart) & THRE)
Aubrey Li5c4e4722007-05-21 18:09:38 +0800341 bfin_serial_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700342 spin_unlock(&uart->port.lock);
Sonic Zhang759eb042007-11-21 17:00:32 +0800343
Bryan Wu194de562007-05-06 14:50:30 -0700344 return IRQ_HANDLED;
345}
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800346#endif
Bryan Wu194de562007-05-06 14:50:30 -0700347
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800348#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -0700349static void bfin_serial_do_work(struct work_struct *work)
350{
351 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
352
353 bfin_serial_mctrl_check(uart);
354}
Bryan Wu194de562007-05-06 14:50:30 -0700355#endif
356
357#ifdef CONFIG_SERIAL_BFIN_DMA
358static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
359{
360 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700361
Bryan Wu194de562007-05-06 14:50:30 -0700362 uart->tx_done = 0;
363
Bryan Wu194de562007-05-06 14:50:30 -0700364 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
Sonic Zhang0711d852008-02-25 15:16:50 +0800365 uart->tx_count = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700366 uart->tx_done = 1;
367 return;
368 }
369
Sonic Zhang1b733512007-12-21 16:45:12 +0800370 if (uart->port.x_char) {
371 UART_PUT_CHAR(uart, uart->port.x_char);
372 uart->port.icount.tx++;
373 uart->port.x_char = 0;
374 }
375
376 /*
377 * Check the modem control lines before
378 * transmitting anything.
379 */
380 bfin_serial_mctrl_check(uart);
381
Bryan Wu194de562007-05-06 14:50:30 -0700382 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
383 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
384 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
385 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
386 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
387 set_dma_config(uart->tx_dma_channel,
388 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
389 INTR_ON_BUF,
390 DIMENSION_LINEAR,
Michael Hennerich2047e402008-01-22 15:29:18 +0800391 DATA_SIZE_8,
392 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700393 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
394 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
395 set_dma_x_modify(uart->tx_dma_channel, 1);
396 enable_dma(uart->tx_dma_channel);
Sonic Zhang99ee7b52007-12-21 17:12:55 +0800397
Roy Huangf4d640c2007-07-12 16:43:46 +0800398 UART_SET_IER(uart, ETBEI);
Bryan Wu194de562007-05-06 14:50:30 -0700399}
400
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800401static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
Bryan Wu194de562007-05-06 14:50:30 -0700402{
403 struct tty_struct *tty = uart->port.info->tty;
404 int i, flg, status;
405
406 status = UART_GET_LSR(uart);
Mike Frysinger0bcfd702007-12-24 19:40:05 +0800407 UART_CLEAR_LSR(uart);
408
Sonic Zhang56f5de82008-02-25 15:19:09 +0800409 uart->port.icount.rx +=
410 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
411 UART_XMIT_SIZE);
Bryan Wu194de562007-05-06 14:50:30 -0700412
413 if (status & BI) {
414 uart->port.icount.brk++;
415 if (uart_handle_break(&uart->port))
416 goto dma_ignore_char;
Mike Frysinger98089012007-06-11 15:31:30 +0800417 status &= ~(PE | FE);
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800418 }
419 if (status & PE)
Bryan Wu194de562007-05-06 14:50:30 -0700420 uart->port.icount.parity++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800421 if (status & OE)
Bryan Wu194de562007-05-06 14:50:30 -0700422 uart->port.icount.overrun++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800423 if (status & FE)
Bryan Wu194de562007-05-06 14:50:30 -0700424 uart->port.icount.frame++;
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800425
426 status &= uart->port.read_status_mask;
427
428 if (status & BI)
429 flg = TTY_BREAK;
430 else if (status & PE)
431 flg = TTY_PARITY;
432 else if (status & FE)
433 flg = TTY_FRAME;
434 else
Bryan Wu194de562007-05-06 14:50:30 -0700435 flg = TTY_NORMAL;
436
Sonic Zhang56f5de82008-02-25 15:19:09 +0800437 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
438 if (i >= UART_XMIT_SIZE)
439 i = 0;
440 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
441 uart_insert_char(&uart->port, status, OE,
442 uart->rx_dma_buf.buf[i], flg);
Bryan Wu194de562007-05-06 14:50:30 -0700443 }
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800444
445 dma_ignore_char:
Bryan Wu194de562007-05-06 14:50:30 -0700446 tty_flip_buffer_push(tty);
447}
448
449void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
450{
451 int x_pos, pos;
Bryan Wu194de562007-05-06 14:50:30 -0700452
Sonic Zhang56f5de82008-02-25 15:19:09 +0800453 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
454 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
455 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
456 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
457 uart->rx_dma_nrows = 0;
458 x_pos = DMA_RX_XCOUNT - x_pos;
Bryan Wu194de562007-05-06 14:50:30 -0700459 if (x_pos == DMA_RX_XCOUNT)
460 x_pos = 0;
461
462 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
Sonic Zhang56f5de82008-02-25 15:19:09 +0800463 if (pos != uart->rx_dma_buf.tail) {
464 uart->rx_dma_buf.head = pos;
Bryan Wu194de562007-05-06 14:50:30 -0700465 bfin_serial_dma_rx_chars(uart);
Sonic Zhang56f5de82008-02-25 15:19:09 +0800466 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
Bryan Wu194de562007-05-06 14:50:30 -0700467 }
Sonic Zhang0aef4562008-02-29 12:08:42 +0800468
Sonic Zhang0a278422008-04-25 04:36:47 +0800469 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
Bryan Wu194de562007-05-06 14:50:30 -0700470}
471
472static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
473{
474 struct bfin_serial_port *uart = dev_id;
475 struct circ_buf *xmit = &uart->port.info->xmit;
Bryan Wu194de562007-05-06 14:50:30 -0700476
477 spin_lock(&uart->port.lock);
478 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
Bryan Wu194de562007-05-06 14:50:30 -0700479 disable_dma(uart->tx_dma_channel);
Sonic Zhang0711d852008-02-25 15:16:50 +0800480 clear_dma_irqstat(uart->tx_dma_channel);
Roy Huangf4d640c2007-07-12 16:43:46 +0800481 UART_CLEAR_IER(uart, ETBEI);
Sonic Zhang0711d852008-02-25 15:16:50 +0800482 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
483 uart->port.icount.tx += uart->tx_count;
Sonic Zhang1b733512007-12-21 16:45:12 +0800484
Sonic Zhang56f5de82008-02-25 15:19:09 +0800485 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
486 uart_write_wakeup(&uart->port);
487
Sonic Zhang1b733512007-12-21 16:45:12 +0800488 bfin_serial_dma_tx_chars(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700489 }
490
491 spin_unlock(&uart->port.lock);
492 return IRQ_HANDLED;
493}
494
495static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
496{
497 struct bfin_serial_port *uart = dev_id;
498 unsigned short irqstat;
Sonic Zhang0711d852008-02-25 15:16:50 +0800499
Bryan Wu194de562007-05-06 14:50:30 -0700500 spin_lock(&uart->port.lock);
501 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
502 clear_dma_irqstat(uart->rx_dma_channel);
Bryan Wu194de562007-05-06 14:50:30 -0700503 spin_unlock(&uart->port.lock);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800504
Sonic Zhang0a278422008-04-25 04:36:47 +0800505 mod_timer(&(uart->rx_dma_timer), jiffies);
Sonic Zhang0aef4562008-02-29 12:08:42 +0800506
Bryan Wu194de562007-05-06 14:50:30 -0700507 return IRQ_HANDLED;
508}
509#endif
510
511/*
512 * Return TIOCSER_TEMT when transmitter is not busy.
513 */
514static unsigned int bfin_serial_tx_empty(struct uart_port *port)
515{
516 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
517 unsigned short lsr;
518
519 lsr = UART_GET_LSR(uart);
520 if (lsr & TEMT)
521 return TIOCSER_TEMT;
522 else
523 return 0;
524}
525
526static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
527{
528#ifdef CONFIG_SERIAL_BFIN_CTSRTS
529 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
530 if (uart->cts_pin < 0)
531 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
532
Sonic Zhang1feaa512008-06-03 12:19:45 +0800533 if (UART_GET_CTS(uart))
Bryan Wu194de562007-05-06 14:50:30 -0700534 return TIOCM_DSR | TIOCM_CAR;
535 else
536#endif
537 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
538}
539
540static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
541{
542#ifdef CONFIG_SERIAL_BFIN_CTSRTS
543 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
544 if (uart->rts_pin < 0)
545 return;
546
547 if (mctrl & TIOCM_RTS)
Sonic Zhang1feaa512008-06-03 12:19:45 +0800548 UART_CLEAR_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700549 else
Sonic Zhang1feaa512008-06-03 12:19:45 +0800550 UART_SET_RTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700551#endif
552}
553
554/*
555 * Handle any change of modem status signal since we were last called.
556 */
557static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
558{
559#ifdef CONFIG_SERIAL_BFIN_CTSRTS
560 unsigned int status;
Bryan Wu194de562007-05-06 14:50:30 -0700561 struct uart_info *info = uart->port.info;
562 struct tty_struct *tty = info->tty;
563
564 status = bfin_serial_get_mctrl(&uart->port);
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800565 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
Bryan Wu194de562007-05-06 14:50:30 -0700566 if (!(status & TIOCM_CTS)) {
567 tty->hw_stopped = 1;
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800568 schedule_work(&uart->cts_workqueue);
Bryan Wu194de562007-05-06 14:50:30 -0700569 } else {
570 tty->hw_stopped = 0;
571 }
Bryan Wu194de562007-05-06 14:50:30 -0700572#endif
573}
574
575/*
576 * Interrupts are always disabled.
577 */
578static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
579{
Mike Frysingercf6867622007-06-11 16:12:49 +0800580 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
581 u16 lcr = UART_GET_LCR(uart);
582 if (break_state)
583 lcr |= SB;
584 else
585 lcr &= ~SB;
586 UART_PUT_LCR(uart, lcr);
587 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700588}
589
590static int bfin_serial_startup(struct uart_port *port)
591{
592 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
593
594#ifdef CONFIG_SERIAL_BFIN_DMA
595 dma_addr_t dma_handle;
596
597 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
598 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
599 return -EBUSY;
600 }
601
602 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
603 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
604 free_dma(uart->rx_dma_channel);
605 return -EBUSY;
606 }
607
608 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
609 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
610
611 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
612 uart->rx_dma_buf.head = 0;
613 uart->rx_dma_buf.tail = 0;
614 uart->rx_dma_nrows = 0;
615
616 set_dma_config(uart->rx_dma_channel,
617 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
618 INTR_ON_ROW, DIMENSION_2D,
Michael Hennerich2047e402008-01-22 15:29:18 +0800619 DATA_SIZE_8,
620 DMA_SYNC_RESTART));
Bryan Wu194de562007-05-06 14:50:30 -0700621 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
622 set_dma_x_modify(uart->rx_dma_channel, 1);
623 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
624 set_dma_y_modify(uart->rx_dma_channel, 1);
625 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
626 enable_dma(uart->rx_dma_channel);
627
628 uart->rx_dma_timer.data = (unsigned long)(uart);
629 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
630 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
631 add_timer(&(uart->rx_dma_timer));
632#else
Sonic Zhanga359cca2007-10-10 16:47:58 +0800633 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700634 "BFIN_UART_RX", uart)) {
Sonic Zhanga359cca2007-10-10 16:47:58 +0800635# ifdef CONFIG_KGDB_UART
636 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
637# endif
Bryan Wu194de562007-05-06 14:50:30 -0700638 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
639 return -EBUSY;
Sonic Zhanga359cca2007-10-10 16:47:58 +0800640# ifdef CONFIG_KGDB_UART
641 }
642# endif
Bryan Wu194de562007-05-06 14:50:30 -0700643 }
644
645 if (request_irq
Aubrey Li5c4e4722007-05-21 18:09:38 +0800646 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
Bryan Wu194de562007-05-06 14:50:30 -0700647 "BFIN_UART_TX", uart)) {
648 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
649 free_irq(uart->port.irq, uart);
650 return -EBUSY;
651 }
652#endif
Roy Huangf4d640c2007-07-12 16:43:46 +0800653 UART_SET_IER(uart, ERBFI);
Bryan Wu194de562007-05-06 14:50:30 -0700654 return 0;
655}
656
657static void bfin_serial_shutdown(struct uart_port *port)
658{
659 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
660
661#ifdef CONFIG_SERIAL_BFIN_DMA
662 disable_dma(uart->tx_dma_channel);
663 free_dma(uart->tx_dma_channel);
664 disable_dma(uart->rx_dma_channel);
665 free_dma(uart->rx_dma_channel);
666 del_timer(&(uart->rx_dma_timer));
Sonic Zhang75b780b2007-12-21 17:03:39 +0800667 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
Bryan Wu194de562007-05-06 14:50:30 -0700668#else
Sonic Zhang474f1a62007-06-29 16:35:17 +0800669#ifdef CONFIG_KGDB_UART
670 if (uart->port.line != CONFIG_KGDB_UART_PORT)
671#endif
Bryan Wu194de562007-05-06 14:50:30 -0700672 free_irq(uart->port.irq, uart);
673 free_irq(uart->port.irq+1, uart);
674#endif
675}
676
677static void
678bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
679 struct ktermios *old)
680{
681 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
682 unsigned long flags;
683 unsigned int baud, quot;
Mike Frysinger0c44a862008-04-24 04:56:02 +0800684 unsigned short val, ier, lcr = 0;
Bryan Wu194de562007-05-06 14:50:30 -0700685
686 switch (termios->c_cflag & CSIZE) {
687 case CS8:
688 lcr = WLS(8);
689 break;
690 case CS7:
691 lcr = WLS(7);
692 break;
693 case CS6:
694 lcr = WLS(6);
695 break;
696 case CS5:
697 lcr = WLS(5);
698 break;
699 default:
700 printk(KERN_ERR "%s: word lengh not supported\n",
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700701 __func__);
Bryan Wu194de562007-05-06 14:50:30 -0700702 }
703
704 if (termios->c_cflag & CSTOPB)
705 lcr |= STB;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800706 if (termios->c_cflag & PARENB)
Bryan Wu194de562007-05-06 14:50:30 -0700707 lcr |= PEN;
Mike Frysinger19aa6382007-06-11 16:16:45 +0800708 if (!(termios->c_cflag & PARODD))
709 lcr |= EPS;
710 if (termios->c_cflag & CMSPAR)
711 lcr |= STP;
Bryan Wu194de562007-05-06 14:50:30 -0700712
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800713 port->read_status_mask = OE;
714 if (termios->c_iflag & INPCK)
715 port->read_status_mask |= (FE | PE);
716 if (termios->c_iflag & (BRKINT | PARMRK))
717 port->read_status_mask |= BI;
Bryan Wu194de562007-05-06 14:50:30 -0700718
Mike Frysinger2ac5ee42007-05-21 18:09:39 +0800719 /*
720 * Characters to ignore
721 */
722 port->ignore_status_mask = 0;
723 if (termios->c_iflag & IGNPAR)
724 port->ignore_status_mask |= FE | PE;
725 if (termios->c_iflag & IGNBRK) {
726 port->ignore_status_mask |= BI;
727 /*
728 * If we're ignoring parity and break indicators,
729 * ignore overruns too (for real raw support).
730 */
731 if (termios->c_iflag & IGNPAR)
732 port->ignore_status_mask |= OE;
733 }
Bryan Wu194de562007-05-06 14:50:30 -0700734
735 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
736 quot = uart_get_divisor(port, baud);
737 spin_lock_irqsave(&uart->port.lock, flags);
738
Mike Frysinger8851c712007-12-24 19:48:04 +0800739 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
740
Bryan Wu194de562007-05-06 14:50:30 -0700741 /* Disable UART */
742 ier = UART_GET_IER(uart);
Sonic Zhang1feaa512008-06-03 12:19:45 +0800743 UART_DISABLE_INTS(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700744
745 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800746 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700747
748 UART_PUT_DLL(uart, quot & 0xFF);
Bryan Wu194de562007-05-06 14:50:30 -0700749 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
750 SSYNC();
751
752 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800753 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700754
755 UART_PUT_LCR(uart, lcr);
756
757 /* Enable UART */
Sonic Zhang1feaa512008-06-03 12:19:45 +0800758 UART_ENABLE_INTS(uart, ier);
Bryan Wu194de562007-05-06 14:50:30 -0700759
760 val = UART_GET_GCTL(uart);
761 val |= UCEN;
762 UART_PUT_GCTL(uart, val);
763
764 spin_unlock_irqrestore(&uart->port.lock, flags);
765}
766
767static const char *bfin_serial_type(struct uart_port *port)
768{
769 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
770
771 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
772}
773
774/*
775 * Release the memory region(s) being used by 'port'.
776 */
777static void bfin_serial_release_port(struct uart_port *port)
778{
779}
780
781/*
782 * Request the memory region(s) being used by 'port'.
783 */
784static int bfin_serial_request_port(struct uart_port *port)
785{
786 return 0;
787}
788
789/*
790 * Configure/autoconfigure the port.
791 */
792static void bfin_serial_config_port(struct uart_port *port, int flags)
793{
794 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
795
796 if (flags & UART_CONFIG_TYPE &&
797 bfin_serial_request_port(&uart->port) == 0)
798 uart->port.type = PORT_BFIN;
799}
800
801/*
802 * Verify the new serial_struct (for TIOCSSERIAL).
803 * The only change we allow are to the flags and type, and
804 * even then only between PORT_BFIN and PORT_UNKNOWN
805 */
806static int
807bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
808{
809 return 0;
810}
811
Graf Yang7d01b472008-02-29 11:31:08 +0800812/*
813 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
814 * In other cases, disable IrDA function.
815 */
816static void bfin_set_ldisc(struct tty_struct *tty)
817{
818 int line = tty->index;
819 unsigned short val;
820
821 if (line >= tty->driver->num)
822 return;
823
824 switch (tty->ldisc.num) {
825 case N_IRDA:
826 val = UART_GET_GCTL(&bfin_serial_ports[line]);
827 val |= (IREN | RPOLC);
828 UART_PUT_GCTL(&bfin_serial_ports[line], val);
829 break;
830 default:
831 val = UART_GET_GCTL(&bfin_serial_ports[line]);
832 val &= ~(IREN | RPOLC);
833 UART_PUT_GCTL(&bfin_serial_ports[line], val);
834 }
835}
836
Bryan Wu194de562007-05-06 14:50:30 -0700837static struct uart_ops bfin_serial_pops = {
838 .tx_empty = bfin_serial_tx_empty,
839 .set_mctrl = bfin_serial_set_mctrl,
840 .get_mctrl = bfin_serial_get_mctrl,
841 .stop_tx = bfin_serial_stop_tx,
842 .start_tx = bfin_serial_start_tx,
843 .stop_rx = bfin_serial_stop_rx,
844 .enable_ms = bfin_serial_enable_ms,
845 .break_ctl = bfin_serial_break_ctl,
846 .startup = bfin_serial_startup,
847 .shutdown = bfin_serial_shutdown,
848 .set_termios = bfin_serial_set_termios,
849 .type = bfin_serial_type,
850 .release_port = bfin_serial_release_port,
851 .request_port = bfin_serial_request_port,
852 .config_port = bfin_serial_config_port,
853 .verify_port = bfin_serial_verify_port,
854};
855
856static void __init bfin_serial_init_ports(void)
857{
858 static int first = 1;
859 int i;
860
861 if (!first)
862 return;
863 first = 0;
864
865 for (i = 0; i < nr_ports; i++) {
866 bfin_serial_ports[i].port.uartclk = get_sclk();
867 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
868 bfin_serial_ports[i].port.line = i;
869 bfin_serial_ports[i].port.iotype = UPIO_MEM;
870 bfin_serial_ports[i].port.membase =
871 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
872 bfin_serial_ports[i].port.mapbase =
873 bfin_serial_resource[i].uart_base_addr;
874 bfin_serial_ports[i].port.irq =
875 bfin_serial_resource[i].uart_irq;
876 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
877#ifdef CONFIG_SERIAL_BFIN_DMA
878 bfin_serial_ports[i].tx_done = 1;
879 bfin_serial_ports[i].tx_count = 0;
880 bfin_serial_ports[i].tx_dma_channel =
881 bfin_serial_resource[i].uart_tx_dma_channel;
882 bfin_serial_ports[i].rx_dma_channel =
883 bfin_serial_resource[i].uart_rx_dma_channel;
884 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
Bryan Wu194de562007-05-06 14:50:30 -0700885#endif
886#ifdef CONFIG_SERIAL_BFIN_CTSRTS
Sonic Zhang4cb4f222008-02-02 14:29:25 +0800887 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
Bryan Wu194de562007-05-06 14:50:30 -0700888 bfin_serial_ports[i].cts_pin =
889 bfin_serial_resource[i].uart_cts_pin;
890 bfin_serial_ports[i].rts_pin =
891 bfin_serial_resource[i].uart_rts_pin;
892#endif
893 bfin_serial_hw_init(&bfin_serial_ports[i]);
Bryan Wu194de562007-05-06 14:50:30 -0700894 }
Roy Huangf4d640c2007-07-12 16:43:46 +0800895
Bryan Wu194de562007-05-06 14:50:30 -0700896}
897
898#ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700899/*
900 * If the port was already initialised (eg, by a boot loader),
901 * try to determine the current setup.
902 */
903static void __init
904bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
905 int *parity, int *bits)
906{
907 unsigned short status;
908
909 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
910 if (status == (ERBFI | ETBEI)) {
911 /* ok, the port was enabled */
Mike Frysinger45828b82008-05-07 11:41:26 +0800912 u16 lcr, dlh, dll;
Bryan Wu194de562007-05-06 14:50:30 -0700913
914 lcr = UART_GET_LCR(uart);
915
916 *parity = 'n';
917 if (lcr & PEN) {
918 if (lcr & EPS)
919 *parity = 'e';
920 else
921 *parity = 'o';
922 }
923 switch (lcr & 0x03) {
924 case 0: *bits = 5; break;
925 case 1: *bits = 6; break;
926 case 2: *bits = 7; break;
927 case 3: *bits = 8; break;
928 }
929 /* Set DLAB in LCR to Access DLL and DLH */
Mike Frysinger45828b82008-05-07 11:41:26 +0800930 UART_SET_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700931
932 dll = UART_GET_DLL(uart);
933 dlh = UART_GET_DLH(uart);
934
935 /* Clear DLAB in LCR to Access THR RBR IER */
Mike Frysinger45828b82008-05-07 11:41:26 +0800936 UART_CLEAR_DLAB(uart);
Bryan Wu194de562007-05-06 14:50:30 -0700937
938 *baud = get_sclk() / (16*(dll | dlh << 8));
939 }
Harvey Harrison71cc2c22008-04-30 00:55:10 -0700940 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
Bryan Wu194de562007-05-06 14:50:30 -0700941}
Robin Getz0ae53642007-10-09 17:24:49 +0800942#endif
943
944#if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
945static struct uart_driver bfin_serial_reg;
Bryan Wu194de562007-05-06 14:50:30 -0700946
947static int __init
948bfin_serial_console_setup(struct console *co, char *options)
949{
950 struct bfin_serial_port *uart;
Robin Getz0ae53642007-10-09 17:24:49 +0800951# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700952 int baud = 57600;
953 int bits = 8;
954 int parity = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +0800955# ifdef CONFIG_SERIAL_BFIN_CTSRTS
Bryan Wu194de562007-05-06 14:50:30 -0700956 int flow = 'r';
Robin Getz0ae53642007-10-09 17:24:49 +0800957# else
Bryan Wu194de562007-05-06 14:50:30 -0700958 int flow = 'n';
Robin Getz0ae53642007-10-09 17:24:49 +0800959# endif
960# endif
Bryan Wu194de562007-05-06 14:50:30 -0700961
962 /*
963 * Check whether an invalid uart number has been specified, and
964 * if so, search for the first available port that does have
965 * console support.
966 */
967 if (co->index == -1 || co->index >= nr_ports)
968 co->index = 0;
969 uart = &bfin_serial_ports[co->index];
970
Robin Getz0ae53642007-10-09 17:24:49 +0800971# ifdef CONFIG_SERIAL_BFIN_CONSOLE
Bryan Wu194de562007-05-06 14:50:30 -0700972 if (options)
973 uart_parse_options(options, &baud, &parity, &bits, &flow);
974 else
975 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
976
977 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
Robin Getz0ae53642007-10-09 17:24:49 +0800978# else
979 return 0;
980# endif
981}
982#endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
983 defined (CONFIG_EARLY_PRINTK) */
984
985#ifdef CONFIG_SERIAL_BFIN_CONSOLE
986static void bfin_serial_console_putchar(struct uart_port *port, int ch)
987{
988 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
989 while (!(UART_GET_LSR(uart) & THRE))
990 barrier();
991 UART_PUT_CHAR(uart, ch);
992 SSYNC();
Bryan Wu194de562007-05-06 14:50:30 -0700993}
994
Robin Getz0ae53642007-10-09 17:24:49 +0800995/*
996 * Interrupts are disabled on entering
997 */
998static void
999bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1000{
1001 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1002 int flags = 0;
1003
1004 spin_lock_irqsave(&uart->port.lock, flags);
1005 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1006 spin_unlock_irqrestore(&uart->port.lock, flags);
1007
1008}
1009
Bryan Wu194de562007-05-06 14:50:30 -07001010static struct console bfin_serial_console = {
1011 .name = BFIN_SERIAL_NAME,
1012 .write = bfin_serial_console_write,
1013 .device = uart_console_device,
1014 .setup = bfin_serial_console_setup,
1015 .flags = CON_PRINTBUFFER,
1016 .index = -1,
1017 .data = &bfin_serial_reg,
1018};
1019
1020static int __init bfin_serial_rs_console_init(void)
1021{
1022 bfin_serial_init_ports();
1023 register_console(&bfin_serial_console);
Sonic Zhang474f1a62007-06-29 16:35:17 +08001024#ifdef CONFIG_KGDB_UART
1025 kgdb_entry_state = 0;
1026 init_kgdb_uart();
1027#endif
Bryan Wu194de562007-05-06 14:50:30 -07001028 return 0;
1029}
1030console_initcall(bfin_serial_rs_console_init);
1031
1032#define BFIN_SERIAL_CONSOLE &bfin_serial_console
1033#else
1034#define BFIN_SERIAL_CONSOLE NULL
Robin Getz0ae53642007-10-09 17:24:49 +08001035#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1036
1037
1038#ifdef CONFIG_EARLY_PRINTK
1039static __init void early_serial_putc(struct uart_port *port, int ch)
1040{
1041 unsigned timeout = 0xffff;
1042 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1043
1044 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1045 cpu_relax();
1046 UART_PUT_CHAR(uart, ch);
1047}
1048
1049static __init void early_serial_write(struct console *con, const char *s,
1050 unsigned int n)
1051{
1052 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1053 unsigned int i;
1054
1055 for (i = 0; i < n; i++, s++) {
1056 if (*s == '\n')
1057 early_serial_putc(&uart->port, '\r');
1058 early_serial_putc(&uart->port, *s);
1059 }
1060}
1061
1062static struct __init console bfin_early_serial_console = {
1063 .name = "early_BFuart",
1064 .write = early_serial_write,
1065 .device = uart_console_device,
1066 .flags = CON_PRINTBUFFER,
1067 .setup = bfin_serial_console_setup,
1068 .index = -1,
1069 .data = &bfin_serial_reg,
1070};
1071
1072struct console __init *bfin_earlyserial_init(unsigned int port,
1073 unsigned int cflag)
1074{
1075 struct bfin_serial_port *uart;
1076 struct ktermios t;
1077
1078 if (port == -1 || port >= nr_ports)
1079 port = 0;
1080 bfin_serial_init_ports();
1081 bfin_early_serial_console.index = port;
Robin Getz0ae53642007-10-09 17:24:49 +08001082 uart = &bfin_serial_ports[port];
1083 t.c_cflag = cflag;
1084 t.c_iflag = 0;
1085 t.c_oflag = 0;
1086 t.c_lflag = ICANON;
1087 t.c_line = port;
1088 bfin_serial_set_termios(&uart->port, &t, &t);
1089 return &bfin_early_serial_console;
1090}
1091
1092#endif /* CONFIG_SERIAL_BFIN_CONSOLE */
Bryan Wu194de562007-05-06 14:50:30 -07001093
1094static struct uart_driver bfin_serial_reg = {
1095 .owner = THIS_MODULE,
1096 .driver_name = "bfin-uart",
1097 .dev_name = BFIN_SERIAL_NAME,
1098 .major = BFIN_SERIAL_MAJOR,
1099 .minor = BFIN_SERIAL_MINOR,
Graf Yang2ade9722008-04-25 02:55:49 +08001100 .nr = BFIN_UART_NR_PORTS,
Bryan Wu194de562007-05-06 14:50:30 -07001101 .cons = BFIN_SERIAL_CONSOLE,
1102};
1103
1104static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1105{
1106 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1107
1108 if (uart)
1109 uart_suspend_port(&bfin_serial_reg, &uart->port);
1110
1111 return 0;
1112}
1113
1114static int bfin_serial_resume(struct platform_device *dev)
1115{
1116 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1117
1118 if (uart)
1119 uart_resume_port(&bfin_serial_reg, &uart->port);
1120
1121 return 0;
1122}
1123
1124static int bfin_serial_probe(struct platform_device *dev)
1125{
1126 struct resource *res = dev->resource;
1127 int i;
1128
1129 for (i = 0; i < dev->num_resources; i++, res++)
1130 if (res->flags & IORESOURCE_MEM)
1131 break;
1132
1133 if (i < dev->num_resources) {
1134 for (i = 0; i < nr_ports; i++, res++) {
1135 if (bfin_serial_ports[i].port.mapbase != res->start)
1136 continue;
1137 bfin_serial_ports[i].port.dev = &dev->dev;
1138 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1139 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1140 }
1141 }
1142
1143 return 0;
1144}
1145
1146static int bfin_serial_remove(struct platform_device *pdev)
1147{
1148 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1149
1150
1151#ifdef CONFIG_SERIAL_BFIN_CTSRTS
1152 gpio_free(uart->cts_pin);
1153 gpio_free(uart->rts_pin);
1154#endif
1155
1156 platform_set_drvdata(pdev, NULL);
1157
1158 if (uart)
1159 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1160
1161 return 0;
1162}
1163
1164static struct platform_driver bfin_serial_driver = {
1165 .probe = bfin_serial_probe,
1166 .remove = bfin_serial_remove,
1167 .suspend = bfin_serial_suspend,
1168 .resume = bfin_serial_resume,
1169 .driver = {
1170 .name = "bfin-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001171 .owner = THIS_MODULE,
Bryan Wu194de562007-05-06 14:50:30 -07001172 },
1173};
1174
1175static int __init bfin_serial_init(void)
1176{
1177 int ret;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001178#ifdef CONFIG_KGDB_UART
1179 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
Sonic Zhanga359cca2007-10-10 16:47:58 +08001180 struct ktermios t;
Sonic Zhang474f1a62007-06-29 16:35:17 +08001181#endif
Bryan Wu194de562007-05-06 14:50:30 -07001182
1183 pr_info("Serial: Blackfin serial driver\n");
1184
1185 bfin_serial_init_ports();
1186
1187 ret = uart_register_driver(&bfin_serial_reg);
1188 if (ret == 0) {
Graf Yang7d01b472008-02-29 11:31:08 +08001189 bfin_serial_reg.tty_driver->set_ldisc = bfin_set_ldisc;
Bryan Wu194de562007-05-06 14:50:30 -07001190 ret = platform_driver_register(&bfin_serial_driver);
1191 if (ret) {
1192 pr_debug("uart register failed\n");
1193 uart_unregister_driver(&bfin_serial_reg);
1194 }
1195 }
Sonic Zhang474f1a62007-06-29 16:35:17 +08001196#ifdef CONFIG_KGDB_UART
1197 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
Sonic Zhanga359cca2007-10-10 16:47:58 +08001198 request_irq(uart->port.irq, bfin_serial_rx_int,
Sonic Zhang474f1a62007-06-29 16:35:17 +08001199 IRQF_DISABLED, "BFIN_UART_RX", uart);
1200 pr_info("Request irq for kgdb uart port\n");
Sonic Zhanga359cca2007-10-10 16:47:58 +08001201 UART_SET_IER(uart, ERBFI);
Mike Frysingerd5148ff2007-07-25 11:57:42 +08001202 SSYNC();
Sonic Zhang474f1a62007-06-29 16:35:17 +08001203 t.c_cflag = CS8|B57600;
1204 t.c_iflag = 0;
1205 t.c_oflag = 0;
1206 t.c_lflag = ICANON;
1207 t.c_line = CONFIG_KGDB_UART_PORT;
1208 bfin_serial_set_termios(&uart->port, &t, &t);
1209 }
1210#endif
Bryan Wu194de562007-05-06 14:50:30 -07001211 return ret;
1212}
1213
1214static void __exit bfin_serial_exit(void)
1215{
1216 platform_driver_unregister(&bfin_serial_driver);
1217 uart_unregister_driver(&bfin_serial_reg);
1218}
1219
1220module_init(bfin_serial_init);
1221module_exit(bfin_serial_exit);
1222
1223MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1224MODULE_DESCRIPTION("Blackfin generic serial port driver");
1225MODULE_LICENSE("GPL");
1226MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
Kay Sieverse169c132008-04-15 14:34:35 -07001227MODULE_ALIAS("platform:bfin-uart");