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