ia64/linux-2.6.18-xen.hg

view drivers/serial/mcfserial.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

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