2 * UART driver for MPC860 CPM SCC or SMC
3 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
5 * I used the serial.c driver as the framework for this driver.
6 * Give credit to those guys.
7 * The original code was written for the MBX860 board. I tried to make
8 * it generic, but there may be some assumptions in the structures that
9 * have to be fixed later.
10 * To save porting time, I did not bother to change any object names
11 * that are not accessed outside of this file.
12 * It still needs lots of work........When it was easy, I included code
13 * to support the SCCs, but this has never been tested, nor is it complete.
14 * Only the SCCs support modem control, so that is not complete either.
16 * This module exports the following rs232 io functions:
18 * int rs_8xx_init(void);
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serialP.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
40 #include <asm/uaccess.h>
41 #include <asm/8xx_immap.h>
42 #include <asm/mpc8xx.h>
43 #include <asm/commproc.h>
46 #ifdef CONFIG_MAGIC_SYSRQ
47 #include <linux/sysrq.h>
50 extern int kgdb_output_string (const char* s, unsigned int count);
52 #ifdef CONFIG_SERIAL_CONSOLE
53 #include <linux/console.h>
55 /* this defines the index into rs_table for the port to use
57 # ifndef CONFIG_SERIAL_CONSOLE_PORT
58 # ifdef CONFIG_SCC3_ENET
59 # ifdef CONFIG_CONS_SMC2
60 # define CONFIG_SERIAL_CONSOLE_PORT 0 /* Console on SMC2 is 1st port */
62 # error "Can't use SMC1 for console with Ethernet on SCC3"
64 # else /* ! CONFIG_SCC3_ENET */
65 # ifdef CONFIG_CONS_SMC2 /* Console on SMC2 */
66 # define CONFIG_SERIAL_CONSOLE_PORT 1
67 # else /* Console on SMC1 */
68 # define CONFIG_SERIAL_CONSOLE_PORT 0
69 # endif /* CONFIG_CONS_SMC2 */
70 # endif /* CONFIG_SCC3_ENET */
71 # endif /* CONFIG_SERIAL_CONSOLE_PORT */
72 #endif /* CONFIG_SERIAL_CONSOLE */
77 #undef CONFIG_SERIAL_CONSOLE_PORT
78 #define CONFIG_SERIAL_CONSOLE_PORT 2
81 #define TX_WAKEUP ASYNC_SHARE_IRQ
83 static char *serial_name = "CPM UART driver";
84 static char *serial_version = "0.04";
86 /* TX buffer length used by my_console_write.
87 Assume minumun length until it gets set by this driver */
88 static int console_tx_buf_len = 1;
90 static DECLARE_TASK_QUEUE(tq_serial);
92 static struct tty_driver serial_driver, callout_driver;
93 static int serial_refcount;
94 static int serial_console_setup(struct console *co, char *options);
96 static void serial_console_write(struct console *c, const char *s,
98 static kdev_t serial_console_device(struct console *c);
100 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
101 static unsigned long break_pressed; /* break, really ... */
105 * Serial driver configuration section. Here are the various options:
107 #define SERIAL_PARANOIA_CHECK
108 #define CONFIG_SERIAL_NOPAUSE_IO
109 #define SERIAL_DO_RESTART
111 /* Set of debugging defines */
113 #undef SERIAL_DEBUG_INTR
114 #undef SERIAL_DEBUG_OPEN
115 #undef SERIAL_DEBUG_FLOW
116 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
118 #define _INLINE_ inline
122 /* We overload some of the items in the data structure to meet our
123 * needs. For example, the port address is the CPM parameter ram
124 * offset for the SCC or SMC. The maximum number of ports is 4 SCCs and
125 * 2 SMCs. The "hub6" field is used to indicate the channel number, with
126 * a flag indicating SCC or SMC, and the number is used as an index into
127 * the CPM parameter area for this device.
128 * The "type" field is currently set to 0, for PORT_UNKNOWN. It is
129 * not currently used. I should probably use it to indicate the port
130 * type of SMC or SCC.
131 * The SMCs do not support any modem control signals.
133 #define smc_scc_num hub6
134 #define NUM_IS_SCC ((int)0x000100000)
135 #define NUM_BRG ((int)0x0000FF00)
136 #define NUM_BRG_SHIFT 8
137 #define NUM ((int)0x000000FF)
139 #define PORT_NUM(P) ((P) & NUM)
140 #define PORT_NUM_SET(N) (((N)-1) << NUM_SHIFT)
141 #define PORT_IS_SCC(P) ((P) & NUM_IS_SCC)
142 #define PORT_BRG(P) (((P) & NUM_BRG) >> NUM_BRG_SHIFT)
143 #define PORT_BRG_SET(P,B) (P) = (((P) & ~NUM_BRG) | ((B) << NUM_BRG_SHIFT))
145 /* Short names for the ports
147 #define QUICC_CPM_SMC1 (PORT_NUM_SET(1))
148 #define QUICC_CPM_SMC2 (PORT_NUM_SET(2))
149 #define QUICC_CPM_SCC1 (PORT_NUM_SET(1)|NUM_IS_SCC)
150 #define QUICC_CPM_SCC2 (PORT_NUM_SET(2)|NUM_IS_SCC)
151 #define QUICC_CPM_SCC3 (PORT_NUM_SET(3)|NUM_IS_SCC)
152 #define QUICC_CPM_SCC4 (PORT_NUM_SET(4)|NUM_IS_SCC)
153 #define QUICC_MAX_BRG 3 /* BRG1..BRG4 */
155 /* The serial port to use for KGDB. */
156 #ifdef CONFIG_KGDB_TTYS1
157 #define KGDB_SER_IDX 1 /* SCC1/SMC1 */
158 #elif CONFIG_KGDB_TTYS2
159 #define KGDB_SER_IDX 2 /* SCC2/SMC2 */
160 #elif CONFIG_KGDB_TTYS3
161 #define KGDB_SER_IDX 3 /* SCC4/SMC4 (future?) */
162 #else /* Unset, or ttyS0, use SCC1/SMC1 */
163 #define KGDB_SER_IDX 0
166 /* Processors other than the 860 only get SMCs configured by default.
167 * Either they don't have SCCs or they are allocated somewhere else.
168 * Of course, there are now 860s without some SCCs, so we will need to
169 * address that someday.
170 * The Embedded Planet Multimedia I/O cards use TDM interfaces to the
171 * stereo codec parts, and we use SMC2 to help support that.
173 static struct serial_state rs_table[] = {
174 /* UART CLK PORT IRQ FLAGS NUM */
175 #ifdef CONFIG_8xx_SMC1
176 { 0, 0, PROFF_SMC1, CPMVEC_SMC1, 0, QUICC_CPM_SMC1 }, /* SMC1 ttySx */
178 #ifdef CONFIG_8xx_SMC2
179 { 0, 0, PROFF_SMC2, CPMVEC_SMC2, 0, QUICC_CPM_SMC2 }, /* SMC2 ttySx */
181 #ifdef CONFIG_8xx_SCC1
182 { 0, 0, PROFF_SCC1, CPMVEC_SCC1, 0, QUICC_CPM_SCC1 }, /* SCC1 ttySx */
184 #ifdef CONFIG_8xx_SCC2
185 { 0, 0, PROFF_SCC2, CPMVEC_SCC2, 0, QUICC_CPM_SCC2 }, /* SCC2 ttySx */
187 #ifdef CONFIG_8xx_SCC3
188 { 0, 0, PROFF_SCC3, CPMVEC_SCC3, 0, QUICC_CPM_SCC3 }, /* SCC3 ttySx */
190 #ifdef CONFIG_8xx_SCC4
191 { 0, 0, PROFF_SCC4, CPMVEC_SCC4, 0, QUICC_CPM_SCC4 }, /* SCC4 ttySx */
195 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
197 static struct tty_struct *serial_table[NR_PORTS];
198 static struct termios *serial_termios[NR_PORTS];
199 static struct termios *serial_termios_locked[NR_PORTS];
201 /* The number of buffer descriptors and their sizes.
203 #define EARLY_BUF_SIZE 4
204 #define RX_NUM_FIFO 4
205 #define RX_BUF_SIZE 32
206 #define TX_NUM_FIFO 4
207 #define TX_BUF_SIZE 32
210 #define MIN(a,b) ((a) < (b) ? (a) : (b))
213 /* The async_struct in serial.h does not really give us what we
214 * need, so define our own here.
216 typedef struct serial_info {
219 struct serial_state *state;
220 struct tty_struct *tty;
221 int read_status_mask;
222 int ignore_status_mask;
225 int x_char; /* xon/xoff character */
227 unsigned short closing_wait;
228 unsigned short closing_wait2;
230 unsigned long last_active;
231 int blocked_open; /* # of blocked opens */
232 long session; /* Session of opening process */
233 long pgrp; /* pgrp of opening process */
234 struct tq_struct tqueue;
235 struct tq_struct tqueue_hangup;
236 wait_queue_head_t open_wait;
237 wait_queue_head_t close_wait;
239 /* CPM Buffer Descriptor pointers.
246 /* Virtual addresses for the FIFOs because we can't __va() a
247 * physical address anymore.
249 unsigned char *rx_va_base;
250 unsigned char *tx_va_base;
253 static struct console sercons = {
255 write: serial_console_write,
256 device: serial_console_device,
257 setup: serial_console_setup,
258 flags: CON_PRINTBUFFER,
259 index: CONFIG_SERIAL_CONSOLE_PORT,
262 static void change_speed(ser_info_t *info);
263 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout);
265 static inline int serial_paranoia_check(ser_info_t *info,
266 kdev_t device, const char *routine)
268 #ifdef SERIAL_PARANOIA_CHECK
269 static const char *badmagic =
270 "Warning: bad magic number for serial struct (%s) in %s\n";
271 static const char *badinfo =
272 "Warning: null async_struct for (%s) in %s\n";
275 printk(badinfo, kdevname(device), routine);
278 if (info->magic != SERIAL_MAGIC) {
279 printk(badmagic, kdevname(device), routine);
287 * This is used to figure out the divisor speeds and the timeouts,
288 * indexed by the termio value. The generic CPM functions are responsible
289 * for setting and assigning baud rate generators for us.
291 static int baud_table[] = {
292 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
293 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
297 * ------------------------------------------------------------
298 * rs_stop() and rs_start()
300 * This routines are called before setting or resetting tty->stopped.
301 * They enable or disable transmitter interrupts, as necessary.
302 * ------------------------------------------------------------
304 static void rs_8xx_stop(struct tty_struct *tty)
306 ser_info_t *info = (ser_info_t *)tty->driver_data;
309 volatile scc_t *sccp;
310 volatile smc_t *smcp;
312 if (serial_paranoia_check(info, tty->device, "rs_stop"))
315 save_flags(flags); cli();
316 idx = PORT_NUM(info->state->smc_scc_num);
317 if (PORT_IS_SCC(info->state->smc_scc_num)) {
318 sccp = &cpmp->cp_scc[idx];
319 sccp->scc_sccm &= ~UART_SCCM_TX;
322 smcp = &cpmp->cp_smc[idx];
323 smcp->smc_smcm &= ~SMCM_TX;
325 restore_flags(flags);
328 static void rs_8xx_start(struct tty_struct *tty)
330 ser_info_t *info = (ser_info_t *)tty->driver_data;
333 volatile scc_t *sccp;
334 volatile smc_t *smcp;
336 if (serial_paranoia_check(info, tty->device, "rs_stop"))
339 idx = PORT_NUM(info->state->smc_scc_num);
340 save_flags(flags); cli();
341 if (PORT_IS_SCC(info->state->smc_scc_num)) {
342 sccp = &cpmp->cp_scc[idx];
343 sccp->scc_sccm |= UART_SCCM_TX;
346 smcp = &cpmp->cp_smc[idx];
347 smcp->smc_smcm |= SMCM_TX;
349 restore_flags(flags);
353 * ----------------------------------------------------------------------
355 * Here starts the interrupt handling routines. All of the following
356 * subroutines are declared as inline and are folded into
357 * rs_interrupt(). They were separated out for readability's sake.
359 * Note: rs_interrupt() is a "fast" interrupt, which means that it
360 * runs with interrupts turned off. People who may want to modify
361 * rs_interrupt() should try to keep the interrupt handler as fast as
362 * possible. After you are done making modifications, it is not a bad
365 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
367 * and look at the resulting assemble code in serial.s.
369 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
370 * -----------------------------------------------------------------------
374 * This routine is used by the interrupt handler to schedule
375 * processing in the software interrupt portion of the driver.
377 static _INLINE_ void rs_sched_event(ser_info_t *info,
380 info->event |= 1 << event;
381 queue_task(&info->tqueue, &tq_serial);
385 static _INLINE_ void receive_chars(ser_info_t *info, struct pt_regs *regs)
387 struct tty_struct *tty = info->tty;
388 unsigned char ch, *cp;
392 struct async_icount *icount;
395 icount = &info->state->icount;
397 /* Just loop through the closed BDs and copy the characters into
402 if (bdp->cbd_sc & BD_SC_EMPTY) /* If this one is empty */
403 break; /* we are all done */
405 /* The read status mask tell us what we should do with
406 * incoming characters, especially if errors occur.
407 * One special case is the use of BD_SC_EMPTY. If
408 * this is not set, we are supposed to be ignoring
409 * inputs. In this case, just mark the buffer empty and
411 if (!(info->read_status_mask & BD_SC_EMPTY)) {
412 bdp->cbd_sc |= BD_SC_EMPTY;
414 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
416 if (bdp->cbd_sc & BD_SC_WRAP)
417 bdp = info->rx_bd_base;
424 /* Get the number of characters and the buffer pointer.
427 cp = info->rx_va_base + ((bdp - info->rx_bd_base) * RX_BUF_SIZE);
428 status = bdp->cbd_sc;
431 if (info->state->smc_scc_num == KGDB_SER_IDX &&
432 (*cp == 0x03 || *cp == '$')) {
438 /* Check to see if there is room in the tty buffer for
439 * the characters in our BD buffer. If not, we exit
440 * now, leaving the BD with the characters. We'll pick
441 * them up again on the next receive interrupt (which could
444 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
449 *tty->flip.char_buf_ptr = ch;
452 #ifdef SERIAL_DEBUG_INTR
453 printk("DR%02x:%02x...", ch, status);
455 *tty->flip.flag_buf_ptr = 0;
456 if (status & (BD_SC_BR | BD_SC_FR |
457 BD_SC_PR | BD_SC_OV)) {
459 * For statistics only
461 if (status & BD_SC_BR)
463 else if (status & BD_SC_PR)
465 else if (status & BD_SC_FR)
467 if (status & BD_SC_OV)
471 * Now check to see if character should be
472 * ignored, and mask off conditions which
474 if (status & info->ignore_status_mask) {
480 status &= info->read_status_mask;
482 if (status & (BD_SC_BR)) {
483 #ifdef SERIAL_DEBUG_INTR
484 printk("handling break....");
486 *tty->flip.flag_buf_ptr = TTY_BREAK;
487 if (info->flags & ASYNC_SAK)
489 } else if (status & BD_SC_PR)
490 *tty->flip.flag_buf_ptr = TTY_PARITY;
491 else if (status & BD_SC_FR)
492 *tty->flip.flag_buf_ptr = TTY_FRAME;
493 if (status & BD_SC_OV) {
495 * Overrun is special, since it's
496 * reported immediately, and doesn't
497 * affect the current character
499 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
501 tty->flip.flag_buf_ptr++;
502 tty->flip.char_buf_ptr++;
503 *tty->flip.flag_buf_ptr =
508 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
509 if (break_pressed && info->line == sercons.index) {
510 if (ch != 0 && time_before(jiffies,
511 break_pressed + HZ*5)) {
512 handle_sysrq(ch, regs, NULL, NULL);
519 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
522 tty->flip.flag_buf_ptr++;
523 tty->flip.char_buf_ptr++;
527 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
530 /* This BD is ready to be used again. Clear status.
533 bdp->cbd_sc |= BD_SC_EMPTY;
534 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
536 if (bdp->cbd_sc & BD_SC_WRAP)
537 bdp = info->rx_bd_base;
541 info->rx_cur = (cbd_t *)bdp;
543 queue_task(&tty->flip.tqueue, &tq_timer);
546 static _INLINE_ void receive_break(ser_info_t *info, struct pt_regs *regs)
548 struct tty_struct *tty = info->tty;
550 info->state->icount.brk++;
552 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
553 if (info->line == sercons.index) {
554 if (!break_pressed) {
555 break_pressed = jiffies;
562 /* Check to see if there is room in the tty buffer for
563 * the break. If not, we exit now, losing the break. FIXME
565 if ((tty->flip.count + 1) >= TTY_FLIPBUF_SIZE)
567 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
568 *(tty->flip.char_buf_ptr++) = 0;
571 queue_task(&tty->flip.tqueue, &tq_timer);
574 static _INLINE_ void transmit_chars(ser_info_t *info, struct pt_regs *regs)
577 if ((info->flags & TX_WAKEUP) ||
578 (info->tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
579 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
582 #ifdef SERIAL_DEBUG_INTR
588 /* I need to do this for the SCCs, so it is left as a reminder.
590 static _INLINE_ void check_modem_status(struct async_struct *info)
593 struct async_icount *icount;
595 status = serial_in(info, UART_MSR);
597 if (status & UART_MSR_ANY_DELTA) {
598 icount = &info->state->icount;
599 /* update input line counters */
600 if (status & UART_MSR_TERI)
602 if (status & UART_MSR_DDSR)
604 if (status & UART_MSR_DDCD) {
606 #ifdef CONFIG_HARD_PPS
607 if ((info->flags & ASYNC_HARDPPS_CD) &&
608 (status & UART_MSR_DCD))
612 if (status & UART_MSR_DCTS)
614 wake_up_interruptible(&info->delta_msr_wait);
617 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
618 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
619 printk("ttys%d CD now %s...", info->line,
620 (status & UART_MSR_DCD) ? "on" : "off");
622 if (status & UART_MSR_DCD)
623 wake_up_interruptible(&info->open_wait);
624 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
625 (info->flags & ASYNC_CALLOUT_NOHUP))) {
626 #ifdef SERIAL_DEBUG_OPEN
627 printk("scheduling hangup...");
630 if (schedule_task(&info->tqueue_hangup) == 0)
634 if (info->flags & ASYNC_CTS_FLOW) {
635 if (info->tty->hw_stopped) {
636 if (status & UART_MSR_CTS) {
637 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
638 printk("CTS tx start...");
640 info->tty->hw_stopped = 0;
641 info->IER |= UART_IER_THRI;
642 serial_out(info, UART_IER, info->IER);
643 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
647 if (!(status & UART_MSR_CTS)) {
648 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
649 printk("CTS tx stop...");
651 info->tty->hw_stopped = 1;
652 info->IER &= ~UART_IER_THRI;
653 serial_out(info, UART_IER, info->IER);
661 * This is the serial driver's interrupt routine for a single port
663 static void rs_8xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
668 volatile smc_t *smcp;
669 volatile scc_t *sccp;
671 info = (ser_info_t *)dev_id;
673 idx = PORT_NUM(info->state->smc_scc_num);
674 if (PORT_IS_SCC(info->state->smc_scc_num)) {
675 sccp = &cpmp->cp_scc[idx];
676 events = sccp->scc_scce;
677 if (events & SMCM_BRKE)
678 receive_break(info, regs);
679 if (events & SCCM_RX)
680 receive_chars(info, regs);
681 if (events & SCCM_TX)
682 transmit_chars(info, regs);
683 sccp->scc_scce = events;
686 smcp = &cpmp->cp_smc[idx];
687 events = smcp->smc_smce;
688 if (events & SMCM_BRKE)
689 receive_break(info, regs);
690 if (events & SMCM_RX)
691 receive_chars(info, regs);
692 if (events & SMCM_TX)
693 transmit_chars(info, regs);
694 smcp->smc_smce = events;
697 #ifdef SERIAL_DEBUG_INTR
698 printk("rs_interrupt_single(%d, %x)...",
699 info->state->smc_scc_num, events);
702 check_modem_status(info);
704 info->last_active = jiffies;
705 #ifdef SERIAL_DEBUG_INTR
712 * -------------------------------------------------------------------
713 * Here ends the serial interrupt routines.
714 * -------------------------------------------------------------------
718 * This routine is used to handle the "bottom half" processing for the
719 * serial driver, known also the "software interrupt" processing.
720 * This processing is done at the kernel interrupt level, after the
721 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
722 * is where time-consuming activities which can not be done in the
723 * interrupt driver proper are done; the interrupt driver schedules
724 * them using rs_sched_event(), and they get done here.
726 static void do_serial_bh(void)
728 run_task_queue(&tq_serial);
731 static void do_softint(void *private_)
733 ser_info_t *info = (ser_info_t *) private_;
734 struct tty_struct *tty;
740 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
746 * This routine is called from the scheduler tqueue when the interrupt
747 * routine has signalled that a hangup has occurred. The path of
748 * hangup processing is:
750 * serial interrupt routine -> (scheduler tqueue) ->
751 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
754 static void do_serial_hangup(void *private_)
756 struct async_struct *info = (struct async_struct *) private_;
757 struct tty_struct *tty;
765 /*static void rs_8xx_timer(void)
767 printk("rs_8xx_timer\n");
771 static int startup(ser_info_t *info)
776 struct serial_state *state= info->state;
777 volatile smc_t *smcp;
778 volatile scc_t *sccp;
779 volatile smc_uart_t *up;
780 volatile scc_uart_t *scup;
783 save_flags(flags); cli();
785 if (info->flags & ASYNC_INITIALIZED) {
790 if (!state->port || !state->type) {
792 set_bit(TTY_IO_ERROR, &info->tty->flags);
797 #ifdef SERIAL_DEBUG_OPEN
798 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
804 if (info->tty->termios->c_cflag & CBAUD)
805 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
809 clear_bit(TTY_IO_ERROR, &info->tty->flags);
812 * and set the speed of the serial port
816 idx = PORT_NUM(info->state->smc_scc_num);
817 if (PORT_IS_SCC(info->state->smc_scc_num)) {
818 sccp = &cpmp->cp_scc[idx];
819 scup = (scc_uart_t *)&cpmp->cp_dparam[state->port];
820 scup->scc_genscc.scc_mrblr = RX_BUF_SIZE;
821 scup->scc_maxidl = RX_BUF_SIZE;
822 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
823 sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
826 smcp = &cpmp->cp_smc[idx];
828 /* Enable interrupts and I/O.
830 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
831 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
833 /* We can tune the buffer length and idle characters
834 * to take advantage of the entire incoming buffer size.
835 * If mrblr is something other than 1, maxidl has to be
836 * non-zero or we never get an interrupt. The maxidl
837 * is the number of character times we wait after reception
838 * of the last character before we decide no more characters
841 up = (smc_uart_t *)&cpmp->cp_dparam[state->port];
842 up->smc_mrblr = RX_BUF_SIZE;
843 up->smc_maxidl = RX_BUF_SIZE;
844 up->smc_brkcr = 1; /* number of break chars */
847 info->flags |= ASYNC_INITIALIZED;
848 restore_flags(flags);
852 restore_flags(flags);
857 * This routine will shutdown a serial port; interrupts are disabled, and
858 * DTR is dropped if the hangup on close termio flag is on.
860 static void shutdown(ser_info_t * info)
863 struct serial_state *state;
865 volatile smc_t *smcp;
866 volatile scc_t *sccp;
868 if (!(info->flags & ASYNC_INITIALIZED))
873 #ifdef SERIAL_DEBUG_OPEN
874 printk("Shutting down serial port %d (irq %d)....", info->line,
878 save_flags(flags); cli(); /* Disable interrupts */
880 idx = PORT_NUM(state->smc_scc_num);
881 if (PORT_IS_SCC(state->smc_scc_num)) {
882 sccp = &cpmp->cp_scc[idx];
883 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
884 #ifdef CONFIG_SERIAL_CONSOLE
885 /* We can't disable the transmitter if this is the
888 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
890 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
893 smcp = &cpmp->cp_smc[idx];
895 /* Disable interrupts and I/O.
897 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
898 #ifdef CONFIG_SERIAL_CONSOLE
899 /* We can't disable the transmitter if this is the
902 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
904 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
908 set_bit(TTY_IO_ERROR, &info->tty->flags);
910 info->flags &= ~ASYNC_INITIALIZED;
911 restore_flags(flags);
915 * This routine is called to set the UART divisor registers to match
916 * the specified baud rate for a serial port.
918 static void change_speed(ser_info_t *info)
921 unsigned cflag, cval, scval, prev_mode, new_mode;
922 int i, bits, sbits, idx;
924 struct serial_state *state;
925 volatile smc_t *smcp;
926 volatile scc_t *sccp;
928 if (!info->tty || !info->tty->termios)
930 cflag = info->tty->termios->c_cflag;
934 /* Character length programmed into the mode register is the
935 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
936 * 1 or 2 stop bits, minus 1.
937 * The value 'bits' counts this for us.
942 /* byte size and parity */
943 switch (cflag & CSIZE) {
944 case CS5: bits = 5; break;
945 case CS6: bits = 6; break;
946 case CS7: bits = 7; break;
947 case CS8: bits = 8; break;
948 /* Never happens, but GCC is too dumb to figure it out */
949 default: bits = 8; break;
953 if (cflag & CSTOPB) {
954 cval |= SMCMR_SL; /* Two stops */
955 scval |= SCU_PMSR_SL;
958 if (cflag & PARENB) {
960 scval |= SCU_PMSR_PEN;
962 if (!(cflag & PARODD)) {
963 cval |= SMCMR_PM_EVEN;
964 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
968 /* Determine divisor based on baud rate */
970 if (i >= (sizeof(baud_table)/sizeof(int)))
973 baud_rate = baud_table[i];
975 info->timeout = (TX_BUF_SIZE*HZ*bits);
976 info->timeout += HZ/50; /* Add .02 seconds of slop */
979 /* CTS flow control flag and modem status interrupts */
980 info->IER &= ~UART_IER_MSI;
981 if (info->flags & ASYNC_HARDPPS_CD)
982 info->IER |= UART_IER_MSI;
983 if (cflag & CRTSCTS) {
984 info->flags |= ASYNC_CTS_FLOW;
985 info->IER |= UART_IER_MSI;
987 info->flags &= ~ASYNC_CTS_FLOW;
989 info->flags &= ~ASYNC_CHECK_CD;
991 info->flags |= ASYNC_CHECK_CD;
992 info->IER |= UART_IER_MSI;
994 serial_out(info, UART_IER, info->IER);
998 * Set up parity check flag
1000 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1002 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
1003 if (I_INPCK(info->tty))
1004 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
1005 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1006 info->read_status_mask |= BD_SC_BR;
1009 * Characters to ignore
1011 info->ignore_status_mask = 0;
1012 if (I_IGNPAR(info->tty))
1013 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
1014 if (I_IGNBRK(info->tty)) {
1015 info->ignore_status_mask |= BD_SC_BR;
1017 * If we're ignore parity and break indicators, ignore
1018 * overruns too. (For real raw support).
1020 if (I_IGNPAR(info->tty))
1021 info->ignore_status_mask |= BD_SC_OV;
1024 * !!! ignore all characters if CREAD is not set
1026 if ((cflag & CREAD) == 0)
1027 info->read_status_mask &= ~BD_SC_EMPTY;
1028 save_flags(flags); cli();
1030 /* Start bit has not been added (so don't, because we would just
1031 * subtract it later), and we need to add one for the number of
1032 * stops bits (there is always at least one).
1035 idx = PORT_NUM(state->smc_scc_num);
1036 if (PORT_IS_SCC(state->smc_scc_num)) {
1037 sccp = &cpmp->cp_scc[idx];
1038 new_mode = (sbits << 12) | scval;
1039 prev_mode = sccp->scc_pmsr;
1040 if (!(prev_mode & SCU_PMSR_PEN))
1041 /* If parity is disabled, mask out even/odd */
1042 prev_mode &= ~(SCU_PMSR_TPM|SCU_PMSR_RPM);
1043 if (prev_mode != new_mode)
1044 sccp->scc_pmsr = new_mode;
1047 smcp = &cpmp->cp_smc[idx];
1049 /* Set the mode register. We want to keep a copy of the
1050 * enables, because we want to put them back if they were
1053 prev_mode = smcp->smc_smcmr & (SMCMR_REN | SMCMR_TEN);
1054 new_mode = smcr_mk_clen(bits) | cval | SMCMR_SM_UART
1056 if (!(prev_mode & SMCMR_PEN))
1057 /* If parity is disabled, mask out even/odd */
1058 prev_mode &= ~SMCMR_PM_EVEN;
1059 if (prev_mode != new_mode)
1060 smcp->smc_smcmr = new_mode;
1063 m8xx_cpm_setbrg(PORT_BRG(state->smc_scc_num), baud_rate);
1065 restore_flags(flags);
1068 static void rs_8xx_put_char(struct tty_struct *tty, unsigned char ch)
1070 ser_info_t *info = (ser_info_t *)tty->driver_data;
1071 volatile cbd_t *bdp;
1073 unsigned long flags;
1075 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1081 local_irq_save(flags);
1085 if (bdp->cbd_sc & BD_SC_WRAP)
1086 info->tx_cur = info->tx_bd_base;
1088 info->tx_cur = (cbd_t *)bdp + 1;
1089 local_irq_restore(flags);
1091 while (bdp->cbd_sc & BD_SC_READY);
1093 cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
1095 bdp->cbd_datlen = 1;
1096 bdp->cbd_sc |= BD_SC_READY;
1100 static int rs_8xx_write(struct tty_struct * tty, int from_user,
1101 const unsigned char *buf, int count)
1104 ser_info_t *info = (ser_info_t *)tty->driver_data;
1105 volatile cbd_t *bdp;
1107 unsigned long flags;
1109 #ifdef CONFIG_KGDB_CONSOLE
1110 /* Try to let stub handle output. Returns true if it did. */
1111 if (kgdb_output_string(buf, count))
1115 if (serial_paranoia_check(info, tty->device, "rs_write"))
1122 c = MIN(count, TX_BUF_SIZE);
1127 local_irq_save(flags);
1129 if (bdp->cbd_sc & BD_SC_READY) {
1130 info->flags |= TX_WAKEUP;
1131 local_irq_restore(flags);
1136 if (bdp->cbd_sc & BD_SC_WRAP)
1137 info->tx_cur = info->tx_bd_base;
1139 info->tx_cur = (cbd_t *)bdp + 1;
1140 local_irq_restore(flags);
1142 cp = info->tx_va_base + ((bdp - info->tx_bd_base) *
1145 c -= copy_from_user((void *)cp, buf, c);
1147 memcpy((void *)cp, buf, c);
1150 bdp->cbd_datlen = c;
1151 bdp->cbd_sc |= BD_SC_READY;
1153 /* Need to TX at least 1 char to keep CPM sane */
1154 bdp->cbd_datlen = 1;
1156 bdp->cbd_sc |= BD_SC_READY;
1169 static int rs_8xx_write_room(struct tty_struct *tty)
1171 ser_info_t *info = (ser_info_t *)tty->driver_data;
1174 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1177 if ((info->tx_cur->cbd_sc & BD_SC_READY) == 0) {
1178 info->flags &= ~TX_WAKEUP;
1182 info->flags |= TX_WAKEUP;
1188 /* I could track this with transmit counters....maybe later.
1190 static int rs_8xx_chars_in_buffer(struct tty_struct *tty)
1192 ser_info_t *info = (ser_info_t *)tty->driver_data;
1194 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1199 static void rs_8xx_flush_buffer(struct tty_struct *tty)
1201 ser_info_t *info = (ser_info_t *)tty->driver_data;
1203 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1206 /* There is nothing to "flush", whatever we gave the CPM
1207 * is on its way out.
1210 info->flags &= ~TX_WAKEUP;
1214 * This function is used to send a high-priority XON/XOFF character to
1217 static void rs_8xx_send_xchar(struct tty_struct *tty, char ch)
1219 volatile cbd_t *bdp;
1221 unsigned long flags;
1223 ser_info_t *info = (ser_info_t *)tty->driver_data;
1225 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1228 local_irq_save(flags);
1232 if (bdp->cbd_sc & BD_SC_WRAP)
1233 info->tx_cur = info->tx_bd_base;
1235 info->tx_cur = (cbd_t *)bdp + 1;
1236 local_irq_restore(flags);
1237 while (bdp->cbd_sc & BD_SC_READY);
1239 cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
1241 bdp->cbd_datlen = 1;
1242 bdp->cbd_sc |= BD_SC_READY;
1246 * ------------------------------------------------------------
1249 * This routine is called by the upper-layer tty layer to signal that
1250 * incoming characters should be throttled.
1251 * ------------------------------------------------------------
1253 static void rs_8xx_throttle(struct tty_struct * tty)
1255 ser_info_t *info = (ser_info_t *)tty->driver_data;
1256 #ifdef SERIAL_DEBUG_THROTTLE
1259 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1260 tty->ldisc.chars_in_buffer(tty));
1263 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1267 rs_8xx_send_xchar(tty, STOP_CHAR(tty));
1269 #ifdef modem_control
1270 if (tty->termios->c_cflag & CRTSCTS)
1271 info->MCR &= ~UART_MCR_RTS;
1274 serial_out(info, UART_MCR, info->MCR);
1279 static void rs_8xx_unthrottle(struct tty_struct * tty)
1281 ser_info_t *info = (ser_info_t *)tty->driver_data;
1282 #ifdef SERIAL_DEBUG_THROTTLE
1285 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1286 tty->ldisc.chars_in_buffer(tty));
1289 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1296 rs_8xx_send_xchar(tty, START_CHAR(tty));
1298 #ifdef modem_control
1299 if (tty->termios->c_cflag & CRTSCTS)
1300 info->MCR |= UART_MCR_RTS;
1302 serial_out(info, UART_MCR, info->MCR);
1308 * ------------------------------------------------------------
1309 * rs_ioctl() and friends
1310 * ------------------------------------------------------------
1315 * get_lsr_info - get line status register info
1317 * Purpose: Let user call ioctl() to get info when the UART physically
1318 * is emptied. On bus types like RS485, the transmitter must
1319 * release the bus after transmitting. This must be done when
1320 * the transmit shift register is empty, not be done when the
1321 * transmit holding register is empty. This functionality
1322 * allows an RS485 driver to be written in user space.
1324 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1326 unsigned char status;
1327 unsigned int result;
1330 status = serial_in(info, UART_LSR);
1332 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1333 return put_user(result,value);
1337 static int get_modem_info(ser_info_t *info, unsigned int *value)
1339 unsigned int result = 0;
1340 #ifdef modem_control
1341 unsigned char control, status;
1343 control = info->MCR;
1345 status = serial_in(info, UART_MSR);
1347 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1348 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1350 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1351 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1353 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1354 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1355 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1356 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1358 return put_user(result,value);
1361 static int set_modem_info(ser_info_t *info, unsigned int cmd,
1362 unsigned int *value)
1367 error = get_user(arg, value);
1370 #ifdef modem_control
1373 if (arg & TIOCM_RTS)
1374 info->MCR |= UART_MCR_RTS;
1375 if (arg & TIOCM_DTR)
1376 info->MCR |= UART_MCR_DTR;
1378 if (arg & TIOCM_OUT1)
1379 info->MCR |= UART_MCR_OUT1;
1380 if (arg & TIOCM_OUT2)
1381 info->MCR |= UART_MCR_OUT2;
1385 if (arg & TIOCM_RTS)
1386 info->MCR &= ~UART_MCR_RTS;
1387 if (arg & TIOCM_DTR)
1388 info->MCR &= ~UART_MCR_DTR;
1390 if (arg & TIOCM_OUT1)
1391 info->MCR &= ~UART_MCR_OUT1;
1392 if (arg & TIOCM_OUT2)
1393 info->MCR &= ~UART_MCR_OUT2;
1397 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1403 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1405 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1406 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1408 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1414 serial_out(info, UART_MCR, info->MCR);
1420 /* Sending a break is a two step process on the SMC/SCC. It is accomplished
1421 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1422 * command. We take advantage of the begin/end functions to make this
1425 static ushort smc_chan_map[] = {
1430 static ushort scc_chan_map[] = {
1437 static void begin_break(ser_info_t *info)
1439 volatile cpm8xx_t *cp;
1445 idx = PORT_NUM(info->state->smc_scc_num);
1446 if (PORT_IS_SCC(info->state->smc_scc_num))
1447 chan = scc_chan_map[idx];
1449 chan = smc_chan_map[idx];
1450 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1451 while (cp->cp_cpcr & CPM_CR_FLG);
1454 static void end_break(ser_info_t *info)
1456 volatile cpm8xx_t *cp;
1462 idx = PORT_NUM(info->state->smc_scc_num);
1463 if (PORT_IS_SCC(info->state->smc_scc_num))
1464 chan = scc_chan_map[idx];
1466 chan = smc_chan_map[idx];
1467 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1468 while (cp->cp_cpcr & CPM_CR_FLG);
1472 * This routine sends a break character out the serial port.
1474 static void send_break(ser_info_t *info, int duration)
1476 current->state = TASK_INTERRUPTIBLE;
1477 #ifdef SERIAL_DEBUG_SEND_BREAK
1478 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1481 schedule_timeout(duration);
1483 #ifdef SERIAL_DEBUG_SEND_BREAK
1484 printk("done jiffies=%lu\n", jiffies);
1489 static int rs_8xx_ioctl(struct tty_struct *tty, struct file * file,
1490 unsigned int cmd, unsigned long arg)
1493 ser_info_t *info = (ser_info_t *)tty->driver_data;
1495 struct async_icount cnow; /* kernel counter temps */
1496 struct serial_icounter_struct *p_cuser; /* user space */
1498 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1501 if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1502 if (tty->flags & (1 << TTY_IO_ERROR))
1507 case TCSBRK: /* SVID version: non-zero arg --> no break */
1508 retval = tty_check_change(tty);
1511 tty_wait_until_sent(tty, 0);
1512 if (signal_pending(current))
1515 send_break(info, HZ/4); /* 1/4 second */
1516 if (signal_pending(current))
1520 case TCSBRKP: /* support for POSIX tcsendbreak() */
1521 retval = tty_check_change(tty);
1524 tty_wait_until_sent(tty, 0);
1525 if (signal_pending(current))
1527 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1528 if (signal_pending(current))
1532 retval = tty_check_change(tty);
1535 tty_wait_until_sent(tty, 0);
1539 retval = tty_check_change(tty);
1545 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1547 error = get_user(arg, (unsigned int *) arg);
1550 tty->termios->c_cflag =
1551 ((tty->termios->c_cflag & ~CLOCAL) |
1552 (arg ? CLOCAL : 0));
1555 return get_modem_info(info, (unsigned int *) arg);
1559 return set_modem_info(info, cmd, (unsigned int *) arg);
1561 case TIOCSERGETLSR: /* Get line status register */
1562 return get_lsr_info(info, (unsigned int *) arg);
1565 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1566 * - mask passed in arg for lines of interest
1567 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1568 * Caller should use TIOCGICOUNT to see which one it was
1571 #ifdef modem_control
1573 /* note the counters on entry */
1574 cprev = info->state->icount;
1577 interruptible_sleep_on(&info->delta_msr_wait);
1578 /* see if a signal did it */
1579 if (signal_pending(current))
1580 return -ERESTARTSYS;
1582 cnow = info->state->icount; /* atomic copy */
1584 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1585 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1586 return -EIO; /* no change => error */
1587 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1588 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1589 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1590 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1601 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1602 * Return: write counters to the user passed counter struct
1603 * NB: both 1->0 and 0->1 transitions are counted except for
1604 * RI where only 0->1 is counted.
1608 cnow = info->state->icount;
1610 p_cuser = (struct serial_icounter_struct *) arg;
1611 error = put_user(cnow.cts, &p_cuser->cts);
1612 if (error) return error;
1613 error = put_user(cnow.dsr, &p_cuser->dsr);
1614 if (error) return error;
1615 error = put_user(cnow.rng, &p_cuser->rng);
1616 if (error) return error;
1617 error = put_user(cnow.dcd, &p_cuser->dcd);
1618 if (error) return error;
1622 return -ENOIOCTLCMD;
1627 /* FIX UP modem control here someday......
1629 static void rs_8xx_set_termios(struct tty_struct *tty, struct termios *old_termios)
1631 ser_info_t *info = (ser_info_t *)tty->driver_data;
1633 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1634 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1635 == RELEVANT_IFLAG(old_termios->c_iflag)))
1640 #ifdef modem_control
1641 /* Handle transition to B0 status */
1642 if ((old_termios->c_cflag & CBAUD) &&
1643 !(tty->termios->c_cflag & CBAUD)) {
1644 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1646 serial_out(info, UART_MCR, info->MCR);
1650 /* Handle transition away from B0 status */
1651 if (!(old_termios->c_cflag & CBAUD) &&
1652 (tty->termios->c_cflag & CBAUD)) {
1653 info->MCR |= UART_MCR_DTR;
1654 if (!tty->hw_stopped ||
1655 !(tty->termios->c_cflag & CRTSCTS)) {
1656 info->MCR |= UART_MCR_RTS;
1659 serial_out(info, UART_MCR, info->MCR);
1663 /* Handle turning off CRTSCTS */
1664 if ((old_termios->c_cflag & CRTSCTS) &&
1665 !(tty->termios->c_cflag & CRTSCTS)) {
1666 tty->hw_stopped = 0;
1673 * No need to wake up processes in open wait, since they
1674 * sample the CLOCAL flag once, and don't recheck it.
1675 * XXX It's not clear whether the current behavior is correct
1676 * or not. Hence, this may change.....
1678 if (!(old_termios->c_cflag & CLOCAL) &&
1679 (tty->termios->c_cflag & CLOCAL))
1680 wake_up_interruptible(&info->open_wait);
1685 * ------------------------------------------------------------
1688 * This routine is called when the serial port gets closed. First, we
1689 * wait for the last remaining data to be sent. Then, we unlink its
1690 * async structure from the interrupt chain if necessary, and we free
1691 * that IRQ if nothing is left in the chain.
1692 * ------------------------------------------------------------
1694 static void rs_8xx_close(struct tty_struct *tty, struct file * filp)
1696 ser_info_t *info = (ser_info_t *)tty->driver_data;
1697 struct serial_state *state;
1698 unsigned long flags;
1700 volatile smc_t *smcp;
1701 volatile scc_t *sccp;
1703 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1706 state = info->state;
1708 save_flags(flags); cli();
1710 if (tty_hung_up_p(filp)) {
1711 DBG_CNT("before DEC-hung");
1713 restore_flags(flags);
1717 #ifdef SERIAL_DEBUG_OPEN
1718 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1720 if ((tty->count == 1) && (state->count != 1)) {
1722 * Uh, oh. tty->count is 1, which means that the tty
1723 * structure will be freed. state->count should always
1724 * be one in these conditions. If it's greater than
1725 * one, we've got real problems, since it means the
1726 * serial port won't be shutdown.
1728 printk("rs_close: bad serial port count; tty->count is 1, "
1729 "state->count is %d\n", state->count);
1732 if (--state->count < 0) {
1733 printk("rs_close: bad serial port count for ttys%d: %d\n",
1734 info->line, state->count);
1738 DBG_CNT("before DEC-2");
1740 restore_flags(flags);
1743 info->flags |= ASYNC_CLOSING;
1745 * Save the termios structure, since this port may have
1746 * separate termios for callout and dialin.
1748 if (info->flags & ASYNC_NORMAL_ACTIVE)
1749 info->state->normal_termios = *tty->termios;
1750 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1751 info->state->callout_termios = *tty->termios;
1753 * Now we wait for the transmit buffer to clear; and we notify
1754 * the line discipline to only process XON/XOFF characters.
1757 if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1758 tty_wait_until_sent(tty, state->closing_wait);
1760 * At this point we stop accepting input. To do this, we
1761 * disable the receive line status interrupts, and tell the
1762 * interrupt driver to stop checking the data ready bit in the
1763 * line status register.
1765 info->read_status_mask &= ~BD_SC_EMPTY;
1766 if (info->flags & ASYNC_INITIALIZED) {
1767 idx = PORT_NUM(info->state->smc_scc_num);
1768 if (PORT_IS_SCC(info->state->smc_scc_num)) {
1769 sccp = &cpmp->cp_scc[idx];
1770 sccp->scc_sccm &= ~UART_SCCM_RX;
1771 sccp->scc_gsmrl &= ~SCC_GSMRL_ENR;
1774 smcp = &cpmp->cp_smc[idx];
1775 smcp->smc_smcm &= ~SMCM_RX;
1776 smcp->smc_smcmr &= ~SMCMR_REN;
1779 * Before we drop DTR, make sure the UART transmitter
1780 * has completely drained; this is especially
1781 * important if there is a transmit FIFO!
1783 rs_8xx_wait_until_sent(tty, info->timeout);
1786 if (tty->driver.flush_buffer)
1787 tty->driver.flush_buffer(tty);
1788 tty_ldisc_flush(tty);
1792 if (info->blocked_open) {
1793 if (state->close_delay) {
1794 current->state = TASK_INTERRUPTIBLE;
1795 schedule_timeout(state->close_delay);
1797 wake_up_interruptible(&info->open_wait);
1799 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1801 wake_up_interruptible(&info->close_wait);
1803 restore_flags(flags);
1807 * rs_wait_until_sent() --- wait until the transmitter is empty
1809 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout)
1811 ser_info_t *info = (ser_info_t *)tty->driver_data;
1812 unsigned long orig_jiffies, char_time, tst_res;
1814 volatile cbd_t *bdp;
1816 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1820 if (info->state->type == PORT_UNKNOWN)
1824 orig_jiffies = jiffies;
1826 * Set the check interval to be 1/5 of the estimated time to
1827 * send a single character, and make it at least 1. The check
1828 * interval should also be less than the timeout.
1830 * Note: we have to use pretty tight timings here to satisfy
1835 char_time = MIN(char_time, timeout);
1836 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1837 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1838 printk("jiff=%lu...", jiffies);
1841 /* We go through the loop at least once because we can't tell
1842 * exactly when the last character exits the shifter. There can
1843 * be at least two characters waiting to be sent after the buffers
1847 unsigned long flags;
1848 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1849 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1851 current->state = TASK_INTERRUPTIBLE;
1852 /* current->counter = 0; make us low-priority */
1853 schedule_timeout(char_time);
1854 if (signal_pending(current))
1856 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1859 /* The 'tx_cur' is really the next buffer to send. We
1860 * have to back up to the previous BD and wait for it
1861 * to go. This isn't perfect, because all this indicates
1862 * is the buffer is available. There are still characters
1865 local_irq_save(flags);
1867 if (bdp == info->tx_bd_base)
1868 bdp += (TX_NUM_FIFO-1);
1871 tst_res = !!(bdp->cbd_sc & BD_SC_READY);
1872 local_irq_restore(flags);
1874 current->state = TASK_RUNNING;
1875 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1876 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1881 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1883 static void rs_8xx_hangup(struct tty_struct *tty)
1885 ser_info_t *info = (ser_info_t *)tty->driver_data;
1886 struct serial_state *state = info->state;
1888 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1891 state = info->state;
1893 rs_8xx_flush_buffer(tty);
1897 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1899 wake_up_interruptible(&info->open_wait);
1903 * ------------------------------------------------------------
1904 * rs_open() and friends
1905 * ------------------------------------------------------------
1907 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1910 #ifdef DO_THIS_LATER
1911 DECLARE_WAITQUEUE(wait, current);
1913 struct serial_state *state = info->state;
1918 * If the device is in the middle of being closed, then block
1919 * until it's done, and then try again.
1921 if (tty_hung_up_p(filp) ||
1922 (info->flags & ASYNC_CLOSING)) {
1923 if (info->flags & ASYNC_CLOSING)
1924 interruptible_sleep_on(&info->close_wait);
1925 #ifdef SERIAL_DO_RESTART
1926 if (info->flags & ASYNC_HUP_NOTIFY)
1929 return -ERESTARTSYS;
1936 * If this is a callout device, then just make sure the normal
1937 * device isn't being used.
1939 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1940 if (info->flags & ASYNC_NORMAL_ACTIVE)
1942 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1943 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1944 (info->session != current->session))
1946 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1947 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1948 (info->pgrp != current->pgrp))
1950 info->flags |= ASYNC_CALLOUT_ACTIVE;
1955 * If non-blocking mode is set, or the port is not enabled,
1956 * then make the check up front and then exit.
1957 * If this is an SMC port, we don't have modem control to wait
1958 * for, so just get out here.
1960 if ((filp->f_flags & O_NONBLOCK) ||
1961 (tty->flags & (1 << TTY_IO_ERROR)) ||
1962 !(PORT_IS_SCC(info->state->smc_scc_num))) {
1963 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1965 info->flags |= ASYNC_NORMAL_ACTIVE;
1969 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1970 if (state->normal_termios.c_cflag & CLOCAL)
1973 if (tty->termios->c_cflag & CLOCAL)
1978 * Block waiting for the carrier detect and the line to become
1979 * free (i.e., not in use by the callout). While we are in
1980 * this loop, state->count is dropped by one, so that
1981 * rs_close() knows when to free things. We restore it upon
1982 * exit, either normal or abnormal.
1985 #ifdef DO_THIS_LATER
1986 add_wait_queue(&info->open_wait, &wait);
1987 #ifdef SERIAL_DEBUG_OPEN
1988 printk("block_til_ready before block: ttys%d, count = %d\n",
1989 state->line, state->count);
1992 if (!tty_hung_up_p(filp))
1995 info->blocked_open++;
1998 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1999 (tty->termios->c_cflag & CBAUD))
2000 serial_out(info, UART_MCR,
2001 serial_inp(info, UART_MCR) |
2002 (UART_MCR_DTR | UART_MCR_RTS));
2004 set_current_state(TASK_INTERRUPTIBLE);
2005 if (tty_hung_up_p(filp) ||
2006 !(info->flags & ASYNC_INITIALIZED)) {
2007 #ifdef SERIAL_DO_RESTART
2008 if (info->flags & ASYNC_HUP_NOTIFY)
2011 retval = -ERESTARTSYS;
2017 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2018 !(info->flags & ASYNC_CLOSING) &&
2019 (do_clocal || (serial_in(info, UART_MSR) &
2022 if (signal_pending(current)) {
2023 retval = -ERESTARTSYS;
2026 #ifdef SERIAL_DEBUG_OPEN
2027 printk("block_til_ready blocking: ttys%d, count = %d\n",
2028 info->line, state->count);
2032 current->state = TASK_RUNNING;
2033 remove_wait_queue(&info->open_wait, &wait);
2034 if (!tty_hung_up_p(filp))
2036 info->blocked_open--;
2037 #ifdef SERIAL_DEBUG_OPEN
2038 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2039 info->line, state->count);
2041 #endif /* DO_THIS_LATER */
2044 info->flags |= ASYNC_NORMAL_ACTIVE;
2048 static int get_async_struct(int line, ser_info_t **ret_info)
2050 struct serial_state *sstate;
2052 sstate = rs_table + line;
2055 *ret_info = (ser_info_t *)sstate->info;
2064 * This routine is called whenever a serial port is opened. It
2065 * enables interrupts for a serial port, linking in its async structure into
2066 * the IRQ chain. It also performs the serial-specific
2067 * initialization for the tty structure.
2069 static int rs_8xx_open(struct tty_struct *tty, struct file * filp)
2074 line = MINOR(tty->device) - tty->driver.minor_start;
2075 if ((line < 0) || (line >= NR_PORTS))
2077 retval = get_async_struct(line, &info);
2080 if (serial_paranoia_check(info, tty->device, "rs_open"))
2083 #ifdef SERIAL_DEBUG_OPEN
2084 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2085 info->state->count);
2087 tty->driver_data = info;
2091 * Start up serial port
2093 retval = startup(info);
2098 retval = block_til_ready(tty, filp, info);
2100 #ifdef SERIAL_DEBUG_OPEN
2101 printk("rs_open returning after block_til_ready with %d\n",
2108 if ((info->state->count == 1) &&
2109 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2110 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2111 *tty->termios = info->state->normal_termios;
2113 *tty->termios = info->state->callout_termios;
2117 info->session = current->session;
2118 info->pgrp = current->pgrp;
2120 #ifdef SERIAL_DEBUG_OPEN
2121 printk("rs_open ttys%d successful...", info->line);
2127 * /proc fs routines....
2130 static int inline line_info(char *buf, struct serial_state *state)
2133 struct async_struct *info = state->info, scr_info;
2134 char stat_buf[30], control, status;
2138 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2140 (PORT_IS_SCC(state->smc_scc_num)) ? "SCC" : "SMC",
2141 (unsigned int)(state->port), state->irq);
2143 if (!state->port || (state->type == PORT_UNKNOWN)) {
2144 ret += sprintf(buf+ret, "\n");
2150 * Figure out the current RS-232 lines
2153 info = &scr_info; /* This is just for serial_{in,out} */
2155 info->magic = SERIAL_MAGIC;
2156 info->port = state->port;
2157 info->flags = state->flags;
2162 status = serial_in(info, UART_MSR);
2163 control = info ? info->MCR : serial_in(info, UART_MCR);
2168 if (control & UART_MCR_RTS)
2169 strcat(stat_buf, "|RTS");
2170 if (status & UART_MSR_CTS)
2171 strcat(stat_buf, "|CTS");
2172 if (control & UART_MCR_DTR)
2173 strcat(stat_buf, "|DTR");
2174 if (status & UART_MSR_DSR)
2175 strcat(stat_buf, "|DSR");
2176 if (status & UART_MSR_DCD)
2177 strcat(stat_buf, "|CD");
2178 if (status & UART_MSR_RI)
2179 strcat(stat_buf, "|RI");
2182 ret += sprintf(buf+ret, " baud:%d",
2183 state->baud_base / info->quot);
2186 ret += sprintf(buf+ret, " tx:%d rx:%d",
2187 state->icount.tx, state->icount.rx);
2189 if (state->icount.frame)
2190 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2192 if (state->icount.parity)
2193 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2195 if (state->icount.brk)
2196 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2198 if (state->icount.overrun)
2199 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2202 * Last thing is the RS-232 status lines
2204 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2209 int rs_8xx_read_proc(char *page, char **start, off_t off, int count,
2210 int *eof, void *data)
2215 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2216 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2217 len += line_info(page + len, &rs_table[i]);
2218 if (len+begin > off+count)
2220 if (len+begin < off) {
2227 if (off >= len+begin)
2229 *start = page + (begin-off);
2230 return ((count < begin+len-off) ? count : begin+len-off);
2234 * ---------------------------------------------------------------------
2235 * rs_init() and friends
2237 * rs_init() is called at boot-time to initialize the serial driver.
2238 * ---------------------------------------------------------------------
2242 * This routine prints out the appropriate serial driver version
2243 * number, and identifies which options were configured into this
2246 static _INLINE_ void show_serial_version(void)
2248 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2253 * The serial console driver used during boot. Note that these names
2254 * clash with those found in "serial.c", so we currently can't support
2255 * the 16xxx uarts and these at the same time. I will fix this to become
2256 * an indirect function call from tty_io.c (or something).
2259 #ifdef CONFIG_SERIAL_CONSOLE
2261 /* I need this just so I can store the virtual addresses and have
2262 * common functions for the early console printing.
2264 static ser_info_t consinfo;
2267 * Print a string to the serial port trying not to disturb any possible
2268 * real use of the port...
2270 static void my_console_write(int idx, const char *s,
2273 struct serial_state *ser;
2275 unsigned i, c, cr_missing, max_tx_size;
2276 volatile cbd_t *bdp, *bdbase;
2277 volatile smc_uart_t *up;
2278 volatile u_char *cp;
2279 unsigned long flags;
2281 ser = rs_table + idx;
2283 /* If the port has been initialized for general use, we have
2284 * to use the buffer descriptors allocated there. Otherwise,
2285 * we simply use the single buffer allocated.
2287 if ((info = (ser_info_t *)ser->info) != NULL) {
2288 bdbase = info->tx_bd_base;
2290 /* Pointer to UART in parameter ram. */
2291 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2293 /* Get the address of the host memory buffer.*/
2295 info->tx_bd_base = (cbd_t *)bdbase = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2296 info->tx_cur = (cbd_t *)bdbase;
2298 max_tx_size = console_tx_buf_len;
2301 c = MIN(max_tx_size, count);
2305 local_irq_save(flags);
2307 bdbase = info->tx_bd_base;
2308 if (bdp->cbd_sc & BD_SC_WRAP)
2309 info->tx_cur = (cbd_t *)bdbase;
2311 info->tx_cur = (cbd_t *)(bdp+1);
2312 local_irq_restore(flags);
2314 /* Wait for transmitter fifo to empty.
2315 * Ready indicates output is ready, and xmt is doing
2316 * that, not that it is ready for us to send.
2318 while (bdp->cbd_sc & BD_SC_READY);
2320 /* Send the characters out.
2321 * If the buffer address is in the CPM DPRAM, don't
2324 if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2325 cp = (u_char *)(bdp->cbd_bufaddr);
2327 cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
2329 i=1; /* Keeps track of consumed TX buffer space */
2331 /* Previus loop didn't have room for the CR, insert it first in this */
2336 for (; i <= c; i++) {
2337 if ((*cp++ = *s++) != '\n')
2338 continue; /* Copy bytes until a NewLine is found */
2339 /* NewLine found, see if there is space in the TX buffer to add a CR */
2340 if (i < max_tx_size) {
2341 *cp++ = '\r'; /* yes, there is space to add a CR */
2344 cr_missing = 1; /* No space in the TX buffer,
2345 rember it so it can be inserted in the next loop */
2347 count -= (c-cr_missing);
2348 bdp->cbd_datlen = i-1;
2349 bdp->cbd_sc |= BD_SC_READY;
2352 /* while (bdp->cbd_sc & BD_SC_READY); is this really needed? */
2354 static void serial_console_write(struct console *c, const char *s,
2357 #ifdef CONFIG_KGDB_CONSOLE
2358 /* Try to let stub handle output. Returns true if it did. */
2359 if (kgdb_output_string(s, count))
2362 my_console_write(c->index, s, count);
2367 xmon_8xx_write(const char *s, unsigned count)
2369 my_console_write(0, s, count);
2374 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON)
2376 * Receive character from the serial port. This only works well
2377 * before the port is initialized for real use.
2379 static int my_console_wait_key(int idx, int xmon, char *obuf)
2381 struct serial_state *ser;
2384 volatile cbd_t *bdp;
2385 volatile smc_uart_t *up;
2388 ser = rs_table + idx;
2390 /* Pointer to UART in parameter ram.
2392 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2394 /* Get the address of the host memory buffer.
2395 * If the port has been initialized for general use, we must
2396 * use information from the port structure.
2398 if ((info = (ser_info_t *)ser->info)) {
2402 bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2407 * We need to gracefully shut down the receiver, disable
2408 * interrupts, then read the input.
2409 * XMON just wants a poll. If no character, return -1, else
2410 * return the character.
2413 while (bdp->cbd_sc & BD_SC_EMPTY);
2416 if (bdp->cbd_sc & BD_SC_EMPTY)
2420 /* If the buffer address is in the CPM DPRAM, don't
2423 if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2424 cp = (u_char *)(bdp->cbd_bufaddr);
2426 cp = info->rx_va_base + ((bdp - info->rx_bd_base) * RX_BUF_SIZE);
2429 i = c = bdp->cbd_datlen;
2436 bdp->cbd_sc |= BD_SC_EMPTY;
2439 if (bdp->cbd_sc & BD_SC_WRAP) {
2440 bdp = info->rx_bd_base;
2445 info->rx_cur = (cbd_t *)bdp;
2450 #endif /* CONFIG_KGDB || CONFIG_XMON */
2454 xmon_8xx_read_poll(void)
2456 return(my_console_wait_key(0, 1, NULL));
2460 xmon_8xx_read_char(void)
2462 return(my_console_wait_key(0, 0, NULL));
2467 static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2468 static int kgdb_chars;
2471 putDebugChar(char ch)
2473 my_console_write(0, &ch, 1);
2479 if (kgdb_chars <= 0) {
2480 kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2488 void kgdb_interruptible(int yes)
2490 volatile smc_t *smcp;
2492 smcp = &cpmp->cp_smc[KGDB_SER_IDX];
2495 smcp->smc_smcm |= SMCM_RX;
2497 smcp->smc_smcm &= ~SMCM_RX;
2500 void kgdb_map_scc(void)
2502 struct serial_state *ser;
2504 volatile cbd_t *bdp;
2505 volatile smc_uart_t *up;
2507 cpmp = (cpm8xx_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2509 /* To avoid data cache CPM DMA coherency problems, allocate a
2510 * buffer in the CPM DPRAM. This will work until the CPM and
2511 * serial ports are initialized. At that time a memory buffer
2512 * will be allocated.
2513 * The port is already initialized from the boot procedure, all
2514 * we do here is give it a different buffer and make it a FIFO.
2519 /* Right now, assume we are using SMCs.
2521 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2523 /* Allocate space for an input FIFO, plus a few bytes for output.
2524 * Allocate bytes to maintain word alignment.
2526 mem_addr = (uint)(&cpmp->cp_dpmem[0xa00]);
2528 /* Set the physical address of the host memory buffers in
2529 * the buffer descriptors.
2531 bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2532 bdp->cbd_bufaddr = mem_addr;
2534 bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2535 bdp->cbd_bufaddr = mem_addr+RX_BUF_SIZE;
2537 up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */
2538 up->smc_maxidl = RX_BUF_SIZE;
2542 static kdev_t serial_console_device(struct console *c)
2544 return MKDEV(TTY_MAJOR, 64 + c->index);
2550 long __init console_8xx_init(long kmem_start, long kmem_end)
2552 register_console(&sercons);
2556 #endif /* CONFIG_SERIAL_CONSOLE */
2558 /* Index in baud rate table of the default console baud rate.
2560 static int baud_idx;
2563 * The serial driver boot-time initialization code!
2566 int __init rs_8xx_alloc_brg(int port)
2569 volatile cpm8xx_t *cp = cpmp;
2572 /* "Wire" the BRG to the specified port
2575 case QUICC_CPM_SMC1:
2576 cp->cp_simode = (cp->cp_simode & ~(0x07<<12)) | (brg<<12);
2578 case QUICC_CPM_SMC2:
2579 cp->cp_simode = (cp->cp_simode & ~(0x07<<28)) | (brg<<28);
2581 case QUICC_CPM_SCC1:
2582 cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<0)) | (((brg<<3)|(brg<<0))<<0);
2584 case QUICC_CPM_SCC2:
2585 cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<8)) | (((brg<<3)|(brg<<0))<<8);
2587 case QUICC_CPM_SCC3:
2588 cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<16)) | (((brg<<3)|(brg<<0))<<16);
2590 case QUICC_CPM_SCC4:
2591 cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<24)) | (((brg<<3)|(brg<<0))<<24);
2594 /* Consume this BRG - Note: the last BRG will be reused if this
2596 /* function is called too many times!
2598 if (brg < QUICC_MAX_BRG) brg++;
2602 int __init rs_8xx_init(void)
2604 struct serial_state * state;
2606 uint mem_addr, dp_addr, iobits;
2609 volatile cbd_t *bdp;
2610 volatile cpm8xx_t *cp;
2612 volatile smc_uart_t *up;
2613 volatile scc_t *scp;
2614 volatile scc_uart_t *sup;
2615 volatile immap_t *immap;
2617 init_bh(SERIAL_BH, do_serial_bh);
2619 show_serial_version();
2621 /* Initialize the tty_driver structure */
2623 __clear_user(&serial_driver,sizeof(struct tty_driver));
2624 serial_driver.magic = TTY_DRIVER_MAGIC;
2625 serial_driver.driver_name = "serial";
2626 #ifdef CONFIG_DEVFS_FS
2627 serial_driver.name = "tts/%d";
2629 serial_driver.name = "ttyS";
2631 serial_driver.major = TTY_MAJOR;
2632 serial_driver.minor_start = 64;
2633 serial_driver.num = NR_PORTS;
2634 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2635 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2636 serial_driver.init_termios = tty_std_termios;
2637 serial_driver.init_termios.c_cflag =
2638 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2639 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2640 serial_driver.refcount = &serial_refcount;
2641 serial_driver.table = serial_table;
2642 serial_driver.termios = serial_termios;
2643 serial_driver.termios_locked = serial_termios_locked;
2645 serial_driver.open = rs_8xx_open;
2646 serial_driver.close = rs_8xx_close;
2647 serial_driver.write = rs_8xx_write;
2648 serial_driver.put_char = rs_8xx_put_char;
2649 serial_driver.write_room = rs_8xx_write_room;
2650 serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer;
2651 serial_driver.flush_buffer = rs_8xx_flush_buffer;
2652 serial_driver.ioctl = rs_8xx_ioctl;
2653 serial_driver.throttle = rs_8xx_throttle;
2654 serial_driver.unthrottle = rs_8xx_unthrottle;
2655 serial_driver.send_xchar = rs_8xx_send_xchar;
2656 serial_driver.set_termios = rs_8xx_set_termios;
2657 serial_driver.stop = rs_8xx_stop;
2658 serial_driver.start = rs_8xx_start;
2659 serial_driver.hangup = rs_8xx_hangup;
2660 serial_driver.wait_until_sent = rs_8xx_wait_until_sent;
2661 serial_driver.read_proc = rs_8xx_read_proc;
2664 * The callout device is just like normal device except for
2665 * major number and the subtype code.
2667 callout_driver = serial_driver;
2668 #ifdef CONFIG_DEVFS_FS
2669 callout_driver.name = "cua/%d";
2671 callout_driver.name = "cua";
2673 callout_driver.major = TTYAUX_MAJOR;
2674 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2675 callout_driver.read_proc = 0;
2676 callout_driver.proc_entry = 0;
2678 if (tty_register_driver(&serial_driver))
2679 panic("Couldn't register serial driver\n");
2680 if (tty_register_driver(&callout_driver))
2681 panic("Couldn't register callout driver\n");
2683 cp = cpmp; /* Get pointer to Communication Processor */
2684 immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
2687 /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2689 #ifdef CONFIG_USE_SCC_IO
2691 /* The "standard" configuration through the 860.
2693 immap->im_ioport.iop_papar |= 0x00fc;
2694 immap->im_ioport.iop_padir &= ~0x00fc;
2695 immap->im_ioport.iop_paodr &= ~0x00fc;
2697 /* On the MBX, SCC3 is through Port D.
2699 immap->im_ioport.iop_papar |= 0x000c; /* SCC2 on port A */
2700 immap->im_ioport.iop_padir &= ~0x000c;
2701 immap->im_ioport.iop_paodr &= ~0x000c;
2703 immap->im_ioport.iop_pdpar |= 0x0030; /* SCC3 on port D */
2706 /* Since we don't yet do modem control, connect the port C pins
2707 * as general purpose I/O. This will assert CTS and CD for the
2710 immap->im_ioport.iop_pcdir |= 0x03c6;
2711 immap->im_ioport.iop_pcpar &= ~0x03c6;
2713 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2716 cp->cp_sicr &= ~0x00ffff00;
2717 cp->cp_sicr |= 0x001b1200;
2720 /* Frequentis PP04 forced to RS-232 until we know better.
2721 * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2723 immap->im_ioport.iop_pcdir |= 0x000c;
2724 immap->im_ioport.iop_pcpar &= ~0x000c;
2725 immap->im_ioport.iop_pcdat &= ~0x000c;
2727 /* This enables the TX driver.
2729 cp->cp_pbpar &= ~0x6000;
2730 cp->cp_pbdat &= ~0x6000;
2734 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2735 state->magic = SSTATE_MAGIC;
2737 state->type = PORT_UNKNOWN;
2738 state->custom_divisor = 0;
2739 state->close_delay = 5*HZ/10;
2740 state->closing_wait = 30*HZ;
2741 state->callout_termios = callout_driver.init_termios;
2742 state->normal_termios = serial_driver.init_termios;
2743 state->icount.cts = state->icount.dsr =
2744 state->icount.rng = state->icount.dcd = 0;
2745 state->icount.rx = state->icount.tx = 0;
2746 state->icount.frame = state->icount.parity = 0;
2747 state->icount.overrun = state->icount.brk = 0;
2748 PORT_BRG_SET(state->smc_scc_num, rs_8xx_alloc_brg(state->smc_scc_num));
2749 printk(KERN_INFO "ttyS%d at 0x%04x is on %s%d using BRG%d\n",
2750 i, (unsigned int)(state->port),
2751 PORT_IS_SCC(state->smc_scc_num) ? "SCC" : "SMC",
2752 PORT_NUM(state->smc_scc_num)+1,
2753 PORT_BRG(state->smc_scc_num)+1);
2754 #ifdef CONFIG_SERIAL_CONSOLE
2755 /* If we just printed the message on the console port, and
2756 * we are about to initialize it for general use, we have
2757 * to wait a couple of character times for the CR/NL to
2758 * make it out of the transmit buffer.
2760 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2763 info = kmalloc(sizeof(ser_info_t), GFP_KERNEL);
2765 __clear_user(info,sizeof(ser_info_t));
2766 init_waitqueue_head(&info->open_wait);
2767 init_waitqueue_head(&info->close_wait);
2768 info->magic = SERIAL_MAGIC;
2769 info->flags = state->flags;
2770 info->tqueue.routine = do_softint;
2771 info->tqueue.data = info;
2772 info->tqueue_hangup.routine = do_serial_hangup;
2773 info->tqueue_hangup.data = info;
2775 info->state = state;
2776 state->info = (struct async_struct *)info;
2778 /* We need to allocate a transmit and receive buffer
2779 * descriptors from dual port ram, and a character
2780 * buffer area from host mem.
2782 dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * RX_NUM_FIFO);
2784 /* Allocate space for FIFOs in the host memory.
2786 mem_addr = m8xx_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE);
2787 info->rx_va_base = (unsigned char *)mem_addr;
2789 /* Set the physical address of the host memory
2790 * buffers in the buffer descriptors, and the
2791 * virtual address for us to work with.
2793 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2794 info->rx_cur = info->rx_bd_base = (cbd_t *)bdp;
2796 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2797 bdp->cbd_bufaddr = iopa(mem_addr);
2798 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2799 mem_addr += RX_BUF_SIZE;
2802 bdp->cbd_bufaddr = iopa(mem_addr);
2803 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2805 idx = PORT_NUM(info->state->smc_scc_num);
2806 if (PORT_IS_SCC(info->state->smc_scc_num)) {
2807 scp = &cp->cp_scc[idx];
2808 sup = (scc_uart_t *)&cp->cp_dparam[state->port];
2809 sup->scc_genscc.scc_rbase = dp_addr;
2812 sp = &cp->cp_smc[idx];
2813 up = (smc_uart_t *)&cp->cp_dparam[state->port];
2814 up->smc_rbase = dp_addr;
2817 dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * TX_NUM_FIFO);
2819 /* Allocate space for FIFOs in the host memory.
2821 mem_addr = m8xx_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE);
2822 info->tx_va_base = (unsigned char *)mem_addr;
2824 /* Set the physical address of the host memory
2825 * buffers in the buffer descriptors, and the
2826 * virtual address for us to work with.
2828 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2829 info->tx_cur = info->tx_bd_base = (cbd_t *)bdp;
2831 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2832 bdp->cbd_bufaddr = iopa(mem_addr);
2833 bdp->cbd_sc = BD_SC_INTRPT;
2834 mem_addr += TX_BUF_SIZE;
2837 bdp->cbd_bufaddr = iopa(mem_addr);
2838 bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2840 if (PORT_IS_SCC(info->state->smc_scc_num)) {
2841 sup->scc_genscc.scc_tbase = dp_addr;
2843 /* Set up the uart parameters in the
2846 sup->scc_genscc.scc_rfcr = SMC_EB;
2847 sup->scc_genscc.scc_tfcr = SMC_EB;
2849 /* Set this to 1 for now, so we get single
2850 * character interrupts. Using idle charater
2851 * time requires some additional tuning.
2853 sup->scc_genscc.scc_mrblr = 1;
2854 sup->scc_maxidl = 0;
2860 sup->scc_uaddr1 = 0;
2861 sup->scc_uaddr2 = 0;
2863 sup->scc_char1 = 0x8000;
2864 sup->scc_char2 = 0x8000;
2865 sup->scc_char3 = 0x8000;
2866 sup->scc_char4 = 0x8000;
2867 sup->scc_char5 = 0x8000;
2868 sup->scc_char6 = 0x8000;
2869 sup->scc_char7 = 0x8000;
2870 sup->scc_char8 = 0x8000;
2871 sup->scc_rccm = 0xc0ff;
2873 /* Send the CPM an initialize command.
2875 chan = scc_chan_map[idx];
2877 cp->cp_cpcr = mk_cr_cmd(chan,
2878 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2879 while (cp->cp_cpcr & CPM_CR_FLG);
2881 /* Set UART mode, 8 bit, no parity, one stop.
2882 * Enable receive and transmit.
2886 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2888 /* Disable all interrupts and clear all pending
2892 scp->scc_scce = 0xffff;
2893 scp->scc_dsr = 0x7e7e;
2894 scp->scc_pmsr = 0x3000;
2896 /* If the port is the console, enable Rx and Tx.
2898 #ifdef CONFIG_SERIAL_CONSOLE
2899 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2900 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2904 /* Configure SMCs Tx/Rx instead of port B
2905 * parallel I/O. On 823/850 these are on
2908 #ifndef CONFIG_ALTSMC2
2909 iobits = 0xc0 << (idx * 4);
2910 cp->cp_pbpar |= iobits;
2911 cp->cp_pbdir &= ~iobits;
2912 cp->cp_pbodr &= ~iobits;
2916 /* SMC1 on Port B, like all 8xx.
2918 cp->cp_pbpar |= iobits;
2919 cp->cp_pbdir &= ~iobits;
2920 cp->cp_pbodr &= ~iobits;
2923 /* SMC2 is on Port A.
2925 immap->im_ioport.iop_papar |= iobits;
2926 immap->im_ioport.iop_padir &= ~iobits;
2927 immap->im_ioport.iop_paodr &= ~iobits;
2929 #endif /* CONFIG_ALTSMC2 */
2932 /* Connect the baud rate generator to the
2933 * SMC based upon index in rs_table. Also
2934 * make sure it is connected to NMSI.
2936 cp->cp_simode &= ~(0xffff << (idx * 16));
2937 cp->cp_simode |= (i << ((idx * 16) + 12));
2940 up->smc_tbase = dp_addr;
2942 /* Set up the uart parameters in the
2945 up->smc_rfcr = SMC_EB;
2946 up->smc_tfcr = SMC_EB;
2948 /* Set this to 1 for now, so we get single
2949 * character interrupts. Using idle charater
2950 * time requires some additional tuning.
2956 /* Send the CPM an initialize command.
2958 chan = smc_chan_map[idx];
2960 cp->cp_cpcr = mk_cr_cmd(chan,
2961 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2962 while (cp->cp_cpcr & CPM_CR_FLG);
2964 /* Set UART mode, 8 bit, no parity, one stop.
2965 * Enable receive and transmit.
2967 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2969 /* Disable all interrupts and clear all pending
2973 sp->smc_smce = 0xff;
2975 /* If the port is the console, enable Rx and Tx.
2977 #ifdef CONFIG_SERIAL_CONSOLE
2978 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2979 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2983 /* Install interrupt handler.
2985 if ((request_irq(CPM_IRQ_OFFSET + state->irq, rs_8xx_interrupt, 0, cpm_int_name[state->irq], info)) != 0)
2986 panic("Could not allocate UART IRQ!");
2988 /* Set up the baud rate generator.
2990 m8xx_cpm_setbrg(PORT_BRG(state->smc_scc_num), baud_table[baud_idx]);
2994 console_tx_buf_len = TX_BUF_SIZE;
2998 /* This must always be called before the rs_8xx_init() function, otherwise
2999 * it blows away the port control information.
3001 static int __init serial_console_setup(struct console *co, char *options)
3003 struct serial_state *ser;
3004 uint mem_addr, dp_addr, bidx, idx;
3006 volatile cbd_t *bdp;
3007 volatile cpm8xx_t *cp;
3009 volatile scc_t *scp;
3010 volatile smc_uart_t *up;
3011 volatile scc_uart_t *sup;
3016 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
3017 if (bd->bi_baudrate == baud_table[bidx])
3019 /* make sure we have a useful value */
3020 if (bidx == (sizeof(baud_table) / sizeof(int)))
3021 bidx = 13; /* B9600 */
3023 co->cflag = CREAD|CLOCAL|bidx|CS8;
3026 ser = rs_table + co->index;
3028 cp = cpmp; /* Get pointer to Communication Processor */
3030 idx = PORT_NUM(ser->smc_scc_num);
3031 if (PORT_IS_SCC(ser->smc_scc_num)) {
3032 scp = &cp->cp_scc[idx];
3033 sup = (scc_uart_t *)&cp->cp_dparam[ser->port];
3036 sp = &cp->cp_smc[idx];
3037 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
3040 /* When we get here, the CPM has been reset, so we need
3041 * to configure the port.
3042 * We need to allocate a transmit and receive buffer descriptor
3043 * from dual port ram, and a character buffer area from host mem.
3046 /* Allocate space for two FIFOs. We can't allocate from host
3047 * memory yet because vm allocator isn't initialized
3048 * during this early console init.
3050 dp_addr = m8xx_cpm_dpalloc(2*EARLY_BUF_SIZE);
3051 mem_addr = (uint)(&cpmp->cp_dpmem[dp_addr]);
3053 /* Allocate space for two buffer descriptors in the DP ram.
3055 dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * 2);
3057 /* Set the physical address of the host memory buffers in
3058 * the buffer descriptors.
3060 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
3061 bdp->cbd_bufaddr = iopa(mem_addr);
3062 (bdp+1)->cbd_bufaddr = iopa(mem_addr+4);
3064 consinfo.rx_va_base = (unsigned char *) mem_addr;
3065 consinfo.rx_bd_base = (cbd_t *) bdp;
3066 consinfo.tx_va_base = (unsigned char *) (mem_addr + EARLY_BUF_SIZE);
3067 consinfo.tx_bd_base = (cbd_t *) (bdp+1);
3069 /* For the receive, set empty and wrap.
3070 * For transmit, set wrap.
3072 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
3073 (bdp+1)->cbd_sc = BD_SC_WRAP;
3075 /* Set up the uart parameters in the parameter ram.
3077 if (PORT_IS_SCC(ser->smc_scc_num)) {
3079 sup->scc_genscc.scc_rbase = dp_addr;
3080 sup->scc_genscc.scc_tbase = dp_addr + sizeof(cbd_t);
3082 /* Set up the uart parameters in the
3085 sup->scc_genscc.scc_rfcr = SMC_EB;
3086 sup->scc_genscc.scc_tfcr = SMC_EB;
3088 /* Set this to 1 for now, so we get single
3089 * character interrupts. Using idle charater
3090 * time requires some additional tuning.
3092 sup->scc_genscc.scc_mrblr = 1;
3093 sup->scc_maxidl = 0;
3099 sup->scc_uaddr1 = 0;
3100 sup->scc_uaddr2 = 0;
3102 sup->scc_char1 = 0x8000;
3103 sup->scc_char2 = 0x8000;
3104 sup->scc_char3 = 0x8000;
3105 sup->scc_char4 = 0x8000;
3106 sup->scc_char5 = 0x8000;
3107 sup->scc_char6 = 0x8000;
3108 sup->scc_char7 = 0x8000;
3109 sup->scc_char8 = 0x8000;
3110 sup->scc_rccm = 0xc0ff;
3112 /* Send the CPM an initialize command.
3114 chan = scc_chan_map[idx];
3116 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3117 while (cp->cp_cpcr & CPM_CR_FLG);
3119 /* Set UART mode, 8 bit, no parity, one stop.
3120 * Enable receive and transmit.
3124 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
3126 /* Disable all interrupts and clear all pending
3130 scp->scc_scce = 0xffff;
3131 scp->scc_dsr = 0x7e7e;
3132 scp->scc_pmsr = 0x3000;
3134 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
3138 up->smc_rbase = dp_addr; /* Base of receive buffer desc. */
3139 up->smc_tbase = dp_addr+sizeof(cbd_t); /* Base of xmt buffer desc. */
3140 up->smc_rfcr = SMC_EB;
3141 up->smc_tfcr = SMC_EB;
3143 /* Set this to 1 for now, so we get single character interrupts.
3145 up->smc_mrblr = 1; /* receive buffer length */
3146 up->smc_maxidl = 0; /* wait forever for next char */
3148 /* Send the CPM an initialize command.
3150 chan = smc_chan_map[idx];
3151 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3153 while (cp->cp_cpcr & CPM_CR_FLG);
3155 /* Set UART mode, 8 bit, no parity, one stop.
3156 * Enable receive and transmit.
3158 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
3160 /* And finally, enable Rx and Tx.
3162 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
3165 /* Set up the baud rate generator.
3167 m8xx_cpm_setbrg((ser - rs_table), bd->bi_baudrate);
3168 console_tx_buf_len = EARLY_BUF_SIZE;