blob: 8cbbb954df2cbdf4b4d360f469f7a5408ffbfa15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
3 *
4 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
7 *
8 * Based on code from 68332serial.c which was:
9 *
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
13 *
14 * Changes:
15 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
16 * some cleanups in mcfrs_write.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/signal.h>
23#include <linux/sched.h>
24#include <linux/timer.h>
25#include <linux/wait.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/mm.h>
32#include <linux/kernel.h>
33#include <linux/serial.h>
34#include <linux/serialP.h>
35#include <linux/console.h>
36#include <linux/init.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/semaphore.h>
44#include <asm/delay.h>
45#include <asm/coldfire.h>
46#include <asm/mcfsim.h>
47#include <asm/mcfuart.h>
48#include <asm/nettel.h>
49#include <asm/uaccess.h>
50#include "mcfserial.h"
51
52struct timer_list mcfrs_timer_struct;
53
54/*
55 * Default console baud rate, we use this as the default
56 * for all ports so init can just open /dev/console and
57 * keep going. Perhaps one day the cflag settings for the
58 * console can be used instead.
59 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#if defined(CONFIG_HW_FEITH)
Greg Ungererb0433b92005-09-12 11:18:10 +100061#define CONSOLE_BAUD_RATE 38400
62#define DEFAULT_CBAUD B38400
Greg Ungerera9cdffb2006-02-08 09:19:17 +100063#elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB)
Greg Ungererb0433b92005-09-12 11:18:10 +100064#define CONSOLE_BAUD_RATE 115200
65#define DEFAULT_CBAUD B115200
Greg Ungerera9cdffb2006-02-08 09:19:17 +100066#elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
67 defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
68#define CONSOLE_BAUD_RATE 19200
69#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif
71
72#ifndef CONSOLE_BAUD_RATE
73#define CONSOLE_BAUD_RATE 9600
74#define DEFAULT_CBAUD B9600
75#endif
76
77int mcfrs_console_inited = 0;
78int mcfrs_console_port = -1;
79int mcfrs_console_baud = CONSOLE_BAUD_RATE;
80int mcfrs_console_cbaud = DEFAULT_CBAUD;
81
82/*
83 * Driver data structures.
84 */
85static struct tty_driver *mcfrs_serial_driver;
86
87/* number of characters left in xmit buffer before we ask for more */
88#define WAKEUP_CHARS 256
89
90/* Debugging...
91 */
92#undef SERIAL_DEBUG_OPEN
93#undef SERIAL_DEBUG_FLOW
94
Greg Ungerer7f04d622005-11-07 14:09:50 +100095#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
96 defined(CONFIG_M520x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
98#else
99#define IRQBASE 73
100#endif
101
102/*
103 * Configuration table, UARTs to look for at startup.
104 */
105static struct mcf_serial mcfrs_table[] = {
106 { /* ttyS0 */
107 .magic = 0,
108 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
109 .irq = IRQBASE,
110 .flags = ASYNC_BOOT_AUTOCONF,
111 },
112 { /* ttyS1 */
113 .magic = 0,
114 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
115 .irq = IRQBASE+1,
116 .flags = ASYNC_BOOT_AUTOCONF,
117 },
118};
119
120
121#define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
122
123/*
124 * This is used to figure out the divisor speeds and the timeouts.
125 */
126static int mcfrs_baud_table[] = {
127 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
128 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
129};
130#define MCFRS_BAUD_TABLE_SIZE \
131 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
132
133
134#ifdef CONFIG_MAGIC_SYSRQ
135/*
136 * Magic system request keys. Used for debugging...
137 */
138extern int magic_sysrq_key(int ch);
139#endif
140
141
142/*
143 * Forware declarations...
144 */
145static void mcfrs_change_speed(struct mcf_serial *info);
146static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
147
148
149static inline int serial_paranoia_check(struct mcf_serial *info,
150 char *name, const char *routine)
151{
152#ifdef SERIAL_PARANOIA_CHECK
153 static const char badmagic[] =
154 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
155 static const char badinfo[] =
156 "MCFRS(warning): null mcf_serial for %s in %s\n";
157
158 if (!info) {
159 printk(badinfo, name, routine);
160 return 1;
161 }
162 if (info->magic != SERIAL_MAGIC) {
163 printk(badmagic, name, routine);
164 return 1;
165 }
166#endif
167 return 0;
168}
169
170/*
171 * Sets or clears DTR and RTS on the requested line.
172 */
173static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
174{
175 volatile unsigned char *uartp;
176 unsigned long flags;
177
178#if 0
179 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
180 __FILE__, __LINE__, info, dtr, rts);
181#endif
182
183 local_irq_save(flags);
184 if (dtr >= 0) {
185#ifdef MCFPP_DTR0
186 if (info->line)
187 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
188 else
189 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
190#endif
191 }
192 if (rts >= 0) {
193 uartp = info->addr;
194 if (rts) {
195 info->sigs |= TIOCM_RTS;
196 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
197 } else {
198 info->sigs &= ~TIOCM_RTS;
199 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
200 }
201 }
202 local_irq_restore(flags);
203 return;
204}
205
206/*
207 * Gets values of serial signals.
208 */
209static int mcfrs_getsignals(struct mcf_serial *info)
210{
211 volatile unsigned char *uartp;
212 unsigned long flags;
213 int sigs;
214#if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
215 unsigned short ppdata;
216#endif
217
218#if 0
219 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
220#endif
221
222 local_irq_save(flags);
223 uartp = info->addr;
224 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
225 sigs |= (info->sigs & TIOCM_RTS);
226
227#ifdef MCFPP_DCD0
228{
229 unsigned int ppdata;
230 ppdata = mcf_getppdata();
231 if (info->line == 0) {
232 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
233 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
234 } else if (info->line == 1) {
235 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
236 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
237 }
238}
239#endif
240
241 local_irq_restore(flags);
242 return(sigs);
243}
244
245/*
246 * ------------------------------------------------------------
247 * mcfrs_stop() and mcfrs_start()
248 *
249 * This routines are called before setting or resetting tty->stopped.
250 * They enable or disable transmitter interrupts, as necessary.
251 * ------------------------------------------------------------
252 */
253static void mcfrs_stop(struct tty_struct *tty)
254{
255 volatile unsigned char *uartp;
256 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
257 unsigned long flags;
258
259 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
260 return;
261
262 local_irq_save(flags);
263 uartp = info->addr;
264 info->imr &= ~MCFUART_UIR_TXREADY;
265 uartp[MCFUART_UIMR] = info->imr;
266 local_irq_restore(flags);
267}
268
269static void mcfrs_start(struct tty_struct *tty)
270{
271 volatile unsigned char *uartp;
272 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
273 unsigned long flags;
274
275 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
276 return;
277
278 local_irq_save(flags);
279 if (info->xmit_cnt && info->xmit_buf) {
280 uartp = info->addr;
281 info->imr |= MCFUART_UIR_TXREADY;
282 uartp[MCFUART_UIMR] = info->imr;
283 }
284 local_irq_restore(flags);
285}
286
287/*
288 * ----------------------------------------------------------------------
289 *
290 * Here starts the interrupt handling routines. All of the following
291 * subroutines are declared as inline and are folded into
292 * mcfrs_interrupt(). They were separated out for readability's sake.
293 *
294 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
295 * runs with interrupts turned off. People who may want to modify
296 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
297 * possible. After you are done making modifications, it is not a bad
298 * idea to do:
299 *
300 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
301 *
302 * and look at the resulting assemble code in serial.s.
303 *
304 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
305 * -----------------------------------------------------------------------
306 */
307
308static inline void receive_chars(struct mcf_serial *info)
309{
310 volatile unsigned char *uartp;
311 struct tty_struct *tty = info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800312 unsigned char status, ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (!tty)
315 return;
316
317 uartp = info->addr;
318
319 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ch = uartp[MCFUART_URB];
321 info->stats.rx++;
322
323#ifdef CONFIG_MAGIC_SYSRQ
324 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
325 if (magic_sysrq_key(ch))
326 continue;
327 }
328#endif
329
Alan Cox33f0f882006-01-09 20:54:13 -0800330 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (status & MCFUART_USR_RXERR) {
332 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
333 if (status & MCFUART_USR_RXBREAK) {
334 info->stats.rxbreak++;
Alan Cox33f0f882006-01-09 20:54:13 -0800335 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else if (status & MCFUART_USR_RXPARITY) {
337 info->stats.rxparity++;
Alan Cox33f0f882006-01-09 20:54:13 -0800338 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else if (status & MCFUART_USR_RXOVERRUN) {
340 info->stats.rxoverrun++;
Alan Cox33f0f882006-01-09 20:54:13 -0800341 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 } else if (status & MCFUART_USR_RXFRAMING) {
343 info->stats.rxframing++;
Alan Cox33f0f882006-01-09 20:54:13 -0800344 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Alan Cox33f0f882006-01-09 20:54:13 -0800347 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Greg Ungerera9cdffb2006-02-08 09:19:17 +1000349 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351}
352
353static inline void transmit_chars(struct mcf_serial *info)
354{
355 volatile unsigned char *uartp;
356
357 uartp = info->addr;
358
359 if (info->x_char) {
360 /* Send special char - probably flow control */
361 uartp[MCFUART_UTB] = info->x_char;
362 info->x_char = 0;
363 info->stats.tx++;
364 }
365
366 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
367 info->imr &= ~MCFUART_UIR_TXREADY;
368 uartp[MCFUART_UIMR] = info->imr;
369 return;
370 }
371
372 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
373 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
374 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
375 info->stats.tx++;
376 if (--info->xmit_cnt <= 0)
377 break;
378 }
379
380 if (info->xmit_cnt < WAKEUP_CHARS)
381 schedule_work(&info->tqueue);
382 return;
383}
384
385/*
386 * This is the serial driver's generic interrupt routine
387 */
388irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
389{
390 struct mcf_serial *info;
391 unsigned char isr;
392
393 info = &mcfrs_table[(irq - IRQBASE)];
394 isr = info->addr[MCFUART_UISR] & info->imr;
395
396 if (isr & MCFUART_UIR_RXREADY)
397 receive_chars(info);
398 if (isr & MCFUART_UIR_TXREADY)
399 transmit_chars(info);
400 return IRQ_HANDLED;
401}
402
403/*
404 * -------------------------------------------------------------------
405 * Here ends the serial interrupt routines.
406 * -------------------------------------------------------------------
407 */
408
409static void mcfrs_offintr(void *private)
410{
411 struct mcf_serial *info = (struct mcf_serial *) private;
412 struct tty_struct *tty;
413
414 tty = info->tty;
415 if (!tty)
416 return;
417 tty_wakeup(tty);
418}
419
420
421/*
422 * Change of state on a DCD line.
423 */
424void mcfrs_modem_change(struct mcf_serial *info, int dcd)
425{
426 if (info->count == 0)
427 return;
428
429 if (info->flags & ASYNC_CHECK_CD) {
430 if (dcd)
431 wake_up_interruptible(&info->open_wait);
432 else
433 schedule_work(&info->tqueue_hangup);
434 }
435}
436
437
438#ifdef MCFPP_DCD0
439
440unsigned short mcfrs_ppstatus;
441
442/*
443 * This subroutine is called when the RS_TIMER goes off. It is used
444 * to monitor the state of the DCD lines - since they have no edge
445 * sensors and interrupt generators.
446 */
447static void mcfrs_timer(void)
448{
449 unsigned int ppstatus, dcdval, i;
450
451 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
452
453 if (ppstatus != mcfrs_ppstatus) {
454 for (i = 0; (i < 2); i++) {
455 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
456 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
457 mcfrs_modem_change(&mcfrs_table[i],
458 ((ppstatus & dcdval) ? 0 : 1));
459 }
460 }
461 }
462 mcfrs_ppstatus = ppstatus;
463
464 /* Re-arm timer */
465 mcfrs_timer_struct.expires = jiffies + HZ/25;
466 add_timer(&mcfrs_timer_struct);
467}
468
469#endif /* MCFPP_DCD0 */
470
471
472/*
473 * This routine is called from the scheduler tqueue when the interrupt
474 * routine has signalled that a hangup has occurred. The path of
475 * hangup processing is:
476 *
477 * serial interrupt routine -> (scheduler tqueue) ->
478 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
479 *
480 */
481static void do_serial_hangup(void *private)
482{
483 struct mcf_serial *info = (struct mcf_serial *) private;
484 struct tty_struct *tty;
485
486 tty = info->tty;
487 if (!tty)
488 return;
489
490 tty_hangup(tty);
491}
492
493static int startup(struct mcf_serial * info)
494{
495 volatile unsigned char *uartp;
496 unsigned long flags;
497
498 if (info->flags & ASYNC_INITIALIZED)
499 return 0;
500
501 if (!info->xmit_buf) {
502 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
503 if (!info->xmit_buf)
504 return -ENOMEM;
505 }
506
507 local_irq_save(flags);
508
509#ifdef SERIAL_DEBUG_OPEN
510 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
511#endif
512
513 /*
514 * Reset UART, get it into known state...
515 */
516 uartp = info->addr;
517 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
518 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
519 mcfrs_setsignals(info, 1, 1);
520
521 if (info->tty)
522 clear_bit(TTY_IO_ERROR, &info->tty->flags);
523 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
524
525 /*
526 * and set the speed of the serial port
527 */
528 mcfrs_change_speed(info);
529
530 /*
531 * Lastly enable the UART transmitter and receiver, and
532 * interrupt enables.
533 */
534 info->imr = MCFUART_UIR_RXREADY;
535 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
536 uartp[MCFUART_UIMR] = info->imr;
537
538 info->flags |= ASYNC_INITIALIZED;
539 local_irq_restore(flags);
540 return 0;
541}
542
543/*
544 * This routine will shutdown a serial port; interrupts are disabled, and
545 * DTR is dropped if the hangup on close termio flag is on.
546 */
547static void shutdown(struct mcf_serial * info)
548{
549 volatile unsigned char *uartp;
550 unsigned long flags;
551
552 if (!(info->flags & ASYNC_INITIALIZED))
553 return;
554
555#ifdef SERIAL_DEBUG_OPEN
556 printk("Shutting down serial port %d (irq %d)....\n", info->line,
557 info->irq);
558#endif
559
560 local_irq_save(flags);
561
562 uartp = info->addr;
563 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
564 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
565 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
566
567 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
568 mcfrs_setsignals(info, 0, 0);
569
570 if (info->xmit_buf) {
571 free_page((unsigned long) info->xmit_buf);
572 info->xmit_buf = 0;
573 }
574
575 if (info->tty)
576 set_bit(TTY_IO_ERROR, &info->tty->flags);
577
578 info->flags &= ~ASYNC_INITIALIZED;
579 local_irq_restore(flags);
580}
581
582
583/*
584 * This routine is called to set the UART divisor registers to match
585 * the specified baud rate for a serial port.
586 */
587static void mcfrs_change_speed(struct mcf_serial *info)
588{
589 volatile unsigned char *uartp;
590 unsigned int baudclk, cflag;
591 unsigned long flags;
592 unsigned char mr1, mr2;
593 int i;
594#ifdef CONFIG_M5272
595 unsigned int fraction;
596#endif
597
598 if (!info->tty || !info->tty->termios)
599 return;
600 cflag = info->tty->termios->c_cflag;
601 if (info->addr == 0)
602 return;
603
604#if 0
605 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
606#endif
607
608 i = cflag & CBAUD;
609 if (i & CBAUDEX) {
610 i &= ~CBAUDEX;
611 if (i < 1 || i > 4)
612 info->tty->termios->c_cflag &= ~CBAUDEX;
613 else
614 i += 15;
615 }
616 if (i == 0) {
617 mcfrs_setsignals(info, 0, -1);
618 return;
619 }
620
621 /* compute the baudrate clock */
622#ifdef CONFIG_M5272
623 /*
624 * For the MCF5272, also compute the baudrate fraction.
625 */
626 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
627 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
628 fraction *= 16;
629 fraction /= (32 * mcfrs_baud_table[i]);
630#else
631 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
632#endif
633
634 info->baud = mcfrs_baud_table[i];
635
636 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
637 mr2 = 0;
638
639 switch (cflag & CSIZE) {
640 case CS5: mr1 |= MCFUART_MR1_CS5; break;
641 case CS6: mr1 |= MCFUART_MR1_CS6; break;
642 case CS7: mr1 |= MCFUART_MR1_CS7; break;
643 case CS8:
644 default: mr1 |= MCFUART_MR1_CS8; break;
645 }
646
647 if (cflag & PARENB) {
648 if (cflag & CMSPAR) {
649 if (cflag & PARODD)
650 mr1 |= MCFUART_MR1_PARITYMARK;
651 else
652 mr1 |= MCFUART_MR1_PARITYSPACE;
653 } else {
654 if (cflag & PARODD)
655 mr1 |= MCFUART_MR1_PARITYODD;
656 else
657 mr1 |= MCFUART_MR1_PARITYEVEN;
658 }
659 } else {
660 mr1 |= MCFUART_MR1_PARITYNONE;
661 }
662
663 if (cflag & CSTOPB)
664 mr2 |= MCFUART_MR2_STOP2;
665 else
666 mr2 |= MCFUART_MR2_STOP1;
667
668 if (cflag & CRTSCTS) {
669 mr1 |= MCFUART_MR1_RXRTS;
670 mr2 |= MCFUART_MR2_TXCTS;
671 }
672
673 if (cflag & CLOCAL)
674 info->flags &= ~ASYNC_CHECK_CD;
675 else
676 info->flags |= ASYNC_CHECK_CD;
677
678 uartp = info->addr;
679
680 local_irq_save(flags);
681#if 0
682 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
683 mr1, mr2, baudclk);
684#endif
685 /*
686 Note: pg 12-16 of MCF5206e User's Manual states that a
687 software reset should be performed prior to changing
688 UMR1,2, UCSR, UACR, bit 7
689 */
690 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
691 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
692 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
693 uartp[MCFUART_UMR] = mr1;
694 uartp[MCFUART_UMR] = mr2;
695 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
696 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
697#ifdef CONFIG_M5272
698 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
699#endif
700 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
701 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
702 mcfrs_setsignals(info, 1, -1);
703 local_irq_restore(flags);
704 return;
705}
706
707static void mcfrs_flush_chars(struct tty_struct *tty)
708{
709 volatile unsigned char *uartp;
710 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
711 unsigned long flags;
712
713 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
714 return;
715
716 uartp = (volatile unsigned char *) info->addr;
717
718 /*
719 * re-enable receiver interrupt
720 */
721 local_irq_save(flags);
722 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
723 (info->flags & ASYNC_INITIALIZED) ) {
724 info->imr |= MCFUART_UIR_RXREADY;
725 uartp[MCFUART_UIMR] = info->imr;
726 }
727 local_irq_restore(flags);
728
729 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
730 !info->xmit_buf)
731 return;
732
733 /* Enable transmitter */
734 local_irq_save(flags);
735 info->imr |= MCFUART_UIR_TXREADY;
736 uartp[MCFUART_UIMR] = info->imr;
737 local_irq_restore(flags);
738}
739
740static int mcfrs_write(struct tty_struct * tty,
741 const unsigned char *buf, int count)
742{
743 volatile unsigned char *uartp;
744 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
745 unsigned long flags;
746 int c, total = 0;
747
748#if 0
749 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
750 __FILE__, __LINE__, (int)tty, (int)buf, count);
751#endif
752
753 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
754 return 0;
755
756 if (!tty || !info->xmit_buf)
757 return 0;
758
759 local_save_flags(flags);
760 while (1) {
761 local_irq_disable();
762 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
763 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
764 local_irq_restore(flags);
765
766 if (c <= 0)
767 break;
768
769 memcpy(info->xmit_buf + info->xmit_head, buf, c);
770
771 local_irq_disable();
772 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
773 info->xmit_cnt += c;
774 local_irq_restore(flags);
775
776 buf += c;
777 count -= c;
778 total += c;
779 }
780
781 local_irq_disable();
782 uartp = info->addr;
783 info->imr |= MCFUART_UIR_TXREADY;
784 uartp[MCFUART_UIMR] = info->imr;
785 local_irq_restore(flags);
786
787 return total;
788}
789
790static int mcfrs_write_room(struct tty_struct *tty)
791{
792 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
793 int ret;
794
795 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
796 return 0;
797 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
798 if (ret < 0)
799 ret = 0;
800 return ret;
801}
802
803static int mcfrs_chars_in_buffer(struct tty_struct *tty)
804{
805 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
806
807 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
808 return 0;
809 return info->xmit_cnt;
810}
811
812static void mcfrs_flush_buffer(struct tty_struct *tty)
813{
814 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
815 unsigned long flags;
816
817 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
818 return;
819
820 local_irq_save(flags);
821 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
822 local_irq_restore(flags);
823
824 tty_wakeup(tty);
825}
826
827/*
828 * ------------------------------------------------------------
829 * mcfrs_throttle()
830 *
831 * This routine is called by the upper-layer tty layer to signal that
832 * incoming characters should be throttled.
833 * ------------------------------------------------------------
834 */
835static void mcfrs_throttle(struct tty_struct * tty)
836{
837 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
838#ifdef SERIAL_DEBUG_THROTTLE
839 char buf[64];
840
841 printk("throttle %s: %d....\n", _tty_name(tty, buf),
842 tty->ldisc.chars_in_buffer(tty));
843#endif
844
845 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
846 return;
847
848 if (I_IXOFF(tty))
849 info->x_char = STOP_CHAR(tty);
850
851 /* Turn off RTS line (do this atomic) */
852}
853
854static void mcfrs_unthrottle(struct tty_struct * tty)
855{
856 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
857#ifdef SERIAL_DEBUG_THROTTLE
858 char buf[64];
859
860 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
861 tty->ldisc.chars_in_buffer(tty));
862#endif
863
864 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
865 return;
866
867 if (I_IXOFF(tty)) {
868 if (info->x_char)
869 info->x_char = 0;
870 else
871 info->x_char = START_CHAR(tty);
872 }
873
874 /* Assert RTS line (do this atomic) */
875}
876
877/*
878 * ------------------------------------------------------------
879 * mcfrs_ioctl() and friends
880 * ------------------------------------------------------------
881 */
882
883static int get_serial_info(struct mcf_serial * info,
884 struct serial_struct * retinfo)
885{
886 struct serial_struct tmp;
887
888 if (!retinfo)
889 return -EFAULT;
890 memset(&tmp, 0, sizeof(tmp));
891 tmp.type = info->type;
892 tmp.line = info->line;
893 tmp.port = (unsigned int) info->addr;
894 tmp.irq = info->irq;
895 tmp.flags = info->flags;
896 tmp.baud_base = info->baud_base;
897 tmp.close_delay = info->close_delay;
898 tmp.closing_wait = info->closing_wait;
899 tmp.custom_divisor = info->custom_divisor;
900 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
901}
902
903static int set_serial_info(struct mcf_serial * info,
904 struct serial_struct * new_info)
905{
906 struct serial_struct new_serial;
907 struct mcf_serial old_info;
908 int retval = 0;
909
910 if (!new_info)
911 return -EFAULT;
912 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
913 return -EFAULT;
914 old_info = *info;
915
916 if (!capable(CAP_SYS_ADMIN)) {
917 if ((new_serial.baud_base != info->baud_base) ||
918 (new_serial.type != info->type) ||
919 (new_serial.close_delay != info->close_delay) ||
920 ((new_serial.flags & ~ASYNC_USR_MASK) !=
921 (info->flags & ~ASYNC_USR_MASK)))
922 return -EPERM;
923 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
924 (new_serial.flags & ASYNC_USR_MASK));
925 info->custom_divisor = new_serial.custom_divisor;
926 goto check_and_exit;
927 }
928
929 if (info->count > 1)
930 return -EBUSY;
931
932 /*
933 * OK, past this point, all the error checking has been done.
934 * At this point, we start making changes.....
935 */
936
937 info->baud_base = new_serial.baud_base;
938 info->flags = ((info->flags & ~ASYNC_FLAGS) |
939 (new_serial.flags & ASYNC_FLAGS));
940 info->type = new_serial.type;
941 info->close_delay = new_serial.close_delay;
942 info->closing_wait = new_serial.closing_wait;
943
944check_and_exit:
945 retval = startup(info);
946 return retval;
947}
948
949/*
950 * get_lsr_info - get line status register info
951 *
952 * Purpose: Let user call ioctl() to get info when the UART physically
953 * is emptied. On bus types like RS485, the transmitter must
954 * release the bus after transmitting. This must be done when
955 * the transmit shift register is empty, not be done when the
956 * transmit holding register is empty. This functionality
957 * allows an RS485 driver to be written in user space.
958 */
959static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
960{
961 volatile unsigned char *uartp;
962 unsigned long flags;
963 unsigned char status;
964
965 local_irq_save(flags);
966 uartp = info->addr;
967 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
968 local_irq_restore(flags);
969
970 return put_user(status,value);
971}
972
973/*
974 * This routine sends a break character out the serial port.
975 */
976static void send_break( struct mcf_serial * info, int duration)
977{
978 volatile unsigned char *uartp;
979 unsigned long flags;
980
981 if (!info->addr)
982 return;
983 set_current_state(TASK_INTERRUPTIBLE);
984 uartp = info->addr;
985
986 local_irq_save(flags);
987 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
988 schedule_timeout(duration);
989 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
990 local_irq_restore(flags);
991}
992
993static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
994{
995 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
996
997 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
998 return -ENODEV;
999 if (tty->flags & (1 << TTY_IO_ERROR))
1000 return -EIO;
1001
1002 return mcfrs_getsignals(info);
1003}
1004
1005static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1006 unsigned int set, unsigned int clear)
1007{
1008 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1009 int rts = -1, dtr = -1;
1010
1011 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1012 return -ENODEV;
1013 if (tty->flags & (1 << TTY_IO_ERROR))
1014 return -EIO;
1015
1016 if (set & TIOCM_RTS)
1017 rts = 1;
1018 if (set & TIOCM_DTR)
1019 dtr = 1;
1020 if (clear & TIOCM_RTS)
1021 rts = 0;
1022 if (clear & TIOCM_DTR)
1023 dtr = 0;
1024
1025 mcfrs_setsignals(info, dtr, rts);
1026
1027 return 0;
1028}
1029
1030static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1031 unsigned int cmd, unsigned long arg)
1032{
1033 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1034 int retval, error;
1035
1036 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1037 return -ENODEV;
1038
1039 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1040 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1041 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1042 if (tty->flags & (1 << TTY_IO_ERROR))
1043 return -EIO;
1044 }
1045
1046 switch (cmd) {
1047 case TCSBRK: /* SVID version: non-zero arg --> no break */
1048 retval = tty_check_change(tty);
1049 if (retval)
1050 return retval;
1051 tty_wait_until_sent(tty, 0);
1052 if (!arg)
1053 send_break(info, HZ/4); /* 1/4 second */
1054 return 0;
1055 case TCSBRKP: /* support for POSIX tcsendbreak() */
1056 retval = tty_check_change(tty);
1057 if (retval)
1058 return retval;
1059 tty_wait_until_sent(tty, 0);
1060 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1061 return 0;
1062 case TIOCGSOFTCAR:
1063 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1064 (unsigned long *) arg);
1065 if (error)
1066 return error;
1067 return 0;
1068 case TIOCSSOFTCAR:
1069 get_user(arg, (unsigned long *) arg);
1070 tty->termios->c_cflag =
1071 ((tty->termios->c_cflag & ~CLOCAL) |
1072 (arg ? CLOCAL : 0));
1073 return 0;
1074 case TIOCGSERIAL:
1075 if (access_ok(VERIFY_WRITE, (void *) arg,
1076 sizeof(struct serial_struct)))
1077 return get_serial_info(info,
1078 (struct serial_struct *) arg);
1079 return -EFAULT;
1080 case TIOCSSERIAL:
1081 return set_serial_info(info,
1082 (struct serial_struct *) arg);
1083 case TIOCSERGETLSR: /* Get line status register */
1084 if (access_ok(VERIFY_WRITE, (void *) arg,
1085 sizeof(unsigned int)))
1086 return get_lsr_info(info, (unsigned int *) arg);
1087 return -EFAULT;
1088 case TIOCSERGSTRUCT:
1089 error = copy_to_user((struct mcf_serial *) arg,
1090 info, sizeof(struct mcf_serial));
1091 if (error)
1092 return -EFAULT;
1093 return 0;
1094
1095#ifdef TIOCSET422
1096 case TIOCSET422: {
1097 unsigned int val;
1098 get_user(val, (unsigned int *) arg);
1099 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1100 break;
1101 }
1102 case TIOCGET422: {
1103 unsigned int val;
1104 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1105 put_user(val, (unsigned int *) arg);
1106 break;
1107 }
1108#endif
1109
1110 default:
1111 return -ENOIOCTLCMD;
1112 }
1113 return 0;
1114}
1115
1116static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1117{
1118 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1119
1120 if (tty->termios->c_cflag == old_termios->c_cflag)
1121 return;
1122
1123 mcfrs_change_speed(info);
1124
1125 if ((old_termios->c_cflag & CRTSCTS) &&
1126 !(tty->termios->c_cflag & CRTSCTS)) {
1127 tty->hw_stopped = 0;
1128 mcfrs_setsignals(info, -1, 1);
1129#if 0
1130 mcfrs_start(tty);
1131#endif
1132 }
1133}
1134
1135/*
1136 * ------------------------------------------------------------
1137 * mcfrs_close()
1138 *
1139 * This routine is called when the serial port gets closed. First, we
1140 * wait for the last remaining data to be sent. Then, we unlink its
1141 * S structure from the interrupt chain if necessary, and we free
1142 * that IRQ if nothing is left in the chain.
1143 * ------------------------------------------------------------
1144 */
1145static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1146{
1147 volatile unsigned char *uartp;
1148 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1149 unsigned long flags;
1150
1151 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1152 return;
1153
1154 local_irq_save(flags);
1155
1156 if (tty_hung_up_p(filp)) {
1157 local_irq_restore(flags);
1158 return;
1159 }
1160
1161#ifdef SERIAL_DEBUG_OPEN
1162 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1163#endif
1164 if ((tty->count == 1) && (info->count != 1)) {
1165 /*
1166 * Uh, oh. tty->count is 1, which means that the tty
1167 * structure will be freed. Info->count should always
1168 * be one in these conditions. If it's greater than
1169 * one, we've got real problems, since it means the
1170 * serial port won't be shutdown.
1171 */
1172 printk("MCFRS: bad serial port count; tty->count is 1, "
1173 "info->count is %d\n", info->count);
1174 info->count = 1;
1175 }
1176 if (--info->count < 0) {
1177 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1178 info->line, info->count);
1179 info->count = 0;
1180 }
1181 if (info->count) {
1182 local_irq_restore(flags);
1183 return;
1184 }
1185 info->flags |= ASYNC_CLOSING;
1186
1187 /*
1188 * Now we wait for the transmit buffer to clear; and we notify
1189 * the line discipline to only process XON/XOFF characters.
1190 */
1191 tty->closing = 1;
1192 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1193 tty_wait_until_sent(tty, info->closing_wait);
1194
1195 /*
1196 * At this point we stop accepting input. To do this, we
1197 * disable the receive line status interrupts, and tell the
1198 * interrupt driver to stop checking the data ready bit in the
1199 * line status register.
1200 */
1201 info->imr &= ~MCFUART_UIR_RXREADY;
1202 uartp = info->addr;
1203 uartp[MCFUART_UIMR] = info->imr;
1204
1205#if 0
1206 /* FIXME: do we need to keep this enabled for console?? */
1207 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1208 /* Do not disable the UART */ ;
1209 } else
1210#endif
1211 shutdown(info);
1212 if (tty->driver->flush_buffer)
1213 tty->driver->flush_buffer(tty);
1214 tty_ldisc_flush(tty);
1215
1216 tty->closing = 0;
1217 info->event = 0;
1218 info->tty = 0;
1219#if 0
1220 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1221 if (tty->ldisc.close)
1222 (tty->ldisc.close)(tty);
1223 tty->ldisc = ldiscs[N_TTY];
1224 tty->termios->c_line = N_TTY;
1225 if (tty->ldisc.open)
1226 (tty->ldisc.open)(tty);
1227 }
1228#endif
1229 if (info->blocked_open) {
1230 if (info->close_delay) {
1231 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1232 }
1233 wake_up_interruptible(&info->open_wait);
1234 }
1235 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1236 wake_up_interruptible(&info->close_wait);
1237 local_irq_restore(flags);
1238}
1239
1240/*
1241 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1242 */
1243static void
1244mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1245{
1246#ifdef CONFIG_M5272
1247#define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1248
1249 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1250 volatile unsigned char *uartp;
1251 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1252
1253 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1254 return;
1255
1256 orig_jiffies = jiffies;
1257
1258 /*
1259 * Set the check interval to be 1/5 of the approximate time
1260 * to send the entire fifo, and make it at least 1. The check
1261 * interval should also be less than the timeout.
1262 *
1263 * Note: we have to use pretty tight timings here to satisfy
1264 * the NIST-PCTS.
1265 */
1266 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1267 char_time = fifo_time / 5;
1268 if (char_time == 0)
1269 char_time = 1;
1270 if (timeout && timeout < char_time)
1271 char_time = timeout;
1272
1273 /*
1274 * Clamp the timeout period at 2 * the time to empty the
1275 * fifo. Just to be safe, set the minimum at .5 seconds.
1276 */
1277 fifo_time *= 2;
1278 if (fifo_time < (HZ/2))
1279 fifo_time = HZ/2;
1280 if (!timeout || timeout > fifo_time)
1281 timeout = fifo_time;
1282
1283 /*
1284 * Account for the number of bytes in the UART
1285 * transmitter FIFO plus any byte being shifted out.
1286 */
1287 uartp = (volatile unsigned char *) info->addr;
1288 for (;;) {
1289 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1290 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1291 MCFUART_USR_TXEMPTY)) ==
1292 MCFUART_USR_TXREADY)
1293 fifo_cnt++;
1294 if (fifo_cnt == 0)
1295 break;
1296 msleep_interruptible(jiffies_to_msecs(char_time));
1297 if (signal_pending(current))
1298 break;
1299 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1300 break;
1301 }
1302#else
1303 /*
1304 * For the other coldfire models, assume all data has been sent
1305 */
1306#endif
1307}
1308
1309/*
1310 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1311 */
1312void mcfrs_hangup(struct tty_struct *tty)
1313{
1314 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1315
1316 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1317 return;
1318
1319 mcfrs_flush_buffer(tty);
1320 shutdown(info);
1321 info->event = 0;
1322 info->count = 0;
1323 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1324 info->tty = 0;
1325 wake_up_interruptible(&info->open_wait);
1326}
1327
1328/*
1329 * ------------------------------------------------------------
1330 * mcfrs_open() and friends
1331 * ------------------------------------------------------------
1332 */
1333static int block_til_ready(struct tty_struct *tty, struct file * filp,
1334 struct mcf_serial *info)
1335{
1336 DECLARE_WAITQUEUE(wait, current);
1337 int retval;
1338 int do_clocal = 0;
1339
1340 /*
1341 * If the device is in the middle of being closed, then block
1342 * until it's done, and then try again.
1343 */
1344 if (info->flags & ASYNC_CLOSING) {
1345 interruptible_sleep_on(&info->close_wait);
1346#ifdef SERIAL_DO_RESTART
1347 if (info->flags & ASYNC_HUP_NOTIFY)
1348 return -EAGAIN;
1349 else
1350 return -ERESTARTSYS;
1351#else
1352 return -EAGAIN;
1353#endif
1354 }
1355
1356 /*
1357 * If non-blocking mode is set, or the port is not enabled,
1358 * then make the check up front and then exit.
1359 */
1360 if ((filp->f_flags & O_NONBLOCK) ||
1361 (tty->flags & (1 << TTY_IO_ERROR))) {
1362 info->flags |= ASYNC_NORMAL_ACTIVE;
1363 return 0;
1364 }
1365
1366 if (tty->termios->c_cflag & CLOCAL)
1367 do_clocal = 1;
1368
1369 /*
1370 * Block waiting for the carrier detect and the line to become
1371 * free (i.e., not in use by the callout). While we are in
1372 * this loop, info->count is dropped by one, so that
1373 * mcfrs_close() knows when to free things. We restore it upon
1374 * exit, either normal or abnormal.
1375 */
1376 retval = 0;
1377 add_wait_queue(&info->open_wait, &wait);
1378#ifdef SERIAL_DEBUG_OPEN
1379 printk("block_til_ready before block: ttyS%d, count = %d\n",
1380 info->line, info->count);
1381#endif
1382 info->count--;
1383 info->blocked_open++;
1384 while (1) {
1385 local_irq_disable();
1386 mcfrs_setsignals(info, 1, 1);
1387 local_irq_enable();
1388 current->state = TASK_INTERRUPTIBLE;
1389 if (tty_hung_up_p(filp) ||
1390 !(info->flags & ASYNC_INITIALIZED)) {
1391#ifdef SERIAL_DO_RESTART
1392 if (info->flags & ASYNC_HUP_NOTIFY)
1393 retval = -EAGAIN;
1394 else
1395 retval = -ERESTARTSYS;
1396#else
1397 retval = -EAGAIN;
1398#endif
1399 break;
1400 }
1401 if (!(info->flags & ASYNC_CLOSING) &&
1402 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1403 break;
1404 if (signal_pending(current)) {
1405 retval = -ERESTARTSYS;
1406 break;
1407 }
1408#ifdef SERIAL_DEBUG_OPEN
1409 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1410 info->line, info->count);
1411#endif
1412 schedule();
1413 }
1414 current->state = TASK_RUNNING;
1415 remove_wait_queue(&info->open_wait, &wait);
1416 if (!tty_hung_up_p(filp))
1417 info->count++;
1418 info->blocked_open--;
1419#ifdef SERIAL_DEBUG_OPEN
1420 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1421 info->line, info->count);
1422#endif
1423 if (retval)
1424 return retval;
1425 info->flags |= ASYNC_NORMAL_ACTIVE;
1426 return 0;
1427}
1428
1429/*
1430 * This routine is called whenever a serial port is opened. It
1431 * enables interrupts for a serial port, linking in its structure into
1432 * the IRQ chain. It also performs the serial-specific
1433 * initialization for the tty structure.
1434 */
1435int mcfrs_open(struct tty_struct *tty, struct file * filp)
1436{
1437 struct mcf_serial *info;
1438 int retval, line;
1439
1440 line = tty->index;
1441 if ((line < 0) || (line >= NR_PORTS))
1442 return -ENODEV;
1443 info = mcfrs_table + line;
1444 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1445 return -ENODEV;
1446#ifdef SERIAL_DEBUG_OPEN
1447 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1448#endif
1449 info->count++;
1450 tty->driver_data = info;
1451 info->tty = tty;
1452
1453 /*
1454 * Start up serial port
1455 */
1456 retval = startup(info);
1457 if (retval)
1458 return retval;
1459
1460 retval = block_til_ready(tty, filp, info);
1461 if (retval) {
1462#ifdef SERIAL_DEBUG_OPEN
1463 printk("mcfrs_open returning after block_til_ready with %d\n",
1464 retval);
1465#endif
1466 return retval;
1467 }
1468
1469#ifdef SERIAL_DEBUG_OPEN
1470 printk("mcfrs_open %s successful...\n", tty->name);
1471#endif
1472 return 0;
1473}
1474
1475/*
1476 * Based on the line number set up the internal interrupt stuff.
1477 */
1478static void mcfrs_irqinit(struct mcf_serial *info)
1479{
1480#if defined(CONFIG_M5272)
1481 volatile unsigned long *icrp;
1482 volatile unsigned long *portp;
1483 volatile unsigned char *uartp;
1484
1485 uartp = info->addr;
1486 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1487
1488 switch (info->line) {
1489 case 0:
1490 *icrp = 0xe0000000;
1491 break;
1492 case 1:
1493 *icrp = 0x0e000000;
1494 break;
1495 default:
1496 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1497 info->line);
1498 return;
1499 }
1500
1501 /* Enable the output lines for the serial ports */
1502 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1503 *portp = (*portp & ~0x000000ff) | 0x00000055;
1504 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1505 *portp = (*portp & ~0x000003fc) | 0x000002a8;
Greg Ungererb0433b92005-09-12 11:18:10 +10001506#elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 volatile unsigned char *icrp, *uartp;
1508 volatile unsigned long *imrp;
1509
1510 uartp = info->addr;
1511
1512 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1513 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
Greg Ungerer082f2c12006-01-10 16:59:04 +10001514 *icrp = 0x30 + info->line; /* level 6, line based priority */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1517 MCFINTC_IMRL);
1518 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
Greg Ungerer7f04d622005-11-07 14:09:50 +10001519#elif defined(CONFIG_M520x)
1520 volatile unsigned char *icrp, *uartp;
1521 volatile unsigned long *imrp;
1522
1523 uartp = info->addr;
1524
1525 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1526 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1527 *icrp = 0x03;
1528
1529 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1530 MCFINTC_IMRL);
1531 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1532 if (info->line < 2) {
1533 unsigned short *uart_par;
1534 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1535 if (info->line == 0)
1536 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1537 | MCF_GPIO_PAR_UART_PAR_URXD0;
1538 else if (info->line == 1)
1539 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1540 | MCF_GPIO_PAR_UART_PAR_URXD1;
1541 } else if (info->line == 2) {
1542 unsigned char *feci2c_par;
1543 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1544 *feci2c_par &= ~0x0F;
1545 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1546 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548#else
1549 volatile unsigned char *icrp, *uartp;
1550
1551 switch (info->line) {
1552 case 0:
1553 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1554 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1555 MCFSIM_ICR_PRI1;
1556 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1557 break;
1558 case 1:
1559 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1560 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1561 MCFSIM_ICR_PRI2;
1562 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1563 break;
1564 default:
1565 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1566 info->line);
1567 return;
1568 }
1569
1570 uartp = info->addr;
1571 uartp[MCFUART_UIVR] = info->irq;
1572#endif
1573
1574 /* Clear mask, so no surprise interrupts. */
1575 uartp[MCFUART_UIMR] = 0;
1576
1577 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1578 "ColdFire UART", NULL)) {
1579 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1580 "vector=%d\n", info->line, info->irq);
1581 }
1582
1583 return;
1584}
1585
1586
1587char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1588
1589
1590/*
1591 * Serial stats reporting...
1592 */
1593int mcfrs_readproc(char *page, char **start, off_t off, int count,
1594 int *eof, void *data)
1595{
1596 struct mcf_serial *info;
1597 char str[20];
1598 int len, sigs, i;
1599
1600 len = sprintf(page, mcfrs_drivername);
1601 for (i = 0; (i < NR_PORTS); i++) {
1602 info = &mcfrs_table[i];
1603 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1604 i, (unsigned int) info->addr, info->irq, info->baud);
1605 if (info->stats.rx || info->stats.tx)
1606 len += sprintf((page + len), "tx:%d rx:%d ",
1607 info->stats.tx, info->stats.rx);
1608 if (info->stats.rxframing)
1609 len += sprintf((page + len), "fe:%d ",
1610 info->stats.rxframing);
1611 if (info->stats.rxparity)
1612 len += sprintf((page + len), "pe:%d ",
1613 info->stats.rxparity);
1614 if (info->stats.rxbreak)
1615 len += sprintf((page + len), "brk:%d ",
1616 info->stats.rxbreak);
1617 if (info->stats.rxoverrun)
1618 len += sprintf((page + len), "oe:%d ",
1619 info->stats.rxoverrun);
1620
1621 str[0] = str[1] = 0;
1622 if ((sigs = mcfrs_getsignals(info))) {
1623 if (sigs & TIOCM_RTS)
1624 strcat(str, "|RTS");
1625 if (sigs & TIOCM_CTS)
1626 strcat(str, "|CTS");
1627 if (sigs & TIOCM_DTR)
1628 strcat(str, "|DTR");
1629 if (sigs & TIOCM_CD)
1630 strcat(str, "|CD");
1631 }
1632
1633 len += sprintf((page + len), "%s\n", &str[1]);
1634 }
1635
1636 return(len);
1637}
1638
1639
1640/* Finally, routines used to initialize the serial driver. */
1641
1642static void show_serial_version(void)
1643{
1644 printk(mcfrs_drivername);
1645}
1646
1647static struct tty_operations mcfrs_ops = {
1648 .open = mcfrs_open,
1649 .close = mcfrs_close,
1650 .write = mcfrs_write,
1651 .flush_chars = mcfrs_flush_chars,
1652 .write_room = mcfrs_write_room,
1653 .chars_in_buffer = mcfrs_chars_in_buffer,
1654 .flush_buffer = mcfrs_flush_buffer,
1655 .ioctl = mcfrs_ioctl,
1656 .throttle = mcfrs_throttle,
1657 .unthrottle = mcfrs_unthrottle,
1658 .set_termios = mcfrs_set_termios,
1659 .stop = mcfrs_stop,
1660 .start = mcfrs_start,
1661 .hangup = mcfrs_hangup,
1662 .read_proc = mcfrs_readproc,
1663 .wait_until_sent = mcfrs_wait_until_sent,
1664 .tiocmget = mcfrs_tiocmget,
1665 .tiocmset = mcfrs_tiocmset,
1666};
1667
1668/* mcfrs_init inits the driver */
1669static int __init
1670mcfrs_init(void)
1671{
1672 struct mcf_serial *info;
1673 unsigned long flags;
1674 int i;
1675
1676 /* Setup base handler, and timer table. */
1677#ifdef MCFPP_DCD0
1678 init_timer(&mcfrs_timer_struct);
1679 mcfrs_timer_struct.function = mcfrs_timer;
1680 mcfrs_timer_struct.data = 0;
1681 mcfrs_timer_struct.expires = jiffies + HZ/25;
1682 add_timer(&mcfrs_timer_struct);
1683 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1684#endif
1685 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1686 if (!mcfrs_serial_driver)
1687 return -ENOMEM;
1688
1689 show_serial_version();
1690
1691 /* Initialize the tty_driver structure */
1692 mcfrs_serial_driver->owner = THIS_MODULE;
1693 mcfrs_serial_driver->name = "ttyS";
1694 mcfrs_serial_driver->devfs_name = "ttys/";
1695 mcfrs_serial_driver->driver_name = "serial";
1696 mcfrs_serial_driver->major = TTY_MAJOR;
1697 mcfrs_serial_driver->minor_start = 64;
1698 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1699 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1700 mcfrs_serial_driver->init_termios = tty_std_termios;
1701
1702 mcfrs_serial_driver->init_termios.c_cflag =
1703 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1704 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1705
1706 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1707
1708 if (tty_register_driver(mcfrs_serial_driver)) {
1709 printk("MCFRS: Couldn't register serial driver\n");
1710 put_tty_driver(mcfrs_serial_driver);
1711 return(-EBUSY);
1712 }
1713
1714 local_irq_save(flags);
1715
1716 /*
1717 * Configure all the attached serial ports.
1718 */
1719 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1720 info->magic = SERIAL_MAGIC;
1721 info->line = i;
1722 info->tty = 0;
1723 info->custom_divisor = 16;
1724 info->close_delay = 50;
1725 info->closing_wait = 3000;
1726 info->x_char = 0;
1727 info->event = 0;
1728 info->count = 0;
1729 info->blocked_open = 0;
1730 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1731 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1732 init_waitqueue_head(&info->open_wait);
1733 init_waitqueue_head(&info->close_wait);
1734
1735 info->imr = 0;
1736 mcfrs_setsignals(info, 0, 0);
1737 mcfrs_irqinit(info);
1738
1739 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1740 (unsigned int) info->addr, info->irq);
1741 printk(" is a builtin ColdFire UART\n");
1742 }
1743
1744 local_irq_restore(flags);
1745 return 0;
1746}
1747
1748module_init(mcfrs_init);
1749
1750/****************************************************************************/
1751/* Serial Console */
1752/****************************************************************************/
1753
1754/*
1755 * Quick and dirty UART initialization, for console output.
1756 */
1757
1758void mcfrs_init_console(void)
1759{
1760 volatile unsigned char *uartp;
1761 unsigned int clk;
1762
1763 /*
1764 * Reset UART, get it into known state...
1765 */
1766 uartp = (volatile unsigned char *) (MCF_MBAR +
1767 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1768
1769 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1770 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1771 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1772
1773 /*
1774 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1775 */
1776 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1777 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1778
1779 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1780 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1781 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1782
1783 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1784 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1785
1786 mcfrs_console_inited++;
1787 return;
1788}
1789
1790
1791/*
1792 * Setup for console. Argument comes from the boot command line.
1793 */
1794
1795int mcfrs_console_setup(struct console *cp, char *arg)
1796{
1797 int i, n = CONSOLE_BAUD_RATE;
1798
1799 if (!cp)
1800 return(-1);
1801
1802 if (!strncmp(cp->name, "ttyS", 4))
1803 mcfrs_console_port = cp->index;
1804 else if (!strncmp(cp->name, "cua", 3))
1805 mcfrs_console_port = cp->index;
1806 else
1807 return(-1);
1808
1809 if (arg)
1810 n = simple_strtoul(arg,NULL,0);
1811 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1812 if (mcfrs_baud_table[i] == n)
1813 break;
1814 if (i < MCFRS_BAUD_TABLE_SIZE) {
1815 mcfrs_console_baud = n;
1816 mcfrs_console_cbaud = 0;
1817 if (i > 15) {
1818 mcfrs_console_cbaud |= CBAUDEX;
1819 i -= 15;
1820 }
1821 mcfrs_console_cbaud |= i;
1822 }
1823 mcfrs_init_console(); /* make sure baud rate changes */
1824 return(0);
1825}
1826
1827
1828static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1829{
1830 *index = c->index;
1831 return mcfrs_serial_driver;
1832}
1833
1834
1835/*
1836 * Output a single character, using UART polled mode.
1837 * This is used for console output.
1838 */
1839
1840void mcfrs_put_char(char ch)
1841{
1842 volatile unsigned char *uartp;
1843 unsigned long flags;
1844 int i;
1845
1846 uartp = (volatile unsigned char *) (MCF_MBAR +
1847 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1848
1849 local_irq_save(flags);
1850 for (i = 0; (i < 0x10000); i++) {
1851 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1852 break;
1853 }
1854 if (i < 0x10000) {
1855 uartp[MCFUART_UTB] = ch;
1856 for (i = 0; (i < 0x10000); i++)
1857 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1858 break;
1859 }
1860 if (i >= 0x10000)
1861 mcfrs_init_console(); /* try and get it back */
1862 local_irq_restore(flags);
1863
1864 return;
1865}
1866
1867
1868/*
1869 * rs_console_write is registered for printk output.
1870 */
1871
1872void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1873{
1874 if (!mcfrs_console_inited)
1875 mcfrs_init_console();
1876 while (len-- > 0) {
1877 if (*p == '\n')
1878 mcfrs_put_char('\r');
1879 mcfrs_put_char(*p++);
1880 }
1881}
1882
1883/*
1884 * declare our consoles
1885 */
1886
1887struct console mcfrs_console = {
1888 .name = "ttyS",
1889 .write = mcfrs_console_write,
1890 .device = mcfrs_console_device,
1891 .setup = mcfrs_console_setup,
1892 .flags = CON_PRINTBUFFER,
1893 .index = -1,
1894};
1895
1896static int __init mcfrs_console_init(void)
1897{
1898 register_console(&mcfrs_console);
1899 return 0;
1900}
1901
1902console_initcall(mcfrs_console_init);
1903
1904/****************************************************************************/