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