patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc / 4xx_io / serial_sicc.c
1 /*
2  *  arch/ppc/4xx_io/serial_sicc.c
3  *
4  *  Driver for IBM STB3xxx SICC serial port
5  *
6  *  Based on drivers/char/serial_amba.c, by ARM Ltd.
7  *
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>
12  *
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.
17  *
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.
22  *
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
26  *
27  *
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.
31  */
32
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>
47 #include <linux/mm.h>
48 #include <linux/slab.h>
49 #include <linux/init.h>
50 #include <linux/circ_buf.h>
51 #include <linux/serial.h>
52 #include <linux/console.h>
53 #include <linux/sysrq.h>
54
55 #include <asm/system.h>
56 #include <asm/io.h>
57 #include <asm/irq.h>
58 #include <asm/uaccess.h>
59 #include <asm/bitops.h>
60 #include <asm/serial.h>
61
62
63 #include <linux/serialP.h>
64
65
66 /* -----------------------------------------------------------------------------
67  *  From STB03xxx SICC UART Specification
68  * -----------------------------------------------------------------------------
69  *  UART Register Offsets.
70  */
71
72 #define BL_SICC_LSR   0x0000000      /* line status register read/clear */
73 #define BL_SICC_LSRS  0x0000001      /* set line status register read/set */
74 #define BL_SICC_HSR   0x0000002      /* handshake status register r/clear */
75 #define BL_SICC_HSRS  0x0000003      /* set handshake status register r/set */
76 #define BL_SICC_BRDH  0x0000004      /* baudrate divisor high reg r/w */
77 #define BL_SICC_BRDL  0x0000005      /* baudrate divisor low reg r/w */
78 #define BL_SICC_LCR   0x0000006      /* control register r/w */
79 #define BL_SICC_RCR   0x0000007      /* receiver command register r/w */
80 #define BL_SICC_TxCR  0x0000008      /* transmitter command register r/w */
81 #define BL_SICC_RBR   0x0000009      /* receive buffer r */
82 #define BL_SICC_TBR   0x0000009      /* transmit buffer w */
83 #define BL_SICC_CTL2  0x000000A      /* added for Vesta */
84 #define BL_SICC_IrCR  0x000000B      /* added for Vesta IR */
85
86 /* masks and definitions for serial port control register */
87
88 #define _LCR_LM_MASK  0xc0            /* loop back modes */
89 #define _LCR_DTR_MASK 0x20            /* data terminal ready 0-inactive */
90 #define _LCR_RTS_MASK 0x10            /* request to send 0-inactive */
91 #define _LCR_DB_MASK  0x08            /* data bits mask */
92 #define _LCR_PE_MASK  0x04            /* parity enable */
93 #define _LCR_PTY_MASK 0x02            /* parity */
94 #define _LCR_SB_MASK  0x01            /* stop bit mask */
95
96 #define _LCR_LM_NORM  0x00            /* normal operation */
97 #define _LCR_LM_LOOP  0x40            /* internal loopback mode */
98 #define _LCR_LM_ECHO  0x80            /* automatic echo mode */
99 #define _LCR_LM_RES   0xc0            /* reserved */
100
101 #define _LCR_DTR_ACTIVE       _LCR_DTR_MASK /* DTR is active */
102 #define _LCR_RTS_ACTIVE       _LCR_RTS_MASK /* RTS is active */
103 #define _LCR_DB_8_BITS        _LCR_DB_MASK  /*  8 data bits */
104 #define _LCR_DB_7_BITS        0x00          /*  7 data bits */
105 #define _LCR_PE_ENABLE        _LCR_PE_MASK  /* parity enabled */
106 #define _LCR_PE_DISABLE       0x00          /* parity disabled */
107 #define _LCR_PTY_EVEN         0x00          /* even parity */
108 #define _LCR_PTY_ODD          _LCR_PTY_MASK /* odd parity */
109 #define _LCR_SB_1_BIT         0x00          /* one stop bit */
110 #define _LCR_SB_2_BIT         _LCR_SB_MASK  /* two stop bit */
111
112 /* serial port handshake register */
113
114 #define _HSR_DIS_MASK  0x80            /* DSR input inactive error mask */
115 #define _HSR_CS_MASK   0x40            /* CTS input inactive error mask */
116 #define _HSR_DIS_ACT   0x00            /* dsr input is active */
117 #define _HSR_DIS_INACT _HSR_DIS_MASK   /* dsr input is inactive */
118 #define _HSR_CS_ACT    0x00            /* cts input is active */
119 #define _HSR_CS_INACT  _HSR_CS_MASK    /* cts input is active */
120
121 /* serial port line status register */
122
123 #define _LSR_RBR_MASK  0x80            /* receive buffer ready mask */
124 #define _LSR_FE_MASK   0x40            /* framing error */
125 #define _LSR_OE_MASK   0x20            /* overrun error */
126 #define _LSR_PE_MASK   0x10            /* parity error */
127 #define _LSR_LB_MASK   0x08            /* line break */
128 #define _LSR_TBR_MASK  0x04            /* transmit buffer ready */
129 #define _LSR_TSR_MASK  0x02            /* transmit shift register ready */
130
131 #define _LSR_RBR_FULL  _LSR_RBR_MASK  /* receive buffer is full */
132 #define _LSR_FE_ERROR  _LSR_FE_MASK   /* framing error detected */
133 #define _LSR_OE_ERROR  _LSR_OE_MASK   /* overrun error detected */
134 #define _LSR_PE_ERROR  _LSR_PE_MASK   /* parity error detected */
135 #define _LSR_LB_BREAK  _LSR_LB_MASK   /* line break detected */
136 #define _LSR_TBR_EMPTY _LSR_TBR_MASK  /* transmit buffer is ready */
137 #define _LSR_TSR_EMPTY _LSR_TSR_MASK  /* transmit shift register is empty */
138 #define _LSR_TX_ALL    0x06           /* all physical transmit is done */
139
140 #define _LSR_RX_ERR    (_LSR_LB_BREAK | _LSR_FE_MASK | _LSR_OE_MASK | \
141                          _LSR_PE_MASK )
142
143 /* serial port receiver command register */
144
145 #define _RCR_ER_MASK   0x80           /* enable receiver mask */
146 #define _RCR_DME_MASK  0x60           /* dma mode */
147 #define _RCR_EIE_MASK  0x10           /* error interrupt enable mask */
148 #define _RCR_PME_MASK  0x08           /* pause mode mask */
149
150 #define _RCR_ER_ENABLE _RCR_ER_MASK   /* receiver enabled */
151 #define _RCR_DME_DISABLE 0x00         /* dma disabled */
152 #define _RCR_DME_RXRDY 0x20           /* dma disabled, RxRDY interrupt enabled*/
153 #define _RCR_DME_ENABLE2 0x40         /* dma enabled,receiver src channel 2 */
154 #define _RCR_DME_ENABLE3 0x60         /* dma enabled,receiver src channel 3 */
155 #define _RCR_PME_HARD  _RCR_PME_MASK  /* RTS controlled by hardware */
156 #define _RCR_PME_SOFT  0x00           /* RTS controlled by software */
157
158 /* serial port transmit command register */
159
160 #define _TxCR_ET_MASK   0x80           /* transmiter enable mask */
161 #define _TxCR_DME_MASK  0x60           /* dma mode mask */
162 #define _TxCR_TIE_MASK  0x10           /* empty interrupt enable mask */
163 #define _TxCR_EIE_MASK  0x08           /* error interrupt enable mask */
164 #define _TxCR_SPE_MASK  0x04           /* stop/pause mask */
165 #define _TxCR_TB_MASK   0x02           /* transmit break mask */
166
167 #define _TxCR_ET_ENABLE _TxCR_ET_MASK  /* transmiter enabled */
168 #define _TxCR_DME_DISABLE 0x00         /* transmiter disabled, TBR intr disabled */
169 #define _TxCR_DME_TBR   0x20           /* transmiter disabled, TBR intr enabled */
170 #define _TxCR_DME_CHAN_2 0x40          /* dma enabled, destination chann 2 */
171 #define _TxCR_DME_CHAN_3 0x60          /* dma enabled, destination chann 3 */
172
173 /* serial ctl reg 2 - added for Vesta */
174
175 #define _CTL2_EXTERN  0x80            /*  */
176 #define _CTL2_USEFIFO 0x40            /*  */
177 #define _CTL2_RESETRF 0x08            /*  */
178 #define _CTL2_RESETTF 0x04            /*  */
179
180
181
182 #define SERIAL_SICC_NAME    "ttySICC"
183 #define SERIAL_SICC_MAJOR   150
184 #define SERIAL_SICC_MINOR   1
185 #define SERIAL_SICC_NR      1
186
187 #ifndef TRUE
188 #define TRUE 1
189 #endif
190 #ifndef FALSE
191 #define FALSE 0
192 #endif
193
194 /*
195  * Things needed by tty driver
196  */
197 static struct tty_driver *siccnormal_driver;
198
199 #if defined(CONFIG_SERIAL_SICC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
200 #define SUPPORT_SYSRQ
201 #endif
202
203 /*
204  * Things needed internally to this driver
205  */
206
207 /*
208  * tmp_buf is used as a temporary buffer by serial_write.  We need to
209  * lock it in case the copy_from_user blocks while swapping in a page,
210  * and some other program tries to do a serial write at the same time.
211  * Since the lock will only come under contention when the system is
212  * swapping and available memory is low, it makes sense to share one
213  * buffer across all the serial ports, since it significantly saves
214  * memory if large numbers of serial ports are open.
215  */
216 static u_char *tmp_buf;
217 static DECLARE_MUTEX(tmp_buf_sem);
218
219 #define HIGH_BITS_OFFSET    ((sizeof(long)-sizeof(int))*8)
220
221 /* number of characters left in xmit buffer before we ask for more */
222 #define WAKEUP_CHARS        256
223 #define SICC_ISR_PASS_LIMIT 256
224
225 #define EVT_WRITE_WAKEUP    0
226
227 struct SICC_icount {
228     __u32   cts;
229     __u32   dsr;
230     __u32   rng;
231     __u32   dcd;
232     __u32   rx;
233     __u32   tx;
234     __u32   frame;
235     __u32   overrun;
236     __u32   parity;
237     __u32   brk;
238     __u32   buf_overrun;
239 };
240
241 /*
242  * Static information about the port
243  */
244 struct SICC_port {
245     unsigned int        uart_base;
246     unsigned int        uart_base_phys;
247     unsigned int        irqrx;
248     unsigned int        irqtx;
249     unsigned int        uartclk;
250     unsigned int        fifosize;
251     unsigned int        tiocm_support;
252     void (*set_mctrl)(struct SICC_port *, u_int mctrl);
253 };
254
255 /*
256  * This is the state information which is persistent across opens
257  */
258 struct SICC_state {
259     struct SICC_icount  icount;
260     unsigned int        line;
261     unsigned int        close_delay;
262     unsigned int        closing_wait;
263     unsigned int        custom_divisor;
264     unsigned int        flags;
265     int         count;
266     struct SICC_info    *info;
267 };
268
269 #define SICC_XMIT_SIZE 1024
270 /*
271  * This is the state information which is only valid when the port is open.
272  */
273 struct SICC_info {
274     struct SICC_port    *port;
275     struct SICC_state   *state;
276     struct tty_struct   *tty;
277     unsigned char       x_char;
278     unsigned char       old_status;
279     unsigned char       read_status_mask;
280     unsigned char       ignore_status_mask;
281     struct circ_buf     xmit;
282     unsigned int        flags;
283 #ifdef SUPPORT_SYSRQ
284     unsigned long       sysrq;
285 #endif
286
287     unsigned int        event;
288     unsigned int        timeout;
289     unsigned int        lcr_h;
290     unsigned int        mctrl;
291     int         blocked_open;
292
293     struct tasklet_struct   tlet;
294
295     wait_queue_head_t   open_wait;
296     wait_queue_head_t   close_wait;
297     wait_queue_head_t   delta_msr_wait;
298 };
299
300 #ifdef CONFIG_SERIAL_SICC_CONSOLE
301 static struct console siccuart_cons;
302 #endif
303 static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios);
304 static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout);
305
306
307
308 static void powerpcMtcic_cr(unsigned long value)
309 {
310     mtdcr(DCRN_CICCR, value);
311 }
312
313 static unsigned long powerpcMfcic_cr(void)
314 {
315     return mfdcr(DCRN_CICCR);
316 }
317
318 static unsigned long powerpcMfclkgpcr(void)
319 {
320     return mfdcr(DCRN_SCCR);
321 }
322
323 static void sicc_set_mctrl_null(struct SICC_port *port, u_int mctrl)
324 {
325 }
326
327 static struct SICC_port sicc_ports[SERIAL_SICC_NR] = {
328     {
329         .uart_base = 0,
330         .uart_base_phys = SICC0_IO_BASE,
331         .irqrx =    SICC0_INTRX,
332         .irqtx =    SICC0_INTTX,
333 //      .uartclk =    0,
334         .fifosize = 1,
335         .set_mctrl = sicc_set_mctrl_null,
336     }
337 };
338
339 static struct SICC_state sicc_state[SERIAL_SICC_NR];
340
341 static void siccuart_enable_rx_interrupt(struct SICC_info *info)
342 {
343     unsigned char cr;
344
345     cr = readb(info->port->uart_base+BL_SICC_RCR);
346     cr &= ~_RCR_DME_MASK;
347     cr |= _RCR_DME_RXRDY;
348     writeb(cr, info->port->uart_base+BL_SICC_RCR);
349 }
350
351 static void siccuart_disable_rx_interrupt(struct SICC_info *info)
352 {
353     unsigned char cr;
354
355     cr = readb(info->port->uart_base+BL_SICC_RCR);
356     cr &= ~_RCR_DME_MASK;
357     cr |=  _RCR_DME_DISABLE;
358     writeb(cr, info->port->uart_base+BL_SICC_RCR);
359 }
360
361
362 static void siccuart_enable_tx_interrupt(struct SICC_info *info)
363 {
364     unsigned char cr;
365
366     cr = readb(info->port->uart_base+BL_SICC_TxCR);
367     cr &= ~_TxCR_DME_MASK;
368     cr |= _TxCR_DME_TBR;
369     writeb(cr, info->port->uart_base+BL_SICC_TxCR);
370 }
371
372 static void siccuart_disable_tx_interrupt(struct SICC_info *info)
373 {
374     unsigned char cr;
375
376     cr = readb(info->port->uart_base+BL_SICC_TxCR);
377     cr &= ~_TxCR_DME_MASK;
378     cr |=  _TxCR_DME_DISABLE;
379     writeb(cr, info->port->uart_base+BL_SICC_TxCR);
380 }
381
382
383 static void siccuart_stop(struct tty_struct *tty)
384 {
385     struct SICC_info *info = tty->driver_data;
386     unsigned long flags;
387
388     save_flags(flags); cli();
389     siccuart_disable_tx_interrupt(info);
390     restore_flags(flags);
391 }
392
393 static void siccuart_start(struct tty_struct *tty)
394 {
395     struct SICC_info *info = tty->driver_data;
396     unsigned long flags;
397
398     save_flags(flags); cli();
399     if (info->xmit.head != info->xmit.tail
400         && info->xmit.buf)
401         siccuart_enable_tx_interrupt(info);
402     restore_flags(flags);
403 }
404
405
406 /*
407  * This routine is used by the interrupt handler to schedule
408  * processing in the software interrupt portion of the driver.
409  */
410 static void siccuart_event(struct SICC_info *info, int event)
411 {
412     info->event |= 1 << event;
413     tasklet_schedule(&info->tlet);
414 }
415
416 static void
417 siccuart_rx_chars(struct SICC_info *info, struct pt_regs *regs)
418 {
419     struct tty_struct *tty = info->tty;
420     unsigned int status, ch, rsr, flg, ignored = 0;
421     struct SICC_icount *icount = &info->state->icount;
422     struct SICC_port *port = info->port;
423
424     status = readb(port->uart_base+BL_SICC_LSR );
425     while (status & _LSR_RBR_FULL) {
426         ch = readb(port->uart_base+BL_SICC_RBR);
427
428         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
429             goto ignore_char;
430         icount->rx++;
431
432         flg = TTY_NORMAL;
433
434         /*
435          * Note that the error handling code is
436          * out of the main execution path
437          */
438         rsr = readb(port->uart_base+BL_SICC_LSR);
439         if (rsr & _LSR_RX_ERR)
440             goto handle_error;
441 #ifdef SUPPORT_SYSRQ
442         if (info->sysrq) {
443             if (ch && time_before(jiffies, info->sysrq)) {
444                 handle_sysrq(ch, regs, NULL);
445                 info->sysrq = 0;
446                 goto ignore_char;
447             }
448             info->sysrq = 0;
449         }
450 #endif
451     error_return:
452         *tty->flip.flag_buf_ptr++ = flg;
453         *tty->flip.char_buf_ptr++ = ch;
454         tty->flip.count++;
455     ignore_char:
456         status = readb(port->uart_base+BL_SICC_LSR );
457     }
458 out:
459     tty_flip_buffer_push(tty);
460     return;
461
462 handle_error:
463     if (rsr & _LSR_LB_BREAK) {
464         rsr &= ~(_LSR_FE_MASK | _LSR_PE_MASK);
465         icount->brk++;
466
467 #ifdef SUPPORT_SYSRQ
468         if (info->state->line == siccuart_cons.index) {
469             if (!info->sysrq) {
470                 info->sysrq = jiffies + HZ*5;
471                 goto ignore_char;
472             }
473         }
474 #endif
475     } else if (rsr & _LSR_PE_MASK)
476         icount->parity++;
477     else if (rsr & _LSR_FE_MASK)
478         icount->frame++;
479     if (rsr & _LSR_OE_MASK)
480         icount->overrun++;
481
482     if (rsr & info->ignore_status_mask) {
483         if (++ignored > 100)
484             goto out;
485         goto ignore_char;
486     }
487     rsr &= info->read_status_mask;
488
489     if (rsr & _LSR_LB_BREAK)
490         flg = TTY_BREAK;
491     else if (rsr &  _LSR_PE_MASK)
492         flg = TTY_PARITY;
493     else if (rsr &  _LSR_FE_MASK)
494         flg = TTY_FRAME;
495
496     if (rsr &  _LSR_OE_MASK) {
497         /*
498          * CHECK: does overrun affect the current character?
499          * ASSUMPTION: it does not.
500          */
501         *tty->flip.flag_buf_ptr++ = flg;
502         *tty->flip.char_buf_ptr++ = ch;
503         tty->flip.count++;
504         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
505             goto ignore_char;
506         ch = 0;
507         flg = TTY_OVERRUN;
508     }
509 #ifdef SUPPORT_SYSRQ
510     info->sysrq = 0;
511 #endif
512     goto error_return;
513 }
514
515 static void siccuart_tx_chars(struct SICC_info *info)
516 {
517     struct SICC_port *port = info->port;
518     int count;
519         unsigned char status;
520
521
522     if (info->x_char) {
523         writeb(info->x_char, port->uart_base+ BL_SICC_TBR);
524         info->state->icount.tx++;
525         info->x_char = 0;
526         return;
527     }
528     if (info->xmit.head == info->xmit.tail
529         || info->tty->stopped
530         || info->tty->hw_stopped) {
531         siccuart_disable_tx_interrupt(info);
532                 writeb(status&(~_LSR_RBR_MASK),port->uart_base+BL_SICC_LSR);
533         return;
534     }
535
536     count = port->fifosize;
537     do {
538         writeb(info->xmit.buf[info->xmit.tail], port->uart_base+ BL_SICC_TBR);
539         info->xmit.tail = (info->xmit.tail + 1) & (SICC_XMIT_SIZE - 1);
540         info->state->icount.tx++;
541         if (info->xmit.head == info->xmit.tail)
542             break;
543     } while (--count > 0);
544
545     if (CIRC_CNT(info->xmit.head,
546              info->xmit.tail,
547              SICC_XMIT_SIZE) < WAKEUP_CHARS)
548         siccuart_event(info, EVT_WRITE_WAKEUP);
549
550     if (info->xmit.head == info->xmit.tail) {
551         siccuart_disable_tx_interrupt(info);
552     }
553 }
554
555
556 static irqreturn_t siccuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
557 {
558     struct SICC_info *info = dev_id;
559     siccuart_rx_chars(info, regs);
560     return IRQ_HANDLED;
561 }
562
563
564 static irqreturn_t siccuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
565 {
566     struct SICC_info *info = dev_id;
567     siccuart_tx_chars(info);
568     return IRQ_HANDLED;
569 }
570
571 static void siccuart_tasklet_action(unsigned long data)
572 {
573     struct SICC_info *info = (struct SICC_info *)data;
574     struct tty_struct *tty;
575
576     tty = info->tty;
577     if (!tty || !test_and_clear_bit(EVT_WRITE_WAKEUP, &info->event))
578         return;
579
580     if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
581         tty->ldisc.write_wakeup)
582         (tty->ldisc.write_wakeup)(tty);
583     wake_up_interruptible(&tty->write_wait);
584 }
585
586 static int siccuart_startup(struct SICC_info *info)
587 {
588     unsigned long flags;
589     unsigned long page;
590     int retval = 0;
591
592     if (info->flags & ASYNC_INITIALIZED) {
593         return 0;
594     }
595
596     page = get_zeroed_page(GFP_KERNEL);
597     if (!page)
598         return -ENOMEM;
599
600     if (info->port->uart_base == 0)
601         info->port->uart_base = (int)ioremap(info->port->uart_base_phys, PAGE_SIZE);
602     if (info->port->uart_base == 0) {
603         free_page(page);
604         return -ENOMEM;
605     }
606
607     save_flags(flags); cli();
608
609     if (info->xmit.buf)
610         free_page(page);
611     else
612         info->xmit.buf = (unsigned char *) page;
613
614
615     info->mctrl = 0;
616     if (info->tty->termios->c_cflag & CBAUD)
617         info->mctrl = TIOCM_RTS | TIOCM_DTR;
618     info->port->set_mctrl(info->port, info->mctrl);
619
620     /*
621      * initialise the old status of the modem signals
622      */
623     info->old_status = 0; // UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;
624
625
626     if (info->tty)
627         clear_bit(TTY_IO_ERROR, &info->tty->flags);
628     info->xmit.head = info->xmit.tail = 0;
629
630     /*
631      * Set up the tty->alt_speed kludge
632      */
633     if (info->tty) {
634         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
635             info->tty->alt_speed = 57600;
636         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
637             info->tty->alt_speed = 115200;
638         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
639             info->tty->alt_speed = 230400;
640         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
641             info->tty->alt_speed = 460800;
642     }
643
644
645     writeb( 0x00, info->port->uart_base + BL_SICC_IrCR );  // disable IrDA
646
647
648     /*
649      * and set the speed of the serial port
650      */
651     siccuart_change_speed(info, 0);
652
653     // enable rx/tx ports
654     writeb(_RCR_ER_ENABLE /*| _RCR_PME_HARD*/, info->port->uart_base + BL_SICC_RCR);
655     writeb(_TxCR_ET_ENABLE               , info->port->uart_base + BL_SICC_TxCR);
656
657     readb(info->port->uart_base + BL_SICC_RBR); // clear rx port
658
659     writeb(0xf8, info->port->uart_base + BL_SICC_LSR);   /* reset bits 0-4 of LSR */
660
661     /*
662      * Finally, enable interrupts
663      */
664
665      /*
666      * Allocate the IRQ
667      */
668         retval = request_irq(info->port->irqrx, siccuart_int_rx, 0, "SICC rx", info);
669         if (retval) {
670              if (capable(CAP_SYS_ADMIN)) {
671                    if (info->tty)
672                           set_bit(TTY_IO_ERROR, &info->tty->flags);
673                    retval = 0;
674              }
675               goto errout;
676          }
677     retval = request_irq(info->port->irqtx, siccuart_int_tx, 0, "SICC tx", info);
678     if (retval) {
679         if (capable(CAP_SYS_ADMIN)) {
680             if (info->tty)
681                 set_bit(TTY_IO_ERROR, &info->tty->flags);
682             retval = 0;
683         }
684         free_irq(info->port->irqrx, info);
685         goto errout;
686     }
687
688     siccuart_enable_rx_interrupt(info);
689
690     info->flags |= ASYNC_INITIALIZED;
691     restore_flags(flags);
692     return 0;
693
694
695 errout:
696     restore_flags(flags);
697     return retval;
698 }
699
700 /*
701  * This routine will shutdown a serial port; interrupts are disabled, and
702  * DTR is dropped if the hangup on close termio flag is on.
703  */
704 static void siccuart_shutdown(struct SICC_info *info)
705 {
706     unsigned long flags;
707
708     if (!(info->flags & ASYNC_INITIALIZED))
709         return;
710
711     save_flags(flags); cli(); /* Disable interrupts */
712
713     /*
714      * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
715      * here so the queue might never be woken up
716      */
717     wake_up_interruptible(&info->delta_msr_wait);
718
719     /*
720      * disable all interrupts, disable the port
721      */
722     siccuart_disable_rx_interrupt(info);
723     siccuart_disable_tx_interrupt(info);
724
725     /*
726      * Free the IRQ
727      */
728     free_irq(info->port->irqtx, info);
729     free_irq(info->port->irqrx, info);
730
731     if (info->xmit.buf) {
732         unsigned long pg = (unsigned long) info->xmit.buf;
733         info->xmit.buf = NULL;
734         free_page(pg);
735     }
736
737
738     if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
739         info->mctrl &= ~(TIOCM_DTR|TIOCM_RTS);
740     info->port->set_mctrl(info->port, info->mctrl);
741
742     /* kill off our tasklet */
743     tasklet_kill(&info->tlet);
744     if (info->tty)
745         set_bit(TTY_IO_ERROR, &info->tty->flags);
746
747     info->flags &= ~ASYNC_INITIALIZED;
748
749     restore_flags(flags);
750 }
751
752
753 static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios)
754 {
755     unsigned int lcr_h, baud, quot, cflag, old_rcr, old_tcr, bits;
756     unsigned long flags;
757
758     if (!info->tty || !info->tty->termios)
759         return;
760
761     cflag = info->tty->termios->c_cflag;
762
763     pr_debug("siccuart_set_cflag(0x%x) called\n", cflag);
764     /* byte size and parity */
765     switch (cflag & CSIZE) {
766     case CS7: lcr_h =   _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT; bits = 9;  break;
767     default:  lcr_h =   _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT; bits = 10; break; // CS8
768     }
769     if (cflag & CSTOPB) {
770         lcr_h |= _LCR_SB_2_BIT;
771         bits ++;
772     }
773     if (cflag & PARENB) {
774         lcr_h |=  _LCR_PE_ENABLE;
775         bits++;
776         if (!(cflag & PARODD))
777             lcr_h |=  _LCR_PTY_ODD;
778         else
779             lcr_h |=  _LCR_PTY_EVEN;
780     }
781
782     do {
783         /* Determine divisor based on baud rate */
784         baud = tty_get_baud_rate(info->tty);
785         if (!baud)
786             baud = 9600;
787
788
789         {
790            // here is ppc403SetBaud(com_port, baud);
791            unsigned long divisor, clockSource, temp;
792
793            /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
794            powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
795
796            /* Determine Internal Baud Clock Frequency */
797            /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
798            /* SCCR (Serial Clock Control Register) on Vesta */
799            temp = powerpcMfclkgpcr();
800
801            if(temp & 0x00000080) {
802                clockSource = 324000000;
803            }
804            else {
805                clockSource = 216000000;
806            }
807            clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
808            divisor = clockSource/(16*baud) - 1;
809            /* divisor has only 12 bits of resolution */
810            if(divisor>0x00000FFF){
811                divisor=0x00000FFF;
812            }
813
814            quot = divisor;
815         }
816
817         if (baud == 38400 &&
818             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
819             quot = info->state->custom_divisor;
820
821         if (!quot && old_termios) {
822             info->tty->termios->c_cflag &= ~CBAUD;
823             info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
824             old_termios = NULL;
825         }
826     } while (quot == 0 && old_termios);
827
828     /* As a last resort, if the quotient is zero, default to 9600 bps */
829     if (!quot)
830         quot = (info->port->uartclk / (16 * 9600)) - 1;
831
832     info->timeout = info->port->fifosize * HZ * bits / baud;
833     info->timeout += HZ/50;     /* Add .02 seconds of slop */
834
835     if (cflag & CRTSCTS)
836         info->flags |= ASYNC_CTS_FLOW;
837     else
838         info->flags &= ~ASYNC_CTS_FLOW;
839     if (cflag & CLOCAL)
840         info->flags &= ~ASYNC_CHECK_CD;
841     else
842         info->flags |= ASYNC_CHECK_CD;
843
844     /*
845      * Set up parity check flag
846      */
847 #define RELEVENT_IFLAG(iflag)   ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
848
849     info->read_status_mask = _LSR_OE_MASK;
850     if (I_INPCK(info->tty))
851         info->read_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
852     if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
853         info->read_status_mask |= _LSR_LB_MASK;
854
855     /*
856      * Characters to ignore
857      */
858     info->ignore_status_mask = 0;
859     if (I_IGNPAR(info->tty))
860         info->ignore_status_mask |= _LSR_FE_MASK | _LSR_PE_MASK;
861     if (I_IGNBRK(info->tty)) {
862         info->ignore_status_mask |=  _LSR_LB_MASK;
863         /*
864          * If we're ignoring parity and break indicators,
865          * ignore overruns to (for real raw support).
866          */
867         if (I_IGNPAR(info->tty))
868             info->ignore_status_mask |=  _LSR_OE_MASK;
869     }
870
871     /* first, disable everything */
872     save_flags(flags); cli();
873
874     old_rcr = readb(info->port->uart_base + BL_SICC_RCR);
875     old_tcr = readb(info->port->uart_base + BL_SICC_TxCR);
876
877
878     writeb(0, info->port->uart_base + BL_SICC_RCR);
879     writeb(0, info->port->uart_base + BL_SICC_TxCR);
880
881     /*RLBtrace (&ppc403Chan0, 0x2000000c, 0, 0);*/
882
883
884     restore_flags(flags);
885
886
887     /* Set baud rate */
888     writeb((quot & 0x00000F00)>>8, info->port->uart_base + BL_SICC_BRDH );
889     writeb( quot & 0x00000FF,      info->port->uart_base + BL_SICC_BRDL );
890
891     /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
892     /* For now, do NOT use FIFOs since 403 UART did not have this    */
893     /* capability and this driver was inherited from 403UART.        */
894     writeb(_CTL2_EXTERN, info->port->uart_base + BL_SICC_CTL2);
895
896     writeb(lcr_h, info->port->uart_base + BL_SICC_LCR);
897
898     writeb(old_rcr, info->port->uart_base + BL_SICC_RCR);  // restore rcr
899     writeb(old_tcr, info->port->uart_base + BL_SICC_TxCR); // restore txcr
900
901 }
902
903
904 static void siccuart_put_char(struct tty_struct *tty, u_char ch)
905 {
906     struct SICC_info *info = tty->driver_data;
907     unsigned long flags;
908
909     if (!tty || !info->xmit.buf)
910         return;
911
912     save_flags(flags); cli();
913     if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE) != 0) {
914         info->xmit.buf[info->xmit.head] = ch;
915         info->xmit.head = (info->xmit.head + 1) & (SICC_XMIT_SIZE - 1);
916     }
917     restore_flags(flags);
918 }
919
920 static void siccuart_flush_chars(struct tty_struct *tty)
921 {
922     struct SICC_info *info = tty->driver_data;
923     unsigned long flags;
924
925     if (info->xmit.head == info->xmit.tail
926         || tty->stopped
927         || tty->hw_stopped
928         || !info->xmit.buf)
929         return;
930
931     save_flags(flags); cli();
932     siccuart_enable_tx_interrupt(info);
933     restore_flags(flags);
934 }
935
936 static int siccuart_write(struct tty_struct *tty, int from_user,
937               const u_char * buf, int count)
938 {
939     struct SICC_info *info = tty->driver_data;
940     unsigned long flags;
941     int c, ret = 0;
942
943     if (!tty || !info->xmit.buf || !tmp_buf)
944         return 0;
945
946     save_flags(flags);
947     if (from_user) {
948         down(&tmp_buf_sem);
949         while (1) {
950             int c1;
951             c = CIRC_SPACE_TO_END(info->xmit.head,
952                           info->xmit.tail,
953                           SICC_XMIT_SIZE);
954             if (count < c)
955                 c = count;
956             if (c <= 0)
957                 break;
958
959             c -= copy_from_user(tmp_buf, buf, c);
960             if (!c) {
961                 if (!ret)
962                     ret = -EFAULT;
963                 break;
964             }
965             cli();
966             c1 = CIRC_SPACE_TO_END(info->xmit.head,
967                            info->xmit.tail,
968                            SICC_XMIT_SIZE);
969             if (c1 < c)
970                 c = c1;
971             memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
972             info->xmit.head = (info->xmit.head + c) &
973                       (SICC_XMIT_SIZE - 1);
974             restore_flags(flags);
975             buf += c;
976             count -= c;
977             ret += c;
978         }
979         up(&tmp_buf_sem);
980     } else {
981         cli();
982         while (1) {
983             c = CIRC_SPACE_TO_END(info->xmit.head,
984                           info->xmit.tail,
985                           SICC_XMIT_SIZE);
986             if (count < c)
987                 c = count;
988             if (c <= 0)
989                 break;
990             memcpy(info->xmit.buf + info->xmit.head, buf, c);
991             info->xmit.head = (info->xmit.head + c) &
992                       (SICC_XMIT_SIZE - 1);
993             buf += c;
994             count -= c;
995             ret += c;
996         }
997         restore_flags(flags);
998     }
999     if (info->xmit.head != info->xmit.tail
1000         && !tty->stopped
1001         && !tty->hw_stopped)
1002         siccuart_enable_tx_interrupt(info);
1003     return ret;
1004 }
1005
1006 static int siccuart_write_room(struct tty_struct *tty)
1007 {
1008     struct SICC_info *info = tty->driver_data;
1009
1010     return CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
1011 }
1012
1013 static int siccuart_chars_in_buffer(struct tty_struct *tty)
1014 {
1015     struct SICC_info *info = tty->driver_data;
1016
1017     return CIRC_CNT(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
1018 }
1019
1020 static void siccuart_flush_buffer(struct tty_struct *tty)
1021 {
1022     struct SICC_info *info = tty->driver_data;
1023     unsigned long flags;
1024
1025     pr_debug("siccuart_flush_buffer(%d) called\n", tty->index);
1026     save_flags(flags); cli();
1027     info->xmit.head = info->xmit.tail = 0;
1028     restore_flags(flags);
1029     wake_up_interruptible(&tty->write_wait);
1030     if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1031         tty->ldisc.write_wakeup)
1032         (tty->ldisc.write_wakeup)(tty);
1033 }
1034
1035 /*
1036  * This function is used to send a high-priority XON/XOFF character to
1037  * the device
1038  */
1039 static void siccuart_send_xchar(struct tty_struct *tty, char ch)
1040 {
1041     struct SICC_info *info = tty->driver_data;
1042
1043     info->x_char = ch;
1044     if (ch)
1045        siccuart_enable_tx_interrupt(info);
1046 }
1047
1048 static void siccuart_throttle(struct tty_struct *tty)
1049 {
1050     struct SICC_info *info = tty->driver_data;
1051     unsigned long flags;
1052
1053     if (I_IXOFF(tty))
1054         siccuart_send_xchar(tty, STOP_CHAR(tty));
1055
1056     if (tty->termios->c_cflag & CRTSCTS) {
1057         save_flags(flags); cli();
1058         info->mctrl &= ~TIOCM_RTS;
1059         info->port->set_mctrl(info->port, info->mctrl);
1060         restore_flags(flags);
1061     }
1062 }
1063
1064 static void siccuart_unthrottle(struct tty_struct *tty)
1065 {
1066     struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1067     unsigned long flags;
1068
1069     if (I_IXOFF(tty)) {
1070         if (info->x_char)
1071             info->x_char = 0;
1072         else
1073             siccuart_send_xchar(tty, START_CHAR(tty));
1074     }
1075
1076     if (tty->termios->c_cflag & CRTSCTS) {
1077         save_flags(flags); cli();
1078         info->mctrl |= TIOCM_RTS;
1079         info->port->set_mctrl(info->port, info->mctrl);
1080         restore_flags(flags);
1081     }
1082 }
1083
1084 static int get_serial_info(struct SICC_info *info, struct serial_struct *retinfo)
1085 {
1086     struct SICC_state *state = info->state;
1087     struct SICC_port *port = info->port;
1088     struct serial_struct tmp;
1089
1090     memset(&tmp, 0, sizeof(tmp));
1091     tmp.type       = 0;
1092     tmp.line       = state->line;
1093     tmp.port       = port->uart_base;
1094     if (HIGH_BITS_OFFSET)
1095         tmp.port_high = port->uart_base >> HIGH_BITS_OFFSET;
1096     tmp.irq        = port->irqrx;
1097     tmp.flags      = 0;
1098     tmp.xmit_fifo_size = port->fifosize;
1099     tmp.baud_base      = port->uartclk / 16;
1100     tmp.close_delay    = state->close_delay;
1101     tmp.closing_wait   = state->closing_wait;
1102     tmp.custom_divisor = state->custom_divisor;
1103
1104     if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1105         return -EFAULT;
1106     return 0;
1107 }
1108
1109 static int set_serial_info(struct SICC_info *info,
1110                struct serial_struct *newinfo)
1111 {
1112     struct serial_struct new_serial;
1113     struct SICC_state *state, old_state;
1114     struct SICC_port *port;
1115     unsigned long new_port;
1116     unsigned int i, change_irq, change_port;
1117     int retval = 0;
1118
1119     if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1120         return -EFAULT;
1121
1122     state = info->state;
1123     old_state = *state;
1124     port = info->port;
1125
1126     new_port = new_serial.port;
1127     if (HIGH_BITS_OFFSET)
1128         new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1129
1130     change_irq  = new_serial.irq != port->irqrx;
1131     change_port = new_port != port->uart_base;
1132
1133     if (!capable(CAP_SYS_ADMIN)) {
1134         if (change_irq || change_port ||
1135             (new_serial.baud_base != port->uartclk / 16) ||
1136             (new_serial.close_delay != state->close_delay) ||
1137             (new_serial.xmit_fifo_size != port->fifosize) ||
1138             ((new_serial.flags & ~ASYNC_USR_MASK) !=
1139              (state->flags & ~ASYNC_USR_MASK)))
1140             return -EPERM;
1141         state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1142                 (new_serial.flags & ASYNC_USR_MASK));
1143         info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1144                    (new_serial.flags & ASYNC_USR_MASK));
1145         state->custom_divisor = new_serial.custom_divisor;
1146         goto check_and_exit;
1147     }
1148
1149     if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
1150         (new_serial.baud_base < 9600))
1151         return -EINVAL;
1152
1153     if (new_serial.type && change_port) {
1154         for (i = 0; i < SERIAL_SICC_NR; i++)
1155             if ((port != sicc_ports + i) &&
1156                 sicc_ports[i].uart_base != new_port)
1157                 return -EADDRINUSE;
1158     }
1159
1160     if ((change_port || change_irq) && (state->count > 1))
1161         return -EBUSY;
1162
1163     /*
1164      * OK, past this point, all the error checking has been done.
1165      * At this point, we start making changes.....
1166      */
1167     port->uartclk = new_serial.baud_base * 16;
1168     state->flags = ((state->flags & ~ASYNC_FLAGS) |
1169             (new_serial.flags & ASYNC_FLAGS));
1170     info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1171                (info->flags & ASYNC_INTERNAL_FLAGS));
1172     state->custom_divisor = new_serial.custom_divisor;
1173     state->close_delay = new_serial.close_delay * HZ / 100;
1174     state->closing_wait = new_serial.closing_wait * HZ / 100;
1175     info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1176     port->fifosize = new_serial.xmit_fifo_size;
1177
1178     if (change_port || change_irq) {
1179         /*
1180          * We need to shutdown the serial port at the old
1181          * port/irq combination.
1182          */
1183         siccuart_shutdown(info);
1184         port->irqrx = new_serial.irq;
1185         port->uart_base = new_port;
1186     }
1187
1188 check_and_exit:
1189     if (!port->uart_base)
1190         return 0;
1191     if (info->flags & ASYNC_INITIALIZED) {
1192         if ((old_state.flags & ASYNC_SPD_MASK) !=
1193             (state->flags & ASYNC_SPD_MASK) ||
1194             (old_state.custom_divisor != state->custom_divisor)) {
1195             if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1196                 info->tty->alt_speed = 57600;
1197             if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1198                 info->tty->alt_speed = 115200;
1199             if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1200                 info->tty->alt_speed = 230400;
1201             if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1202                 info->tty->alt_speed = 460800;
1203             siccuart_change_speed(info, NULL);
1204         }
1205     } else
1206         retval = siccuart_startup(info);
1207     return retval;
1208 }
1209
1210
1211 /*
1212  * get_lsr_info - get line status register info
1213  */
1214 static int get_lsr_info(struct SICC_info *info, unsigned int *value)
1215 {
1216     unsigned int result, status;
1217     unsigned long flags;
1218
1219     save_flags(flags); cli();
1220     status = readb(info->port->uart_base +  BL_SICC_LSR);
1221     restore_flags(flags);
1222     result = status & _LSR_TSR_EMPTY ? TIOCSER_TEMT : 0;
1223
1224     /*
1225      * If we're about to load something into the transmit
1226      * register, we'll pretend the transmitter isn't empty to
1227      * avoid a race condition (depending on when the transmit
1228      * interrupt happens).
1229      */
1230     if (info->x_char ||
1231         ((CIRC_CNT(info->xmit.head, info->xmit.tail,
1232                SICC_XMIT_SIZE) > 0) &&
1233          !info->tty->stopped && !info->tty->hw_stopped))
1234         result &= TIOCSER_TEMT;
1235
1236     return put_user(result, value);
1237 }
1238
1239 static int get_modem_info(struct SICC_info *info, unsigned int *value)
1240 {
1241     unsigned int result = info->mctrl;
1242
1243     return put_user(result, value);
1244 }
1245
1246 static int set_modem_info(struct SICC_info *info, unsigned int cmd,
1247               unsigned int *value)
1248 {
1249     unsigned int arg, old;
1250     unsigned long flags;
1251
1252     if (get_user(arg, value))
1253         return -EFAULT;
1254
1255     old = info->mctrl;
1256     switch (cmd) {
1257     case TIOCMBIS:
1258         info->mctrl |= arg;
1259         break;
1260
1261     case TIOCMBIC:
1262         info->mctrl &= ~arg;
1263         break;
1264
1265     case TIOCMSET:
1266         info->mctrl = arg;
1267         break;
1268
1269     default:
1270         return -EINVAL;
1271     }
1272     save_flags(flags); cli();
1273     if (old != info->mctrl)
1274         info->port->set_mctrl(info->port, info->mctrl);
1275     restore_flags(flags);
1276     return 0;
1277 }
1278
1279 static void siccuart_break_ctl(struct tty_struct *tty, int break_state)
1280 {
1281     struct SICC_info *info = tty->driver_data;
1282     unsigned long flags;
1283     unsigned int lcr_h;
1284
1285
1286     save_flags(flags); cli();
1287     lcr_h = readb(info->port + BL_SICC_LSR);
1288     if (break_state == -1)
1289         lcr_h |=  _LSR_LB_MASK;
1290     else
1291         lcr_h &= ~_LSR_LB_MASK;
1292     writeb(lcr_h, info->port + BL_SICC_LSRS);
1293     restore_flags(flags);
1294 }
1295
1296 static int siccuart_ioctl(struct tty_struct *tty, struct file *file,
1297                unsigned int cmd, unsigned long arg)
1298 {
1299     struct SICC_info *info = tty->driver_data;
1300     struct SICC_icount cnow;
1301     struct serial_icounter_struct icount;
1302     unsigned long flags;
1303
1304     if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1305         (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1306         (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1307         if (tty->flags & (1 << TTY_IO_ERROR))
1308             return -EIO;
1309     }
1310
1311     switch (cmd) {
1312         case TIOCMGET:
1313             return get_modem_info(info, (unsigned int *)arg);
1314         case TIOCMBIS:
1315         case TIOCMBIC:
1316         case TIOCMSET:
1317             return set_modem_info(info, cmd, (unsigned int *)arg);
1318         case TIOCGSERIAL:
1319             return get_serial_info(info,
1320                            (struct serial_struct *)arg);
1321         case TIOCSSERIAL:
1322             return set_serial_info(info,
1323                            (struct serial_struct *)arg);
1324         case TIOCSERGETLSR: /* Get line status register */
1325             return get_lsr_info(info, (unsigned int *)arg);
1326         /*
1327          * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1328          * - mask passed in arg for lines of interest
1329          *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1330          * Caller should use TIOCGICOUNT to see which one it was
1331          */
1332         case TIOCMIWAIT:
1333             return 0;
1334         /*
1335          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1336          * Return: write counters to the user passed counter struct
1337          * NB: both 1->0 and 0->1 transitions are counted except for
1338          *     RI where only 0->1 is counted.
1339          */
1340         case TIOCGICOUNT:
1341             save_flags(flags); cli();
1342             cnow = info->state->icount;
1343             restore_flags(flags);
1344             icount.cts = cnow.cts;
1345             icount.dsr = cnow.dsr;
1346             icount.rng = cnow.rng;
1347             icount.dcd = cnow.dcd;
1348             icount.rx  = cnow.rx;
1349             icount.tx  = cnow.tx;
1350             icount.frame = cnow.frame;
1351             icount.overrun = cnow.overrun;
1352             icount.parity = cnow.parity;
1353             icount.brk = cnow.brk;
1354             icount.buf_overrun = cnow.buf_overrun;
1355
1356             return copy_to_user((void *)arg, &icount, sizeof(icount))
1357                     ? -EFAULT : 0;
1358
1359         default:
1360             return -ENOIOCTLCMD;
1361     }
1362     return 0;
1363 }
1364
1365 static void siccuart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1366 {
1367     struct SICC_info *info = tty->driver_data;
1368     unsigned long flags;
1369     unsigned int cflag = tty->termios->c_cflag;
1370
1371     if ((cflag ^ old_termios->c_cflag) == 0 &&
1372         RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1373         return;
1374
1375     siccuart_change_speed(info, old_termios);
1376
1377     /* Handle transition to B0 status */
1378     if ((old_termios->c_cflag & CBAUD) &&
1379         !(cflag & CBAUD)) {
1380         save_flags(flags); cli();
1381         info->mctrl &= ~(TIOCM_RTS | TIOCM_DTR);
1382         info->port->set_mctrl(info->port, info->mctrl);
1383         restore_flags(flags);
1384     }
1385
1386     /* Handle transition away from B0 status */
1387     if (!(old_termios->c_cflag & CBAUD) &&
1388         (cflag & CBAUD)) {
1389         save_flags(flags); cli();
1390         info->mctrl |= TIOCM_DTR;
1391         if (!(cflag & CRTSCTS) ||
1392             !test_bit(TTY_THROTTLED, &tty->flags))
1393             info->mctrl |= TIOCM_RTS;
1394         info->port->set_mctrl(info->port, info->mctrl);
1395         restore_flags(flags);
1396     }
1397
1398     /* Handle turning off CRTSCTS */
1399     if ((old_termios->c_cflag & CRTSCTS) &&
1400         !(cflag & CRTSCTS)) {
1401         tty->hw_stopped = 0;
1402         siccuart_start(tty);
1403     }
1404
1405 #if 0
1406     /*
1407      * No need to wake up processes in open wait, since they
1408      * sample the CLOCAL flag once, and don't recheck it.
1409      * XXX  It's not clear whether the current behavior is correct
1410      * or not.  Hence, this may change.....
1411      */
1412     if (!(old_termios->c_cflag & CLOCAL) &&
1413         (tty->termios->c_cflag & CLOCAL))
1414         wake_up_interruptible(&info->open_wait);
1415 #endif
1416 }
1417
1418 static void siccuart_close(struct tty_struct *tty, struct file *filp)
1419 {
1420     struct SICC_info *info = tty->driver_data;
1421     struct SICC_state *state;
1422     unsigned long flags;
1423
1424     if (!info)
1425         return;
1426
1427     state = info->state;
1428
1429     //pr_debug("siccuart_close() called\n");
1430
1431     save_flags(flags); cli();
1432
1433     if (tty_hung_up_p(filp)) {
1434         restore_flags(flags);
1435         return;
1436     }
1437
1438     if ((tty->count == 1) && (state->count != 1)) {
1439         /*
1440          * Uh, oh.  tty->count is 1, which means that the tty
1441          * structure will be freed.  state->count should always
1442          * be one in these conditions.  If it's greater than
1443          * one, we've got real problems, since it means the
1444          * serial port won't be shutdown.
1445          */
1446         printk("siccuart_close: bad serial port count; tty->count is 1, state->count is %d\n", state->count);
1447         state->count = 1;
1448     }
1449     if (--state->count < 0) {
1450         printk("rs_close: bad serial port count for %s: %d\n", tty->name, state->count);
1451         state->count = 0;
1452     }
1453     if (state->count) {
1454         restore_flags(flags);
1455         return;
1456     }
1457     info->flags |= ASYNC_CLOSING;
1458     restore_flags(flags);
1459     /*
1460      * Now we wait for the transmit buffer to clear; and we notify
1461      * the line discipline to only process XON/XOFF characters.
1462      */
1463     tty->closing = 1;
1464     if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1465         tty_wait_until_sent(tty, info->state->closing_wait);
1466     /*
1467      * At this point, we stop accepting input.  To do this, we
1468      * disable the receive line status interrupts.
1469      */
1470     if (info->flags & ASYNC_INITIALIZED) {
1471         siccuart_disable_rx_interrupt(info);
1472         /*
1473          * Before we drop DTR, make sure the UART transmitter
1474          * has completely drained; this is especially
1475          * important if there is a transmit FIFO!
1476          */
1477         siccuart_wait_until_sent(tty, info->timeout);
1478     }
1479     siccuart_shutdown(info);
1480     if (tty->driver->flush_buffer)
1481         tty->driver->flush_buffer(tty);
1482     if (tty->ldisc.flush_buffer)
1483         tty->ldisc.flush_buffer(tty);
1484     tty->closing = 0;
1485     info->event = 0;
1486     info->tty = NULL;
1487     if (info->blocked_open) {
1488         if (info->state->close_delay) {
1489             set_current_state(TASK_INTERRUPTIBLE);
1490             schedule_timeout(info->state->close_delay);
1491         }
1492         wake_up_interruptible(&info->open_wait);
1493     }
1494     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1495     wake_up_interruptible(&info->close_wait);
1496 }
1497
1498 static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout)
1499 {
1500     struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1501     unsigned long char_time, expire;
1502
1503     if (info->port->fifosize == 0)
1504         return;
1505
1506     /*
1507      * Set the check interval to be 1/5 of the estimated time to
1508      * send a single character, and make it at least 1.  The check
1509      * interval should also be less than the timeout.
1510      *
1511      * Note: we have to use pretty tight timings here to satisfy
1512      * the NIST-PCTS.
1513      */
1514     char_time = (info->timeout - HZ/50) / info->port->fifosize;
1515     char_time = char_time / 5;
1516     if (char_time == 0)
1517         char_time = 1;
1518
1519     // Crazy!!   sometimes the input arg 'timeout' can be negtive numbers  :-(
1520     if (timeout >= 0 && timeout < char_time)
1521         char_time = timeout;
1522     /*
1523      * If the transmitter hasn't cleared in twice the approximate
1524      * amount of time to send the entire FIFO, it probably won't
1525      * ever clear.  This assumes the UART isn't doing flow
1526      * control, which is currently the case.  Hence, if it ever
1527      * takes longer than info->timeout, this is probably due to a
1528      * UART bug of some kind.  So, we clamp the timeout parameter at
1529      * 2*info->timeout.
1530      */
1531     if (!timeout || timeout > 2 * info->timeout)
1532         timeout = 2 * info->timeout;
1533
1534     expire = jiffies + timeout;
1535     pr_debug("siccuart_wait_until_sent(%d), jiff=%lu, expire=%lu  char_time=%lu...\n",
1536            tty->index, jiffies,
1537            expire, char_time);
1538     while ((readb(info->port->uart_base + BL_SICC_LSR) & _LSR_TX_ALL) != _LSR_TX_ALL) {
1539         set_current_state(TASK_INTERRUPTIBLE);
1540         schedule_timeout(char_time);
1541         if (signal_pending(current))
1542             break;
1543         if (timeout && time_after(jiffies, expire))
1544             break;
1545     }
1546     set_current_state(TASK_RUNNING);
1547 }
1548
1549 static void siccuart_hangup(struct tty_struct *tty)
1550 {
1551     struct SICC_info *info = tty->driver_data;
1552     struct SICC_state *state = info->state;
1553
1554     siccuart_flush_buffer(tty);
1555     if (info->flags & ASYNC_CLOSING)
1556         return;
1557     siccuart_shutdown(info);
1558     info->event = 0;
1559     state->count = 0;
1560     info->flags &= ~ASYNC_NORMAL_ACTIVE;
1561     info->tty = NULL;
1562     wake_up_interruptible(&info->open_wait);
1563 }
1564
1565 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1566                struct SICC_info *info)
1567 {
1568     DECLARE_WAITQUEUE(wait, current);
1569     struct SICC_state *state = info->state;
1570     unsigned long flags;
1571     int do_clocal = 0, extra_count = 0, retval;
1572
1573     /*
1574      * If the device is in the middle of being closed, then block
1575      * until it's done, and then try again.
1576      */
1577     if (tty_hung_up_p(filp) ||
1578         (info->flags & ASYNC_CLOSING)) {
1579         if (info->flags & ASYNC_CLOSING)
1580             interruptible_sleep_on(&info->close_wait);
1581         return (info->flags & ASYNC_HUP_NOTIFY) ?
1582             -EAGAIN : -ERESTARTSYS;
1583     }
1584
1585     /*
1586      * If non-blocking mode is set, or the port is not enabled,
1587      * then make the check up front and then exit.
1588      */
1589     if ((filp->f_flags & O_NONBLOCK) ||
1590         (tty->flags & (1 << TTY_IO_ERROR))) {
1591         info->flags |= ASYNC_NORMAL_ACTIVE;
1592         return 0;
1593     }
1594
1595     if (tty->termios->c_cflag & CLOCAL)
1596         do_clocal = 1;
1597
1598     /*
1599      * Block waiting for the carrier detect and the line to become
1600      * free (i.e., not in use by the callout).  While we are in
1601      * this loop, state->count is dropped by one, so that
1602      * rs_close() knows when to free things.  We restore it upon
1603      * exit, either normal or abnormal.
1604      */
1605     retval = 0;
1606     add_wait_queue(&info->open_wait, &wait);
1607     save_flags(flags); cli();
1608     if (!tty_hung_up_p(filp)) {
1609         extra_count = 1;
1610         state->count--;
1611     }
1612     restore_flags(flags);
1613     info->blocked_open++;
1614     while (1) {
1615         save_flags(flags); cli();
1616         if (tty->termios->c_cflag & CBAUD) {
1617             info->mctrl = TIOCM_DTR | TIOCM_RTS;
1618             info->port->set_mctrl(info->port, info->mctrl);
1619         }
1620         restore_flags(flags);
1621         set_current_state(TASK_INTERRUPTIBLE);
1622         if (tty_hung_up_p(filp) ||
1623             !(info->flags & ASYNC_INITIALIZED)) {
1624             if (info->flags & ASYNC_HUP_NOTIFY)
1625                 retval = -EAGAIN;
1626             else
1627                 retval = -ERESTARTSYS;
1628             break;
1629         }
1630         if (!(info->flags & ASYNC_CLOSING) &&
1631             (do_clocal /*|| (UART_GET_FR(info->port) & SICC_UARTFR_DCD)*/))
1632             break;
1633         if (signal_pending(current)) {
1634             retval = -ERESTARTSYS;
1635             break;
1636         }
1637         schedule();
1638     }
1639     set_current_state(TASK_RUNNING);
1640     remove_wait_queue(&info->open_wait, &wait);
1641     if (extra_count)
1642         state->count++;
1643     info->blocked_open--;
1644     if (retval)
1645         return retval;
1646     info->flags |= ASYNC_NORMAL_ACTIVE;
1647     return 0;
1648 }
1649
1650 static struct SICC_info *siccuart_get(int line)
1651 {
1652     struct SICC_info *info;
1653     struct SICC_state *state = sicc_state + line;
1654
1655     state->count++;
1656     if (state->info)
1657         return state->info;
1658     info = kmalloc(sizeof(struct SICC_info), GFP_KERNEL);
1659     if (info) {
1660         memset(info, 0, sizeof(struct SICC_info));
1661         init_waitqueue_head(&info->open_wait);
1662         init_waitqueue_head(&info->close_wait);
1663         init_waitqueue_head(&info->delta_msr_wait);
1664         info->flags = state->flags;
1665         info->state = state;
1666         info->port  = sicc_ports + line;
1667         tasklet_init(&info->tlet, siccuart_tasklet_action,
1668                  (unsigned long)info);
1669     }
1670     if (state->info) {
1671         kfree(info);
1672         return state->info;
1673     }
1674     state->info = info;
1675     return info;
1676 }
1677
1678 static int siccuart_open(struct tty_struct *tty, struct file *filp)
1679 {
1680     struct SICC_info *info;
1681     int retval, line = tty->index;
1682
1683
1684     // is this a line that we've got?
1685     if (line >= SERIAL_SICC_NR) {
1686         return -ENODEV;
1687     }
1688
1689     info = siccuart_get(line);
1690     if (!info)
1691         return -ENOMEM;
1692
1693     tty->driver_data = info;
1694     info->tty = tty;
1695     info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1696
1697     /*
1698      * Make sure we have the temporary buffer allocated
1699      */
1700     if (!tmp_buf) {
1701         unsigned long page = get_zeroed_page(GFP_KERNEL);
1702         if (tmp_buf)
1703             free_page(page);
1704         else if (!page) {
1705             return -ENOMEM;
1706         }
1707         tmp_buf = (u_char *)page;
1708     }
1709
1710     /*
1711      * If the port is in the middle of closing, bail out now.
1712      */
1713     if (tty_hung_up_p(filp) ||
1714         (info->flags & ASYNC_CLOSING)) {
1715         if (info->flags & ASYNC_CLOSING)
1716             interruptible_sleep_on(&info->close_wait);
1717         return -EAGAIN;
1718     }
1719
1720     /*
1721      * Start up the serial port
1722      */
1723     retval = siccuart_startup(info);
1724     if (retval) {
1725         return retval;
1726     }
1727
1728     retval = block_til_ready(tty, filp, info);
1729     if (retval) {
1730         return retval;
1731     }
1732
1733 #ifdef CONFIG_SERIAL_SICC_CONSOLE
1734     if (siccuart_cons.cflag && siccuart_cons.index == line) {
1735         tty->termios->c_cflag = siccuart_cons.cflag;
1736         siccuart_cons.cflag = 0;
1737         siccuart_change_speed(info, NULL);
1738     }
1739 #endif
1740     return 0;
1741 }
1742
1743 static struct tty_operations sicc_ops = {
1744         .open = siccuart_open,
1745         .close = siccuart_close,
1746         .write = siccuart_write,
1747         .put_char = siccuart_put_char,
1748         .flush_chars = siccuart_flush_chars,
1749         .write_room = siccuart_write_room,
1750         .chars_in_buffer = siccuart_chars_in_buffer,
1751         .flush_buffer  = siccuart_flush_buffer,
1752         .ioctl = siccuart_ioctl,
1753         .throttle = siccuart_throttle,
1754         .unthrottle = siccuart_unthrottle,
1755         .send_xchar = siccuart_send_xchar,
1756         .set_termios = siccuart_set_termios,
1757         .stop = siccuart_stop,
1758         .start = siccuart_start,
1759         .hangup = siccuart_hangup,
1760         .break_ctl = siccuart_break_ctl,
1761         .wait_until_sent = siccuart_wait_until_sent,
1762 };
1763
1764 int __init siccuart_init(void)
1765 {
1766     int i;
1767     siccnormal_driver = alloc_tty_driver(SERIAL_SICC_NR);
1768     if (!siccnormal_driver)
1769         return -ENOMEM;
1770     printk("IBM Vesta SICC serial port driver V 0.1 by Yudong Yang and Yi Ge / IBM CRL .\n");
1771     siccnormal_driver->driver_name = "serial_sicc";
1772     siccnormal_driver->owner = THIS_MODULE;
1773     siccnormal_driver->name = SERIAL_SICC_NAME;
1774     siccnormal_driver->major = SERIAL_SICC_MAJOR;
1775     siccnormal_driver->minor_start = SERIAL_SICC_MINOR;
1776     siccnormal_driver->type = TTY_DRIVER_TYPE_SERIAL;
1777     siccnormal_driver->subtype = SERIAL_TYPE_NORMAL;
1778     siccnormal_driver->init_termios = tty_std_termios;
1779     siccnormal_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1780     siccnormal_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1781     tty_set_operations(siccnormal_driver, &sicc_ops);
1782
1783     if (tty_register_driver(siccnormal_driver))
1784         panic("Couldn't register SICC serial driver\n");
1785
1786     for (i = 0; i < SERIAL_SICC_NR; i++) {
1787         struct SICC_state *state = sicc_state + i;
1788         state->line     = i;
1789         state->close_delay  = 5 * HZ / 10;
1790         state->closing_wait = 30 * HZ;
1791     }
1792
1793
1794     return 0;
1795 }
1796
1797 __initcall(siccuart_init);
1798
1799 #ifdef CONFIG_SERIAL_SICC_CONSOLE
1800 /************** console driver *****************/
1801
1802 /*
1803  * This code is currently never used; console->read is never called.
1804  * Therefore, although we have an implementation, we don't use it.
1805  * FIXME: the "const char *s" should be fixed to "char *s" some day.
1806  * (when the definition in include/linux/console.h is also fixed)
1807  */
1808 #ifdef used_and_not_const_char_pointer
1809 static int siccuart_console_read(struct console *co, const char *s, u_int count)
1810 {
1811     struct SICC_port *port = &sicc_ports[co->index];
1812     unsigned int status;
1813     char *w;
1814     int c;
1815
1816     pr_debug("siccuart_console_read() called\n");
1817
1818     c = 0;
1819     w = s;
1820     while (c < count) {
1821         if(readb(port->uart_base +  BL_SICC_LSR) & _LSR_RBR_FULL) {
1822             *w++ = readb(port->uart_base +  BL_SICC_RBR);
1823             c++;
1824         } else {
1825             // nothing more to get, return
1826             return c;
1827         }
1828     }
1829     // return the count
1830     return c;
1831 }
1832 #endif
1833
1834 /*
1835  *  Print a string to the serial port trying not to disturb
1836  *  any possible real use of the port...
1837  *
1838  *  The console_lock must be held when we get here.
1839  */
1840 static void siccuart_console_write(struct console *co, const char *s, u_int count)
1841 {
1842     struct SICC_port *port = &sicc_ports[co->index];
1843     unsigned int old_cr;
1844     int i;
1845
1846     /*
1847      *  First save the CR then disable the interrupts
1848      */
1849     old_cr = readb(port->uart_base +  BL_SICC_TxCR);
1850     writeb(old_cr & ~_TxCR_DME_MASK, port->uart_base +  BL_SICC_TxCR);
1851
1852     /*
1853      *  Now, do each character
1854      */
1855     for (i = 0; i < count; i++) {
1856         while ((readb(port->uart_base +  BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1857         writeb(s[i], port->uart_base +  BL_SICC_TBR);
1858         if (s[i] == '\n') {
1859             while ((readb(port->uart_base +  BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1860             writeb('\r', port->uart_base +  BL_SICC_TBR);
1861         }
1862     }
1863
1864     /*
1865      *  Finally, wait for transmitter to become empty
1866      *  and restore the TCR
1867      */
1868     while ((readb(port->uart_base +  BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1869     writeb(old_cr, port->uart_base +  BL_SICC_TxCR);
1870 }
1871
1872 /*
1873  *  Receive character from the serial port
1874  */
1875 static int siccuart_console_wait_key(struct console *co)
1876 {
1877     struct SICC_port *port = &sicc_ports[co->index];
1878     int c;
1879
1880     while(!(readb(port->uart_base +  BL_SICC_LSR) & _LSR_RBR_FULL));
1881     c = readb(port->uart_base +  BL_SICC_RBR);
1882     return c;
1883 }
1884
1885 static struct tty_driver *siccuart_console_device(struct console *c, int *index)
1886 {
1887         *index = c->index;
1888         return siccnormal_driver;
1889 }
1890
1891 static int __init siccuart_console_setup(struct console *co, char *options)
1892 {
1893     struct SICC_port *port;
1894     int baud = 9600;
1895     int bits = 8;
1896     int parity = 'n';
1897     u_int cflag = CREAD | HUPCL | CLOCAL;
1898     u_int lcr_h, quot;
1899
1900
1901     if (co->index >= SERIAL_SICC_NR)
1902         co->index = 0;
1903
1904     port = &sicc_ports[co->index];
1905
1906     if (port->uart_base == 0)
1907         port->uart_base = (int)ioremap(port->uart_base_phys, PAGE_SIZE);
1908
1909     if (options) {
1910         char *s = options;
1911         baud = simple_strtoul(s, NULL, 10);
1912         while (*s >= '0' && *s <= '9')
1913             s++;
1914         if (*s) parity = *s++;
1915         if (*s) bits = *s - '0';
1916     }
1917
1918     /*
1919      *    Now construct a cflag setting.
1920      */
1921     switch (baud) {
1922     case 1200:  cflag |= B1200;         break;
1923     case 2400:  cflag |= B2400;         break;
1924     case 4800:  cflag |= B4800;         break;
1925     default:    cflag |= B9600;   baud = 9600;  break;
1926     case 19200: cflag |= B19200;        break;
1927     case 38400: cflag |= B38400;        break;
1928     case 57600: cflag |= B57600;        break;
1929     case 115200:    cflag |= B115200;       break;
1930     }
1931     switch (bits) {
1932     case 7:   cflag |= CS7; lcr_h = _LCR_PE_DISABLE | _LCR_DB_7_BITS | _LCR_SB_1_BIT;   break;
1933     default:  cflag |= CS8; lcr_h = _LCR_PE_DISABLE | _LCR_DB_8_BITS | _LCR_SB_1_BIT;   break;
1934     }
1935     switch (parity) {
1936     case 'o':
1937     case 'O': cflag |= PARODD; lcr_h |= _LCR_PTY_ODD;   break;
1938     case 'e':
1939     case 'E': cflag |= PARENB; lcr_h |= _LCR_PE_ENABLE |  _LCR_PTY_ODD; break;
1940     }
1941
1942     co->cflag = cflag;
1943
1944
1945        {
1946            // a copy of is inserted here ppc403SetBaud(com_port, (int)9600);
1947            unsigned long divisor, clockSource, temp;
1948            unsigned int rate = baud;
1949
1950           /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
1951           powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
1952
1953           /* Determine Internal Baud Clock Frequency */
1954           /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
1955           /* SCCR (Serial Clock Control Register) on Vesta */
1956           temp = powerpcMfclkgpcr();
1957
1958           if(temp & 0x00000080) {
1959               clockSource = 324000000;
1960           }
1961           else {
1962               clockSource = 216000000;
1963           }
1964           clockSource = clockSource/(unsigned long)((temp&0x00FC0000)>>18);
1965           divisor = clockSource/(16*rate) - 1;
1966           /* divisor has only 12 bits of resolution */
1967           if(divisor>0x00000FFF){
1968                divisor=0x00000FFF;
1969           }
1970
1971           quot = divisor;
1972        }
1973
1974     writeb((quot & 0x00000F00)>>8, port->uart_base + BL_SICC_BRDH );
1975     writeb( quot & 0x00000FF,      port->uart_base   + BL_SICC_BRDL );
1976
1977     /* Set CTL2 reg to use external clock (ExtClk) and enable FIFOs. */
1978     /* For now, do NOT use FIFOs since 403 UART did not have this    */
1979     /* capability and this driver was inherited from 403UART.        */
1980     writeb(_CTL2_EXTERN, port->uart_base  + BL_SICC_CTL2);
1981
1982     writeb(lcr_h, port->uart_base + BL_SICC_LCR);
1983     writeb(_RCR_ER_ENABLE | _RCR_PME_HARD, port->uart_base + BL_SICC_RCR);
1984     writeb( _TxCR_ET_ENABLE , port->uart_base + BL_SICC_TxCR);
1985
1986     // writeb(, info->port->uart_base + BL_SICC_RCR );
1987     /*
1988      * Transmitter Command Register: Transmitter enabled & DMA + TBR interrupt
1989      * + Transmitter Empty interrupt + Transmitter error interrupt disabled &
1990      * Stop mode when CTS active enabled & Transmit Break + Pattern Generation
1991      * mode disabled.
1992      */
1993
1994     writeb( 0x00, port->uart_base + BL_SICC_IrCR );  // disable IrDA
1995
1996     readb(port->uart_base + BL_SICC_RBR);
1997
1998     writeb(0xf8, port->uart_base + BL_SICC_LSR);   /* reset bits 0-4 of LSR */
1999
2000     /* we will enable the port as we need it */
2001
2002     return 0;
2003 }
2004
2005 static struct console siccuart_cons =
2006 {
2007     .name =     SERIAL_SICC_NAME,
2008     .write =    siccuart_console_write,
2009 #ifdef used_and_not_const_char_pointer
2010     .read =     siccuart_console_read,
2011 #endif
2012     .device =   siccuart_console_device,
2013     .wait_key = siccuart_console_wait_key,
2014     .setup =    siccuart_console_setup,
2015     .flags =    CON_PRINTBUFFER,
2016     .index =    -1,
2017 };
2018
2019 void __init sicc_console_init(void)
2020 {
2021     register_console(&siccuart_cons);
2022 }
2023
2024 #endif /* CONFIG_SERIAL_SICC_CONSOLE */