2 * BK Id: SCCS/s.uart.c 1.20 11/19/02 11:58:40 trini
5 * UART driver for MPC8260 CPM SCC or SMC
6 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
7 * Copyright (c) 2000 MontaVista Software, Inc. (source@mvista.com)
10 * I used the 8xx uart.c driver as the framework for this driver.
11 * The original code was written for the EST8260 board. I tried to make
12 * it generic, but there may be some assumptions in the structures that
13 * have to be fixed later.
15 * The 8xx and 8260 are similar, but not identical. Over time we
16 * could probably merge these two drivers.
17 * To save porting time, I did not bother to change any object names
18 * that are not accessed outside of this file.
19 * It still needs lots of work........When it was easy, I included code
20 * to support the SCCs.
21 * Only the SCCs support modem control, so that is not complete either.
23 * This module exports the following rs232 io functions:
25 * int rs_8xx_init(void);
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/interrupt.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/serial.h>
38 #include <linux/serialP.h>
39 #include <linux/major.h>
40 #include <linux/string.h>
41 #include <linux/fcntl.h>
42 #include <linux/ptrace.h>
44 #include <linux/slab.h>
45 #include <linux/init.h>
46 #include <linux/delay.h>
47 #include <asm/uaccess.h>
48 #include <asm/immap_8260.h>
49 #include <asm/mpc8260.h>
50 #include <asm/cpm_8260.h>
53 #ifdef CONFIG_SERIAL_CONSOLE
54 #include <linux/console.h>
56 /* SCC Console configuration. Not quite finished. The SCC_CONSOLE
57 * should be the number of the SCC to use, but only SCC1 will
60 #ifdef CONFIG_SCC_CONSOLE
64 /* this defines the index into rs_table for the port to use
66 #ifndef CONFIG_SERIAL_CONSOLE_PORT
67 #define CONFIG_SERIAL_CONSOLE_PORT 0
70 #define CONFIG_SERIAL_CONSOLE_PORT 0
72 #define TX_WAKEUP ASYNC_SHARE_IRQ
74 static char *serial_name = "CPM UART driver";
75 static char *serial_version = "0.01";
77 static DECLARE_TASK_QUEUE(tq_serial);
79 static struct tty_driver serial_driver, callout_driver;
80 static int serial_refcount;
81 static int serial_console_setup(struct console *co, char *options);
84 * Serial driver configuration section. Here are the various options:
86 #define SERIAL_PARANOIA_CHECK
87 #define CONFIG_SERIAL_NOPAUSE_IO
88 #define SERIAL_DO_RESTART
90 /* Set of debugging defines */
92 #undef SERIAL_DEBUG_INTR
93 #undef SERIAL_DEBUG_OPEN
94 #undef SERIAL_DEBUG_FLOW
95 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
97 #define _INLINE_ inline
101 /* We overload some of the items in the data structure to meet our
102 * needs. For example, the port address is the CPM parameter ram
103 * offset for the SCC or SMC. The maximum number of ports is 4 SCCs and
104 * 2 SMCs. The "hub6" field is used to indicate the channel number, with
105 * 0 and 1 indicating the SMCs and 2, 3, 4, and 5 are the SCCs.
106 * Since these ports are so versatile, I don't yet have a strategy for
107 * their management. For example, SCC1 is used for Ethernet. Right
108 * now, just don't put them in the table. Of course, right now I just
109 * want the SMC to work as a uart :-)..
110 * The "type" field is currently set to 0, for PORT_UNKNOWN. It is
111 * not currently used. I should probably use it to indicate the port
112 * type of CMS or SCC.
113 * The SMCs do not support any modem control signals.
115 #define smc_scc_num hub6
117 /* The choice of serial port to use for KGDB. If the system has
118 * two ports, you can use one for console and one for KGDB (which
119 * doesn't make sense to me, but people asked for it).
121 #ifdef CONFIG_KGDB_TTYS1
122 #define KGDB_SER_IDX 1 /* SCC2/SMC2 */
124 #define KGDB_SER_IDX 0 /* SCC1/SMC1 */
129 /* SMC2 is sometimes used for low performance TDM interfaces. Define
130 * this as 1 if you want SMC2 as a serial port UART managed by this driver.
131 * Define this as 0 if you wish to use SMC2 for something else.
135 /* Define SCC to ttySx mapping.
137 #define SCC_NUM_BASE (USE_SMC2 + 1) /* SCC base tty "number" */
139 /* Define which SCC is the first one to use for a serial port. These
140 * are 0-based numbers, i.e. this assumes the first SCC (SCC1) is used
141 * for Ethernet, and the first available SCC for serial UART is SCC2.
142 * NOTE: IF YOU CHANGE THIS, you have to change the PROFF_xxx and
143 * interrupt vectors in the table below to match.
145 #define SCC_IDX_BASE 1 /* table index */
147 static struct serial_state rs_table[] = {
148 /* UART CLK PORT IRQ FLAGS NUM */
149 { 0, 0, PROFF_SMC1, SIU_INT_SMC1, 0, 0 }, /* SMC1 ttyS0 */
151 { 0, 0, PROFF_SMC2, SIU_INT_SMC2, 0, 1 }, /* SMC2 ttyS1 */
153 #ifndef CONFIG_SCC1_ENET
154 { 0, 0, PROFF_SCC1, SIU_INT_SCC1, 0, SCC_NUM_BASE}, /* SCC1 ttyS2 */
156 #ifndef CONFIG_SCC2_ENET
157 { 0, 0, PROFF_SCC2, SIU_INT_SCC2, 0, SCC_NUM_BASE + 1}, /* SCC2 ttyS3 */
161 #else /* SCC_CONSOLE */
162 #define SCC_NUM_BASE 0 /* SCC base tty "number" */
163 #define SCC_IDX_BASE 0 /* table index */
164 static struct serial_state rs_table[] = {
165 /* UART CLK PORT IRQ FLAGS NUM */
166 { 0, 0, PROFF_SCC1, SIU_INT_SCC1, 0, SCC_NUM_BASE}, /* SCC1 ttyS2 */
167 { 0, 0, PROFF_SCC2, SIU_INT_SCC2, 0, SCC_NUM_BASE + 1}, /* SCC2 ttyS3 */
169 #endif /* SCC_CONSOLE */
171 #define PORT_NUM(P) (((P) < (SCC_NUM_BASE)) ? (P) : (P)-(SCC_NUM_BASE))
173 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
175 static struct tty_struct *serial_table[NR_PORTS];
176 static struct termios *serial_termios[NR_PORTS];
177 static struct termios *serial_termios_locked[NR_PORTS];
179 /* The number of buffer descriptors and their sizes.
181 #define RX_NUM_FIFO 4
182 #define RX_BUF_SIZE 32
183 #define TX_NUM_FIFO 4
184 #define TX_BUF_SIZE 32
187 #define MIN(a,b) ((a) < (b) ? (a) : (b))
190 /* The async_struct in serial.h does not really give us what we
191 * need, so define our own here.
193 typedef struct serial_info {
196 struct serial_state *state;
197 struct tty_struct *tty;
198 int read_status_mask;
199 int ignore_status_mask;
202 int x_char; /* xon/xoff character */
204 unsigned short closing_wait;
205 unsigned short closing_wait2;
207 unsigned long last_active;
208 int blocked_open; /* # of blocked opens */
209 long session; /* Session of opening process */
210 long pgrp; /* pgrp of opening process */
211 struct tq_struct tqueue;
212 struct tq_struct tqueue_hangup;
213 wait_queue_head_t open_wait;
214 wait_queue_head_t close_wait;
216 /* CPM Buffer Descriptor pointers.
224 static void change_speed(ser_info_t *info);
225 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout);
227 static inline int serial_paranoia_check(ser_info_t *info,
228 kdev_t device, const char *routine)
230 #ifdef SERIAL_PARANOIA_CHECK
231 static const char *badmagic =
232 "Warning: bad magic number for serial struct (%s) in %s\n";
233 static const char *badinfo =
234 "Warning: null async_struct for (%s) in %s\n";
237 printk(badinfo, kdevname(device), routine);
240 if (info->magic != SERIAL_MAGIC) {
241 printk(badmagic, kdevname(device), routine);
249 * This is used to figure out the divisor speeds and the timeouts,
250 * indexed by the termio value. The generic CPM functions are responsible
251 * for setting and assigning baud rate generators for us.
253 static int baud_table[] = {
254 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
255 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
259 * ------------------------------------------------------------
260 * rs_stop() and rs_start()
262 * This routines are called before setting or resetting tty->stopped.
263 * They enable or disable transmitter interrupts, as necessary.
264 * ------------------------------------------------------------
266 static void rs_8xx_stop(struct tty_struct *tty)
268 ser_info_t *info = (ser_info_t *)tty->driver_data;
271 volatile scc_t *sccp;
272 volatile smc_t *smcp;
274 if (serial_paranoia_check(info, tty->device, "rs_stop"))
277 save_flags(flags); cli();
278 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
279 smcp = &immr->im_smc[idx];
280 smcp->smc_smcm &= ~SMCM_TX;
283 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
284 sccp->scc_sccm &= ~UART_SCCM_TX;
286 restore_flags(flags);
289 static void rs_8xx_start(struct tty_struct *tty)
291 ser_info_t *info = (ser_info_t *)tty->driver_data;
294 volatile scc_t *sccp;
295 volatile smc_t *smcp;
297 if (serial_paranoia_check(info, tty->device, "rs_stop"))
300 save_flags(flags); cli();
301 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
302 smcp = &immr->im_smc[idx];
303 smcp->smc_smcm |= SMCM_TX;
306 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
307 sccp->scc_sccm |= UART_SCCM_TX;
309 restore_flags(flags);
313 * ----------------------------------------------------------------------
315 * Here starts the interrupt handling routines. All of the following
316 * subroutines are declared as inline and are folded into
317 * rs_interrupt(). They were separated out for readability's sake.
319 * Note: rs_interrupt() is a "fast" interrupt, which means that it
320 * runs with interrupts turned off. People who may want to modify
321 * rs_interrupt() should try to keep the interrupt handler as fast as
322 * possible. After you are done making modifications, it is not a bad
325 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
327 * and look at the resulting assemble code in serial.s.
329 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
330 * -----------------------------------------------------------------------
334 * This routine is used by the interrupt handler to schedule
335 * processing in the software interrupt portion of the driver.
337 static _INLINE_ void rs_sched_event(ser_info_t *info,
340 info->event |= 1 << event;
341 queue_task(&info->tqueue, &tq_serial);
345 static _INLINE_ void receive_chars(ser_info_t *info)
347 struct tty_struct *tty = info->tty;
348 unsigned char ch, *cp;
352 struct async_icount *icount;
355 icount = &info->state->icount;
357 /* Just loop through the closed BDs and copy the characters into
362 if (bdp->cbd_sc & BD_SC_EMPTY) /* If this one is empty */
363 break; /* we are all done */
365 /* The read status mask tell us what we should do with
366 * incoming characters, especially if errors occur.
367 * One special case is the use of BD_SC_EMPTY. If
368 * this is not set, we are supposed to be ignoring
369 * inputs. In this case, just mark the buffer empty and
371 if (!(info->read_status_mask & BD_SC_EMPTY)) {
372 bdp->cbd_sc |= BD_SC_EMPTY;
374 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
376 if (bdp->cbd_sc & BD_SC_WRAP)
377 bdp = info->rx_bd_base;
384 /* Get the number of characters and the buffer pointer.
387 cp = (unsigned char *)__va(bdp->cbd_bufaddr);
388 status = bdp->cbd_sc;
390 if (info->state->smc_scc_num == KGDB_SER_IDX) {
391 if (*cp == 0x03 || *cp == '$')
397 /* Check to see if there is room in the tty buffer for
398 * the characters in our BD buffer. If not, we exit
399 * now, leaving the BD with the characters. We'll pick
400 * them up again on the next receive interrupt (which could
403 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
408 *tty->flip.char_buf_ptr = ch;
411 #ifdef SERIAL_DEBUG_INTR
412 printk("DR%02x:%02x...", ch, *status);
414 *tty->flip.flag_buf_ptr = 0;
415 if (status & (BD_SC_BR | BD_SC_FR |
416 BD_SC_PR | BD_SC_OV)) {
418 * For statistics only
420 if (status & BD_SC_BR)
422 else if (status & BD_SC_PR)
424 else if (status & BD_SC_FR)
426 if (status & BD_SC_OV)
430 * Now check to see if character should be
431 * ignored, and mask off conditions which
433 if (status & info->ignore_status_mask) {
439 status &= info->read_status_mask;
441 if (status & (BD_SC_BR)) {
442 #ifdef SERIAL_DEBUG_INTR
443 printk("handling break....");
445 *tty->flip.flag_buf_ptr = TTY_BREAK;
446 if (info->flags & ASYNC_SAK)
448 } else if (status & BD_SC_PR)
449 *tty->flip.flag_buf_ptr = TTY_PARITY;
450 else if (status & BD_SC_FR)
451 *tty->flip.flag_buf_ptr = TTY_FRAME;
452 if (status & BD_SC_OV) {
454 * Overrun is special, since it's
455 * reported immediately, and doesn't
456 * affect the current character
458 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
460 tty->flip.flag_buf_ptr++;
461 tty->flip.char_buf_ptr++;
462 *tty->flip.flag_buf_ptr =
467 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
470 tty->flip.flag_buf_ptr++;
471 tty->flip.char_buf_ptr++;
475 /* This BD is ready to be used again. Clear status.
478 bdp->cbd_sc |= BD_SC_EMPTY;
479 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
481 if (bdp->cbd_sc & BD_SC_WRAP)
482 bdp = info->rx_bd_base;
487 info->rx_cur = (cbd_t *)bdp;
489 queue_task(&tty->flip.tqueue, &tq_timer);
492 static _INLINE_ void transmit_chars(ser_info_t *info)
495 if (info->flags & TX_WAKEUP) {
496 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
499 #ifdef SERIAL_DEBUG_INTR
505 /* I need to do this for the SCCs, so it is left as a reminder.
507 static _INLINE_ void check_modem_status(struct async_struct *info)
510 struct async_icount *icount;
512 status = serial_in(info, UART_MSR);
514 if (status & UART_MSR_ANY_DELTA) {
515 icount = &info->state->icount;
516 /* update input line counters */
517 if (status & UART_MSR_TERI)
519 if (status & UART_MSR_DDSR)
521 if (status & UART_MSR_DDCD) {
523 #ifdef CONFIG_HARD_PPS
524 if ((info->flags & ASYNC_HARDPPS_CD) &&
525 (status & UART_MSR_DCD))
529 if (status & UART_MSR_DCTS)
531 wake_up_interruptible(&info->delta_msr_wait);
534 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
535 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
536 printk("ttys%d CD now %s...", info->line,
537 (status & UART_MSR_DCD) ? "on" : "off");
539 if (status & UART_MSR_DCD)
540 wake_up_interruptible(&info->open_wait);
541 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
542 (info->flags & ASYNC_CALLOUT_NOHUP))) {
543 #ifdef SERIAL_DEBUG_OPEN
544 printk("scheduling hangup...");
547 if (schedule_task(&info->tqueue_hangup) == 0)
551 if (info->flags & ASYNC_CTS_FLOW) {
552 if (info->tty->hw_stopped) {
553 if (status & UART_MSR_CTS) {
554 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
555 printk("CTS tx start...");
557 info->tty->hw_stopped = 0;
558 info->IER |= UART_IER_THRI;
559 serial_out(info, UART_IER, info->IER);
560 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
564 if (!(status & UART_MSR_CTS)) {
565 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
566 printk("CTS tx stop...");
568 info->tty->hw_stopped = 1;
569 info->IER &= ~UART_IER_THRI;
570 serial_out(info, UART_IER, info->IER);
578 * This is the serial driver's interrupt routine for a single port
580 static void rs_8xx_interrupt(int irq, void * dev_id, struct pt_regs * regs)
585 volatile smc_t *smcp;
586 volatile scc_t *sccp;
588 info = (ser_info_t *)dev_id;
590 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
591 smcp = &immr->im_smc[idx];
592 events = smcp->smc_smce;
593 if (events & SMCM_RX)
595 if (events & SMCM_TX)
596 transmit_chars(info);
597 smcp->smc_smce = events;
600 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
601 events = sccp->scc_scce;
602 if (events & SCCM_RX)
604 if (events & SCCM_TX)
605 transmit_chars(info);
606 sccp->scc_scce = events;
609 #ifdef SERIAL_DEBUG_INTR
610 printk("rs_interrupt_single(%d, %x)...",
611 info->state->smc_scc_num, events);
614 check_modem_status(info);
616 info->last_active = jiffies;
617 #ifdef SERIAL_DEBUG_INTR
624 * -------------------------------------------------------------------
625 * Here ends the serial interrupt routines.
626 * -------------------------------------------------------------------
630 * This routine is used to handle the "bottom half" processing for the
631 * serial driver, known also the "software interrupt" processing.
632 * This processing is done at the kernel interrupt level, after the
633 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
634 * is where time-consuming activities which can not be done in the
635 * interrupt driver proper are done; the interrupt driver schedules
636 * them using rs_sched_event(), and they get done here.
638 static void do_serial_bh(void)
640 run_task_queue(&tq_serial);
643 static void do_softint(void *private_)
645 ser_info_t *info = (ser_info_t *) private_;
646 struct tty_struct *tty;
652 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
653 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
654 tty->ldisc.write_wakeup)
655 (tty->ldisc.write_wakeup)(tty);
656 wake_up_interruptible(&tty->write_wait);
661 * This routine is called from the scheduler tqueue when the interrupt
662 * routine has signalled that a hangup has occurred. The path of
663 * hangup processing is:
665 * serial interrupt routine -> (scheduler tqueue) ->
666 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
669 static void do_serial_hangup(void *private_)
671 struct async_struct *info = (struct async_struct *) private_;
672 struct tty_struct *tty;
680 /*static void rs_8xx_timer(void)
682 printk("rs_8xx_timer\n");
686 static int startup(ser_info_t *info)
691 struct serial_state *state= info->state;
692 volatile smc_t *smcp;
693 volatile scc_t *sccp;
694 volatile smc_uart_t *up;
695 volatile scc_uart_t *scup;
698 save_flags(flags); cli();
700 if (info->flags & ASYNC_INITIALIZED) {
705 if (!state->port || !state->type) {
707 set_bit(TTY_IO_ERROR, &info->tty->flags);
712 #ifdef SERIAL_DEBUG_OPEN
713 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
719 if (info->tty->termios->c_cflag & CBAUD)
720 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
724 clear_bit(TTY_IO_ERROR, &info->tty->flags);
727 * and set the speed of the serial port
731 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
732 smcp = &immr->im_smc[idx];
734 /* Enable interrupts and I/O.
736 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
737 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
739 /* We can tune the buffer length and idle characters
740 * to take advantage of the entire incoming buffer size.
741 * If mrblr is something other than 1, maxidl has to be
742 * non-zero or we never get an interrupt. The maxidl
743 * is the number of character times we wait after reception
744 * of the last character before we decide no more characters
747 up = (smc_uart_t *)&immr->im_dprambase[state->port];
749 up->smc_mrblr = 1; /* receive buffer length */
750 up->smc_maxidl = 0; /* wait forever for next char */
752 up->smc_mrblr = RX_BUF_SIZE;
753 up->smc_maxidl = RX_BUF_SIZE;
755 up->smc_brkcr = 1; /* number of break chars */
758 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
759 scup = (scc_uart_t *)&immr->im_dprambase[state->port];
761 scup->scc_genscc.scc_mrblr = 1; /* receive buffer length */
762 scup->scc_maxidl = 0; /* wait forever for next char */
764 scup->scc_genscc.scc_mrblr = RX_BUF_SIZE;
765 scup->scc_maxidl = RX_BUF_SIZE;
768 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
769 sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
772 info->flags |= ASYNC_INITIALIZED;
773 restore_flags(flags);
777 restore_flags(flags);
782 * This routine will shutdown a serial port; interrupts are disabled, and
783 * DTR is dropped if the hangup on close termio flag is on.
785 static void shutdown(ser_info_t * info)
788 struct serial_state *state;
790 volatile smc_t *smcp;
791 volatile scc_t *sccp;
793 if (!(info->flags & ASYNC_INITIALIZED))
798 #ifdef SERIAL_DEBUG_OPEN
799 printk("Shutting down serial port %d (irq %d)....", info->line,
803 save_flags(flags); cli(); /* Disable interrupts */
805 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
806 smcp = &immr->im_smc[idx];
808 /* Disable interrupts and I/O.
810 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
811 #ifdef CONFIG_SERIAL_CONSOLE
812 /* We can't disable the transmitter if this is the
815 if (idx != CONFIG_SERIAL_CONSOLE_PORT)
817 smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
820 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
821 sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
822 #ifdef CONFIG_SERIAL_CONSOLE
823 if (idx != CONFIG_SERIAL_CONSOLE_PORT)
824 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
829 set_bit(TTY_IO_ERROR, &info->tty->flags);
831 info->flags &= ~ASYNC_INITIALIZED;
832 restore_flags(flags);
836 * This routine is called to set the UART divisor registers to match
837 * the specified baud rate for a serial port.
839 static void change_speed(ser_info_t *info)
842 unsigned cflag, cval, scval, prev_mode;
843 int i, bits, sbits, idx;
845 volatile smc_t *smcp;
846 volatile scc_t *sccp;
848 if (!info->tty || !info->tty->termios)
850 cflag = info->tty->termios->c_cflag;
852 /* Character length programmed into the mode register is the
853 * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
854 * 1 or 2 stop bits, minus 1.
855 * The value 'bits' counts this for us.
860 /* byte size and parity */
861 switch (cflag & CSIZE) {
862 case CS5: bits = 5; break;
863 case CS6: bits = 6; break;
864 case CS7: bits = 7; break;
865 case CS8: bits = 8; break;
866 /* Never happens, but GCC is too dumb to figure it out */
867 default: bits = 8; break;
871 if (cflag & CSTOPB) {
872 cval |= SMCMR_SL; /* Two stops */
873 scval |= SCU_PMSR_SL;
876 if (cflag & PARENB) {
878 scval |= SCU_PMSR_PEN;
881 if (!(cflag & PARODD)) {
882 cval |= SMCMR_PM_EVEN;
883 scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
886 /* Determine divisor based on baud rate */
888 if (i >= (sizeof(baud_table)/sizeof(int)))
891 baud_rate = baud_table[i];
893 info->timeout = (TX_BUF_SIZE*HZ*bits);
894 info->timeout += HZ/50; /* Add .02 seconds of slop */
897 /* CTS flow control flag and modem status interrupts */
898 info->IER &= ~UART_IER_MSI;
899 if (info->flags & ASYNC_HARDPPS_CD)
900 info->IER |= UART_IER_MSI;
901 if (cflag & CRTSCTS) {
902 info->flags |= ASYNC_CTS_FLOW;
903 info->IER |= UART_IER_MSI;
905 info->flags &= ~ASYNC_CTS_FLOW;
907 info->flags &= ~ASYNC_CHECK_CD;
909 info->flags |= ASYNC_CHECK_CD;
910 info->IER |= UART_IER_MSI;
912 serial_out(info, UART_IER, info->IER);
916 * Set up parity check flag
918 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
920 info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
921 if (I_INPCK(info->tty))
922 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
923 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
924 info->read_status_mask |= BD_SC_BR;
927 * Characters to ignore
929 info->ignore_status_mask = 0;
930 if (I_IGNPAR(info->tty))
931 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
932 if (I_IGNBRK(info->tty)) {
933 info->ignore_status_mask |= BD_SC_BR;
935 * If we're ignore parity and break indicators, ignore
936 * overruns too. (For real raw support).
938 if (I_IGNPAR(info->tty))
939 info->ignore_status_mask |= BD_SC_OV;
942 * !!! ignore all characters if CREAD is not set
944 if ((cflag & CREAD) == 0)
945 info->read_status_mask &= ~BD_SC_EMPTY;
946 save_flags(flags); cli();
948 /* Start bit has not been added (so don't, because we would just
949 * subtract it later), and we need to add one for the number of
950 * stops bits (there is always at least one).
953 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
954 smcp = &immr->im_smc[idx];
956 /* Set the mode register. We want to keep a copy of the
957 * enables, because we want to put them back if they were
960 prev_mode = smcp->smc_smcmr & (SMCMR_REN | SMCMR_TEN);
961 smcp->smc_smcmr = smcr_mk_clen(bits) | cval | SMCMR_SM_UART
965 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
966 sccp->scc_pmsr = (sbits << 12) | scval;
969 m8260_cpm_setbrg(info->state->smc_scc_num, baud_rate);
971 restore_flags(flags);
974 static void rs_8xx_put_char(struct tty_struct *tty, unsigned char ch)
976 ser_info_t *info = (ser_info_t *)tty->driver_data;
979 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
986 while (bdp->cbd_sc & BD_SC_READY);
988 *((char *)__va(bdp->cbd_bufaddr)) = ch;
990 bdp->cbd_sc |= BD_SC_READY;
994 if (bdp->cbd_sc & BD_SC_WRAP)
995 bdp = info->tx_bd_base;
999 info->tx_cur = (cbd_t *)bdp;
1003 static int rs_8xx_write(struct tty_struct * tty, int from_user,
1004 const unsigned char *buf, int count)
1007 ser_info_t *info = (ser_info_t *)tty->driver_data;
1008 volatile cbd_t *bdp;
1010 if (serial_paranoia_check(info, tty->device, "rs_write"))
1019 c = MIN(count, TX_BUF_SIZE);
1024 if (bdp->cbd_sc & BD_SC_READY) {
1025 info->flags |= TX_WAKEUP;
1030 if (copy_from_user(__va(bdp->cbd_bufaddr), buf, c)) {
1036 memcpy(__va(bdp->cbd_bufaddr), buf, c);
1039 bdp->cbd_datlen = c;
1040 bdp->cbd_sc |= BD_SC_READY;
1048 if (bdp->cbd_sc & BD_SC_WRAP)
1049 bdp = info->tx_bd_base;
1052 info->tx_cur = (cbd_t *)bdp;
1057 static int rs_8xx_write_room(struct tty_struct *tty)
1059 ser_info_t *info = (ser_info_t *)tty->driver_data;
1062 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1065 if ((info->tx_cur->cbd_sc & BD_SC_READY) == 0) {
1066 info->flags &= ~TX_WAKEUP;
1070 info->flags |= TX_WAKEUP;
1076 /* I could track this with transmit counters....maybe later.
1078 static int rs_8xx_chars_in_buffer(struct tty_struct *tty)
1080 ser_info_t *info = (ser_info_t *)tty->driver_data;
1082 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1087 static void rs_8xx_flush_buffer(struct tty_struct *tty)
1089 ser_info_t *info = (ser_info_t *)tty->driver_data;
1091 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1094 /* There is nothing to "flush", whatever we gave the CPM
1095 * is on its way out.
1097 wake_up_interruptible(&tty->write_wait);
1098 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1099 tty->ldisc.write_wakeup)
1100 (tty->ldisc.write_wakeup)(tty);
1101 info->flags &= ~TX_WAKEUP;
1105 * This function is used to send a high-priority XON/XOFF character to
1108 static void rs_8xx_send_xchar(struct tty_struct *tty, char ch)
1110 volatile cbd_t *bdp;
1112 ser_info_t *info = (ser_info_t *)tty->driver_data;
1114 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1118 while (bdp->cbd_sc & BD_SC_READY);
1120 *((char *)__va(bdp->cbd_bufaddr)) = ch;
1121 bdp->cbd_datlen = 1;
1122 bdp->cbd_sc |= BD_SC_READY;
1126 if (bdp->cbd_sc & BD_SC_WRAP)
1127 bdp = info->tx_bd_base;
1131 info->tx_cur = (cbd_t *)bdp;
1135 * ------------------------------------------------------------
1138 * This routine is called by the upper-layer tty layer to signal that
1139 * incoming characters should be throttled.
1140 * ------------------------------------------------------------
1142 static void rs_8xx_throttle(struct tty_struct * tty)
1144 ser_info_t *info = (ser_info_t *)tty->driver_data;
1145 #ifdef SERIAL_DEBUG_THROTTLE
1148 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1149 tty->ldisc.chars_in_buffer(tty));
1152 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1156 rs_8xx_send_xchar(tty, STOP_CHAR(tty));
1158 #ifdef modem_control
1159 if (tty->termios->c_cflag & CRTSCTS)
1160 info->MCR &= ~UART_MCR_RTS;
1163 serial_out(info, UART_MCR, info->MCR);
1168 static void rs_8xx_unthrottle(struct tty_struct * tty)
1170 ser_info_t *info = (ser_info_t *)tty->driver_data;
1171 #ifdef SERIAL_DEBUG_THROTTLE
1174 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1175 tty->ldisc.chars_in_buffer(tty));
1178 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1185 rs_8xx_send_xchar(tty, START_CHAR(tty));
1187 #ifdef modem_control
1188 if (tty->termios->c_cflag & CRTSCTS)
1189 info->MCR |= UART_MCR_RTS;
1191 serial_out(info, UART_MCR, info->MCR);
1197 * ------------------------------------------------------------
1198 * rs_ioctl() and friends
1199 * ------------------------------------------------------------
1204 * get_lsr_info - get line status register info
1206 * Purpose: Let user call ioctl() to get info when the UART physically
1207 * is emptied. On bus types like RS485, the transmitter must
1208 * release the bus after transmitting. This must be done when
1209 * the transmit shift register is empty, not be done when the
1210 * transmit holding register is empty. This functionality
1211 * allows an RS485 driver to be written in user space.
1213 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1215 unsigned char status;
1216 unsigned int result;
1219 status = serial_in(info, UART_LSR);
1221 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1222 return put_user(result,value);
1226 static int get_modem_info(ser_info_t *info, unsigned int *value)
1228 unsigned int result = 0;
1229 #ifdef modem_control
1230 unsigned char control, status;
1232 control = info->MCR;
1234 status = serial_in(info, UART_MSR);
1236 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1237 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1239 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1240 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1242 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1243 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1244 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1245 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1247 return put_user(result,value);
1250 static int set_modem_info(ser_info_t *info, unsigned int cmd,
1251 unsigned int *value)
1256 error = get_user(arg, value);
1259 #ifdef modem_control
1262 if (arg & TIOCM_RTS)
1263 info->MCR |= UART_MCR_RTS;
1264 if (arg & TIOCM_DTR)
1265 info->MCR |= UART_MCR_DTR;
1267 if (arg & TIOCM_OUT1)
1268 info->MCR |= UART_MCR_OUT1;
1269 if (arg & TIOCM_OUT2)
1270 info->MCR |= UART_MCR_OUT2;
1274 if (arg & TIOCM_RTS)
1275 info->MCR &= ~UART_MCR_RTS;
1276 if (arg & TIOCM_DTR)
1277 info->MCR &= ~UART_MCR_DTR;
1279 if (arg & TIOCM_OUT1)
1280 info->MCR &= ~UART_MCR_OUT1;
1281 if (arg & TIOCM_OUT2)
1282 info->MCR &= ~UART_MCR_OUT2;
1286 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1292 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1294 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1295 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1297 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1303 serial_out(info, UART_MCR, info->MCR);
1309 /* Sending a break is a two step process on the SMC/SCC. It is accomplished
1310 * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1311 * command. We take advantage of the begin/end functions to make this
1314 static void begin_break(ser_info_t *info)
1316 volatile cpm8260_t *cp;
1322 if ((num = info->state->smc_scc_num) < SCC_NUM_BASE) {
1324 page = CPM_CR_SMC1_PAGE;
1325 sblock = CPM_CR_SMC1_SBLOCK;
1328 page = CPM_CR_SMC2_PAGE;
1329 sblock = CPM_CR_SMC2_SBLOCK;
1333 num -= SCC_NUM_BASE;
1336 page = CPM_CR_SCC1_PAGE;
1337 sblock = CPM_CR_SCC1_SBLOCK;
1340 page = CPM_CR_SCC2_PAGE;
1341 sblock = CPM_CR_SCC2_SBLOCK;
1344 page = CPM_CR_SCC3_PAGE;
1345 sblock = CPM_CR_SCC3_SBLOCK;
1348 page = CPM_CR_SCC4_PAGE;
1349 sblock = CPM_CR_SCC4_SBLOCK;
1354 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0, CPM_CR_STOP_TX) | CPM_CR_FLG;
1355 while (cp->cp_cpcr & CPM_CR_FLG);
1358 static void end_break(ser_info_t *info)
1360 volatile cpm8260_t *cp;
1366 if ((num = info->state->smc_scc_num) < SCC_NUM_BASE) {
1368 page = CPM_CR_SMC1_PAGE;
1369 sblock = CPM_CR_SMC1_SBLOCK;
1372 page = CPM_CR_SMC2_PAGE;
1373 sblock = CPM_CR_SMC2_SBLOCK;
1377 num -= SCC_NUM_BASE;
1380 page = CPM_CR_SCC1_PAGE;
1381 sblock = CPM_CR_SCC1_SBLOCK;
1384 page = CPM_CR_SCC2_PAGE;
1385 sblock = CPM_CR_SCC2_SBLOCK;
1388 page = CPM_CR_SCC3_PAGE;
1389 sblock = CPM_CR_SCC3_SBLOCK;
1392 page = CPM_CR_SCC4_PAGE;
1393 sblock = CPM_CR_SCC4_SBLOCK;
1398 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1399 while (cp->cp_cpcr & CPM_CR_FLG);
1403 * This routine sends a break character out the serial port.
1405 static void send_break(ser_info_t *info, int duration)
1407 current->state = TASK_INTERRUPTIBLE;
1408 #ifdef SERIAL_DEBUG_SEND_BREAK
1409 printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1412 schedule_timeout(duration);
1414 #ifdef SERIAL_DEBUG_SEND_BREAK
1415 printk("done jiffies=%lu\n", jiffies);
1420 static int rs_8xx_ioctl(struct tty_struct *tty, struct file * file,
1421 unsigned int cmd, unsigned long arg)
1424 ser_info_t *info = (ser_info_t *)tty->driver_data;
1426 struct async_icount cnow; /* kernel counter temps */
1427 struct serial_icounter_struct *p_cuser; /* user space */
1429 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1432 if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1433 if (tty->flags & (1 << TTY_IO_ERROR))
1438 case TCSBRK: /* SVID version: non-zero arg --> no break */
1439 retval = tty_check_change(tty);
1442 tty_wait_until_sent(tty, 0);
1443 if (signal_pending(current))
1446 send_break(info, HZ/4); /* 1/4 second */
1447 if (signal_pending(current))
1451 case TCSBRKP: /* support for POSIX tcsendbreak() */
1452 retval = tty_check_change(tty);
1455 tty_wait_until_sent(tty, 0);
1456 if (signal_pending(current))
1458 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1459 if (signal_pending(current))
1463 retval = tty_check_change(tty);
1466 tty_wait_until_sent(tty, 0);
1470 retval = tty_check_change(tty);
1476 return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1478 error = get_user(arg, (unsigned int *) arg);
1481 tty->termios->c_cflag =
1482 ((tty->termios->c_cflag & ~CLOCAL) |
1483 (arg ? CLOCAL : 0));
1486 return get_modem_info(info, (unsigned int *) arg);
1490 return set_modem_info(info, cmd, (unsigned int *) arg);
1492 case TIOCSERGETLSR: /* Get line status register */
1493 return get_lsr_info(info, (unsigned int *) arg);
1496 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1497 * - mask passed in arg for lines of interest
1498 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1499 * Caller should use TIOCGICOUNT to see which one it was
1502 #ifdef modem_control
1504 /* note the counters on entry */
1505 cprev = info->state->icount;
1508 interruptible_sleep_on(&info->delta_msr_wait);
1509 /* see if a signal did it */
1510 if (signal_pending(current))
1511 return -ERESTARTSYS;
1513 cnow = info->state->icount; /* atomic copy */
1515 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1516 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1517 return -EIO; /* no change => error */
1518 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1519 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1520 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1521 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1532 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1533 * Return: write counters to the user passed counter struct
1534 * NB: both 1->0 and 0->1 transitions are counted except for
1535 * RI where only 0->1 is counted.
1539 cnow = info->state->icount;
1541 p_cuser = (struct serial_icounter_struct *) arg;
1542 error = put_user(cnow.cts, &p_cuser->cts);
1543 if (error) return error;
1544 error = put_user(cnow.dsr, &p_cuser->dsr);
1545 if (error) return error;
1546 error = put_user(cnow.rng, &p_cuser->rng);
1547 if (error) return error;
1548 error = put_user(cnow.dcd, &p_cuser->dcd);
1549 if (error) return error;
1553 return -ENOIOCTLCMD;
1558 /* FIX UP modem control here someday......
1560 static void rs_8xx_set_termios(struct tty_struct *tty, struct termios *old_termios)
1562 ser_info_t *info = (ser_info_t *)tty->driver_data;
1564 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1565 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1566 == RELEVANT_IFLAG(old_termios->c_iflag)))
1571 #ifdef modem_control
1572 /* Handle transition to B0 status */
1573 if ((old_termios->c_cflag & CBAUD) &&
1574 !(tty->termios->c_cflag & CBAUD)) {
1575 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1577 serial_out(info, UART_MCR, info->MCR);
1581 /* Handle transition away from B0 status */
1582 if (!(old_termios->c_cflag & CBAUD) &&
1583 (tty->termios->c_cflag & CBAUD)) {
1584 info->MCR |= UART_MCR_DTR;
1585 if (!tty->hw_stopped ||
1586 !(tty->termios->c_cflag & CRTSCTS)) {
1587 info->MCR |= UART_MCR_RTS;
1590 serial_out(info, UART_MCR, info->MCR);
1594 /* Handle turning off CRTSCTS */
1595 if ((old_termios->c_cflag & CRTSCTS) &&
1596 !(tty->termios->c_cflag & CRTSCTS)) {
1597 tty->hw_stopped = 0;
1604 * No need to wake up processes in open wait, since they
1605 * sample the CLOCAL flag once, and don't recheck it.
1606 * XXX It's not clear whether the current behavior is correct
1607 * or not. Hence, this may change.....
1609 if (!(old_termios->c_cflag & CLOCAL) &&
1610 (tty->termios->c_cflag & CLOCAL))
1611 wake_up_interruptible(&info->open_wait);
1616 * ------------------------------------------------------------
1619 * This routine is called when the serial port gets closed. First, we
1620 * wait for the last remaining data to be sent. Then, we unlink its
1621 * async structure from the interrupt chain if necessary, and we free
1622 * that IRQ if nothing is left in the chain.
1623 * ------------------------------------------------------------
1625 static void rs_8xx_close(struct tty_struct *tty, struct file * filp)
1627 ser_info_t *info = (ser_info_t *)tty->driver_data;
1628 struct serial_state *state;
1629 unsigned long flags;
1631 volatile smc_t *smcp;
1632 volatile scc_t *sccp;
1634 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1637 state = info->state;
1639 save_flags(flags); cli();
1641 if (tty_hung_up_p(filp)) {
1642 DBG_CNT("before DEC-hung");
1644 restore_flags(flags);
1648 #ifdef SERIAL_DEBUG_OPEN
1649 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1651 if ((tty->count == 1) && (state->count != 1)) {
1653 * Uh, oh. tty->count is 1, which means that the tty
1654 * structure will be freed. state->count should always
1655 * be one in these conditions. If it's greater than
1656 * one, we've got real problems, since it means the
1657 * serial port won't be shutdown.
1659 printk("rs_close: bad serial port count; tty->count is 1, "
1660 "state->count is %d\n", state->count);
1663 if (--state->count < 0) {
1664 printk("rs_close: bad serial port count for ttys%d: %d\n",
1665 info->line, state->count);
1669 DBG_CNT("before DEC-2");
1671 restore_flags(flags);
1674 info->flags |= ASYNC_CLOSING;
1676 * Save the termios structure, since this port may have
1677 * separate termios for callout and dialin.
1679 if (info->flags & ASYNC_NORMAL_ACTIVE)
1680 info->state->normal_termios = *tty->termios;
1681 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1682 info->state->callout_termios = *tty->termios;
1684 * Now we wait for the transmit buffer to clear; and we notify
1685 * the line discipline to only process XON/XOFF characters.
1688 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1689 tty_wait_until_sent(tty, info->closing_wait);
1691 * At this point we stop accepting input. To do this, we
1692 * disable the receive line status interrupts, and tell the
1693 * interrupt driver to stop checking the data ready bit in the
1694 * line status register.
1696 info->read_status_mask &= ~BD_SC_EMPTY;
1697 if (info->flags & ASYNC_INITIALIZED) {
1698 if ((idx = info->state->smc_scc_num) < SCC_NUM_BASE) {
1699 smcp = &immr->im_smc[idx];
1700 smcp->smc_smcm &= ~SMCM_RX;
1701 smcp->smc_smcmr &= ~SMCMR_REN;
1704 sccp = &immr->im_scc[idx - SCC_IDX_BASE];
1705 sccp->scc_sccm &= ~UART_SCCM_RX;
1706 sccp->scc_gsmrl &= ~SCC_GSMRL_ENR;
1709 * Before we drop DTR, make sure the UART transmitter
1710 * has completely drained; this is especially
1711 * important if there is a transmit FIFO!
1713 rs_8xx_wait_until_sent(tty, info->timeout);
1716 if (tty->driver.flush_buffer)
1717 tty->driver.flush_buffer(tty);
1718 if (tty->ldisc.flush_buffer)
1719 tty->ldisc.flush_buffer(tty);
1723 if (info->blocked_open) {
1724 if (info->close_delay) {
1725 current->state = TASK_INTERRUPTIBLE;
1726 schedule_timeout(info->close_delay);
1728 wake_up_interruptible(&info->open_wait);
1730 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1732 wake_up_interruptible(&info->close_wait);
1734 restore_flags(flags);
1738 * rs_wait_until_sent() --- wait until the transmitter is empty
1740 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout)
1742 ser_info_t *info = (ser_info_t *)tty->driver_data;
1743 unsigned long orig_jiffies, char_time;
1745 volatile cbd_t *bdp;
1747 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1751 if (info->state->type == PORT_UNKNOWN)
1755 orig_jiffies = jiffies;
1757 * Set the check interval to be 1/5 of the estimated time to
1758 * send a single character, and make it at least 1. The check
1759 * interval should also be less than the timeout.
1761 * Note: we have to use pretty tight timings here to satisfy
1766 char_time = MIN(char_time, timeout);
1767 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1768 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1769 printk("jiff=%lu...", jiffies);
1772 /* We go through the loop at least once because we can't tell
1773 * exactly when the last character exits the shifter. There can
1774 * be at least two characters waiting to be sent after the buffers
1778 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1779 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1781 current->state = TASK_INTERRUPTIBLE;
1782 /* current->counter = 0; make us low-priority */
1783 schedule_timeout(char_time);
1784 if (signal_pending(current))
1786 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1789 } while (bdp->cbd_sc & BD_SC_READY);
1790 current->state = TASK_RUNNING;
1791 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1792 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1797 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1799 static void rs_8xx_hangup(struct tty_struct *tty)
1801 ser_info_t *info = (ser_info_t *)tty->driver_data;
1802 struct serial_state *state = info->state;
1804 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1807 state = info->state;
1809 rs_8xx_flush_buffer(tty);
1813 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1815 wake_up_interruptible(&info->open_wait);
1819 * ------------------------------------------------------------
1820 * rs_open() and friends
1821 * ------------------------------------------------------------
1823 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1826 #ifdef DO_THIS_LATER
1827 DECLARE_WAITQUEUE(wait, current);
1829 struct serial_state *state = info->state;
1834 * If the device is in the middle of being closed, then block
1835 * until it's done, and then try again.
1837 if (tty_hung_up_p(filp) ||
1838 (info->flags & ASYNC_CLOSING)) {
1839 if (info->flags & ASYNC_CLOSING)
1840 interruptible_sleep_on(&info->close_wait);
1841 #ifdef SERIAL_DO_RESTART
1842 if (info->flags & ASYNC_HUP_NOTIFY)
1845 return -ERESTARTSYS;
1852 * If this is a callout device, then just make sure the normal
1853 * device isn't being used.
1855 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1856 if (info->flags & ASYNC_NORMAL_ACTIVE)
1858 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1859 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1860 (info->session != current->session))
1862 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1863 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1864 (info->pgrp != current->pgrp))
1866 info->flags |= ASYNC_CALLOUT_ACTIVE;
1871 * If non-blocking mode is set, or the port is not enabled,
1872 * then make the check up front and then exit.
1873 * If this is an SMC port, we don't have modem control to wait
1874 * for, so just get out here.
1876 if ((filp->f_flags & O_NONBLOCK) ||
1877 (tty->flags & (1 << TTY_IO_ERROR)) ||
1878 (info->state->smc_scc_num < SCC_NUM_BASE)) {
1879 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1881 info->flags |= ASYNC_NORMAL_ACTIVE;
1885 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1886 if (state->normal_termios.c_cflag & CLOCAL)
1889 if (tty->termios->c_cflag & CLOCAL)
1894 * Block waiting for the carrier detect and the line to become
1895 * free (i.e., not in use by the callout). While we are in
1896 * this loop, state->count is dropped by one, so that
1897 * rs_close() knows when to free things. We restore it upon
1898 * exit, either normal or abnormal.
1901 #ifdef DO_THIS_LATER
1902 add_wait_queue(&info->open_wait, &wait);
1903 #ifdef SERIAL_DEBUG_OPEN
1904 printk("block_til_ready before block: ttys%d, count = %d\n",
1905 state->line, state->count);
1908 if (!tty_hung_up_p(filp))
1911 info->blocked_open++;
1914 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1915 (tty->termios->c_cflag & CBAUD))
1916 serial_out(info, UART_MCR,
1917 serial_inp(info, UART_MCR) |
1918 (UART_MCR_DTR | UART_MCR_RTS));
1920 set_current_state(TASK_INTERRUPTIBLE);
1921 if (tty_hung_up_p(filp) ||
1922 !(info->flags & ASYNC_INITIALIZED)) {
1923 #ifdef SERIAL_DO_RESTART
1924 if (info->flags & ASYNC_HUP_NOTIFY)
1927 retval = -ERESTARTSYS;
1933 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1934 !(info->flags & ASYNC_CLOSING) &&
1935 (do_clocal || (serial_in(info, UART_MSR) &
1938 if (signal_pending(current)) {
1939 retval = -ERESTARTSYS;
1942 #ifdef SERIAL_DEBUG_OPEN
1943 printk("block_til_ready blocking: ttys%d, count = %d\n",
1944 info->line, state->count);
1948 current->state = TASK_RUNNING;
1949 remove_wait_queue(&info->open_wait, &wait);
1950 if (!tty_hung_up_p(filp))
1952 info->blocked_open--;
1953 #ifdef SERIAL_DEBUG_OPEN
1954 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1955 info->line, state->count);
1957 #endif /* DO_THIS_LATER */
1960 info->flags |= ASYNC_NORMAL_ACTIVE;
1964 static int get_async_struct(int line, ser_info_t **ret_info)
1966 struct serial_state *sstate;
1968 sstate = rs_table + line;
1971 *ret_info = (ser_info_t *)sstate->info;
1980 * This routine is called whenever a serial port is opened. It
1981 * enables interrupts for a serial port, linking in its async structure into
1982 * the IRQ chain. It also performs the serial-specific
1983 * initialization for the tty structure.
1985 static int rs_8xx_open(struct tty_struct *tty, struct file * filp)
1990 line = MINOR(tty->device) - tty->driver.minor_start;
1991 if ((line < 0) || (line >= NR_PORTS))
1993 retval = get_async_struct(line, &info);
1996 if (serial_paranoia_check(info, tty->device, "rs_open"))
1999 #ifdef SERIAL_DEBUG_OPEN
2000 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2001 info->state->count);
2003 tty->driver_data = info;
2007 * Start up serial port
2009 retval = startup(info);
2014 retval = block_til_ready(tty, filp, info);
2016 #ifdef SERIAL_DEBUG_OPEN
2017 printk("rs_open returning after block_til_ready with %d\n",
2023 if ((info->state->count == 1) &&
2024 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2025 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2026 *tty->termios = info->state->normal_termios;
2028 *tty->termios = info->state->callout_termios;
2032 info->session = current->session;
2033 info->pgrp = current->pgrp;
2035 #ifdef SERIAL_DEBUG_OPEN
2036 printk("rs_open ttys%d successful...", info->line);
2042 * /proc fs routines....
2045 static int inline line_info(char *buf, struct serial_state *state)
2048 struct async_struct *info = state->info, scr_info;
2049 char stat_buf[30], control, status;
2053 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2055 (state->smc_scc_num < SCC_NUM_BASE) ? "SMC" : "SCC",
2056 (unsigned int)(state->port), state->irq);
2058 if (!state->port || (state->type == PORT_UNKNOWN)) {
2059 ret += sprintf(buf+ret, "\n");
2065 * Figure out the current RS-232 lines
2068 info = &scr_info; /* This is just for serial_{in,out} */
2070 info->magic = SERIAL_MAGIC;
2071 info->port = state->port;
2072 info->flags = state->flags;
2077 status = serial_in(info, UART_MSR);
2078 control = info ? info->MCR : serial_in(info, UART_MCR);
2083 if (control & UART_MCR_RTS)
2084 strcat(stat_buf, "|RTS");
2085 if (status & UART_MSR_CTS)
2086 strcat(stat_buf, "|CTS");
2087 if (control & UART_MCR_DTR)
2088 strcat(stat_buf, "|DTR");
2089 if (status & UART_MSR_DSR)
2090 strcat(stat_buf, "|DSR");
2091 if (status & UART_MSR_DCD)
2092 strcat(stat_buf, "|CD");
2093 if (status & UART_MSR_RI)
2094 strcat(stat_buf, "|RI");
2097 ret += sprintf(buf+ret, " baud:%d",
2098 state->baud_base / info->quot);
2101 ret += sprintf(buf+ret, " tx:%d rx:%d",
2102 state->icount.tx, state->icount.rx);
2104 if (state->icount.frame)
2105 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2107 if (state->icount.parity)
2108 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2110 if (state->icount.brk)
2111 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2113 if (state->icount.overrun)
2114 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2117 * Last thing is the RS-232 status lines
2119 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2124 int rs_8xx_read_proc(char *page, char **start, off_t off, int count,
2125 int *eof, void *data)
2130 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2131 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2132 len += line_info(page + len, &rs_table[i]);
2133 if (len+begin > off+count)
2135 if (len+begin < off) {
2142 if (off >= len+begin)
2144 *start = page + (begin-off);
2145 return ((count < begin+len-off) ? count : begin+len-off);
2149 * ---------------------------------------------------------------------
2150 * rs_init() and friends
2152 * rs_init() is called at boot-time to initialize the serial driver.
2153 * ---------------------------------------------------------------------
2157 * This routine prints out the appropriate serial driver version
2158 * number, and identifies which options were configured into this
2161 static _INLINE_ void show_serial_version(void)
2163 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2168 * The serial console driver used during boot. Note that these names
2169 * clash with those found in "serial.c", so we currently can't support
2170 * the 16xxx uarts and these at the same time. I will fix this to become
2171 * an indirect function call from tty_io.c (or something).
2174 #ifdef CONFIG_SERIAL_CONSOLE
2177 * Print a string to the serial port trying not to disturb any possible
2178 * real use of the port...
2179 * These funcitons work equally well for SCC, even though they are
2180 * designed for SMC. Our only interests are the transmit/receive
2181 * buffers, which are identically mapped for either the SCC or SMC.
2183 static void my_console_write(int idx, const char *s,
2186 struct serial_state *ser;
2189 volatile cbd_t *bdp, *bdbase;
2190 volatile smc_uart_t *up;
2191 volatile u_char *cp;
2193 ser = rs_table + idx;
2195 /* If the port has been initialized for general use, we have
2196 * to use the buffer descriptors allocated there. Otherwise,
2197 * we simply use the single buffer allocated.
2199 if ((info = (ser_info_t *)ser->info) != NULL) {
2201 bdbase = info->tx_bd_base;
2204 /* Pointer to UART in parameter ram.
2206 up = (smc_uart_t *)&immr->im_dprambase[ser->port];
2208 /* Get the address of the host memory buffer.
2210 bdp = bdbase = (cbd_t *)&immr->im_dprambase[up->smc_tbase];
2214 * We need to gracefully shut down the transmitter, disable
2215 * interrupts, then send our bytes out.
2219 * Now, do each character. This is not as bad as it looks
2220 * since this is a holding FIFO and not a transmitting FIFO.
2221 * We could add the complexity of filling the entire transmit
2222 * buffer, but we would just wait longer between accesses......
2224 for (i = 0; i < count; i++, s++) {
2225 /* Wait for transmitter fifo to empty.
2226 * Ready indicates output is ready, and xmt is doing
2227 * that, not that it is ready for us to send.
2229 while (bdp->cbd_sc & BD_SC_READY);
2231 /* Send the character out.
2232 * If the buffer address is in the CPM DPRAM, don't
2235 if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2236 cp = (u_char *)(bdp->cbd_bufaddr);
2238 cp = __va(bdp->cbd_bufaddr);
2241 bdp->cbd_datlen = 1;
2242 bdp->cbd_sc |= BD_SC_READY;
2244 if (bdp->cbd_sc & BD_SC_WRAP)
2249 /* if a LF, also do CR... */
2251 while (bdp->cbd_sc & BD_SC_READY);
2252 cp = __va(bdp->cbd_bufaddr);
2254 bdp->cbd_datlen = 1;
2255 bdp->cbd_sc |= BD_SC_READY;
2257 if (bdp->cbd_sc & BD_SC_WRAP) {
2267 * Finally, Wait for transmitter & holding register to empty
2268 * and restore the IER
2270 while (bdp->cbd_sc & BD_SC_READY);
2273 info->tx_cur = (cbd_t *)bdp;
2276 static void serial_console_write(struct console *c, const char *s,
2279 #if defined(CONFIG_KGDB) && !defined(CONFIG_USE_SERIAL2_KGDB)
2280 /* Try to let stub handle output. Returns true if it did. */
2281 if (kgdb_output_string(s, count))
2284 my_console_write(c->index, s, count);
2289 xmon_8xx_write(const char *s, unsigned count)
2291 my_console_write(KGDB_SER_IDX, s, count);
2298 putDebugChar(char ch)
2300 my_console_write(KGDB_SER_IDX, &ch, 1);
2304 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON)
2306 * Receive character from the serial port. This only works well
2307 * before the port is initialize for real use.
2309 static int my_console_wait_key(int idx, int xmon, char *obuf)
2311 struct serial_state *ser;
2314 volatile cbd_t *bdp;
2315 volatile smc_uart_t *up;
2318 ser = rs_table + idx;
2320 /* Pointer to UART in parameter ram.
2322 up = (smc_uart_t *)&immr->im_dprambase[ser->port];
2324 /* Get the address of the host memory buffer.
2325 * If the port has been initialized for general use, we must
2326 * use information from the port structure.
2328 if ((info = (ser_info_t *)ser->info))
2331 bdp = (cbd_t *)&immr->im_dprambase[up->smc_rbase];
2334 * We need to gracefully shut down the receiver, disable
2335 * interrupts, then read the input.
2336 * XMON just wants a poll. If no character, return -1, else
2337 * return the character.
2340 while (bdp->cbd_sc & BD_SC_EMPTY);
2343 if (bdp->cbd_sc & BD_SC_EMPTY)
2347 /* If the buffer address is in the CPM DPRAM, don't
2350 if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2351 cp = (u_char *)(bdp->cbd_bufaddr);
2353 cp = __va(bdp->cbd_bufaddr);
2356 i = c = bdp->cbd_datlen;
2363 bdp->cbd_sc |= BD_SC_EMPTY;
2366 if (bdp->cbd_sc & BD_SC_WRAP) {
2367 bdp = info->rx_bd_base;
2372 info->rx_cur = (cbd_t *)bdp;
2381 xmon_8xx_read_poll(void)
2383 return(my_console_wait_key(KGDB_SER_IDX, 1, NULL));
2387 xmon_8xx_read_char(void)
2389 return(my_console_wait_key(KGDB_SER_IDX, 0, NULL));
2394 static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2395 static int kgdb_chars;
2400 if (kgdb_chars <= 0) {
2401 kgdb_chars = my_console_wait_key(KGDB_SER_IDX, 0, kgdb_buf);
2409 void kgdb_interruptible(int yes)
2411 volatile smc_t *smcp;
2413 smcp = &immr->im_smc[KGDB_SER_IDX];
2416 smcp->smc_smcm |= SMCM_RX;
2418 smcp->smc_smcm &= ~SMCM_RX;
2421 void kgdb_map_scc(void)
2425 volatile cbd_t *bdp;
2426 volatile smc_uart_t *up;
2428 /* The serial port has already been initialized before
2429 * we get here. We have to assign some pointers needed by
2430 * the kernel, and grab a memory location in the CPM that will
2431 * work until the driver is really initialized.
2433 immr = (immap_t *)IMAP_ADDR;
2435 /* Right now, assume we are using SMCs.
2437 #ifdef USE_KGDB_SMC2
2438 *(ushort *)(&immr->im_dprambase[PROFF_SMC2_BASE]) = serbase = PROFF_SMC2;
2440 *(ushort *)(&immr->im_dprambase[PROFF_SMC1_BASE]) = serbase = PROFF_SMC1;
2442 up = (smc_uart_t *)&immr->im_dprambase[serbase];
2444 /* Allocate space for an input FIFO, plus a few bytes for output.
2445 * Allocate bytes to maintain word alignment.
2447 mem_addr = (uint)(&immr->im_dprambase[0x1000]);
2449 /* Set the physical address of the host memory buffers in
2450 * the buffer descriptors.
2452 bdp = (cbd_t *)&immr->im_dprambase[up->smc_rbase];
2453 bdp->cbd_bufaddr = mem_addr;
2455 bdp = (cbd_t *)&immr->im_dprambase[up->smc_tbase];
2456 bdp->cbd_bufaddr = mem_addr+RX_BUF_SIZE;
2458 up->smc_mrblr = RX_BUF_SIZE; /* receive buffer length */
2459 up->smc_maxidl = RX_BUF_SIZE;
2463 static kdev_t serial_console_device(struct console *c)
2465 return MKDEV(TTY_MAJOR, 64 + c->index);
2469 static struct console sercons = {
2471 write: serial_console_write,
2472 device: serial_console_device,
2473 setup: serial_console_setup,
2474 flags: CON_PRINTBUFFER,
2475 index: CONFIG_SERIAL_CONSOLE_PORT,
2481 long __init console_8xx_init(long kmem_start, long kmem_end)
2483 register_console(&sercons);
2489 /* Default console baud rate as determined by the board information
2492 static int baud_idx;
2495 * The serial driver boot-time initialization code!
2497 int __init rs_8xx_init(void)
2499 struct serial_state * state;
2501 uint mem_addr, dp_addr;
2504 volatile cbd_t *bdp;
2505 volatile cpm8260_t *cp;
2507 volatile smc_uart_t *up;
2508 volatile scc_t *scp;
2509 volatile scc_uart_t *sup;
2510 volatile immap_t *immap;
2511 volatile iop8260_t *io;
2513 init_bh(SERIAL_BH, do_serial_bh);
2515 show_serial_version();
2517 /* Initialize the tty_driver structure */
2519 /*memset(&serial_driver, 0, sizeof(struct tty_driver));*/
2520 __clear_user(&serial_driver,sizeof(struct tty_driver));
2521 serial_driver.magic = TTY_DRIVER_MAGIC;
2522 serial_driver.driver_name = "serial";
2523 #ifdef CONFIG_DEVFS_FS
2524 serial_driver.name = "tts/%d";
2526 serial_driver.name = "ttyS";
2528 serial_driver.major = TTY_MAJOR;
2529 serial_driver.minor_start = 64;
2530 serial_driver.num = NR_PORTS;
2531 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2532 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2533 serial_driver.init_termios = tty_std_termios;
2534 serial_driver.init_termios.c_cflag =
2535 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2536 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2537 serial_driver.refcount = &serial_refcount;
2538 serial_driver.table = serial_table;
2539 serial_driver.termios = serial_termios;
2540 serial_driver.termios_locked = serial_termios_locked;
2542 serial_driver.open = rs_8xx_open;
2543 serial_driver.close = rs_8xx_close;
2544 serial_driver.write = rs_8xx_write;
2545 serial_driver.put_char = rs_8xx_put_char;
2546 serial_driver.write_room = rs_8xx_write_room;
2547 serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer;
2548 serial_driver.flush_buffer = rs_8xx_flush_buffer;
2549 serial_driver.ioctl = rs_8xx_ioctl;
2550 serial_driver.throttle = rs_8xx_throttle;
2551 serial_driver.unthrottle = rs_8xx_unthrottle;
2552 serial_driver.send_xchar = rs_8xx_send_xchar;
2553 serial_driver.set_termios = rs_8xx_set_termios;
2554 serial_driver.stop = rs_8xx_stop;
2555 serial_driver.start = rs_8xx_start;
2556 serial_driver.hangup = rs_8xx_hangup;
2557 serial_driver.wait_until_sent = rs_8xx_wait_until_sent;
2558 serial_driver.read_proc = rs_8xx_read_proc;
2561 * The callout device is just like normal device except for
2562 * major number and the subtype code.
2564 callout_driver = serial_driver;
2565 #ifdef CONFIG_DEVFS_FS
2566 callout_driver.name = "cua/%d";
2568 callout_driver.name = "cua";
2570 callout_driver.major = TTYAUX_MAJOR;
2571 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2572 callout_driver.read_proc = 0;
2573 callout_driver.proc_entry = 0;
2575 if (tty_register_driver(&serial_driver))
2576 panic("Couldn't register serial driver\n");
2577 if (tty_register_driver(&callout_driver))
2578 panic("Couldn't register callout driver\n");
2581 cp = &immap->im_cpm;
2582 io = &immap->im_ioport;
2584 /* This should have been done long ago by the early boot code,
2585 * but do it again to make sure.
2587 *(ushort *)(&immap->im_dprambase[PROFF_SMC1_BASE]) = PROFF_SMC1;
2588 *(ushort *)(&immap->im_dprambase[PROFF_SMC2_BASE]) = PROFF_SMC2;
2590 /* Geeze, here we go....Picking I/O port bits....Lots of
2591 * choices. If you don't like mine, pick your own.
2592 * Configure SMCs Tx/Rx. SMC1 is only on Port D, SMC2 is
2593 * only on Port A. You either pick 'em, or not.
2596 io->iop_ppard |= 0x00c00000;
2597 io->iop_pdird |= 0x00400000;
2598 io->iop_pdird &= ~0x00800000;
2599 io->iop_psord &= ~0x00c00000;
2601 io->iop_ppara |= 0x00c00000;
2602 io->iop_pdira |= 0x00400000;
2603 io->iop_pdira &= ~0x00800000;
2604 io->iop_psora &= ~0x00c00000;
2607 /* Configure SCC2 and SCC3. Be careful about the fine print.
2608 * Secondary options are only available when you take away
2609 * the primary option. Unless the pins are used for something
2610 * else, SCC2 and SCC3 are on Port B.
2611 * Port B, 8 - SCC3 TxD
2612 * Port B, 12 - SCC2 TxD
2613 * Port B, 14 - SCC3 RxD
2614 * Port B, 15 - SCC2 RxD
2616 io->iop_pparb |= 0x008b0000;
2617 io->iop_pdirb |= 0x00880000;
2618 io->iop_psorb |= 0x00880000;
2619 io->iop_pdirb &= ~0x00030000;
2620 io->iop_psorb &= ~0x00030000;
2622 /* Wire BRG1 to SMC1 and BRG2 to SMC2.
2624 immap->im_cpmux.cmx_smr = 0;
2626 /* Connect SCC2 and SCC3 to NMSI. Connect BRG3 to SCC2 and
2629 immap->im_cpmux.cmx_scr &= ~0x00ffff00;
2630 immap->im_cpmux.cmx_scr |= 0x00121b00;
2632 io->iop_pparb |= 0x008b0000;
2633 io->iop_pdirb |= 0x00880000;
2634 io->iop_psorb |= 0x00880000;
2635 io->iop_pdirb &= ~0x00030000;
2636 io->iop_psorb &= ~0x00030000;
2638 /* Use Port D for SCC1 instead of other functions.
2640 io->iop_ppard |= 0x00000003;
2641 io->iop_psord &= ~0x00000001; /* Rx */
2642 io->iop_psord |= 0x00000002; /* Tx */
2643 io->iop_pdird &= ~0x00000001; /* Rx */
2644 io->iop_pdird |= 0x00000002; /* Tx */
2646 /* Connect SCC1, SCC2, SCC3 to NMSI. Connect BRG1 to SCC1,
2647 * BRG2 to SCC2, BRG3 to SCC3.
2649 immap->im_cpmux.cmx_scr &= ~0xffffff00;
2650 immap->im_cpmux.cmx_scr |= 0x00091200;
2653 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2654 state->magic = SSTATE_MAGIC;
2656 state->type = PORT_UNKNOWN;
2657 state->custom_divisor = 0;
2658 state->close_delay = 5*HZ/10;
2659 state->closing_wait = 30*HZ;
2660 state->callout_termios = callout_driver.init_termios;
2661 state->normal_termios = serial_driver.init_termios;
2662 state->icount.cts = state->icount.dsr =
2663 state->icount.rng = state->icount.dcd = 0;
2664 state->icount.rx = state->icount.tx = 0;
2665 state->icount.frame = state->icount.parity = 0;
2666 state->icount.overrun = state->icount.brk = 0;
2667 printk (KERN_INFO "ttyS%d on %s%d at 0x%04x, BRG%d\n",
2669 (state->smc_scc_num < SCC_NUM_BASE) ? "SMC" : "SCC",
2670 PORT_NUM(state->smc_scc_num) + 1,
2671 (unsigned int)(state->port),
2672 state->smc_scc_num + 1);
2673 #ifdef CONFIG_SERIAL_CONSOLE
2674 /* If we just printed the message on the console port, and
2675 * we are about to initialize it for general use, we have
2676 * to wait a couple of character times for the CR/NL to
2677 * make it out of the transmit buffer.
2679 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2682 info = kmalloc(sizeof(ser_info_t), GFP_KERNEL);
2684 /*memset(info, 0, sizeof(ser_info_t));*/
2685 __clear_user(info,sizeof(ser_info_t));
2686 init_waitqueue_head(&info->open_wait);
2687 init_waitqueue_head(&info->close_wait);
2688 info->magic = SERIAL_MAGIC;
2689 info->flags = state->flags;
2690 info->tqueue.routine = do_softint;
2691 info->tqueue.data = info;
2692 info->tqueue_hangup.routine = do_serial_hangup;
2693 info->tqueue_hangup.data = info;
2695 info->state = state;
2696 state->info = (struct async_struct *)info;
2698 /* We need to allocate a transmit and receive buffer
2699 * descriptors from dual port ram, and a character
2700 * buffer area from host mem.
2702 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * RX_NUM_FIFO, 8);
2704 /* Allocate space for FIFOs in the host memory.
2706 mem_addr = m8260_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE, 1);
2708 /* Set the physical address of the host memory
2709 * buffers in the buffer descriptors, and the
2710 * virtual address for us to work with.
2712 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2713 info->rx_cur = info->rx_bd_base = (cbd_t *)bdp;
2715 for (j=0; j<(RX_NUM_FIFO-1); j++) {
2716 bdp->cbd_bufaddr = __pa(mem_addr);
2717 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2718 mem_addr += RX_BUF_SIZE;
2721 bdp->cbd_bufaddr = __pa(mem_addr);
2722 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2724 if ((idx = state->smc_scc_num) < SCC_NUM_BASE) {
2725 sp = &immap->im_smc[idx];
2726 up = (smc_uart_t *)&immap->im_dprambase[state->port];
2727 up->smc_rbase = dp_addr;
2730 scp = &immap->im_scc[idx - SCC_IDX_BASE];
2731 sup = (scc_uart_t *)&immap->im_dprambase[state->port];
2732 scp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2733 sup->scc_genscc.scc_rbase = dp_addr;
2736 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * TX_NUM_FIFO, 8);
2738 /* Allocate space for FIFOs in the host memory.
2740 mem_addr = m8260_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE, 1);
2742 /* Set the physical address of the host memory
2743 * buffers in the buffer descriptors, and the
2744 * virtual address for us to work with.
2746 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2747 info->tx_cur = info->tx_bd_base = (cbd_t *)bdp;
2749 for (j=0; j<(TX_NUM_FIFO-1); j++) {
2750 bdp->cbd_bufaddr = __pa(mem_addr);
2751 bdp->cbd_sc = BD_SC_INTRPT;
2752 mem_addr += TX_BUF_SIZE;
2755 bdp->cbd_bufaddr = __pa(mem_addr);
2756 bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2758 if (idx < SCC_NUM_BASE) {
2759 up->smc_tbase = dp_addr;
2761 /* Set up the uart parameters in the
2764 up->smc_rfcr = CPMFCR_GBL | CPMFCR_EB;
2765 up->smc_tfcr = CPMFCR_GBL | CPMFCR_EB;
2767 /* Set this to 1 for now, so we get single
2768 * character interrupts. Using idle charater
2769 * time requires some additional tuning.
2775 /* Send the CPM an initialize command.
2777 if (state->smc_scc_num == 0) {
2778 page = CPM_CR_SMC1_PAGE;
2779 sblock = CPM_CR_SMC1_SBLOCK;
2782 page = CPM_CR_SMC2_PAGE;
2783 sblock = CPM_CR_SMC2_SBLOCK;
2786 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0,
2787 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2788 while (cp->cp_cpcr & CPM_CR_FLG);
2790 /* Set UART mode, 8 bit, no parity, one stop.
2791 * Enable receive and transmit.
2793 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2795 /* Disable all interrupts and clear all pending
2799 sp->smc_smce = 0xff;
2802 sup->scc_genscc.scc_tbase = dp_addr;
2804 /* Set up the uart parameters in the
2807 sup->scc_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
2808 sup->scc_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
2810 /* Set this to 1 for now, so we get single
2811 * character interrupts. Using idle charater
2812 * time requires some additional tuning.
2814 sup->scc_genscc.scc_mrblr = 1;
2815 sup->scc_maxidl = 0;
2821 sup->scc_uaddr1 = 0;
2822 sup->scc_uaddr2 = 0;
2824 sup->scc_char1 = 0x8000;
2825 sup->scc_char2 = 0x8000;
2826 sup->scc_char3 = 0x8000;
2827 sup->scc_char4 = 0x8000;
2828 sup->scc_char5 = 0x8000;
2829 sup->scc_char6 = 0x8000;
2830 sup->scc_char7 = 0x8000;
2831 sup->scc_char8 = 0x8000;
2832 sup->scc_rccm = 0xc0ff;
2834 /* Send the CPM an initialize command.
2837 switch (state->smc_scc_num) {
2839 page = CPM_CR_SCC1_PAGE;
2840 sblock = CPM_CR_SCC1_SBLOCK;
2843 page = CPM_CR_SCC2_PAGE;
2844 sblock = CPM_CR_SCC2_SBLOCK;
2847 page = CPM_CR_SCC3_PAGE;
2848 sblock = CPM_CR_SCC3_SBLOCK;
2852 if (state->smc_scc_num == 2) {
2853 page = CPM_CR_SCC2_PAGE;
2854 sblock = CPM_CR_SCC2_SBLOCK;
2857 page = CPM_CR_SCC3_PAGE;
2858 sblock = CPM_CR_SCC3_SBLOCK;
2862 cp->cp_cpcr = mk_cr_cmd(page, sblock, 0,
2863 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2864 while (cp->cp_cpcr & CPM_CR_FLG);
2866 /* Set UART mode, 8 bit, no parity, one stop.
2867 * Enable receive and transmit.
2871 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2873 /* Disable all interrupts and clear all pending
2877 scp->scc_scce = 0xffff;
2878 scp->scc_dsr = 0x7e7e;
2879 scp->scc_pmsr = 0x3000;
2882 /* Install interrupt handler.
2884 request_8xxirq(state->irq, rs_8xx_interrupt, 0, "uart", info);
2886 /* Set up the baud rate generator.
2888 m8260_cpm_setbrg(state->smc_scc_num,
2889 baud_table[baud_idx]);
2891 /* If the port is the console, enable Rx and Tx.
2893 #ifdef CONFIG_SERIAL_CONSOLE
2894 if (i == CONFIG_SERIAL_CONSOLE_PORT) {
2895 if (idx < SCC_NUM_BASE)
2896 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2898 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2906 /* This must always be called before the rs_8xx_init() function, otherwise
2907 * it blows away the port control information.
2909 static int __init serial_console_setup(struct console *co, char *options)
2911 struct serial_state *ser;
2912 uint mem_addr, dp_addr, bidx;
2913 volatile cbd_t *bdp;
2914 volatile cpm8260_t *cp;
2915 volatile immap_t *immap;
2918 volatile smc_uart_t *up;
2920 volatile scc_t *scp;
2921 volatile scc_uart_t *sup;
2922 volatile iop8260_t *io;
2927 for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
2928 if (bd->bi_baudrate == baud_table[bidx])
2931 co->cflag = CREAD|CLOCAL|bidx|CS8;
2934 ser = rs_table + co->index;
2937 cp = &immap->im_cpm;
2938 io = &immap->im_ioport;
2941 scp = (scc_t *)&(immap->im_scc[SCC_CONSOLE-1]);
2942 sup = (scc_uart_t *)&immap->im_dprambase[PROFF_SCC1 + ((SCC_CONSOLE-1) << 8)];
2943 scp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
2944 scp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2946 /* Use Port D for SCC1 instead of other functions.
2948 io->iop_ppard |= 0x00000003;
2949 io->iop_psord &= ~0x00000001; /* Rx */
2950 io->iop_psord |= 0x00000002; /* Tx */
2951 io->iop_pdird &= ~0x00000001; /* Rx */
2952 io->iop_pdird |= 0x00000002; /* Tx */
2955 /* This should have been done long ago by the early boot code,
2956 * but do it again to make sure.
2958 *(ushort *)(&immap->im_dprambase[PROFF_SMC1_BASE]) = PROFF_SMC1;
2959 *(ushort *)(&immap->im_dprambase[PROFF_SMC2_BASE]) = PROFF_SMC2;
2961 /* Right now, assume we are using SMCs.
2963 sp = &immap->im_smc[ser->smc_scc_num];
2965 /* When we get here, the CPM has been reset, so we need
2966 * to configure the port.
2967 * We need to allocate a transmit and receive buffer descriptor
2968 * from dual port ram, and a character buffer area from host mem.
2970 up = (smc_uart_t *)&immap->im_dprambase[ser->port];
2972 /* Disable transmitter/receiver.
2974 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
2976 /* Use Port D for SMC1 instead of other functions.
2978 io->iop_ppard |= 0x00c00000;
2979 io->iop_pdird |= 0x00400000;
2980 io->iop_pdird &= ~0x00800000;
2981 io->iop_psord &= ~0x00c00000;
2984 /* Allocate space for two buffer descriptors in the DP ram.
2986 dp_addr = m8260_cpm_dpalloc(sizeof(cbd_t) * 2, 8);
2988 /* Allocate space for two 2 byte FIFOs in the host memory.
2990 mem_addr = m8260_cpm_hostalloc(4, 1);
2992 /* Set the physical address of the host memory buffers in
2993 * the buffer descriptors.
2995 bdp = (cbd_t *)&immap->im_dprambase[dp_addr];
2996 bdp->cbd_bufaddr = __pa(mem_addr);
2997 (bdp+1)->cbd_bufaddr = __pa(mem_addr+2);
2999 /* For the receive, set empty and wrap.
3000 * For transmit, set wrap.
3002 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
3003 (bdp+1)->cbd_sc = BD_SC_WRAP;
3005 /* Set up the uart parameters in the parameter ram.
3008 sup->scc_genscc.scc_rbase = dp_addr;
3009 sup->scc_genscc.scc_tbase = dp_addr + sizeof(cbd_t);
3011 /* Set up the uart parameters in the
3014 sup->scc_genscc.scc_rfcr = CPMFCR_GBL | CPMFCR_EB;
3015 sup->scc_genscc.scc_tfcr = CPMFCR_GBL | CPMFCR_EB;
3017 sup->scc_genscc.scc_mrblr = 1;
3018 sup->scc_maxidl = 0;
3024 sup->scc_uaddr1 = 0;
3025 sup->scc_uaddr2 = 0;
3027 sup->scc_char1 = 0x8000;
3028 sup->scc_char2 = 0x8000;
3029 sup->scc_char3 = 0x8000;
3030 sup->scc_char4 = 0x8000;
3031 sup->scc_char5 = 0x8000;
3032 sup->scc_char6 = 0x8000;
3033 sup->scc_char7 = 0x8000;
3034 sup->scc_char8 = 0x8000;
3035 sup->scc_rccm = 0xc0ff;
3037 /* Send the CPM an initialize command.
3039 cp->cp_cpcr = mk_cr_cmd(CPM_CR_SCC1_PAGE, CPM_CR_SCC1_SBLOCK, 0,
3040 CPM_CR_INIT_TRX) | CPM_CR_FLG;
3041 while (cp->cp_cpcr & CPM_CR_FLG);
3043 /* Set UART mode, 8 bit, no parity, one stop.
3044 * Enable receive and transmit.
3048 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
3050 /* Disable all interrupts and clear all pending
3054 scp->scc_scce = 0xffff;
3055 scp->scc_dsr = 0x7e7e;
3056 scp->scc_pmsr = 0x3000;
3058 /* Wire BRG1 to SCC1. The serial init will take care of
3061 immap->im_cpmux.cmx_scr = 0;
3063 /* Set up the baud rate generator.
3065 m8260_cpm_setbrg(ser->smc_scc_num, bd->bi_baudrate);
3067 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
3069 up->smc_rbase = dp_addr; /* Base of receive buffer desc. */
3070 up->smc_tbase = dp_addr+sizeof(cbd_t); /* Base of xmt buffer desc. */
3071 up->smc_rfcr = CPMFCR_GBL | CPMFCR_EB;
3072 up->smc_tfcr = CPMFCR_GBL | CPMFCR_EB;
3074 /* Set this to 1 for now, so we get single character interrupts.
3076 up->smc_mrblr = 1; /* receive buffer length */
3077 up->smc_maxidl = 0; /* wait forever for next char */
3079 /* Send the CPM an initialize command.
3081 cp->cp_cpcr = mk_cr_cmd(CPM_CR_SMC1_PAGE, CPM_CR_SMC1_SBLOCK, 0,
3082 CPM_CR_INIT_TRX) | CPM_CR_FLG;
3083 while (cp->cp_cpcr & CPM_CR_FLG);
3085 /* Set UART mode, 8 bit, no parity, one stop.
3086 * Enable receive and transmit.
3088 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
3090 /* Set up the baud rate generator.
3092 m8260_cpm_setbrg(ser->smc_scc_num, bd->bi_baudrate);
3094 /* And finally, enable Rx and Tx.
3096 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;