Merge branch 'drm-forlinus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.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/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>
56
57 #include <asm/system.h>
58 #include <asm/io.h>
59 #include <asm/irq.h>
60 #include <asm/uaccess.h>
61 #include <asm/serial.h>
62
63
64 #include <linux/serialP.h>
65
66
67 /* -----------------------------------------------------------------------------
68  *  From STB03xxx SICC UART Specification
69  * -----------------------------------------------------------------------------
70  *  UART Register Offsets.
71  */
72
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 */
86
87 /* masks and definitions for serial port control register */
88
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 */
96
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 */
101
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 */
112
113 /* serial port handshake register */
114
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 */
121
122 /* serial port line status register */
123
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 */
131
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 */
140
141 #define _LSR_RX_ERR    (_LSR_LB_BREAK | _LSR_FE_MASK | _LSR_OE_MASK | \
142                          _LSR_PE_MASK )
143
144 /* serial port receiver command register */
145
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 */
150
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 */
158
159 /* serial port transmit command register */
160
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 */
167
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 */
173
174 /* serial ctl reg 2 - added for Vesta */
175
176 #define _CTL2_EXTERN  0x80            /*  */
177 #define _CTL2_USEFIFO 0x40            /*  */
178 #define _CTL2_RESETRF 0x08            /*  */
179 #define _CTL2_RESETTF 0x04            /*  */
180
181
182
183 #define SERIAL_SICC_NAME    "ttySICC"
184 #define SERIAL_SICC_MAJOR   150
185 #define SERIAL_SICC_MINOR   1
186 #define SERIAL_SICC_NR      1
187
188 #ifndef TRUE
189 #define TRUE 1
190 #endif
191 #ifndef FALSE
192 #define FALSE 0
193 #endif
194
195 /*
196  * Things needed by tty driver
197  */
198 static struct tty_driver *siccnormal_driver;
199
200 #if defined(CONFIG_SERIAL_SICC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
201 #define SUPPORT_SYSRQ
202 #endif
203
204 /*
205  * Things needed internally to this driver
206  */
207
208 /*
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.
216  */
217 static u_char *tmp_buf;
218 static DECLARE_MUTEX(tmp_buf_sem);
219
220 #define HIGH_BITS_OFFSET    ((sizeof(long)-sizeof(int))*8)
221
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
225
226 #define EVT_WRITE_WAKEUP    0
227
228 struct SICC_icount {
229     __u32   cts;
230     __u32   dsr;
231     __u32   rng;
232     __u32   dcd;
233     __u32   rx;
234     __u32   tx;
235     __u32   frame;
236     __u32   overrun;
237     __u32   parity;
238     __u32   brk;
239     __u32   buf_overrun;
240 };
241
242 /*
243  * Static information about the port
244  */
245 struct SICC_port {
246     unsigned int        uart_base;
247     unsigned int        uart_base_phys;
248     unsigned int        irqrx;
249     unsigned int        irqtx;
250     unsigned int        uartclk;
251     unsigned int        fifosize;
252     unsigned int        tiocm_support;
253     void (*set_mctrl)(struct SICC_port *, u_int mctrl);
254 };
255
256 /*
257  * This is the state information which is persistent across opens
258  */
259 struct SICC_state {
260     struct SICC_icount  icount;
261     unsigned int        line;
262     unsigned int        close_delay;
263     unsigned int        closing_wait;
264     unsigned int        custom_divisor;
265     unsigned int        flags;
266     int         count;
267     struct SICC_info    *info;
268     spinlock_t          sicc_lock;
269 };
270
271 #define SICC_XMIT_SIZE 1024
272 /*
273  * This is the state information which is only valid when the port is open.
274  */
275 struct SICC_info {
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;
284     unsigned int        flags;
285 #ifdef SUPPORT_SYSRQ
286     unsigned long       sysrq;
287 #endif
288
289     unsigned int        event;
290     unsigned int        timeout;
291     unsigned int        lcr_h;
292     unsigned int        mctrl;
293     int         blocked_open;
294
295     struct tasklet_struct   tlet;
296
297     wait_queue_head_t   open_wait;
298     wait_queue_head_t   close_wait;
299     wait_queue_head_t   delta_msr_wait;
300 };
301
302 #ifdef CONFIG_SERIAL_SICC_CONSOLE
303 static struct console siccuart_cons;
304 #endif
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);
307
308
309
310 static void powerpcMtcic_cr(unsigned long value)
311 {
312     mtdcr(DCRN_CICCR, value);
313 }
314
315 static unsigned long powerpcMfcic_cr(void)
316 {
317     return mfdcr(DCRN_CICCR);
318 }
319
320 static unsigned long powerpcMfclkgpcr(void)
321 {
322     return mfdcr(DCRN_SCCR);
323 }
324
325 static void sicc_set_mctrl_null(struct SICC_port *port, u_int mctrl)
326 {
327 }
328
329 static struct SICC_port sicc_ports[SERIAL_SICC_NR] = {
330     {
331         .uart_base = 0,
332         .uart_base_phys = SICC0_IO_BASE,
333         .irqrx =    SICC0_INTRX,
334         .irqtx =    SICC0_INTTX,
335 //      .uartclk =    0,
336         .fifosize = 1,
337         .set_mctrl = sicc_set_mctrl_null,
338     }
339 };
340
341 static struct SICC_state sicc_state[SERIAL_SICC_NR];
342
343 static void siccuart_enable_rx_interrupt(struct SICC_info *info)
344 {
345     unsigned char cr;
346
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);
351 }
352
353 static void siccuart_disable_rx_interrupt(struct SICC_info *info)
354 {
355     unsigned char cr;
356
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);
361 }
362
363
364 static void siccuart_enable_tx_interrupt(struct SICC_info *info)
365 {
366     unsigned char cr;
367
368     cr = readb(info->port->uart_base+BL_SICC_TxCR);
369     cr &= ~_TxCR_DME_MASK;
370     cr |= _TxCR_DME_TBR;
371     writeb(cr, info->port->uart_base+BL_SICC_TxCR);
372 }
373
374 static void siccuart_disable_tx_interrupt(struct SICC_info *info)
375 {
376     unsigned char cr;
377
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);
382 }
383
384
385 static void siccuart_stop(struct tty_struct *tty)
386 {
387     struct SICC_info *info = tty->driver_data;
388     unsigned long flags;
389
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);
394 }
395
396 static void siccuart_start(struct tty_struct *tty)
397 {
398     struct SICC_info *info = tty->driver_data;
399     unsigned long flags;
400
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
404         && info->xmit.buf)
405         siccuart_enable_tx_interrupt(info);
406     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
407 }
408
409
410 /*
411  * This routine is used by the interrupt handler to schedule
412  * processing in the software interrupt portion of the driver.
413  */
414 static void siccuart_event(struct SICC_info *info, int event)
415 {
416     info->event |= 1 << event;
417     tasklet_schedule(&info->tlet);
418 }
419
420 static void
421 siccuart_rx_chars(struct SICC_info *info, struct pt_regs *regs)
422 {
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;
427
428     status = readb(port->uart_base+BL_SICC_LSR );
429     while (status & _LSR_RBR_FULL) {
430         ch = readb(port->uart_base+BL_SICC_RBR);
431
432         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
433             goto ignore_char;
434         icount->rx++;
435
436         flg = TTY_NORMAL;
437
438         /*
439          * Note that the error handling code is
440          * out of the main execution path
441          */
442         rsr = readb(port->uart_base+BL_SICC_LSR);
443         if (rsr & _LSR_RX_ERR)
444             goto handle_error;
445 #ifdef SUPPORT_SYSRQ
446         if (info->sysrq) {
447             if (ch && time_before(jiffies, info->sysrq)) {
448                 handle_sysrq(ch, regs, NULL);
449                 info->sysrq = 0;
450                 goto ignore_char;
451             }
452             info->sysrq = 0;
453         }
454 #endif
455     error_return:
456         *tty->flip.flag_buf_ptr++ = flg;
457         *tty->flip.char_buf_ptr++ = ch;
458         tty->flip.count++;
459     ignore_char:
460         status = readb(port->uart_base+BL_SICC_LSR );
461     }
462 out:
463     tty_flip_buffer_push(tty);
464     return;
465
466 handle_error:
467     if (rsr & _LSR_LB_BREAK) {
468         rsr &= ~(_LSR_FE_MASK | _LSR_PE_MASK);
469         icount->brk++;
470
471 #ifdef SUPPORT_SYSRQ
472         if (info->state->line == siccuart_cons.index) {
473             if (!info->sysrq) {
474                 info->sysrq = jiffies + HZ*5;
475                 goto ignore_char;
476             }
477         }
478 #endif
479     } else if (rsr & _LSR_PE_MASK)
480         icount->parity++;
481     else if (rsr & _LSR_FE_MASK)
482         icount->frame++;
483     if (rsr & _LSR_OE_MASK)
484         icount->overrun++;
485
486     if (rsr & info->ignore_status_mask) {
487         if (++ignored > 100)
488             goto out;
489         goto ignore_char;
490     }
491     rsr &= info->read_status_mask;
492
493     if (rsr & _LSR_LB_BREAK)
494         flg = TTY_BREAK;
495     else if (rsr &  _LSR_PE_MASK)
496         flg = TTY_PARITY;
497     else if (rsr &  _LSR_FE_MASK)
498         flg = TTY_FRAME;
499
500     if (rsr &  _LSR_OE_MASK) {
501         /*
502          * CHECK: does overrun affect the current character?
503          * ASSUMPTION: it does not.
504          */
505         *tty->flip.flag_buf_ptr++ = flg;
506         *tty->flip.char_buf_ptr++ = ch;
507         tty->flip.count++;
508         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
509             goto ignore_char;
510         ch = 0;
511         flg = TTY_OVERRUN;
512     }
513 #ifdef SUPPORT_SYSRQ
514     info->sysrq = 0;
515 #endif
516     goto error_return;
517 }
518
519 static void siccuart_tx_chars(struct SICC_info *info)
520 {
521     struct SICC_port *port = info->port;
522     int count;
523         unsigned char status;
524
525
526     if (info->x_char) {
527         writeb(info->x_char, port->uart_base+ BL_SICC_TBR);
528         info->state->icount.tx++;
529         info->x_char = 0;
530         return;
531     }
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);
537         return;
538     }
539
540     count = port->fifosize;
541     do {
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)
546             break;
547     } while (--count > 0);
548
549     if (CIRC_CNT(info->xmit.head,
550              info->xmit.tail,
551              SICC_XMIT_SIZE) < WAKEUP_CHARS)
552         siccuart_event(info, EVT_WRITE_WAKEUP);
553
554     if (info->xmit.head == info->xmit.tail) {
555         siccuart_disable_tx_interrupt(info);
556     }
557 }
558
559
560 static irqreturn_t siccuart_int_rx(int irq, void *dev_id, struct pt_regs *regs)
561 {
562     struct SICC_info *info = dev_id;
563     siccuart_rx_chars(info, regs);
564     return IRQ_HANDLED;
565 }
566
567
568 static irqreturn_t siccuart_int_tx(int irq, void *dev_id, struct pt_regs *regs)
569 {
570     struct SICC_info *info = dev_id;
571     siccuart_tx_chars(info);
572     return IRQ_HANDLED;
573 }
574
575 static void siccuart_tasklet_action(unsigned long data)
576 {
577     struct SICC_info *info = (struct SICC_info *)data;
578     struct tty_struct *tty;
579
580     tty = info->tty;
581     if (!tty || !test_and_clear_bit(EVT_WRITE_WAKEUP, &info->event))
582         return;
583
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);
588 }
589
590 static int siccuart_startup(struct SICC_info *info)
591 {
592     unsigned long flags;
593     unsigned long page;
594     int retval = 0;
595
596     if (info->flags & ASYNC_INITIALIZED) {
597         return 0;
598     }
599
600     page = get_zeroed_page(GFP_KERNEL);
601     if (!page)
602         return -ENOMEM;
603
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) {
607         free_page(page);
608         return -ENOMEM;
609     }
610
611     /* lock access to info while doing setup */
612     spin_lock_irqsave(&info->state->sicc_lock,flags);
613
614     if (info->xmit.buf)
615         free_page(page);
616     else
617         info->xmit.buf = (unsigned char *) page;
618
619
620     info->mctrl = 0;
621     if (info->tty->termios->c_cflag & CBAUD)
622         info->mctrl = TIOCM_RTS | TIOCM_DTR;
623     info->port->set_mctrl(info->port, info->mctrl);
624
625     /*
626      * initialise the old status of the modem signals
627      */
628     info->old_status = 0; // UART_GET_FR(info->port) & AMBA_UARTFR_MODEM_ANY;
629
630
631     if (info->tty)
632         clear_bit(TTY_IO_ERROR, &info->tty->flags);
633     info->xmit.head = info->xmit.tail = 0;
634
635     /*
636      * Set up the tty->alt_speed kludge
637      */
638     if (info->tty) {
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;
647     }
648
649
650     writeb( 0x00, info->port->uart_base + BL_SICC_IrCR );  // disable IrDA
651
652
653     /*
654      * and set the speed of the serial port
655      */
656     siccuart_change_speed(info, 0);
657
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);
661
662     readb(info->port->uart_base + BL_SICC_RBR); // clear rx port
663
664     writeb(0xf8, info->port->uart_base + BL_SICC_LSR);   /* reset bits 0-4 of LSR */
665
666     /*
667      * Finally, enable interrupts
668      */
669
670      /*
671      * Allocate the IRQ
672      */
673         retval = request_irq(info->port->irqrx, siccuart_int_rx, 0, "SICC rx", info);
674         if (retval) {
675              if (capable(CAP_SYS_ADMIN)) {
676                    if (info->tty)
677                           set_bit(TTY_IO_ERROR, &info->tty->flags);
678                    retval = 0;
679              }
680               goto errout;
681          }
682     retval = request_irq(info->port->irqtx, siccuart_int_tx, 0, "SICC tx", info);
683     if (retval) {
684         if (capable(CAP_SYS_ADMIN)) {
685             if (info->tty)
686                 set_bit(TTY_IO_ERROR, &info->tty->flags);
687             retval = 0;
688         }
689         free_irq(info->port->irqrx, info);
690         goto errout;
691     }
692
693     siccuart_enable_rx_interrupt(info);
694
695     info->flags |= ASYNC_INITIALIZED;
696     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
697     return 0;
698
699
700 errout:
701     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
702     return retval;
703 }
704
705 /*
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.
708  */
709 static void siccuart_shutdown(struct SICC_info *info)
710 {
711     unsigned long flags;
712
713     if (!(info->flags & ASYNC_INITIALIZED))
714         return;
715
716     /* lock while shutting down port */
717     spin_lock_irqsave(&info->state->sicc_lock,flags); /* Disable interrupts */
718
719     /*
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
722      */
723     wake_up_interruptible(&info->delta_msr_wait);
724
725     /*
726      * disable all interrupts, disable the port
727      */
728     siccuart_disable_rx_interrupt(info);
729     siccuart_disable_tx_interrupt(info);
730
731     /*
732      * Free the IRQ
733      */
734     free_irq(info->port->irqtx, info);
735     free_irq(info->port->irqrx, info);
736
737     if (info->xmit.buf) {
738         unsigned long pg = (unsigned long) info->xmit.buf;
739         info->xmit.buf = NULL;
740         free_page(pg);
741     }
742
743
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);
747
748     /* kill off our tasklet */
749     tasklet_kill(&info->tlet);
750     if (info->tty)
751         set_bit(TTY_IO_ERROR, &info->tty->flags);
752
753     info->flags &= ~ASYNC_INITIALIZED;
754
755     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
756 }
757
758
759 static void siccuart_change_speed(struct SICC_info *info, struct termios *old_termios)
760 {
761     unsigned int lcr_h, baud, quot, cflag, old_rcr, old_tcr, bits;
762     unsigned long flags;
763
764     if (!info->tty || !info->tty->termios)
765         return;
766
767     cflag = info->tty->termios->c_cflag;
768
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
774     }
775     if (cflag & CSTOPB) {
776         lcr_h |= _LCR_SB_2_BIT;
777         bits ++;
778     }
779     if (cflag & PARENB) {
780         lcr_h |=  _LCR_PE_ENABLE;
781         bits++;
782         if (!(cflag & PARODD))
783             lcr_h |=  _LCR_PTY_ODD;
784         else
785             lcr_h |=  _LCR_PTY_EVEN;
786     }
787
788     do {
789         /* Determine divisor based on baud rate */
790         baud = tty_get_baud_rate(info->tty);
791         if (!baud)
792             baud = 9600;
793
794
795         {
796            // here is ppc403SetBaud(com_port, baud);
797            unsigned long divisor, clockSource, temp;
798
799            /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
800            powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
801
802            /* Determine Internal Baud Clock Frequency */
803            /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
804            /* SCCR (Serial Clock Control Register) on Vesta */
805            temp = powerpcMfclkgpcr();
806
807            if(temp & 0x00000080) {
808                clockSource = 324000000;
809            }
810            else {
811                clockSource = 216000000;
812            }
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){
817                divisor=0x00000FFF;
818            }
819
820            quot = divisor;
821         }
822
823         if (baud == 38400 &&
824             ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
825             quot = info->state->custom_divisor;
826
827         if (!quot && old_termios) {
828             info->tty->termios->c_cflag &= ~CBAUD;
829             info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
830             old_termios = NULL;
831         }
832     } while (quot == 0 && old_termios);
833
834     /* As a last resort, if the quotient is zero, default to 9600 bps */
835     if (!quot)
836         quot = (info->port->uartclk / (16 * 9600)) - 1;
837
838     info->timeout = info->port->fifosize * HZ * bits / baud;
839     info->timeout += HZ/50;     /* Add .02 seconds of slop */
840
841     if (cflag & CRTSCTS)
842         info->flags |= ASYNC_CTS_FLOW;
843     else
844         info->flags &= ~ASYNC_CTS_FLOW;
845     if (cflag & CLOCAL)
846         info->flags &= ~ASYNC_CHECK_CD;
847     else
848         info->flags |= ASYNC_CHECK_CD;
849
850     /*
851      * Set up parity check flag
852      */
853 #define RELEVENT_IFLAG(iflag)   ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
854
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;
860
861     /*
862      * Characters to ignore
863      */
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;
869         /*
870          * If we're ignoring parity and break indicators,
871          * ignore overruns to (for real raw support).
872          */
873         if (I_IGNPAR(info->tty))
874             info->ignore_status_mask |=  _LSR_OE_MASK;
875     }
876
877     /* disable interrupts while reading and clearing registers */
878     spin_lock_irqsave(&info->state->sicc_lock,flags);
879
880     old_rcr = readb(info->port->uart_base + BL_SICC_RCR);
881     old_tcr = readb(info->port->uart_base + BL_SICC_TxCR);
882
883
884     writeb(0, info->port->uart_base + BL_SICC_RCR);
885     writeb(0, info->port->uart_base + BL_SICC_TxCR);
886
887     /*RLBtrace (&ppc403Chan0, 0x2000000c, 0, 0);*/
888
889
890     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
891
892
893     /* Set baud rate */
894     writeb((quot & 0x00000F00)>>8, info->port->uart_base + BL_SICC_BRDH );
895     writeb( quot & 0x00000FF,      info->port->uart_base + BL_SICC_BRDL );
896
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);
901
902     writeb(lcr_h, info->port->uart_base + BL_SICC_LCR);
903
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
906
907 }
908
909
910 static void siccuart_put_char(struct tty_struct *tty, u_char ch)
911 {
912     struct SICC_info *info = tty->driver_data;
913     unsigned long flags;
914
915     if (!tty || !info->xmit.buf)
916         return;
917
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);
923     }
924     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
925 }
926
927 static void siccuart_flush_chars(struct tty_struct *tty)
928 {
929     struct SICC_info *info = tty->driver_data;
930     unsigned long flags;
931
932     if (info->xmit.head == info->xmit.tail
933         || tty->stopped
934         || tty->hw_stopped
935         || !info->xmit.buf)
936         return;
937
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);
942 }
943
944 static int siccuart_write(struct tty_struct *tty,
945               const u_char * buf, int count)
946 {
947     struct SICC_info *info = tty->driver_data;
948     unsigned long flags;
949     int c, ret = 0;
950
951     if (!tty || !info->xmit.buf || !tmp_buf)
952         return 0;
953
954     /* lock info->xmit while removing characters from buffer */
955     spin_lock_irqsave(&info->state->sicc_lock,flags);
956     while (1) {
957         c = CIRC_SPACE_TO_END(info->xmit.head,
958                       info->xmit.tail,
959                       SICC_XMIT_SIZE);
960         if (count < c)
961             c = count;
962         if (c <= 0)
963             break;
964         memcpy(info->xmit.buf + info->xmit.head, buf, c);
965         info->xmit.head = (info->xmit.head + c) &
966                   (SICC_XMIT_SIZE - 1);
967         buf += c;
968         count -= c;
969         ret += c;
970     }
971     if (info->xmit.head != info->xmit.tail
972         && !tty->stopped
973         && !tty->hw_stopped)
974         siccuart_enable_tx_interrupt(info);
975     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
976     return ret;
977 }
978
979 static int siccuart_write_room(struct tty_struct *tty)
980 {
981     struct SICC_info *info = tty->driver_data;
982
983     return CIRC_SPACE(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
984 }
985
986 static int siccuart_chars_in_buffer(struct tty_struct *tty)
987 {
988     struct SICC_info *info = tty->driver_data;
989
990     return CIRC_CNT(info->xmit.head, info->xmit.tail, SICC_XMIT_SIZE);
991 }
992
993 static void siccuart_flush_buffer(struct tty_struct *tty)
994 {
995     struct SICC_info *info = tty->driver_data;
996     unsigned long flags;
997
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);
1007 }
1008
1009 /*
1010  * This function is used to send a high-priority XON/XOFF character to
1011  * the device
1012  */
1013 static void siccuart_send_xchar(struct tty_struct *tty, char ch)
1014 {
1015     struct SICC_info *info = tty->driver_data;
1016
1017     info->x_char = ch;
1018     if (ch)
1019        siccuart_enable_tx_interrupt(info);
1020 }
1021
1022 static void siccuart_throttle(struct tty_struct *tty)
1023 {
1024     struct SICC_info *info = tty->driver_data;
1025     unsigned long flags;
1026
1027     if (I_IXOFF(tty))
1028         siccuart_send_xchar(tty, STOP_CHAR(tty));
1029
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);
1036     }
1037 }
1038
1039 static void siccuart_unthrottle(struct tty_struct *tty)
1040 {
1041     struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1042     unsigned long flags;
1043
1044     if (I_IXOFF(tty)) {
1045         if (info->x_char)
1046             info->x_char = 0;
1047         else
1048             siccuart_send_xchar(tty, START_CHAR(tty));
1049     }
1050
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);
1057     }
1058 }
1059
1060 static int get_serial_info(struct SICC_info *info, struct serial_struct *retinfo)
1061 {
1062     struct SICC_state *state = info->state;
1063     struct SICC_port *port = info->port;
1064     struct serial_struct tmp;
1065
1066     memset(&tmp, 0, sizeof(tmp));
1067     tmp.type       = 0;
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;
1073     tmp.flags      = 0;
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;
1079
1080     if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1081         return -EFAULT;
1082     return 0;
1083 }
1084
1085 static int set_serial_info(struct SICC_info *info,
1086                struct serial_struct *newinfo)
1087 {
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;
1093     int retval = 0;
1094
1095     if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1096         return -EFAULT;
1097
1098     state = info->state;
1099     old_state = *state;
1100     port = info->port;
1101
1102     new_port = new_serial.port;
1103     if (HIGH_BITS_OFFSET)
1104         new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1105
1106     change_irq  = new_serial.irq != port->irqrx;
1107     change_port = new_port != port->uart_base;
1108
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)))
1116             return -EPERM;
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;
1123     }
1124
1125     if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) ||
1126         (new_serial.baud_base < 9600))
1127         return -EINVAL;
1128
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)
1133                 return -EADDRINUSE;
1134     }
1135
1136     if ((change_port || change_irq) && (state->count > 1))
1137         return -EBUSY;
1138
1139     /*
1140      * OK, past this point, all the error checking has been done.
1141      * At this point, we start making changes.....
1142      */
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;
1153
1154     if (change_port || change_irq) {
1155         /*
1156          * We need to shutdown the serial port at the old
1157          * port/irq combination.
1158          */
1159         siccuart_shutdown(info);
1160         port->irqrx = new_serial.irq;
1161         port->uart_base = new_port;
1162     }
1163
1164 check_and_exit:
1165     if (!port->uart_base)
1166         return 0;
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);
1180         }
1181     } else
1182         retval = siccuart_startup(info);
1183     return retval;
1184 }
1185
1186
1187 /*
1188  * get_lsr_info - get line status register info
1189  */
1190 static int get_lsr_info(struct SICC_info *info, unsigned int *value)
1191 {
1192     unsigned int result, status;
1193     unsigned long flags;
1194
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;
1200
1201     /*
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).
1206      */
1207     if (info->x_char ||
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;
1212
1213     return put_user(result, value);
1214 }
1215
1216 static int get_modem_info(struct SICC_info *info, unsigned int *value)
1217 {
1218     unsigned int result = info->mctrl;
1219
1220     return put_user(result, value);
1221 }
1222
1223 static int set_modem_info(struct SICC_info *info, unsigned int cmd,
1224               unsigned int *value)
1225 {
1226     unsigned int arg, old;
1227     unsigned long flags;
1228
1229     if (get_user(arg, value))
1230         return -EFAULT;
1231
1232     old = info->mctrl;
1233     switch (cmd) {
1234     case TIOCMBIS:
1235         info->mctrl |= arg;
1236         break;
1237
1238     case TIOCMBIC:
1239         info->mctrl &= ~arg;
1240         break;
1241
1242     case TIOCMSET:
1243         info->mctrl = arg;
1244         break;
1245
1246     default:
1247         return -EINVAL;
1248     }
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);
1254     return 0;
1255 }
1256
1257 static void siccuart_break_ctl(struct tty_struct *tty, int break_state)
1258 {
1259     struct SICC_info *info = tty->driver_data;
1260     unsigned long flags;
1261     unsigned int lcr_h;
1262
1263
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;
1269     else
1270         lcr_h &= ~_LSR_LB_MASK;
1271     writeb(lcr_h, info->port + BL_SICC_LSRS);
1272     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1273 }
1274
1275 static int siccuart_ioctl(struct tty_struct *tty, struct file *file,
1276                unsigned int cmd, unsigned long arg)
1277 {
1278     struct SICC_info *info = tty->driver_data;
1279     struct SICC_icount cnow;
1280     struct serial_icounter_struct icount;
1281     unsigned long flags;
1282
1283     if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1284         (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1285         (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1286         if (tty->flags & (1 << TTY_IO_ERROR))
1287             return -EIO;
1288     }
1289
1290     switch (cmd) {
1291         case TIOCMGET:
1292             return get_modem_info(info, (unsigned int *)arg);
1293         case TIOCMBIS:
1294         case TIOCMBIC:
1295         case TIOCMSET:
1296             return set_modem_info(info, cmd, (unsigned int *)arg);
1297         case TIOCGSERIAL:
1298             return get_serial_info(info,
1299                            (struct serial_struct *)arg);
1300         case TIOCSSERIAL:
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);
1305         /*
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
1310          */
1311         case TIOCMIWAIT:
1312             return 0;
1313         /*
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.
1318          */
1319         case TIOCGICOUNT:
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;
1335
1336             return copy_to_user((void *)arg, &icount, sizeof(icount))
1337                     ? -EFAULT : 0;
1338
1339         default:
1340             return -ENOIOCTLCMD;
1341     }
1342     return 0;
1343 }
1344
1345 static void siccuart_set_termios(struct tty_struct *tty, struct termios *old_termios)
1346 {
1347     struct SICC_info *info = tty->driver_data;
1348     unsigned long flags;
1349     unsigned int cflag = tty->termios->c_cflag;
1350
1351     if ((cflag ^ old_termios->c_cflag) == 0 &&
1352         RELEVENT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
1353         return;
1354
1355     siccuart_change_speed(info, old_termios);
1356
1357     /* Handle transition to B0 status */
1358     if ((old_termios->c_cflag & CBAUD) &&
1359         !(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);
1365     }
1366
1367     /* Handle transition away from B0 status */
1368     if (!(old_termios->c_cflag & CBAUD) &&
1369         (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);
1378     }
1379
1380     /* Handle turning off CRTSCTS */
1381     if ((old_termios->c_cflag & CRTSCTS) &&
1382         !(cflag & CRTSCTS)) {
1383         tty->hw_stopped = 0;
1384         siccuart_start(tty);
1385     }
1386
1387 #if 0
1388     /*
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.....
1393      */
1394     if (!(old_termios->c_cflag & CLOCAL) &&
1395         (tty->termios->c_cflag & CLOCAL))
1396         wake_up_interruptible(&info->open_wait);
1397 #endif
1398 }
1399
1400 static void siccuart_close(struct tty_struct *tty, struct file *filp)
1401 {
1402     struct SICC_info *info = tty->driver_data;
1403     struct SICC_state *state;
1404     unsigned long flags;
1405
1406     if (!info)
1407         return;
1408
1409     state = info->state;
1410
1411     //pr_debug("siccuart_close() called\n");
1412
1413     /* lock tty->driver_data while closing port */
1414     spin_lock_irqsave(&info->state->sicc_lock,flags);
1415
1416     if (tty_hung_up_p(filp)) {
1417         goto quick_close;
1418     }
1419
1420     if ((tty->count == 1) && (state->count != 1)) {
1421         /*
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.
1427          */
1428         printk("siccuart_close: bad serial port count; tty->count is 1, state->count is %d\n", state->count);
1429         state->count = 1;
1430     }
1431     if (--state->count < 0) {
1432         printk("rs_close: bad serial port count for %s: %d\n", tty->name, state->count);
1433         state->count = 0;
1434     }
1435     if (state->count) {
1436         goto quick_close;
1437     }
1438     info->flags |= ASYNC_CLOSING;
1439     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1440     /*
1441      * Now we wait for the transmit buffer to clear; and we notify
1442      * the line discipline to only process XON/XOFF characters.
1443      */
1444     tty->closing = 1;
1445     if (info->state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1446         tty_wait_until_sent(tty, info->state->closing_wait);
1447     /*
1448      * At this point, we stop accepting input.  To do this, we
1449      * disable the receive line status interrupts.
1450      */
1451     if (info->flags & ASYNC_INITIALIZED) {
1452         siccuart_disable_rx_interrupt(info);
1453         /*
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!
1457          */
1458         siccuart_wait_until_sent(tty, info->timeout);
1459     }
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);
1465     tty->closing = 0;
1466     info->event = 0;
1467     info->tty = NULL;
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);
1472     }
1473     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1474     wake_up_interruptible(&info->close_wait);
1475     return;
1476
1477 quick_close:
1478     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1479     return;
1480 }
1481
1482 static void siccuart_wait_until_sent(struct tty_struct *tty, int timeout)
1483 {
1484     struct SICC_info *info = (struct SICC_info *) tty->driver_data;
1485     unsigned long char_time, expire;
1486
1487     if (info->port->fifosize == 0)
1488         return;
1489
1490     /*
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.
1494      *
1495      * Note: we have to use pretty tight timings here to satisfy
1496      * the NIST-PCTS.
1497      */
1498     char_time = (info->timeout - msecs_to_jiffies(20)) / info->port->fifosize;
1499     char_time = char_time / 5;
1500     if (char_time == 0)
1501         char_time = 1;
1502
1503     // Crazy!!   sometimes the input arg 'timeout' can be negtive numbers  :-(
1504     if (timeout >= 0 && timeout < char_time)
1505         char_time = timeout;
1506     /*
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
1513      * 2*info->timeout.
1514      */
1515     if (!timeout || timeout > 2 * info->timeout)
1516         timeout = 2 * info->timeout;
1517
1518     expire = jiffies + timeout;
1519     pr_debug("siccuart_wait_until_sent(%d), jiff=%lu, expire=%lu  char_time=%lu...\n",
1520            tty->index, jiffies,
1521            expire, char_time);
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))
1525             break;
1526         if (timeout && time_after(jiffies, expire))
1527             break;
1528     }
1529     set_current_state(TASK_RUNNING);
1530 }
1531
1532 static void siccuart_hangup(struct tty_struct *tty)
1533 {
1534     struct SICC_info *info = tty->driver_data;
1535     struct SICC_state *state = info->state;
1536
1537     siccuart_flush_buffer(tty);
1538     if (info->flags & ASYNC_CLOSING)
1539         return;
1540     siccuart_shutdown(info);
1541     info->event = 0;
1542     state->count = 0;
1543     info->flags &= ~ASYNC_NORMAL_ACTIVE;
1544     info->tty = NULL;
1545     wake_up_interruptible(&info->open_wait);
1546 }
1547
1548 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1549                struct SICC_info *info)
1550 {
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;
1555
1556     /*
1557      * If the device is in the middle of being closed, then block
1558      * until it's done, and then try again.
1559      */
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;
1566     }
1567
1568     /*
1569      * If non-blocking mode is set, or the port is not enabled,
1570      * then make the check up front and then exit.
1571      */
1572     if ((filp->f_flags & O_NONBLOCK) ||
1573         (tty->flags & (1 << TTY_IO_ERROR))) {
1574         info->flags |= ASYNC_NORMAL_ACTIVE;
1575         return 0;
1576     }
1577
1578     if (tty->termios->c_cflag & CLOCAL)
1579         do_clocal = 1;
1580
1581     /*
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.
1587      */
1588     retval = 0;
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)) {
1593         extra_count = 1;
1594         state->count--;
1595     }
1596     spin_unlock_irqrestore(&info->state->sicc_lock,flags);
1597     info->blocked_open++;
1598     while (1) {
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);
1604         }
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)
1610                 retval = -EAGAIN;
1611             else
1612                 retval = -ERESTARTSYS;
1613             break;
1614         }
1615         if (!(info->flags & ASYNC_CLOSING) &&
1616             (do_clocal /*|| (UART_GET_FR(info->port) & SICC_UARTFR_DCD)*/))
1617             break;
1618         if (signal_pending(current)) {
1619             retval = -ERESTARTSYS;
1620             break;
1621         }
1622         schedule();
1623     }
1624     set_current_state(TASK_RUNNING);
1625     remove_wait_queue(&info->open_wait, &wait);
1626     if (extra_count)
1627         state->count++;
1628     info->blocked_open--;
1629     if (retval)
1630         return retval;
1631     info->flags |= ASYNC_NORMAL_ACTIVE;
1632     return 0;
1633 }
1634
1635 static struct SICC_info *siccuart_get(int line)
1636 {
1637     struct SICC_info *info;
1638     struct SICC_state *state = sicc_state + line;
1639
1640     state->count++;
1641     if (state->info)
1642         return state->info;
1643     info = kmalloc(sizeof(struct SICC_info), GFP_KERNEL);
1644     if (info) {
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);
1654     }
1655     if (state->info) {
1656         kfree(info);
1657         return state->info;
1658     }
1659     state->info = info;
1660     return info;
1661 }
1662
1663 static int siccuart_open(struct tty_struct *tty, struct file *filp)
1664 {
1665     struct SICC_info *info;
1666     int retval, line = tty->index;
1667
1668
1669     // is this a line that we've got?
1670     if (line >= SERIAL_SICC_NR) {
1671         return -ENODEV;
1672     }
1673
1674     info = siccuart_get(line);
1675     if (!info)
1676         return -ENOMEM;
1677
1678     tty->driver_data = info;
1679     info->tty = tty;
1680     info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1681
1682     /*
1683      * Make sure we have the temporary buffer allocated
1684      */
1685     if (!tmp_buf) {
1686         unsigned long page = get_zeroed_page(GFP_KERNEL);
1687         if (tmp_buf)
1688             free_page(page);
1689         else if (!page) {
1690             return -ENOMEM;
1691         }
1692         tmp_buf = (u_char *)page;
1693     }
1694
1695     /*
1696      * If the port is in the middle of closing, bail out now.
1697      */
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);
1702         return -EAGAIN;
1703     }
1704
1705     /*
1706      * Start up the serial port
1707      */
1708     retval = siccuart_startup(info);
1709     if (retval) {
1710         return retval;
1711     }
1712
1713     retval = block_til_ready(tty, filp, info);
1714     if (retval) {
1715         return retval;
1716     }
1717
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);
1723     }
1724 #endif
1725     return 0;
1726 }
1727
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,
1747 };
1748
1749 int __init siccuart_init(void)
1750 {
1751     int i;
1752     siccnormal_driver = alloc_tty_driver(SERIAL_SICC_NR);
1753     if (!siccnormal_driver)
1754         return -ENOMEM;
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);
1767
1768     if (tty_register_driver(siccnormal_driver))
1769         panic("Couldn't register SICC serial driver\n");
1770
1771     for (i = 0; i < SERIAL_SICC_NR; i++) {
1772         struct SICC_state *state = sicc_state + i;
1773         state->line     = i;
1774         state->close_delay  = msecs_to_jiffies(500);
1775         state->closing_wait = 30 * HZ;
1776         spin_lock_init(&state->sicc_lock);
1777     }
1778
1779
1780     return 0;
1781 }
1782
1783 __initcall(siccuart_init);
1784
1785 #ifdef CONFIG_SERIAL_SICC_CONSOLE
1786 /************** console driver *****************/
1787
1788 /*
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)
1793  */
1794 #ifdef used_and_not_const_char_pointer
1795 static int siccuart_console_read(struct console *co, const char *s, u_int count)
1796 {
1797     struct SICC_port *port = &sicc_ports[co->index];
1798     unsigned int status;
1799     char *w;
1800     int c;
1801
1802     pr_debug("siccuart_console_read() called\n");
1803
1804     c = 0;
1805     w = s;
1806     while (c < count) {
1807         if(readb(port->uart_base +  BL_SICC_LSR) & _LSR_RBR_FULL) {
1808             *w++ = readb(port->uart_base +  BL_SICC_RBR);
1809             c++;
1810         } else {
1811             // nothing more to get, return
1812             return c;
1813         }
1814     }
1815     // return the count
1816     return c;
1817 }
1818 #endif
1819
1820 /*
1821  *  Print a string to the serial port trying not to disturb
1822  *  any possible real use of the port...
1823  *
1824  *  The console_lock must be held when we get here.
1825  */
1826 static void siccuart_console_write(struct console *co, const char *s, u_int count)
1827 {
1828     struct SICC_port *port = &sicc_ports[co->index];
1829     unsigned int old_cr;
1830     int i;
1831
1832     /*
1833      *  First save the CR then disable the interrupts
1834      */
1835     old_cr = readb(port->uart_base +  BL_SICC_TxCR);
1836     writeb(old_cr & ~_TxCR_DME_MASK, port->uart_base +  BL_SICC_TxCR);
1837
1838     /*
1839      *  Now, do each character
1840      */
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);
1844         if (s[i] == '\n') {
1845             while ((readb(port->uart_base +  BL_SICC_LSR)&_LSR_TX_ALL) != _LSR_TX_ALL);
1846             writeb('\r', port->uart_base +  BL_SICC_TBR);
1847         }
1848     }
1849
1850     /*
1851      *  Finally, wait for transmitter to become empty
1852      *  and restore the TCR
1853      */
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);
1856 }
1857
1858 /*
1859  *  Receive character from the serial port
1860  */
1861 static int siccuart_console_wait_key(struct console *co)
1862 {
1863     struct SICC_port *port = &sicc_ports[co->index];
1864     int c;
1865
1866     while(!(readb(port->uart_base +  BL_SICC_LSR) & _LSR_RBR_FULL));
1867     c = readb(port->uart_base +  BL_SICC_RBR);
1868     return c;
1869 }
1870
1871 static struct tty_driver *siccuart_console_device(struct console *c, int *index)
1872 {
1873         *index = c->index;
1874         return siccnormal_driver;
1875 }
1876
1877 static int __init siccuart_console_setup(struct console *co, char *options)
1878 {
1879     struct SICC_port *port;
1880     int baud = 9600;
1881     int bits = 8;
1882     int parity = 'n';
1883     u_int cflag = CREAD | HUPCL | CLOCAL;
1884     u_int lcr_h, quot;
1885
1886
1887     if (co->index >= SERIAL_SICC_NR)
1888         co->index = 0;
1889
1890     port = &sicc_ports[co->index];
1891
1892     if (port->uart_base == 0)
1893         port->uart_base = (int)ioremap(port->uart_base_phys, PAGE_SIZE);
1894
1895     if (options) {
1896         char *s = options;
1897         baud = simple_strtoul(s, NULL, 10);
1898         while (*s >= '0' && *s <= '9')
1899             s++;
1900         if (*s) parity = *s++;
1901         if (*s) bits = *s - '0';
1902     }
1903
1904     /*
1905      *    Now construct a cflag setting.
1906      */
1907     switch (baud) {
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;
1916     }
1917     switch (bits) {
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;
1920     }
1921     switch (parity) {
1922     case 'o':
1923     case 'O': cflag |= PARODD; lcr_h |= _LCR_PTY_ODD;   break;
1924     case 'e':
1925     case 'E': cflag |= PARENB; lcr_h |= _LCR_PE_ENABLE |  _LCR_PTY_ODD; break;
1926     }
1927
1928     co->cflag = cflag;
1929
1930
1931        {
1932            // a copy of is inserted here ppc403SetBaud(com_port, (int)9600);
1933            unsigned long divisor, clockSource, temp;
1934            unsigned int rate = baud;
1935
1936           /* Ensure CICCR[7] is 0 to select Internal Baud Clock */
1937           powerpcMtcic_cr((unsigned long)(powerpcMfcic_cr() & 0xFEFFFFFF));
1938
1939           /* Determine Internal Baud Clock Frequency */
1940           /* powerpcMfclkgpcr() reads DCR 0x120 - the*/
1941           /* SCCR (Serial Clock Control Register) on Vesta */
1942           temp = powerpcMfclkgpcr();
1943
1944           if(temp & 0x00000080) {
1945               clockSource = 324000000;
1946           }
1947           else {
1948               clockSource = 216000000;
1949           }
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){
1954                divisor=0x00000FFF;
1955           }
1956
1957           quot = divisor;
1958        }
1959
1960     writeb((quot & 0x00000F00)>>8, port->uart_base + BL_SICC_BRDH );
1961     writeb( quot & 0x00000FF,      port->uart_base   + BL_SICC_BRDL );
1962
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);
1967
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);
1971
1972     // writeb(, info->port->uart_base + BL_SICC_RCR );
1973     /*
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
1977      * mode disabled.
1978      */
1979
1980     writeb( 0x00, port->uart_base + BL_SICC_IrCR );  // disable IrDA
1981
1982     readb(port->uart_base + BL_SICC_RBR);
1983
1984     writeb(0xf8, port->uart_base + BL_SICC_LSR);   /* reset bits 0-4 of LSR */
1985
1986     /* we will enable the port as we need it */
1987
1988     return 0;
1989 }
1990
1991 static struct console siccuart_cons =
1992 {
1993     .name =     SERIAL_SICC_NAME,
1994     .write =    siccuart_console_write,
1995 #ifdef used_and_not_const_char_pointer
1996     .read =     siccuart_console_read,
1997 #endif
1998     .device =   siccuart_console_device,
1999     .wait_key = siccuart_console_wait_key,
2000     .setup =    siccuart_console_setup,
2001     .flags =    CON_PRINTBUFFER,
2002     .index =    -1,
2003 };
2004
2005 void __init sicc_console_init(void)
2006 {
2007     register_console(&siccuart_cons);
2008 }
2009
2010 #endif /* CONFIG_SERIAL_SICC_CONSOLE */