2 * arch/ppc/4xx_io/serial_sicc.c
4 * Driver for IBM STB3xxx SICC serial port
6 * Based on drivers/char/serial_amba.c, by ARM Ltd.
8 * Copyright 2001 IBM Crop.
9 * Author: IBM China Research Lab
10 * Yudong Yang <yangyud@cn.ibm.com>
11 * Yi Ge <geyi@cn.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * This is a driver for SICC serial port on IBM Redwood 4 evaluation board.
29 * The driver support both as a console device and normal serial device and
30 * is compatible with normal ttyS* devices.
33 #include <linux/config.h>
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/major.h>
43 #include <linux/string.h>
44 #include <linux/fcntl.h>
45 #include <linux/ptrace.h>
46 #include <linux/ioport.h>
48 #include <linux/slab.h>
49 #include <linux/init.h>
50 #include <linux/capability.h>
51 #include <linux/circ_buf.h>
52 #include <linux/serial.h>
53 #include <linux/console.h>
54 #include <linux/sysrq.h>
55 #include <linux/bitops.h>
57 #include <asm/system.h>
60 #include <asm/uaccess.h>
61 #include <asm/serial.h>
64 #include <linux/serialP.h>
67 /* -----------------------------------------------------------------------------
68 * From STB03xxx SICC UART Specification
69 * -----------------------------------------------------------------------------
70 * UART Register Offsets.
73 #define BL_SICC_LSR 0x0000000 /* line status register read/clear */
74 #define BL_SICC_LSRS 0x0000001 /* set line status register read/set */
75 #define BL_SICC_HSR 0x0000002 /* handshake status register r/clear */
76 #define BL_SICC_HSRS 0x0000003 /* set handshake status register r/set */
77 #define BL_SICC_BRDH 0x0000004 /* baudrate divisor high reg r/w */
78 #define BL_SICC_BRDL 0x0000005 /* baudrate divisor low reg r/w */
79 #define BL_SICC_LCR 0x0000006 /* control register r/w */
80 #define BL_SICC_RCR 0x0000007 /* receiver command register r/w */
81 #define BL_SICC_TxCR 0x0000008 /* transmitter command register r/w */
82 #define BL_SICC_RBR 0x0000009 /* receive buffer r */
83 #define BL_SICC_TBR 0x0000009 /* transmit buffer w */
84 #define BL_SICC_CTL2 0x000000A /* added for Vesta */
85 #define BL_SICC_IrCR 0x000000B /* added for Vesta IR */
87 /* masks and definitions for serial port control register */
89 #define _LCR_LM_MASK 0xc0 /* loop back modes */
90 #define _LCR_DTR_MASK 0x20 /* data terminal ready 0-inactive */
91 #define _LCR_RTS_MASK 0x10 /* request to send 0-inactive */
92 #define _LCR_DB_MASK 0x08 /* data bits mask */
93 #define _LCR_PE_MASK 0x04 /* parity enable */
94 #define _LCR_PTY_MASK 0x02 /* parity */
95 #define _LCR_SB_MASK 0x01 /* stop bit mask */
97 #define _LCR_LM_NORM 0x00 /* normal operation */
98 #define _LCR_LM_LOOP 0x40 /* internal loopback mode */
99 #define _LCR_LM_ECHO 0x80 /* automatic echo mode */
100 #define _LCR_LM_RES 0xc0 /* reserved */
102 #define _LCR_DTR_ACTIVE _LCR_DTR_MASK /* DTR is active */
103 #define _LCR_RTS_ACTIVE _LCR_RTS_MASK /* RTS is active */
104 #define _LCR_DB_8_BITS _LCR_DB_MASK /* 8 data bits */
105 #define _LCR_DB_7_BITS 0x00 /* 7 data bits */
106 #define _LCR_PE_ENABLE _LCR_PE_MASK /* parity enabled */
107 #define _LCR_PE_DISABLE 0x00 /* parity disabled */
108 #define _LCR_PTY_EVEN 0x00 /* even parity */
109 #define _LCR_PTY_ODD _LCR_PTY_MASK /* odd parity */
110 #define _LCR_SB_1_BIT 0x00 /* one stop bit */
111 #define _LCR_SB_2_BIT _LCR_SB_MASK /* two stop bit */
113 /* serial port handshake register */
115 #define _HSR_DIS_MASK 0x80 /* DSR input inactive error mask */
116 #define _HSR_CS_MASK 0x40 /* CTS input inactive error mask */
117 #define _HSR_DIS_ACT 0x00 /* dsr input is active */
118 #define _HSR_DIS_INACT _HSR_DIS_MASK /* dsr input is inactive */
119 #define _HSR_CS_ACT 0x00 /* cts input is active */
120 #define _HSR_CS_INACT _HSR_CS_MASK /* cts input is active */
122 /* serial port line status register */
124 #define _LSR_RBR_MASK 0x80 /* receive buffer ready mask */
125 #define _LSR_FE_MASK 0x40 /* framing error */
126 #define _LSR_OE_MASK 0x20 /* overrun error */
127 #define _LSR_PE_MASK 0x10 /* parity error */
128 #define _LSR_LB_MASK 0x08 /* line break */
129 #define _LSR_TBR_MASK 0x04 /* transmit buffer ready */
130 #define _LSR_TSR_MASK 0x02 /* transmit shift register ready */
132 #define _LSR_RBR_FULL _LSR_RBR_MASK /* receive buffer is full */
133 #define _LSR_FE_ERROR _LSR_FE_MASK /* framing error detected */
134 #define _LSR_OE_ERROR _LSR_OE_MASK /* overrun error detected */
135 #define _LSR_PE_ERROR _LSR_PE_MASK /* parity error detected */
136 #define _LSR_LB_BREAK _LSR_LB_MASK /* line break detected */
137 #define _LSR_TBR_EMPTY _LSR_TBR_MASK /* transmit buffer is ready */
138 #define _LSR_TSR_EMPTY _LSR_TSR_MASK /* transmit shift register is empty */
139 #define _LSR_TX_ALL 0x06 /* all physical transmit is done */
141 #define _LSR_RX_ERR (_LSR_LB_BREAK | _LSR_FE_MASK | _LSR_OE_MASK | \
144 /* serial port receiver command register */
146 #define _RCR_ER_MASK 0x80 /* enable receiver mask */
147 #define _RCR_DME_MASK 0x60 /* dma mode */
148 #define _RCR_EIE_MASK 0x10 /* error interrupt enable mask */
149 #define _RCR_PME_MASK 0x08 /* pause mode mask */
151 #define _RCR_ER_ENABLE _RCR_ER_MASK /* receiver enabled */
152 #define _RCR_DME_DISABLE 0x00 /* dma disabled */
153 #define _RCR_DME_RXRDY 0x20 /* dma disabled, RxRDY interrupt enabled*/
154 #define _RCR_DME_ENABLE2 0x40 /* dma enabled,receiver src channel 2 */
155 #define _RCR_DME_ENABLE3 0x60 /* dma enabled,receiver src channel 3 */
156 #define _RCR_PME_HARD _RCR_PME_MASK /* RTS controlled by hardware */
157 #define _RCR_PME_SOFT 0x00 /* RTS controlled by software */
159 /* serial port transmit command register */
161 #define _TxCR_ET_MASK 0x80 /* transmiter enable mask */
162 #define _TxCR_DME_MASK 0x60 /* dma mode mask */
163 #define _TxCR_TIE_MASK 0x10 /* empty interrupt enable mask */
164 #define _TxCR_EIE_MASK 0x08 /* error interrupt enable mask */
165 #define _TxCR_SPE_MASK 0x04 /* stop/pause mask */
166 #define _TxCR_TB_MASK 0x02 /* transmit break mask */
168 #define _TxCR_ET_ENABLE _TxCR_ET_MASK /* transmiter enabled */
169 #define _TxCR_DME_DISABLE 0x00 /* transmiter disabled, TBR intr disabled */
170 #define _TxCR_DME_TBR 0x20 /* transmiter disabled, TBR intr enabled */
171 #define _TxCR_DME_CHAN_2 0x40 /* dma enabled, destination chann 2 */
172 #define _TxCR_DME_CHAN_3 0x60 /* dma enabled, destination chann 3 */
174 /* serial ctl reg 2 - added for Vesta */
176 #define _CTL2_EXTERN 0x80 /* */
177 #define _CTL2_USEFIFO 0x40 /* */
178 #define _CTL2_RESETRF 0x08 /* */
179 #define _CTL2_RESETTF 0x04 /* */
183 #define SERIAL_SICC_NAME "ttySICC"
184 #define SERIAL_SICC_MAJOR 150
185 #define SERIAL_SICC_MINOR 1
186 #define SERIAL_SICC_NR 1
196 * Things needed by tty driver
198 static struct tty_driver *siccnormal_driver;
200 #if defined(CONFIG_SERIAL_SICC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
201 #define SUPPORT_SYSRQ
205 * Things needed internally to this driver
209 * tmp_buf is used as a temporary buffer by serial_write. We need to
210 * lock it in case the copy_from_user blocks while swapping in a page,
211 * and some other program tries to do a serial write at the same time.
212 * Since the lock will only come under contention when the system is
213 * swapping and available memory is low, it makes sense to share one
214 * buffer across all the serial ports, since it significantly saves
215 * memory if large numbers of serial ports are open.
217 static u_char *tmp_buf;
218 static DECLARE_MUTEX(tmp_buf_sem);
220 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
222 /* number of characters left in xmit buffer before we ask for more */
223 #define WAKEUP_CHARS 256
224 #define SICC_ISR_PASS_LIMIT 256
226 #define EVT_WRITE_WAKEUP 0
243 * Static information about the port
246 unsigned int uart_base;
247 unsigned int uart_base_phys;
250 unsigned int uartclk;
251 unsigned int fifosize;
252 unsigned int tiocm_support;
253 void (*set_mctrl)(struct SICC_port *, u_int mctrl);
257 * This is the state information which is persistent across opens
260 struct SICC_icount icount;
262 unsigned int close_delay;
263 unsigned int closing_wait;
264 unsigned int custom_divisor;
267 struct SICC_info *info;
268 spinlock_t sicc_lock;
271 #define SICC_XMIT_SIZE 1024
273 * This is the state information which is only valid when the port is open.
276 struct SICC_port *port;
277 struct SICC_state *state;
278 struct tty_struct *tty;
279 unsigned char x_char;
280 unsigned char old_status;
281 unsigned char read_status_mask;
282 unsigned char ignore_status_mask;
283 struct circ_buf xmit;
290 unsigned int timeout;
295 struct tasklet_struct tlet;
297 wait_queue_head_t open_wait;
298 wait_queue_head_t close_wait;
299 wait_queue_head_t delta_msr_wait;
302 #ifdef CONFIG_SERIAL_SICC_CONSOLE
303 static struct console siccuart_cons;
305 static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios);
306 static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout);
310 static void powerpcMtcic_cr(unsigned long value)
312 mtdcr(DCRN_CICCR, value);
315 static unsigned long powerpcMfcic_cr(void)
317 return mfdcr(DCRN_CICCR);
320 static unsigned long powerpcMfclkgpcr(void)
322 return mfdcr(DCRN_SCCR);
325 static void sicc_set_mctrl_null(struct SICC_port *port, u_int mctrl)
329 static struct SICC_port sicc_ports[SERIAL_SICC_NR] = {
332 .uart_base_phys = SICC0_IO_BASE,
333 .irqrx = SICC0_INTRX,
334 .irqtx = SICC0_INTTX,
337 .set_mctrl = sicc_set_mctrl_null,
341 static struct SICC_state sicc_state[SERIAL_SICC_NR];
343 static void siccuart_enable_rx_interrupt(struct SICC_info *info)
347 cr = readb(info->port->uart_base+BL_SICC_RCR);
348 cr &= ~_RCR_DME_MASK;
349 cr |= _RCR_DME_RXRDY;
350 writeb(cr, info->port->uart_base+BL_SICC_RCR);
353 static void siccuart_disable_rx_interrupt(struct SICC_info *info)
357 cr = readb(info->port->uart_base+BL_SICC_RCR);
358 cr &= ~_RCR_DME_MASK;
359 cr |= _RCR_DME_DISABLE;
360 writeb(cr, info->port->uart_base+BL_SICC_RCR);
364 static void siccuart_enable_tx_interrupt(struct SICC_info *info)
368 cr = readb(info->port->uart_base+BL_SICC_TxCR);
369 cr &= ~_TxCR_DME_MASK;
371 writeb(cr, info->port->uart_base+BL_SICC_TxCR);
374 static void siccuart_disable_tx_interrupt(struct SICC_info *info)
378 cr = readb(info->port->uart_base+BL_SICC_TxCR);
379 cr &= ~_TxCR_DME_MASK;
380 cr |= _TxCR_DME_DISABLE;
381 writeb(cr, info->port->uart_base+BL_SICC_TxCR);
385 static void siccuart_stop(struct tty_struct *tty)
387 struct SICC_info *info = tty->driver_data;
390 /* disable interrupts while stopping serial port interrupts */
391 spin_lock_irqsave(&info->state->sicc_lock,flags);
392 siccuart_disable_tx_interrupt(info);
393 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
396 static void siccuart_start(struct tty_struct *tty)
398 struct SICC_info *info = tty->driver_data;
401 /* disable interrupts while starting serial port interrupts */
402 spin_lock_irqsave(&info->state->sicc_lock,flags);
403 if (info->xmit.head != info->xmit.tail
405 siccuart_enable_tx_interrupt(info);
406 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
411 * This routine is used by the interrupt handler to schedule
412 * processing in the software interrupt portion of the driver.
414 static void siccuart_event(struct SICC_info *info, int event)
416 info->event |= 1 << event;
417 tasklet_schedule(&info->tlet);
421 siccuart_rx_chars(struct SICC_info *info, struct pt_regs *regs)
423 struct tty_struct *tty = info->tty;
424 unsigned int status, ch, rsr, flg, ignored = 0;
425 struct SICC_icount *icount = &info->state->icount;
426 struct SICC_port *port = info->port;
428 status = readb(port->uart_base+BL_SICC_LSR );
429 while (status & _LSR_RBR_FULL) {
430 ch = readb(port->uart_base+BL_SICC_RBR);
432 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
439 * Note that the error handling code is
440 * out of the main execution path
442 rsr = readb(port->uart_base+BL_SICC_LSR);
443 if (rsr & _LSR_RX_ERR)
447 if (ch && time_before(jiffies, info->sysrq)) {
448 handle_sysrq(ch, regs, NULL);
456 *tty->flip.flag_buf_ptr++ = flg;
457 *tty->flip.char_buf_ptr++ = ch;
460 status = readb(port->uart_base+BL_SICC_LSR );
463 tty_flip_buffer_push(tty);
467 if (rsr & _LSR_LB_BREAK) {
468 rsr &= ~(_LSR_FE_MASK | _LSR_PE_MASK);
472 if (info->state->line == siccuart_cons.index) {
474 info->sysrq = jiffies + HZ*5;
479 } else if (rsr & _LSR_PE_MASK)
481 else if (rsr & _LSR_FE_MASK)
483 if (rsr & _LSR_OE_MASK)
486 if (rsr & info->ignore_status_mask) {
491 rsr &= info->read_status_mask;
493 if (rsr & _LSR_LB_BREAK)
495 else if (rsr & _LSR_PE_MASK)
497 else if (rsr & _LSR_FE_MASK)
500 if (rsr & _LSR_OE_MASK) {
502 * CHECK: does overrun affect the current character?
503 * ASSUMPTION: it does not.
505 *tty->flip.flag_buf_ptr++ = flg;
506 *tty->flip.char_buf_ptr++ = ch;
508 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
519 static void siccuart_tx_chars(struct SICC_info *info)
521 struct SICC_port *port = info->port;
523 unsigned char status;
527 writeb(info->x_char, port->uart_base+ BL_SICC_TBR);
528 info->state->icount.tx++;
532 if (info->xmit.head == info->xmit.tail
533 || info->tty->stopped
534 || info->tty->hw_stopped) {
535 siccuart_disable_tx_interrupt(info);
536 writeb(status&(~_LSR_RBR_MASK),port->uart_base+BL_SICC_LSR);
540 count = port->fifosize;
542 writeb(info->xmit.buf[info->xmit.tail], port->uart_base+ BL_SICC_TBR);
543 info->xmit.tail = (info->xmit.tail + 1) & (SICC_XMIT_SIZE - 1);
544 info->state->icount.tx++;
545 if (info->xmit.head == info->xmit.tail)
547 } while (--count > 0);
549 if (CIRC_CNT(info->xmit.head,
551 SICC_XMIT_SIZE) < WAKEUP_CHARS)
552 siccuart_event(info, EVT_WRITE_WAKEUP);
554 if (info->xmit.head == info->xmit.tail) {
555 siccuart_disable_tx_interrupt(info);
560 static irqreturn_t siccuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
562 struct SICC_info *info = dev_id;
563 siccuart_rx_chars(info, regs);
568 static irqreturn_t siccuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
570 struct SICC_info *info = dev_id;
571 siccuart_tx_chars(info);
575 static void siccuart_tasklet_action(unsigned long data)
577 struct SICC_info *info = (struct SICC_info *)data;
578 struct tty_struct *tty;
581 if (!tty || !test_and_clear_bit(EVT_WRITE_WAKEUP, &info->event))
584 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
585 tty->ldisc.write_wakeup)
586 (tty->ldisc.write_wakeup)(tty);
587 wake_up_interruptible(&tty->write_wait);
590 static int siccuart_startup(struct SICC_info *info)
596 if (info->flags & ASYNC_INITIALIZED) {
600 page = get_zeroed_page(GFP_KERNEL);
604 if (info->port->uart_base == 0)
605 info->port->uart_base = (int)ioremap(info->port->uart_base_phys, PAGE_SIZE);
606 if (info->port->uart_base == 0) {
611 /* lock access to info while doing setup */
612 spin_lock_irqsave(&info->state->sicc_lock,flags);
617 info->xmit.buf = (unsigned char *) page;
621 if (info->tty->termios->c_cflag & CBAUD)
622 info->mctrl = TIOCM_RTS | TIOCM_DTR;
623 info->port->set_mctrl(info->port, info->mctrl);
626 * initialise the old status of the modem signals
628 info->old_status = 0; // UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;
632 clear_bit(TTY_IO_ERROR, &info->tty->flags);
633 info->xmit.head = info->xmit.tail = 0;
636 * Set up the tty->alt_speed kludge
639 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
640 info->tty->alt_speed = 57600;
641 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
642 info->tty->alt_speed = 115200;
643 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
644 info->tty->alt_speed = 230400;
645 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
646 info->tty->alt_speed = 460800;
650 writeb( 0x00, info->port->uart_base + BL_SICC_IrCR ); // disable IrDA
654 * and set the speed of the serial port
656 siccuart_change_speed(info, 0);
658 // enable rx/tx ports
659 writeb(_RCR_ER_ENABLE /*| _RCR_PME_HARD*/, info->port->uart_base + BL_SICC_RCR);
660 writeb(_TxCR_ET_ENABLE , info->port->uart_base + BL_SICC_TxCR);
662 readb(info->port->uart_base + BL_SICC_RBR); // clear rx port
664 writeb(0xf8, info->port->uart_base + BL_SICC_LSR); /* reset bits 0-4 of LSR */
667 * Finally, enable interrupts
673 retval = request_irq(info->port->irqrx, siccuart_int_rx, 0, "SICC rx", info);
675 if (capable(CAP_SYS_ADMIN)) {
677 set_bit(TTY_IO_ERROR, &info->tty->flags);
682 retval = request_irq(info->port->irqtx, siccuart_int_tx, 0, "SICC tx", info);
684 if (capable(CAP_SYS_ADMIN)) {
686 set_bit(TTY_IO_ERROR, &info->tty->flags);
689 free_irq(info->port->irqrx, info);
693 siccuart_enable_rx_interrupt(info);
695 info->flags |= ASYNC_INITIALIZED;
696 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
701 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
706 * This routine will shutdown a serial port; interrupts are disabled, and
707 * DTR is dropped if the hangup on close termio flag is on.
709 static void siccuart_shutdown(struct SICC_info *info)
713 if (!(info->flags & ASYNC_INITIALIZED))
716 /* lock while shutting down port */
717 spin_lock_irqsave(&info->state->sicc_lock,flags); /* Disable interrupts */
720 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
721 * here so the queue might never be woken up
723 wake_up_interruptible(&info->delta_msr_wait);
726 * disable all interrupts, disable the port
728 siccuart_disable_rx_interrupt(info);
729 siccuart_disable_tx_interrupt(info);
734 free_irq(info->port->irqtx, info);
735 free_irq(info->port->irqrx, info);
737 if (info->xmit.buf) {
738 unsigned long pg = (unsigned long) info->xmit.buf;
739 info->xmit.buf = NULL;
744 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
745 info->mctrl &= ~(TIOCM_DTR|TIOCM_RTS);
746 info->port->set_mctrl(info->port, info->mctrl);
748 /* kill off our tasklet */
749 tasklet_kill(&info->tlet);
751 set_bit(TTY_IO_ERROR, &info->tty->flags);
753 info->flags &= ~ASYNC_INITIALIZED;
755 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
759 static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios)
761 unsigned int lcr_h, baud, quot, cflag, old_rcr, old_tcr, bits;
764 if (!info->tty || !info->tty->termios)
767 cflag = info->tty->termios->c_cflag;
769 pr_debug("siccuart_set_cflag(0x%x) called\n", cflag);
770 /* byte size and parity */
771 switch (cflag & CSIZE) {
772 case CS7: lcr_h = _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT; bits = 9; break;
773 default: lcr_h = _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT; bits = 10; break; // CS8
775 if (cflag & CSTOPB) {
776 lcr_h |= _LCR_SB_2_BIT;
779 if (cflag & PARENB) {
780 lcr_h |= _LCR_PE_ENABLE;
782 if (!(cflag & PARODD))
783 lcr_h |= _LCR_PTY_ODD;
785 lcr_h |= _LCR_PTY_EVEN;
789 /* Determine divisor based on baud rate */
790 baud = tty_get_baud_rate(info->tty);
796 // here is ppc403SetBaud(com_port, baud);
797 unsigned long divisor, clockSource, temp;
799 /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
800 powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
802 /* Determine Internal Baud Clock Frequency */
803 /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
804 /* SCCR (Serial Clock Control Register) on Vesta */
805 temp = powerpcMfclkgpcr();
807 if(temp & 0x00000080) {
808 clockSource = 324000000;
811 clockSource = 216000000;
813 clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
814 divisor = clockSource/(16*baud) - 1;
815 /* divisor has only 12 bits of resolution */
816 if(divisor>0x00000FFF){
824 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
825 quot = info->state->custom_divisor;
827 if (!quot && old_termios) {
828 info->tty->termios->c_cflag &= ~CBAUD;
829 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
832 } while (quot == 0 && old_termios);
834 /* As a last resort, if the quotient is zero, default to 9600 bps */
836 quot = (info->port->uartclk / (16 * 9600)) - 1;
838 info->timeout = info->port->fifosize * HZ * bits / baud;
839 info->timeout += HZ/50; /* Add .02 seconds of slop */
842 info->flags |= ASYNC_CTS_FLOW;
844 info->flags &= ~ASYNC_CTS_FLOW;
846 info->flags &= ~ASYNC_CHECK_CD;
848 info->flags |= ASYNC_CHECK_CD;
851 * Set up parity check flag
853 #define RELEVENT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
855 info->read_status_mask = _LSR_OE_MASK;
856 if (I_INPCK(info->tty))
857 info->read_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
858 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
859 info->read_status_mask |= _LSR_LB_MASK;
862 * Characters to ignore
864 info->ignore_status_mask = 0;
865 if (I_IGNPAR(info->tty))
866 info->ignore_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
867 if (I_IGNBRK(info->tty)) {
868 info->ignore_status_mask |= _LSR_LB_MASK;
870 * If we're ignoring parity and break indicators,
871 * ignore overruns to (for real raw support).
873 if (I_IGNPAR(info->tty))
874 info->ignore_status_mask |= _LSR_OE_MASK;
877 /* disable interrupts while reading and clearing registers */
878 spin_lock_irqsave(&info->state->sicc_lock,flags);
880 old_rcr = readb(info->port->uart_base + BL_SICC_RCR);
881 old_tcr = readb(info->port->uart_base + BL_SICC_TxCR);
884 writeb(0, info->port->uart_base + BL_SICC_RCR);
885 writeb(0, info->port->uart_base + BL_SICC_TxCR);
887 /*RLBtrace (&ppc403Chan0, 0x2000000c, 0, 0);*/
890 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
894 writeb((quot & 0x00000F00)>>8, info->port->uart_base + BL_SICC_BRDH );
895 writeb( quot & 0x00000FF, info->port->uart_base + BL_SICC_BRDL );
897 /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
898 /* For now, do NOT use FIFOs since 403 UART did not have this */
899 /* capability and this driver was inherited from 403UART. */
900 writeb(_CTL2_EXTERN, info->port->uart_base + BL_SICC_CTL2);
902 writeb(lcr_h, info->port->uart_base + BL_SICC_LCR);
904 writeb(old_rcr, info->port->uart_base + BL_SICC_RCR); // restore rcr
905 writeb(old_tcr, info->port->uart_base + BL_SICC_TxCR); // restore txcr
910 static void siccuart_put_char(struct tty_struct *tty, u_char ch)
912 struct SICC_info *info = tty->driver_data;
915 if (!tty || !info->xmit.buf)
918 /* lock info->xmit while adding character to tx buffer */
919 spin_lock_irqsave(&info->state->sicc_lock,flags);
920 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE) != 0) {
921 info->xmit.buf[info->xmit.head] = ch;
922 info->xmit.head = (info->xmit.head + 1) & (SICC_XMIT_SIZE - 1);
924 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
927 static void siccuart_flush_chars(struct tty_struct *tty)
929 struct SICC_info *info = tty->driver_data;
932 if (info->xmit.head == info->xmit.tail
938 /* disable interrupts while transmitting characters */
939 spin_lock_irqsave(&info->state->sicc_lock,flags);
940 siccuart_enable_tx_interrupt(info);
941 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
944 static int siccuart_write(struct tty_struct *tty,
945 const u_char * buf, int count)
947 struct SICC_info *info = tty->driver_data;
951 if (!tty || !info->xmit.buf || !tmp_buf)
954 /* lock info->xmit while removing characters from buffer */
955 spin_lock_irqsave(&info->state->sicc_lock,flags);
957 c = CIRC_SPACE_TO_END(info->xmit.head,
964 memcpy(info->xmit.buf + info->xmit.head, buf, c);
965 info->xmit.head = (info->xmit.head + c) &
966 (SICC_XMIT_SIZE - 1);
971 if (info->xmit.head != info->xmit.tail
974 siccuart_enable_tx_interrupt(info);
975 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
979 static int siccuart_write_room(struct tty_struct *tty)
981 struct SICC_info *info = tty->driver_data;
983 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
986 static int siccuart_chars_in_buffer(struct tty_struct *tty)
988 struct SICC_info *info = tty->driver_data;
990 return CIRC_CNT(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
993 static void siccuart_flush_buffer(struct tty_struct *tty)
995 struct SICC_info *info = tty->driver_data;
998 pr_debug("siccuart_flush_buffer(%d) called\n", tty->index);
999 /* lock info->xmit while zeroing buffer counts */
1000 spin_lock_irqsave(&info->state->sicc_lock,flags);
1001 info->xmit.head = info->xmit.tail = 0;
1002 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1003 wake_up_interruptible(&tty->write_wait);
1004 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1005 tty->ldisc.write_wakeup)
1006 (tty->ldisc.write_wakeup)(tty);
1010 * This function is used to send a high-priority XON/XOFF character to
1013 static void siccuart_send_xchar(struct tty_struct *tty, char ch)
1015 struct SICC_info *info = tty->driver_data;
1019 siccuart_enable_tx_interrupt(info);
1022 static void siccuart_throttle(struct tty_struct *tty)
1024 struct SICC_info *info = tty->driver_data;
1025 unsigned long flags;
1028 siccuart_send_xchar(tty, STOP_CHAR(tty));
1030 if (tty->termios->c_cflag & CRTSCTS) {
1031 /* disable interrupts while setting modem control lines */
1032 spin_lock_irqsave(&info->state->sicc_lock,flags);
1033 info->mctrl &= ~TIOCM_RTS;
1034 info->port->set_mctrl(info->port, info->mctrl);
1035 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1039 static void siccuart_unthrottle(struct tty_struct *tty)
1041 struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1042 unsigned long flags;
1048 siccuart_send_xchar(tty, START_CHAR(tty));
1051 if (tty->termios->c_cflag & CRTSCTS) {
1052 /* disable interrupts while setting modem control lines */
1053 spin_lock_irqsave(&info->state->sicc_lock,flags);
1054 info->mctrl |= TIOCM_RTS;
1055 info->port->set_mctrl(info->port, info->mctrl);
1056 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1060 static int get_serial_info(struct SICC_info *info, struct serial_struct *retinfo)
1062 struct SICC_state *state = info->state;
1063 struct SICC_port *port = info->port;
1064 struct serial_struct tmp;
1066 memset(&tmp, 0, sizeof(tmp));
1068 tmp.line = state->line;
1069 tmp.port = port->uart_base;
1070 if (HIGH_BITS_OFFSET)
1071 tmp.port_high = port->uart_base >> HIGH_BITS_OFFSET;
1072 tmp.irq = port->irqrx;
1074 tmp.xmit_fifo_size = port->fifosize;
1075 tmp.baud_base = port->uartclk / 16;
1076 tmp.close_delay = state->close_delay;
1077 tmp.closing_wait = state->closing_wait;
1078 tmp.custom_divisor = state->custom_divisor;
1080 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1085 static int set_serial_info(struct SICC_info *info,
1086 struct serial_struct *newinfo)
1088 struct serial_struct new_serial;
1089 struct SICC_state *state, old_state;
1090 struct SICC_port *port;
1091 unsigned long new_port;
1092 unsigned int i, change_irq, change_port;
1095 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1098 state = info->state;
1102 new_port = new_serial.port;
1103 if (HIGH_BITS_OFFSET)
1104 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1106 change_irq = new_serial.irq != port->irqrx;
1107 change_port = new_port != port->uart_base;
1109 if (!capable(CAP_SYS_ADMIN)) {
1110 if (change_irq || change_port ||
1111 (new_serial.baud_base != port->uartclk / 16) ||
1112 (new_serial.close_delay != state->close_delay) ||
1113 (new_serial.xmit_fifo_size != port->fifosize) ||
1114 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1115 (state->flags & ~ASYNC_USR_MASK)))
1117 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1118 (new_serial.flags & ASYNC_USR_MASK));
1119 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1120 (new_serial.flags & ASYNC_USR_MASK));
1121 state->custom_divisor = new_serial.custom_divisor;
1122 goto check_and_exit;
1125 if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
1126 (new_serial.baud_base < 9600))
1129 if (new_serial.type && change_port) {
1130 for (i = 0; i < SERIAL_SICC_NR; i++)
1131 if ((port != sicc_ports + i) &&
1132 sicc_ports[i].uart_base != new_port)
1136 if ((change_port || change_irq) && (state->count > 1))
1140 * OK, past this point, all the error checking has been done.
1141 * At this point, we start making changes.....
1143 port->uartclk = new_serial.baud_base * 16;
1144 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1145 (new_serial.flags & ASYNC_FLAGS));
1146 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1147 (info->flags & ASYNC_INTERNAL_FLAGS));
1148 state->custom_divisor = new_serial.custom_divisor;
1149 state->close_delay = msecs_to_jiffies(10 * new_serial.close_delay);
1150 state->closing_wait = msecs_to_jiffies(10 * new_serial.closing_wait);
1151 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1152 port->fifosize = new_serial.xmit_fifo_size;
1154 if (change_port || change_irq) {
1156 * We need to shutdown the serial port at the old
1157 * port/irq combination.
1159 siccuart_shutdown(info);
1160 port->irqrx = new_serial.irq;
1161 port->uart_base = new_port;
1165 if (!port->uart_base)
1167 if (info->flags & ASYNC_INITIALIZED) {
1168 if ((old_state.flags & ASYNC_SPD_MASK) !=
1169 (state->flags & ASYNC_SPD_MASK) ||
1170 (old_state.custom_divisor != state->custom_divisor)) {
1171 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1172 info->tty->alt_speed = 57600;
1173 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1174 info->tty->alt_speed = 115200;
1175 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1176 info->tty->alt_speed = 230400;
1177 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1178 info->tty->alt_speed = 460800;
1179 siccuart_change_speed(info, NULL);
1182 retval = siccuart_startup(info);
1188 * get_lsr_info - get line status register info
1190 static int get_lsr_info(struct SICC_info *info, unsigned int *value)
1192 unsigned int result, status;
1193 unsigned long flags;
1195 /* disable interrupts while reading status from port */
1196 spin_lock_irqsave(&info->state->sicc_lock,flags);
1197 status = readb(info->port->uart_base + BL_SICC_LSR);
1198 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1199 result = status & _LSR_TSR_EMPTY ? TIOCSER_TEMT : 0;
1202 * If we're about to load something into the transmit
1203 * register, we'll pretend the transmitter isn't empty to
1204 * avoid a race condition (depending on when the transmit
1205 * interrupt happens).
1208 ((CIRC_CNT(info->xmit.head, info->xmit.tail,
1209 SICC_XMIT_SIZE) > 0) &&
1210 !info->tty->stopped && !info->tty->hw_stopped))
1211 result &= TIOCSER_TEMT;
1213 return put_user(result, value);
1216 static int get_modem_info(struct SICC_info *info, unsigned int *value)
1218 unsigned int result = info->mctrl;
1220 return put_user(result, value);
1223 static int set_modem_info(struct SICC_info *info, unsigned int cmd,
1224 unsigned int *value)
1226 unsigned int arg, old;
1227 unsigned long flags;
1229 if (get_user(arg, value))
1239 info->mctrl &= ~arg;
1249 /* disable interrupts while setting modem control lines */
1250 spin_lock_irqsave(&info->state->sicc_lock,flags);
1251 if (old != info->mctrl)
1252 info->port->set_mctrl(info->port, info->mctrl);
1253 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1257 static void siccuart_break_ctl(struct tty_struct *tty, int break_state)
1259 struct SICC_info *info = tty->driver_data;
1260 unsigned long flags;
1264 /* disable interrupts while setting break state */
1265 spin_lock_irqsave(&info->state->sicc_lock,flags);
1266 lcr_h = readb(info->port + BL_SICC_LSR);
1267 if (break_state == -1)
1268 lcr_h |= _LSR_LB_MASK;
1270 lcr_h &= ~_LSR_LB_MASK;
1271 writeb(lcr_h, info->port + BL_SICC_LSRS);
1272 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1275 static int siccuart_ioctl(struct tty_struct *tty, struct file *file,
1276 unsigned int cmd, unsigned long arg)
1278 struct SICC_info *info = tty->driver_data;
1279 struct SICC_icount cnow;
1280 struct serial_icounter_struct icount;
1281 unsigned long flags;
1283 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1284 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1285 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1286 if (tty->flags & (1 << TTY_IO_ERROR))
1292 return get_modem_info(info, (unsigned int *)arg);
1296 return set_modem_info(info, cmd, (unsigned int *)arg);
1298 return get_serial_info(info,
1299 (struct serial_struct *)arg);
1301 return set_serial_info(info,
1302 (struct serial_struct *)arg);
1303 case TIOCSERGETLSR: /* Get line status register */
1304 return get_lsr_info(info, (unsigned int *)arg);
1306 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1307 * - mask passed in arg for lines of interest
1308 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1309 * Caller should use TIOCGICOUNT to see which one it was
1314 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1315 * Return: write counters to the user passed counter struct
1316 * NB: both 1->0 and 0->1 transitions are counted except for
1317 * RI where only 0->1 is counted.
1320 /* disable interrupts while getting interrupt count */
1321 spin_lock_irqsave(&info->state->sicc_lock,flags);
1322 cnow = info->state->icount;
1323 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1324 icount.cts = cnow.cts;
1325 icount.dsr = cnow.dsr;
1326 icount.rng = cnow.rng;
1327 icount.dcd = cnow.dcd;
1328 icount.rx = cnow.rx;
1329 icount.tx = cnow.tx;
1330 icount.frame = cnow.frame;
1331 icount.overrun = cnow.overrun;
1332 icount.parity = cnow.parity;
1333 icount.brk = cnow.brk;
1334 icount.buf_overrun = cnow.buf_overrun;
1336 return copy_to_user((void *)arg, &icount, sizeof(icount))
1340 return -ENOIOCTLCMD;
1345 static void siccuart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1347 struct SICC_info *info = tty->driver_data;
1348 unsigned long flags;
1349 unsigned int cflag = tty->termios->c_cflag;
1351 if ((cflag ^ old_termios->c_cflag) == 0 &&
1352 RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1355 siccuart_change_speed(info, old_termios);
1357 /* Handle transition to B0 status */
1358 if ((old_termios->c_cflag & CBAUD) &&
1360 /* disable interrupts while setting break state */
1361 spin_lock_irqsave(&info->state->sicc_lock,flags);
1362 info->mctrl &= ~(TIOCM_RTS | TIOCM_DTR);
1363 info->port->set_mctrl(info->port, info->mctrl);
1364 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1367 /* Handle transition away from B0 status */
1368 if (!(old_termios->c_cflag & CBAUD) &&
1370 /* disable interrupts while setting break state */
1371 spin_lock_irqsave(&info->state->sicc_lock,flags);
1372 info->mctrl |= TIOCM_DTR;
1373 if (!(cflag & CRTSCTS) ||
1374 !test_bit(TTY_THROTTLED, &tty->flags))
1375 info->mctrl |= TIOCM_RTS;
1376 info->port->set_mctrl(info->port, info->mctrl);
1377 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1380 /* Handle turning off CRTSCTS */
1381 if ((old_termios->c_cflag & CRTSCTS) &&
1382 !(cflag & CRTSCTS)) {
1383 tty->hw_stopped = 0;
1384 siccuart_start(tty);
1389 * No need to wake up processes in open wait, since they
1390 * sample the CLOCAL flag once, and don't recheck it.
1391 * XXX It's not clear whether the current behavior is correct
1392 * or not. Hence, this may change.....
1394 if (!(old_termios->c_cflag & CLOCAL) &&
1395 (tty->termios->c_cflag & CLOCAL))
1396 wake_up_interruptible(&info->open_wait);
1400 static void siccuart_close(struct tty_struct *tty, struct file *filp)
1402 struct SICC_info *info = tty->driver_data;
1403 struct SICC_state *state;
1404 unsigned long flags;
1409 state = info->state;
1411 //pr_debug("siccuart_close() called\n");
1413 /* lock tty->driver_data while closing port */
1414 spin_lock_irqsave(&info->state->sicc_lock,flags);
1416 if (tty_hung_up_p(filp)) {
1420 if ((tty->count == 1) && (state->count != 1)) {
1422 * Uh, oh. tty->count is 1, which means that the tty
1423 * structure will be freed. state->count should always
1424 * be one in these conditions. If it's greater than
1425 * one, we've got real problems, since it means the
1426 * serial port won't be shutdown.
1428 printk("siccuart_close: bad serial port count; tty->count is 1, state->count is %d\n", state->count);
1431 if (--state->count < 0) {
1432 printk("rs_close: bad serial port count for %s: %d\n", tty->name, state->count);
1438 info->flags |= ASYNC_CLOSING;
1439 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1441 * Now we wait for the transmit buffer to clear; and we notify
1442 * the line discipline to only process XON/XOFF characters.
1445 if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1446 tty_wait_until_sent(tty, info->state->closing_wait);
1448 * At this point, we stop accepting input. To do this, we
1449 * disable the receive line status interrupts.
1451 if (info->flags & ASYNC_INITIALIZED) {
1452 siccuart_disable_rx_interrupt(info);
1454 * Before we drop DTR, make sure the UART transmitter
1455 * has completely drained; this is especially
1456 * important if there is a transmit FIFO!
1458 siccuart_wait_until_sent(tty, info->timeout);
1460 siccuart_shutdown(info);
1461 if (tty->driver->flush_buffer)
1462 tty->driver->flush_buffer(tty);
1463 if (tty->ldisc.flush_buffer)
1464 tty->ldisc.flush_buffer(tty);
1468 if (info->blocked_open) {
1469 if (info->state->close_delay)
1470 schedule_timeout_interruptible(info->state->close_delay);
1471 wake_up_interruptible(&info->open_wait);
1473 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1474 wake_up_interruptible(&info->close_wait);
1478 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1482 static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout)
1484 struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1485 unsigned long char_time, expire;
1487 if (info->port->fifosize == 0)
1491 * Set the check interval to be 1/5 of the estimated time to
1492 * send a single character, and make it at least 1. The check
1493 * interval should also be less than the timeout.
1495 * Note: we have to use pretty tight timings here to satisfy
1498 char_time = (info->timeout - msecs_to_jiffies(20)) / info->port->fifosize;
1499 char_time = char_time / 5;
1503 // Crazy!! sometimes the input arg 'timeout' can be negtive numbers :-(
1504 if (timeout >= 0 && timeout < char_time)
1505 char_time = timeout;
1507 * If the transmitter hasn't cleared in twice the approximate
1508 * amount of time to send the entire FIFO, it probably won't
1509 * ever clear. This assumes the UART isn't doing flow
1510 * control, which is currently the case. Hence, if it ever
1511 * takes longer than info->timeout, this is probably due to a
1512 * UART bug of some kind. So, we clamp the timeout parameter at
1515 if (!timeout || timeout > 2 * info->timeout)
1516 timeout = 2 * info->timeout;
1518 expire = jiffies + timeout;
1519 pr_debug("siccuart_wait_until_sent(%d), jiff=%lu, expire=%lu char_time=%lu...\n",
1520 tty->index, jiffies,
1522 while ((readb(info->port->uart_base + BL_SICC_LSR) & _LSR_TX_ALL) != _LSR_TX_ALL) {
1523 schedule_timeout_interruptible(char_time);
1524 if (signal_pending(current))
1526 if (timeout && time_after(jiffies, expire))
1529 set_current_state(TASK_RUNNING);
1532 static void siccuart_hangup(struct tty_struct *tty)
1534 struct SICC_info *info = tty->driver_data;
1535 struct SICC_state *state = info->state;
1537 siccuart_flush_buffer(tty);
1538 if (info->flags & ASYNC_CLOSING)
1540 siccuart_shutdown(info);
1543 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1545 wake_up_interruptible(&info->open_wait);
1548 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1549 struct SICC_info *info)
1551 DECLARE_WAITQUEUE(wait, current);
1552 struct SICC_state *state = info->state;
1553 unsigned long flags;
1554 int do_clocal = 0, extra_count = 0, retval;
1557 * If the device is in the middle of being closed, then block
1558 * until it's done, and then try again.
1560 if (tty_hung_up_p(filp) ||
1561 (info->flags & ASYNC_CLOSING)) {
1562 if (info->flags & ASYNC_CLOSING)
1563 interruptible_sleep_on(&info->close_wait);
1564 return (info->flags & ASYNC_HUP_NOTIFY) ?
1565 -EAGAIN : -ERESTARTSYS;
1569 * If non-blocking mode is set, or the port is not enabled,
1570 * then make the check up front and then exit.
1572 if ((filp->f_flags & O_NONBLOCK) ||
1573 (tty->flags & (1 << TTY_IO_ERROR))) {
1574 info->flags |= ASYNC_NORMAL_ACTIVE;
1578 if (tty->termios->c_cflag & CLOCAL)
1582 * Block waiting for the carrier detect and the line to become
1583 * free (i.e., not in use by the callout). While we are in
1584 * this loop, state->count is dropped by one, so that
1585 * rs_close() knows when to free things. We restore it upon
1586 * exit, either normal or abnormal.
1589 add_wait_queue(&info->open_wait, &wait);
1590 /* lock while decrementing state->count */
1591 spin_lock_irqsave(&info->state->sicc_lock,flags);
1592 if (!tty_hung_up_p(filp)) {
1596 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1597 info->blocked_open++;
1599 /* disable interrupts while setting modem control lines */
1600 spin_lock_irqsave(&info->state->sicc_lock,flags);
1601 if (tty->termios->c_cflag & CBAUD) {
1602 info->mctrl = TIOCM_DTR | TIOCM_RTS;
1603 info->port->set_mctrl(info->port, info->mctrl);
1605 spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1606 set_current_state(TASK_INTERRUPTIBLE);
1607 if (tty_hung_up_p(filp) ||
1608 !(info->flags & ASYNC_INITIALIZED)) {
1609 if (info->flags & ASYNC_HUP_NOTIFY)
1612 retval = -ERESTARTSYS;
1615 if (!(info->flags & ASYNC_CLOSING) &&
1616 (do_clocal /*|| (UART_GET_FR(info->port) & SICC_UARTFR_DCD)*/))
1618 if (signal_pending(current)) {
1619 retval = -ERESTARTSYS;
1624 set_current_state(TASK_RUNNING);
1625 remove_wait_queue(&info->open_wait, &wait);
1628 info->blocked_open--;
1631 info->flags |= ASYNC_NORMAL_ACTIVE;
1635 static struct SICC_info *siccuart_get(int line)
1637 struct SICC_info *info;
1638 struct SICC_state *state = sicc_state + line;
1643 info = kmalloc(sizeof(struct SICC_info), GFP_KERNEL);
1645 memset(info, 0, sizeof(struct SICC_info));
1646 init_waitqueue_head(&info->open_wait);
1647 init_waitqueue_head(&info->close_wait);
1648 init_waitqueue_head(&info->delta_msr_wait);
1649 info->flags = state->flags;
1650 info->state = state;
1651 info->port = sicc_ports + line;
1652 tasklet_init(&info->tlet, siccuart_tasklet_action,
1653 (unsigned long)info);
1663 static int siccuart_open(struct tty_struct *tty, struct file *filp)
1665 struct SICC_info *info;
1666 int retval, line = tty->index;
1669 // is this a line that we've got?
1670 if (line >= SERIAL_SICC_NR) {
1674 info = siccuart_get(line);
1678 tty->driver_data = info;
1680 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1683 * Make sure we have the temporary buffer allocated
1686 unsigned long page = get_zeroed_page(GFP_KERNEL);
1692 tmp_buf = (u_char *)page;
1696 * If the port is in the middle of closing, bail out now.
1698 if (tty_hung_up_p(filp) ||
1699 (info->flags & ASYNC_CLOSING)) {
1700 if (info->flags & ASYNC_CLOSING)
1701 interruptible_sleep_on(&info->close_wait);
1706 * Start up the serial port
1708 retval = siccuart_startup(info);
1713 retval = block_til_ready(tty, filp, info);
1718 #ifdef CONFIG_SERIAL_SICC_CONSOLE
1719 if (siccuart_cons.cflag && siccuart_cons.index == line) {
1720 tty->termios->c_cflag = siccuart_cons.cflag;
1721 siccuart_cons.cflag = 0;
1722 siccuart_change_speed(info, NULL);
1728 static struct tty_operations sicc_ops = {
1729 .open = siccuart_open,
1730 .close = siccuart_close,
1731 .write = siccuart_write,
1732 .put_char = siccuart_put_char,
1733 .flush_chars = siccuart_flush_chars,
1734 .write_room = siccuart_write_room,
1735 .chars_in_buffer = siccuart_chars_in_buffer,
1736 .flush_buffer = siccuart_flush_buffer,
1737 .ioctl = siccuart_ioctl,
1738 .throttle = siccuart_throttle,
1739 .unthrottle = siccuart_unthrottle,
1740 .send_xchar = siccuart_send_xchar,
1741 .set_termios = siccuart_set_termios,
1742 .stop = siccuart_stop,
1743 .start = siccuart_start,
1744 .hangup = siccuart_hangup,
1745 .break_ctl = siccuart_break_ctl,
1746 .wait_until_sent = siccuart_wait_until_sent,
1749 int __init siccuart_init(void)
1752 siccnormal_driver = alloc_tty_driver(SERIAL_SICC_NR);
1753 if (!siccnormal_driver)
1755 printk("IBM Vesta SICC serial port driver V 0.1 by Yudong Yang and Yi Ge / IBM CRL .\n");
1756 siccnormal_driver->driver_name = "serial_sicc";
1757 siccnormal_driver->owner = THIS_MODULE;
1758 siccnormal_driver->name = SERIAL_SICC_NAME;
1759 siccnormal_driver->major = SERIAL_SICC_MAJOR;
1760 siccnormal_driver->minor_start = SERIAL_SICC_MINOR;
1761 siccnormal_driver->type = TTY_DRIVER_TYPE_SERIAL;
1762 siccnormal_driver->subtype = SERIAL_TYPE_NORMAL;
1763 siccnormal_driver->init_termios = tty_std_termios;
1764 siccnormal_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1765 siccnormal_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1766 tty_set_operations(siccnormal_driver, &sicc_ops);
1768 if (tty_register_driver(siccnormal_driver))
1769 panic("Couldn't register SICC serial driver\n");
1771 for (i = 0; i < SERIAL_SICC_NR; i++) {
1772 struct SICC_state *state = sicc_state + i;
1774 state->close_delay = msecs_to_jiffies(500);
1775 state->closing_wait = 30 * HZ;
1776 spin_lock_init(&state->sicc_lock);
1783 __initcall(siccuart_init);
1785 #ifdef CONFIG_SERIAL_SICC_CONSOLE
1786 /************** console driver *****************/
1789 * This code is currently never used; console->read is never called.
1790 * Therefore, although we have an implementation, we don't use it.
1791 * FIXME: the "const char *s" should be fixed to "char *s" some day.
1792 * (when the definition in include/linux/console.h is also fixed)
1794 #ifdef used_and_not_const_char_pointer
1795 static int siccuart_console_read(struct console *co, const char *s, u_int count)
1797 struct SICC_port *port = &sicc_ports[co->index];
1798 unsigned int status;
1802 pr_debug("siccuart_console_read() called\n");
1807 if(readb(port->uart_base + BL_SICC_LSR) & _LSR_RBR_FULL) {
1808 *w++ = readb(port->uart_base + BL_SICC_RBR);
1811 // nothing more to get, return
1821 * Print a string to the serial port trying not to disturb
1822 * any possible real use of the port...
1824 * The console_lock must be held when we get here.
1826 static void siccuart_console_write(struct console *co, const char *s, u_int count)
1828 struct SICC_port *port = &sicc_ports[co->index];
1829 unsigned int old_cr;
1833 * First save the CR then disable the interrupts
1835 old_cr = readb(port->uart_base + BL_SICC_TxCR);
1836 writeb(old_cr & ~_TxCR_DME_MASK, port->uart_base + BL_SICC_TxCR);
1839 * Now, do each character
1841 for (i = 0; i < count; i++) {
1842 while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1843 writeb(s[i], port->uart_base + BL_SICC_TBR);
1845 while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1846 writeb('\r', port->uart_base + BL_SICC_TBR);
1851 * Finally, wait for transmitter to become empty
1852 * and restore the TCR
1854 while ((readb(port->uart_base + BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1855 writeb(old_cr, port->uart_base + BL_SICC_TxCR);
1859 * Receive character from the serial port
1861 static int siccuart_console_wait_key(struct console *co)
1863 struct SICC_port *port = &sicc_ports[co->index];
1866 while(!(readb(port->uart_base + BL_SICC_LSR) & _LSR_RBR_FULL));
1867 c = readb(port->uart_base + BL_SICC_RBR);
1871 static struct tty_driver *siccuart_console_device(struct console *c, int *index)
1874 return siccnormal_driver;
1877 static int __init siccuart_console_setup(struct console *co, char *options)
1879 struct SICC_port *port;
1883 u_int cflag = CREAD | HUPCL | CLOCAL;
1887 if (co->index >= SERIAL_SICC_NR)
1890 port = &sicc_ports[co->index];
1892 if (port->uart_base == 0)
1893 port->uart_base = (int)ioremap(port->uart_base_phys, PAGE_SIZE);
1897 baud = simple_strtoul(s, NULL, 10);
1898 while (*s >= '0' && *s <= '9')
1900 if (*s) parity = *s++;
1901 if (*s) bits = *s - '0';
1905 * Now construct a cflag setting.
1908 case 1200: cflag |= B1200; break;
1909 case 2400: cflag |= B2400; break;
1910 case 4800: cflag |= B4800; break;
1911 default: cflag |= B9600; baud = 9600; break;
1912 case 19200: cflag |= B19200; break;
1913 case 38400: cflag |= B38400; break;
1914 case 57600: cflag |= B57600; break;
1915 case 115200: cflag |= B115200; break;
1918 case 7: cflag |= CS7; lcr_h = _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT; break;
1919 default: cflag |= CS8; lcr_h = _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT; break;
1923 case 'O': cflag |= PARODD; lcr_h |= _LCR_PTY_ODD; break;
1925 case 'E': cflag |= PARENB; lcr_h |= _LCR_PE_ENABLE | _LCR_PTY_ODD; break;
1932 // a copy of is inserted here ppc403SetBaud(com_port, (int)9600);
1933 unsigned long divisor, clockSource, temp;
1934 unsigned int rate = baud;
1936 /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
1937 powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
1939 /* Determine Internal Baud Clock Frequency */
1940 /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
1941 /* SCCR (Serial Clock Control Register) on Vesta */
1942 temp = powerpcMfclkgpcr();
1944 if(temp & 0x00000080) {
1945 clockSource = 324000000;
1948 clockSource = 216000000;
1950 clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
1951 divisor = clockSource/(16*rate) - 1;
1952 /* divisor has only 12 bits of resolution */
1953 if(divisor>0x00000FFF){
1960 writeb((quot & 0x00000F00)>>8, port->uart_base + BL_SICC_BRDH );
1961 writeb( quot & 0x00000FF, port->uart_base + BL_SICC_BRDL );
1963 /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
1964 /* For now, do NOT use FIFOs since 403 UART did not have this */
1965 /* capability and this driver was inherited from 403UART. */
1966 writeb(_CTL2_EXTERN, port->uart_base + BL_SICC_CTL2);
1968 writeb(lcr_h, port->uart_base + BL_SICC_LCR);
1969 writeb(_RCR_ER_ENABLE | _RCR_PME_HARD, port->uart_base + BL_SICC_RCR);
1970 writeb( _TxCR_ET_ENABLE , port->uart_base + BL_SICC_TxCR);
1972 // writeb(, info->port->uart_base + BL_SICC_RCR );
1974 * Transmitter Command Register: Transmitter enabled & DMA + TBR interrupt
1975 * + Transmitter Empty interrupt + Transmitter error interrupt disabled &
1976 * Stop mode when CTS active enabled & Transmit Break + Pattern Generation
1980 writeb( 0x00, port->uart_base + BL_SICC_IrCR ); // disable IrDA
1982 readb(port->uart_base + BL_SICC_RBR);
1984 writeb(0xf8, port->uart_base + BL_SICC_LSR); /* reset bits 0-4 of LSR */
1986 /* we will enable the port as we need it */
1991 static struct console siccuart_cons =
1993 .name = SERIAL_SICC_NAME,
1994 .write = siccuart_console_write,
1995 #ifdef used_and_not_const_char_pointer
1996 .read = siccuart_console_read,
1998 .device = siccuart_console_device,
1999 .wait_key = siccuart_console_wait_key,
2000 .setup = siccuart_console_setup,
2001 .flags = CON_PRINTBUFFER,
2005 void __init sicc_console_init(void)
2007 register_console(&siccuart_cons);
2010 #endif /* CONFIG_SERIAL_SICC_CONSOLE */