root/drivers/sbus/char/sunserial.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. serial_paranoia_check
  2. read_zsreg
  3. write_zsreg
  4. load_zsregs
  5. zs_rtsdtr
  6. kgdb_chaninit
  7. get_zsbaud
  8. rs_stop
  9. rs_start
  10. batten_down_hatches
  11. rs_recv_clear
  12. rs_sched_event
  13. receive_chars
  14. transmit_chars
  15. status_handle
  16. rs_interrupt
  17. do_serial_bh
  18. do_softint
  19. do_serial_hangup
  20. rs_timer
  21. startup
  22. shutdown
  23. change_speed
  24. kbd_put_char
  25. mouse_put_char
  26. rs_put_char
  27. putDebugChar
  28. getDebugChar
  29. rs_fair_output
  30. zs_console_print
  31. rs_flush_chars
  32. rs_write
  33. rs_write_room
  34. rs_chars_in_buffer
  35. rs_flush_buffer
  36. rs_throttle
  37. rs_unthrottle
  38. get_serial_info
  39. set_serial_info
  40. get_lsr_info
  41. send_break
  42. rs_ioctl
  43. rs_set_termios
  44. rs_close
  45. rs_hangup
  46. block_til_ready
  47. rs_open
  48. show_serial_version
  49. get_zs
  50. rs_cons_check
  51. rs_init
  52. register_serial
  53. unregister_serial
  54. rs_cons_hook
  55. rs_kgdb_hook

   1 /* serial.c: Serial port driver for the Sparc.
   2  *
   3  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
   4  */
   5 
   6 #include <linux/errno.h>
   7 #include <linux/signal.h>
   8 #include <linux/sched.h>
   9 #include <linux/timer.h>
  10 #include <linux/interrupt.h>
  11 #include <linux/tty.h>
  12 #include <linux/tty_flip.h>
  13 #include <linux/config.h>
  14 #include <linux/major.h>
  15 #include <linux/string.h>
  16 #include <linux/fcntl.h>
  17 #include <linux/mm.h>
  18 #include <linux/kernel.h>
  19 
  20 #include <asm/io.h>
  21 #include <asm/irq.h>
  22 #include <asm/oplib.h>
  23 #include <asm/system.h>
  24 #include <asm/segment.h>
  25 #include <asm/bitops.h>
  26 #include <asm/delay.h>
  27 #include <asm/kdebug.h>
  28 
  29 #include "sunserial.h"
  30 
  31 #define NUM_SERIAL 2     /* Two chips on board. */
  32 #define NUM_CHANNELS (NUM_SERIAL * 2)
  33 
  34 #define KEYBOARD_LINE 0x2
  35 #define MOUSE_LINE    0x3
  36 
  37 struct sun_zslayout *zs_chips[NUM_SERIAL] = { 0, 0, };
  38 struct sun_zschannel *zs_channels[NUM_CHANNELS] = { 0, 0, 0, 0, };
  39 struct sun_zschannel *zs_conschan;
  40 struct sun_zschannel *zs_mousechan;
  41 struct sun_zschannel *zs_kbdchan;
  42 struct sun_zschannel *zs_kgdbchan;
  43 int zs_nodes[NUM_SERIAL] = { 0, 0, };
  44 
  45 struct sun_serial zs_soft[NUM_CHANNELS];
  46 struct sun_serial *zs_chain;  /* IRQ servicing chain */
  47 int zilog_irq;
  48 
  49 struct tty_struct zs_ttys[NUM_CHANNELS];
  50 /** struct tty_struct *zs_constty; **/
  51 
  52 /* Console hooks... */
  53 static int zs_cons_chanout = 0;
  54 static int zs_cons_chanin = 0;
  55 static struct l1a_kbd_state l1a_state = { 0, 0 };
  56 struct sun_serial *zs_consinfo = 0;
  57 
  58 /* Keyboard defines for L1-A processing... */
  59 #define SUNKBD_RESET   0xff
  60 #define SUNKBD_L1      0x01
  61 #define SUNKBD_UP      0x80
  62 #define SUNKBD_A       0x4d
  63 
  64 extern void sunkbd_inchar(unsigned char ch, unsigned char status, struct pt_regs *regs);
  65 extern void sun_mouse_inbyte(unsigned char byte, unsigned char status);
  66 
  67 static unsigned char kgdb_regs[16] = {
  68         0, 0, 0,                     /* write 0, 1, 2 */
  69         (Rx8 | RxENABLE),            /* write 3 */
  70         (X16CLK | SB1 | PAR_EVEN),   /* write 4 */
  71         (Tx8 | TxENAB),              /* write 5 */
  72         0, 0, 0,                     /* write 6, 7, 8 */
  73         (NV),                        /* write 9 */
  74         (NRZ),                       /* write 10 */
  75         (TCBR | RCBR),               /* write 11 */
  76         0, 0,                        /* BRG time constant, write 12 + 13 */
  77         (BRSRC | BRENABL),           /* write 14 */
  78         (DCDIE)                      /* write 15 */
  79 };
  80 
  81 #define ZS_CLOCK         4915200   /* Zilog input clock rate */
  82 
  83 DECLARE_TASK_QUEUE(tq_serial);
  84 
  85 struct tty_driver serial_driver, callout_driver;
  86 static int serial_refcount;
  87 
  88 /* serial subtype definitions */
  89 #define SERIAL_TYPE_NORMAL      1
  90 #define SERIAL_TYPE_CALLOUT     2
  91   
  92 /* number of characters left in xmit buffer before we ask for more */
  93 #define WAKEUP_CHARS 256
  94 
  95 /* Debugging... DEBUG_INTR is bad to use when one of the zs
  96  * lines is your console ;(
  97  */
  98 #undef SERIAL_DEBUG_INTR
  99 #undef SERIAL_DEBUG_OPEN
 100 #undef SERIAL_DEBUG_FLOW
 101 
 102 #define RS_STROBE_TIME 10
 103 #define RS_ISR_PASS_LIMIT 256
 104 
 105 #define _INLINE_ inline
 106 
 107 static void change_speed(struct sun_serial *info);
 108 
 109 static struct tty_struct *serial_table[NUM_CHANNELS];
 110 static struct termios *serial_termios[NUM_CHANNELS];
 111 static struct termios *serial_termios_locked[NUM_CHANNELS];
 112 
 113 #ifndef MIN
 114 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 115 #endif
 116 
 117 /*
 118  * tmp_buf is used as a temporary buffer by serial_write.  We need to
 119  * lock it in case the memcpy_fromfs blocks while swapping in a page,
 120  * and some other program tries to do a serial write at the same time.
 121  * Since the lock will only come under contention when the system is
 122  * swapping and available memory is low, it makes sense to share one
 123  * buffer across all the serial ports, since it significantly saves
 124  * memory if large numbers of serial ports are open.
 125  */
 126 static unsigned char tmp_buf[4096]; /* This is cheating */
 127 static struct semaphore tmp_buf_sem = MUTEX;
 128 
 129 static inline int serial_paranoia_check(struct sun_serial *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 130                                         dev_t device, const char *routine)
 131 {
 132 #ifdef SERIAL_PARANOIA_CHECK
 133         static const char *badmagic =
 134                 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
 135         static const char *badinfo =
 136                 "Warning: null sun_serial for (%d, %d) in %s\n";
 137 
 138         if (!info) {
 139                 printk(badinfo, MAJOR(device), MINOR(device), routine);
 140                 return 1;
 141         }
 142         if (info->magic != SERIAL_MAGIC) {
 143                 printk(badmagic, MAJOR(device), MINOR(device), routine);
 144                 return 1;
 145         }
 146 #endif
 147         return 0;
 148 }
 149 
 150 /*
 151  * This is used to figure out the divisor speeds and the timeouts
 152  */
 153 static int baud_table[] = {
 154         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 155         9600, 19200, 38400, 57600, 115200, 0 };
 156 
 157 /* Reading and writing Zilog8530 registers.  The delays are to make this
 158  * driver work on the Sun4 which needs a settling delay after each chip
 159  * register access, other machines handle this in hardware via auxiliary
 160  * flip-flops which implement the settle time we do in software.
 161  */
 162 static inline unsigned char read_zsreg(struct sun_zschannel *channel, unsigned char reg)
     /* [previous][next][first][last][top][bottom][index][help] */
 163 {
 164         unsigned char retval;
 165 
 166         channel->control = reg;
 167         udelay(5);
 168         retval = channel->control;
 169         udelay(5);
 170         return retval;
 171 }
 172 
 173 static inline void write_zsreg(struct sun_zschannel *channel, unsigned char reg, unsigned char value)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175         channel->control = reg;
 176         udelay(5);
 177         channel->control = value;
 178         udelay(5);
 179         return;
 180 }
 181 
 182 static inline void load_zsregs(struct sun_zschannel *channel, unsigned char *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184         ZS_CLEARERR(channel);
 185         ZS_CLEARFIFO(channel);
 186         /* Load 'em up */
 187         write_zsreg(channel, R4, regs[R4]);
 188         write_zsreg(channel, R10, regs[R10]);
 189         write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
 190         write_zsreg(channel, R5, regs[R5] & ~TxENAB);
 191         write_zsreg(channel, R1, regs[R1]);
 192         write_zsreg(channel, R9, regs[R9]);
 193         write_zsreg(channel, R11, regs[R11]);
 194         write_zsreg(channel, R12, regs[R12]);
 195         write_zsreg(channel, R13, regs[R13]);
 196         write_zsreg(channel, R14, regs[R14]);
 197         write_zsreg(channel, R15, regs[R15]);
 198         write_zsreg(channel, R3, regs[R3]);
 199         write_zsreg(channel, R5, regs[R5]);
 200         return;
 201 }
 202 
 203 /* Sets or clears DTR/RTS on the requested line */
 204 static inline void zs_rtsdtr(struct sun_serial *ss, int set)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         if(set) {
 207                 ss->curregs[5] |= (RTS | DTR);
 208                 ss->pendregs[5] = ss->curregs[5];
 209                 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
 210         } else {
 211                 ss->curregs[5] &= ~(RTS | DTR);
 212                 ss->pendregs[5] = ss->curregs[5];
 213                 write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
 214         }
 215         return;
 216 }
 217 
 218 static inline void kgdb_chaninit(struct sun_serial *ss, int intson, int bps)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220         int brg;
 221 
 222         if(intson) {
 223                 kgdb_regs[R1] = INT_ALL_Rx;
 224                 kgdb_regs[R9] |= MIE;
 225         } else {
 226                 kgdb_regs[R1] = 0;
 227                 kgdb_regs[R9] &= ~MIE;
 228         }
 229         brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
 230         kgdb_regs[R12] = (brg & 255);
 231         kgdb_regs[R13] = ((brg >> 8) & 255);
 232         load_zsregs(ss->zs_channel, kgdb_regs);
 233 }
 234 
 235 /* Utility routines for the Zilog */
 236 static inline int get_zsbaud(struct sun_serial *ss)
     /* [previous][next][first][last][top][bottom][index][help] */
 237 {
 238         struct sun_zschannel *channel = ss->zs_channel;
 239         int brg;
 240 
 241         /* The baud rate is split up between two 8-bit registers in
 242          * what is termed 'BRG time constant' format in my docs for
 243          * the chip, it is a function of the clk rate the chip is
 244          * receiving which happens to be constant.
 245          */
 246         brg = ((read_zsreg(channel, 13)&0xff) << 8);
 247         brg |= (read_zsreg(channel, 12)&0xff);
 248         return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
 249 }
 250 
 251 /*
 252  * ------------------------------------------------------------
 253  * rs_stop() and rs_start()
 254  *
 255  * This routines are called before setting or resetting tty->stopped.
 256  * They enable or disable transmitter interrupts, as necessary.
 257  * ------------------------------------------------------------
 258  */
 259 static void rs_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 260 {
 261         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
 262         unsigned long flags;
 263 
 264         if (serial_paranoia_check(info, tty->device, "rs_stop"))
 265                 return;
 266         
 267         save_flags(flags); cli();
 268         if (info->curregs[5] & TxENAB) {
 269                 info->curregs[5] &= ~TxENAB;
 270                 info->pendregs[5] &= ~TxENAB;
 271                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
 272         }
 273         restore_flags(flags);
 274 }
 275 
 276 static void rs_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
 279         unsigned long flags;
 280         
 281         if (serial_paranoia_check(info, tty->device, "rs_start"))
 282                 return;
 283         
 284         save_flags(flags); cli();
 285         if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
 286                 info->curregs[5] |= TxENAB;
 287                 info->pendregs[5] = info->curregs[5];
 288                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
 289         }
 290         restore_flags(flags);
 291 }
 292 
 293 /* Drop into either the boot monitor or kadb upon receiving a break
 294  * from keyboard/console input.
 295  */
 296 static void batten_down_hatches(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298         /* If we are doing kadb, we call the debugger
 299          * else we just drop into the boot monitor.
 300          * Note that we must flush the user windows
 301          * first before giving up control.
 302          */
 303         printk("\n");
 304         flush_user_windows();
 305         if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
 306            (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
 307                 sp_enter_debugger();
 308         else
 309                 prom_halt();
 310 
 311         /* XXX We want to notify the keyboard driver that all
 312          * XXX keys are in the up state or else weird things
 313          * XXX happen...
 314          */
 315 
 316         return;
 317 }
 318 
 319 /* On receive, this clears errors and the receiver interrupts */
 320 static inline void rs_recv_clear(struct sun_zschannel *zsc)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         zsc->control = ERR_RES;
 323         udelay(5);
 324         zsc->control = RES_H_IUS;
 325         udelay(5);
 326 }
 327 
 328 /*
 329  * ----------------------------------------------------------------------
 330  *
 331  * Here starts the interrupt handling routines.  All of the following
 332  * subroutines are declared as inline and are folded into
 333  * rs_interrupt().  They were separated out for readability's sake.
 334  *
 335  * Note: rs_interrupt() is a "fast" interrupt, which means that it
 336  * runs with interrupts turned off.  People who may want to modify
 337  * rs_interrupt() should try to keep the interrupt handler as fast as
 338  * possible.  After you are done making modifications, it is not a bad
 339  * idea to do:
 340  * 
 341  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 342  *
 343  * and look at the resulting assemble code in serial.s.
 344  *
 345  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
 346  * -----------------------------------------------------------------------
 347  */
 348 
 349 /*
 350  * This routine is used by the interrupt handler to schedule
 351  * processing in the software interrupt portion of the driver.
 352  */
 353 static _INLINE_ void rs_sched_event(struct sun_serial *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 354                                   int event)
 355 {
 356         info->event |= 1 << event;
 357         queue_task_irq_off(&info->tqueue, &tq_serial);
 358         mark_bh(SERIAL_BH);
 359 }
 360 
 361 extern void breakpoint(void);  /* For the KGDB frame character */
 362 
 363 static _INLINE_ void receive_chars(struct sun_serial *info, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 364 {
 365         struct tty_struct *tty = info->tty;
 366         unsigned char ch, stat;
 367 
 368         ch = info->zs_channel->data;
 369         udelay(5);
 370         stat = read_zsreg(info->zs_channel, R1);
 371         udelay(5);
 372 
 373         /* If this is the console keyboard, we need to handle
 374          * L1-A's here.
 375          */
 376         if(info->cons_keyb) {
 377                 if(ch == SUNKBD_RESET) {
 378                         l1a_state.kbd_id = 1;
 379                         l1a_state.l1_down = 0;
 380                 } else if(l1a_state.kbd_id) {
 381                         l1a_state.kbd_id = 0;
 382                 } else if(ch == SUNKBD_L1) {
 383                         l1a_state.l1_down = 1;
 384                 } else if(ch == (SUNKBD_L1|SUNKBD_UP)) {
 385                         l1a_state.l1_down = 0;
 386                 } else if(ch == SUNKBD_A && l1a_state.l1_down) {
 387                         /* whee... */
 388                         batten_down_hatches();
 389                         /* Clear the line and continue execution... */
 390                         rs_recv_clear(info->zs_channel);
 391                         l1a_state.l1_down = 0;
 392                         l1a_state.kbd_id = 0;
 393                         return;
 394                 }
 395                 rs_recv_clear(info->zs_channel);
 396                 sunkbd_inchar(ch, stat, regs);
 397 
 398                 return;
 399         }
 400         if(info->cons_mouse) {
 401                 rs_recv_clear(info->zs_channel);
 402                 sun_mouse_inbyte(ch, stat);
 403                 return;
 404         }
 405         if(info->is_cons) {
 406                 if(ch==0) { /* whee, break received */
 407                         batten_down_hatches();
 408                         rs_recv_clear(info->zs_channel);
 409                         return;
 410                 } else if (ch == 1) {
 411                         show_state();
 412                         return;
 413                 } else if (ch == 2) {
 414                         show_buffers();
 415                         return;
 416                 }
 417                 /* It is a 'keyboard interrupt' ;-) */
 418                 wake_up(&keypress_wait);
 419         }
 420         /* Look for kgdb 'stop' character, consult the gdb documentation
 421          * for remote target debugging and arch/sparc/kernel/sparc-stub.c
 422          * to see how all this works.
 423          */
 424         if((info->kgdb_channel) && (ch =='\003')) {
 425                 breakpoint();
 426                 goto clear_and_exit;
 427         }
 428 
 429         if(!tty)
 430                 goto clear_and_exit;
 431 
 432         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
 433                 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
 434         tty->flip.count++;
 435         if(stat & PAR_ERR)
 436                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
 437         else if(stat & Rx_OVR)
 438                 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
 439         else if(stat & CRC_ERR)
 440                 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
 441         else
 442                 *tty->flip.flag_buf_ptr++ = 0; /* XXX */
 443         *tty->flip.char_buf_ptr++ = ch;
 444 
 445         queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
 446 
 447 clear_and_exit:
 448         rs_recv_clear(info->zs_channel);
 449         return;
 450 }
 451 
 452 static _INLINE_ void transmit_chars(struct sun_serial *info)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454         /* P3: In theory we have to test readiness here because a
 455          * serial console can clog the chip through rs_put_char().
 456          * David did not do this. I think he relies on 3-chars FIFO in 8530.
 457          * Let's watch for lost _output_ characters. XXX
 458          */
 459 
 460         if (info->x_char) {
 461                 /* Send next char */
 462                 info->zs_channel->data = info->x_char;
 463                 udelay(5);
 464                 info->x_char = 0;
 465                 goto clear_and_return;
 466         }
 467 
 468         if((info->xmit_cnt <= 0) || info->tty->stopped) {
 469                 /* Thats peculiar... */
 470                 info->zs_channel->control = RES_Tx_P;
 471                 udelay(5);
 472                 goto clear_and_return;
 473         }
 474 
 475         /* Send char */
 476         info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
 477         udelay(5);
 478         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
 479         info->xmit_cnt--;
 480 
 481         if (info->xmit_cnt < WAKEUP_CHARS)
 482                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
 483 
 484         if(info->xmit_cnt <= 0) {
 485                 info->zs_channel->control = RES_Tx_P;
 486                 udelay(5);
 487                 goto clear_and_return;
 488         }
 489 
 490 clear_and_return:
 491         /* Clear interrupt */
 492         info->zs_channel->control = RES_H_IUS;
 493         udelay(5);
 494         return;
 495 }
 496 
 497 static _INLINE_ void status_handle(struct sun_serial *info)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499         unsigned char status;
 500 
 501         /* Get status from Read Register 0 */
 502         status = info->zs_channel->control;
 503         udelay(5);
 504         /* Clear status condition... */
 505         info->zs_channel->control = RES_EXT_INT;
 506         udelay(5);
 507         /* Clear the interrupt */
 508         info->zs_channel->control = RES_H_IUS;
 509         udelay(5);
 510 
 511 #if 0
 512         if(status & DCD) {
 513                 if((info->tty->termios->c_cflag & CRTSCTS) &&
 514                    ((info->curregs[3] & AUTO_ENAB)==0)) {
 515                         info->curregs[3] |= AUTO_ENAB;
 516                         info->pendregs[3] |= AUTO_ENAB;
 517                         write_zsreg(info->zs_channel, 3, info->curregs[3]);
 518                 }
 519         } else {
 520                 if((info->curregs[3] & AUTO_ENAB)) {
 521                         info->curregs[3] &= ~AUTO_ENAB;
 522                         info->pendregs[3] &= ~AUTO_ENAB;
 523                         write_zsreg(info->zs_channel, 3, info->curregs[3]);
 524                 }
 525         }
 526 #endif
 527         /* Whee, if this is console input and this is a
 528          * 'break asserted' status change interrupt, call
 529          * the boot prom.
 530          */
 531         if((status & BRK_ABRT) && info->break_abort)
 532                 batten_down_hatches();
 533 
 534         /* XXX Whee, put in a buffer somewhere, the status information
 535          * XXX whee whee whee... Where does the information go...
 536          */
 537         return;
 538 }
 539 
 540 /*
 541  * This is the serial driver's generic interrupt routine
 542  */
 543 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 544 {
 545         struct sun_serial * info;
 546         unsigned char zs_intreg;
 547 
 548         info = zs_chain;
 549         if (!info)
 550                 return;
 551 
 552         zs_intreg = read_zsreg(info->zs_channel, 3);
 553 
 554         /* NOTE: The read register 3, which holds the irq status,
 555          *       does so for both channels on each chip.  Although
 556          *       the status value itself must be read from the A
 557          *       channel and is only valid when read from channel A.
 558          *       Yes... broken hardware...
 559          */
 560 #define CHAN_A_IRQMASK (CHARxIP | CHATxIP | CHAEXT)
 561 #define CHAN_B_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
 562 
 563         /* *** Chip 1 *** */
 564         /* Channel A -- /dev/ttya, could be the console */
 565         if(zs_intreg & CHAN_A_IRQMASK) {
 566                 if (zs_intreg & CHARxIP)
 567                         receive_chars(info, regs);
 568                 if (zs_intreg & CHATxIP)
 569                         transmit_chars(info);
 570                 if (zs_intreg & CHAEXT)
 571                         status_handle(info);
 572         }
 573 
 574         info=info->zs_next;
 575 
 576         /* Channel B -- /dev/ttyb, could be the console */
 577         if(zs_intreg & CHAN_B_IRQMASK) {
 578                 if (zs_intreg & CHBRxIP)
 579                         receive_chars(info, regs);
 580                 if (zs_intreg & CHBTxIP)
 581                         transmit_chars(info);
 582                 if (zs_intreg & CHBEXT)
 583                         status_handle(info);
 584         }
 585 
 586         info = info->zs_next;
 587 
 588         zs_intreg = read_zsreg(info->zs_channel, 3);
 589         /* *** Chip 2 *** */
 590         /* Channel A -- /dev/kbd, pass communication to keyboard driver */
 591         if(zs_intreg & CHAN_A_IRQMASK) {
 592                 if (zs_intreg & CHARxIP)
 593                         receive_chars(info, regs);
 594                 if (zs_intreg & CHATxIP)
 595                         transmit_chars(info);
 596                 if (zs_intreg & CHAEXT)
 597                         status_handle(info);
 598         }
 599 
 600         info=info->zs_next;
 601 
 602         /* Channel B -- /dev/mouse, pass communication to mouse driver */
 603         if(zs_intreg & CHAN_B_IRQMASK) {
 604                 if (zs_intreg & CHBRxIP)
 605                         receive_chars(info, regs);
 606                 if (zs_intreg & CHBTxIP)
 607                         transmit_chars(info);
 608                 if (zs_intreg & CHBEXT)
 609                         status_handle(info);
 610         }
 611 
 612         return;
 613 }
 614 
 615 /*
 616  * -------------------------------------------------------------------
 617  * Here ends the serial interrupt routines.
 618  * -------------------------------------------------------------------
 619  */
 620 
 621 /*
 622  * This routine is used to handle the "bottom half" processing for the
 623  * serial driver, known also the "software interrupt" processing.
 624  * This processing is done at the kernel interrupt level, after the
 625  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 626  * is where time-consuming activities which can not be done in the
 627  * interrupt driver proper are done; the interrupt driver schedules
 628  * them using rs_sched_event(), and they get done here.
 629  */
 630 static void do_serial_bh(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 631 {
 632         run_task_queue(&tq_serial);
 633 }
 634 
 635 static void do_softint(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
 636 {
 637         struct sun_serial       *info = (struct sun_serial *) private_;
 638         struct tty_struct       *tty;
 639         
 640         tty = info->tty;
 641         if (!tty)
 642                 return;
 643 
 644         if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
 645                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 646                     tty->ldisc.write_wakeup)
 647                         (tty->ldisc.write_wakeup)(tty);
 648                 wake_up_interruptible(&tty->write_wait);
 649         }
 650 }
 651 
 652 /*
 653  * This routine is called from the scheduler tqueue when the interrupt
 654  * routine has signalled that a hangup has occurred.  The path of
 655  * hangup processing is:
 656  *
 657  *      serial interrupt routine -> (scheduler tqueue) ->
 658  *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
 659  * 
 660  */
 661 static void do_serial_hangup(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
 662 {
 663         struct sun_serial       *info = (struct sun_serial *) private_;
 664         struct tty_struct       *tty;
 665         
 666         tty = info->tty;
 667         if (!tty)
 668                 return;
 669 
 670         tty_hangup(tty);
 671 }
 672 
 673 
 674 /*
 675  * This subroutine is called when the RS_TIMER goes off.  It is used
 676  * by the serial driver to handle ports that do not have an interrupt
 677  * (irq=0).  This doesn't work very well for 16450's, but gives barely
 678  * passable results for a 16550A.  (Although at the expense of much
 679  * CPU overhead).
 680  */
 681 static void rs_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 682 {
 683         printk("rs_timer called\n");
 684         prom_halt();
 685         return;
 686 }
 687 
 688 static int startup(struct sun_serial * info)
     /* [previous][next][first][last][top][bottom][index][help] */
 689 {
 690         unsigned long flags;
 691 
 692         if (info->flags & ZILOG_INITIALIZED)
 693                 return 0;
 694 
 695         if (!info->xmit_buf) {
 696                 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
 697                 if (!info->xmit_buf)
 698                         return -ENOMEM;
 699         }
 700 
 701         save_flags(flags); cli();
 702 
 703 #ifdef SERIAL_DEBUG_OPEN
 704         printk("starting up ttys%d (irq %d)...", info->line, info->irq);
 705 #endif
 706 
 707         /*
 708          * Clear the FIFO buffers and disable them
 709          * (they will be reenabled in change_speed())
 710          */
 711         ZS_CLEARFIFO(info->zs_channel);
 712         info->xmit_fifo_size = 1;
 713 
 714         /*
 715          * Clear the interrupt registers.
 716          */
 717         info->zs_channel->control = ERR_RES;
 718         udelay(5);
 719         info->zs_channel->control = RES_H_IUS;
 720         udelay(5);
 721 
 722         /*
 723          * Now, initialize the Zilog
 724          */
 725         zs_rtsdtr(info, 1);
 726 
 727         /*
 728          * Finally, enable sequencing and interrupts
 729          */
 730         info->curregs[1] |= (info->curregs[1] & ~0x18) | (EXT_INT_ENAB|INT_ALL_Rx);
 731         info->pendregs[1] = info->curregs[1];
 732         info->curregs[3] |= (RxENABLE | Rx8);
 733         info->pendregs[3] = info->curregs[3];
 734         /* We enable Tx interrupts as needed. */
 735         info->curregs[5] |= (TxENAB | Tx8);
 736         info->pendregs[5] = info->curregs[5];
 737         info->curregs[9] |= (NV | MIE);
 738         info->pendregs[9] = info->curregs[9];
 739         write_zsreg(info->zs_channel, 3, info->curregs[3]);
 740         write_zsreg(info->zs_channel, 5, info->curregs[5]);
 741         write_zsreg(info->zs_channel, 9, info->curregs[9]);
 742         
 743         /*
 744          * And clear the interrupt registers again for luck.
 745          */
 746         info->zs_channel->control = ERR_RES;
 747         udelay(5);
 748         info->zs_channel->control = RES_H_IUS;
 749         udelay(5);
 750 
 751         if (info->tty)
 752                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
 753         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
 754 
 755         /*
 756          * Set up serial timers...
 757          */
 758 #if 0  /* Works well and stops the machine. */
 759         timer_table[RS_TIMER].expires = jiffies + 2;
 760         timer_active |= 1 << RS_TIMER;
 761 #endif
 762 
 763         /*
 764          * and set the speed of the serial port
 765          */
 766         change_speed(info);
 767 
 768         info->flags |= ZILOG_INITIALIZED;
 769         restore_flags(flags);
 770         return 0;
 771 }
 772 
 773 /*
 774  * This routine will shutdown a serial port; interrupts are disabled, and
 775  * DTR is dropped if the hangup on close termio flag is on.
 776  */
 777 static void shutdown(struct sun_serial * info)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779         unsigned long   flags;
 780 
 781         if (!(info->flags & ZILOG_INITIALIZED))
 782                 return;
 783 
 784 #ifdef SERIAL_DEBUG_OPEN
 785         printk("Shutting down serial port %d (irq %d)....", info->line,
 786                info->irq);
 787 #endif
 788         
 789         save_flags(flags); cli(); /* Disable interrupts */
 790         
 791         if (info->xmit_buf) {
 792                 free_page((unsigned long) info->xmit_buf);
 793                 info->xmit_buf = 0;
 794         }
 795 
 796         if (info->tty)
 797                 set_bit(TTY_IO_ERROR, &info->tty->flags);
 798         
 799         info->flags &= ~ZILOG_INITIALIZED;
 800         restore_flags(flags);
 801 }
 802 
 803 /*
 804  * This routine is called to set the UART divisor registers to match
 805  * the specified baud rate for a serial port.
 806  */
 807 static void change_speed(struct sun_serial *info)
     /* [previous][next][first][last][top][bottom][index][help] */
 808 {
 809         unsigned short port;
 810         unsigned cflag;
 811         int     i;
 812         int     brg;
 813 
 814         if (!info->tty || !info->tty->termios)
 815                 return;
 816         cflag = info->tty->termios->c_cflag;
 817         if (!(port = info->port))
 818                 return;
 819         i = cflag & CBAUD;
 820         if (i & CBAUDEX) {
 821                 /* XXX CBAUDEX is not obeyed.
 822                  * It is impossible at a 32bits SPARC.
 823                  * But we have to report this to user ... someday.
 824                  */
 825                 i = B9600;
 826         }
 827         info->zs_baud = baud_table[i];
 828         info->clk_divisor = 16;
 829 
 830         info->curregs[4] = X16CLK;
 831         info->curregs[11] = TCBR | RCBR;
 832         brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
 833         info->curregs[12] = (brg & 255);
 834         info->curregs[13] = ((brg >> 8) & 255);
 835         info->curregs[14] = BRSRC | BRENABL;
 836 
 837         /* byte size and parity */
 838         switch (cflag & CSIZE) {
 839         case CS5:
 840                 info->curregs[3] &= ~(0xc0);
 841                 info->curregs[3] |= Rx5;
 842                 info->pendregs[3] = info->curregs[3];
 843                 info->curregs[5] &= ~(0xe0);
 844                 info->curregs[5] |= Tx5;
 845                 info->pendregs[5] = info->curregs[5];
 846                 break;
 847         case CS6:
 848                 info->curregs[3] &= ~(0xc0);
 849                 info->curregs[3] |= Rx6;
 850                 info->pendregs[3] = info->curregs[3];
 851                 info->curregs[5] &= ~(0xe0);
 852                 info->curregs[5] |= Tx6;
 853                 info->pendregs[5] = info->curregs[5];
 854                 break;
 855         case CS7:
 856                 info->curregs[3] &= ~(0xc0);
 857                 info->curregs[3] |= Rx7;
 858                 info->pendregs[3] = info->curregs[3];
 859                 info->curregs[5] &= ~(0xe0);
 860                 info->curregs[5] |= Tx7;
 861                 info->pendregs[5] = info->curregs[5];
 862                 break;
 863         case CS8:
 864         default: /* defaults to 8 bits */
 865                 info->curregs[3] &= ~(0xc0);
 866                 info->curregs[3] |= Rx8;
 867                 info->pendregs[3] = info->curregs[3];
 868                 info->curregs[5] &= ~(0xe0);
 869                 info->curregs[5] |= Tx8;
 870                 info->pendregs[5] = info->curregs[5];
 871                 break;
 872         }
 873         info->curregs[4] &= ~(0x0c);
 874         if (cflag & CSTOPB) {
 875                 info->curregs[4] |= SB2;
 876         } else {
 877                 info->curregs[4] |= SB1;
 878         }
 879         info->pendregs[4] = info->curregs[4];
 880         if (cflag & PARENB) {
 881                 info->curregs[4] |= PAR_ENA;
 882                 info->pendregs[4] |= PAR_ENA;
 883         } else {
 884                 info->curregs[4] &= ~PAR_ENA;
 885                 info->pendregs[4] &= ~PAR_ENA;
 886         }
 887         if (!(cflag & PARODD)) {
 888                 info->curregs[4] |= PAR_EVEN;
 889                 info->pendregs[4] |= PAR_EVEN;
 890         } else {
 891                 info->curregs[4] &= ~PAR_EVEN;
 892                 info->pendregs[4] &= ~PAR_EVEN;
 893         }
 894 
 895         /* Load up the new values */
 896         load_zsregs(info->zs_channel, info->curregs);
 897 
 898         return;
 899 }
 900 
 901 /* This is for mouse/keyboard output.
 902  * XXX mouse output??? can we send it commands??? XXX
 903  */
 904 void kbd_put_char(unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
 905 {
 906         struct sun_zschannel *chan = zs_kbdchan;
 907         int flags, loops = 0;
 908 
 909         if(!chan)
 910                 return;
 911 
 912         save_flags(flags); cli();
 913         while((chan->control & Tx_BUF_EMP)==0 && loops < 10000) {
 914                 loops++;
 915                 udelay(5);
 916         }
 917 
 918         chan->data = ch;
 919         udelay(5);
 920         restore_flags(flags);
 921 }
 922 
 923 void mouse_put_char(char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925         struct sun_zschannel *chan = zs_mousechan;
 926         int flags, loops = 0;
 927 
 928         if(!chan)
 929                 return;
 930 
 931         save_flags(flags); cli();
 932         while((chan->control & Tx_BUF_EMP)==0 && loops < 10000) {
 933                 loops++;
 934                 udelay(5);
 935         }
 936 
 937         chan->data = ch;
 938         udelay(5);
 939         restore_flags(flags);
 940 }
 941 
 942 
 943 /* This is for console output over ttya/ttyb */
 944 static void rs_put_char(char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
 945 {
 946         struct sun_zschannel *chan = zs_conschan;
 947         int flags, loops = 0;
 948 
 949         if(!chan)
 950                 return;
 951 
 952         save_flags(flags); cli();
 953         while((chan->control & Tx_BUF_EMP)==0 && loops < 10000) {
 954                 loops++;
 955                 udelay(5);
 956         }
 957 
 958         chan->data = ch;
 959         udelay(5);
 960         restore_flags(flags);
 961 }
 962 
 963 /* These are for receiving and sending characters under the kgdb
 964  * source level kernel debugger.
 965  */
 966 void putDebugChar(char kgdb_char)
     /* [previous][next][first][last][top][bottom][index][help] */
 967 {
 968         struct sun_zschannel *chan = zs_kgdbchan;
 969 
 970         while((chan->control & Tx_BUF_EMP)==0)
 971                 udelay(5);
 972 
 973         chan->data = kgdb_char;
 974 }
 975 
 976 char getDebugChar(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 977 {
 978         struct sun_zschannel *chan = zs_kgdbchan;
 979 
 980         while((chan->control & Rx_CH_AV)==0)
 981                 barrier();
 982         return chan->data;
 983 }
 984 
 985 /*
 986  * Fair output driver allows a process to speak.
 987  */
 988 static void rs_fair_output(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 989 {
 990         int left;               /* Output no more than that */
 991         unsigned long flags;
 992         struct sun_serial *info = zs_consinfo;
 993         char c;
 994 
 995         if (info == 0) return;
 996         if (info->xmit_buf == 0) return;
 997 
 998         save_flags(flags);  cli();
 999         left = info->xmit_cnt;
1000         while (left != 0) {
1001                 c = info->xmit_buf[info->xmit_tail];
1002                 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
1003                 info->xmit_cnt--;
1004                 restore_flags(flags);
1005 
1006                 rs_put_char(c);
1007 
1008                 save_flags(flags);  cli();
1009                 left = MIN(info->xmit_cnt, left-1);
1010         }
1011 
1012         /* Last character is being transmitted now (hopefuly). */
1013         zs_conschan->control = RES_Tx_P;
1014         udelay(5);
1015 
1016         restore_flags(flags);
1017         return;
1018 }
1019 
1020 /*
1021  * zs_console_print is registered for printk.
1022  */
1023 static void zs_console_print(const char *p)
     /* [previous][next][first][last][top][bottom][index][help] */
1024 {
1025         char c;
1026 
1027         while((c=*(p++)) != 0) {
1028                 if(c == '\n')
1029                         rs_put_char('\r');
1030                 rs_put_char(c);
1031         }
1032 
1033         /* Comment this if you want to have a strict interrupt-driven output */
1034         rs_fair_output();
1035 
1036         return;
1037 }
1038 
1039 static void rs_flush_chars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1040 {
1041         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1042         unsigned long flags;
1043 
1044         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1045                 return;
1046 
1047         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1048             !info->xmit_buf)
1049                 return;
1050 
1051         /* Enable transmitter */
1052         save_flags(flags); cli();
1053         info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1054         info->pendregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1055         write_zsreg(info->zs_channel, 1, info->curregs[1]);
1056         info->curregs[5] |= TxENAB;
1057         info->pendregs[5] |= TxENAB;
1058         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1059 
1060         /*
1061          * Send a first (bootstrapping) character. A best solution is
1062          * to call transmit_chars() here which handles output in a
1063          * generic way. Current transmit_chars() not only transmits,
1064          * but resets interrupts also what we do not desire here.
1065          * XXX Discuss with David.
1066          */
1067         if (info->zs_channel->control & Tx_BUF_EMP) {
1068                 /* Send char */
1069                 info->zs_channel->data = info->xmit_buf[info->xmit_tail++];
1070                 udelay(5);
1071                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
1072                 info->xmit_cnt--;
1073         }
1074         restore_flags(flags);
1075 }
1076 
1077 static int rs_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
1078                     const unsigned char *buf, int count)
1079 {
1080         int     c, total = 0;
1081         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1082         unsigned long flags;
1083 
1084         if (serial_paranoia_check(info, tty->device, "rs_write"))
1085                 return 0;
1086 
1087         if (!tty || !info->xmit_buf)
1088                 return 0;
1089 
1090         save_flags(flags);
1091         while (1) {
1092                 cli();          
1093                 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1094                                    SERIAL_XMIT_SIZE - info->xmit_head));
1095                 if (c <= 0)
1096                         break;
1097 
1098                 if (from_user) {
1099                         down(&tmp_buf_sem);
1100                         memcpy_fromfs(tmp_buf, buf, c);
1101                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1102                                        SERIAL_XMIT_SIZE - info->xmit_head));
1103                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1104                         up(&tmp_buf_sem);
1105                 } else
1106                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1107                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1108                 info->xmit_cnt += c;
1109                 restore_flags(flags);
1110                 buf += c;
1111                 count -= c;
1112                 total += c;
1113         }
1114         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1115             !(info->curregs[5] & TxENAB)) {
1116                 /* Enable transmitter */
1117                 info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1118                 info->pendregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
1119                 write_zsreg(info->zs_channel, 1, info->curregs[1]);
1120                 info->curregs[5] |= TxENAB;
1121                 info->pendregs[5] |= TxENAB;
1122                 write_zsreg(info->zs_channel, 5, info->curregs[5]);
1123         }
1124         restore_flags(flags);
1125         return total;
1126 }
1127 
1128 static int rs_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1129 {
1130         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1131         int     ret;
1132                                 
1133         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1134                 return 0;
1135         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1136         if (ret < 0)
1137                 ret = 0;
1138         return ret;
1139 }
1140 
1141 static int rs_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1142 {
1143         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1144                                 
1145         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1146                 return 0;
1147         return info->xmit_cnt;
1148 }
1149 
1150 static void rs_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1151 {
1152         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1153                                 
1154         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1155                 return;
1156         cli();
1157         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1158         sti();
1159         wake_up_interruptible(&tty->write_wait);
1160         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1161             tty->ldisc.write_wakeup)
1162                 (tty->ldisc.write_wakeup)(tty);
1163 }
1164 
1165 /*
1166  * ------------------------------------------------------------
1167  * rs_throttle()
1168  * 
1169  * This routine is called by the upper-layer tty layer to signal that
1170  * incoming characters should be throttled.
1171  * ------------------------------------------------------------
1172  */
1173 static void rs_throttle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1174 {
1175         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1176 #ifdef SERIAL_DEBUG_THROTTLE
1177         char    buf[64];
1178         
1179         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1180                tty->ldisc.chars_in_buffer(tty));
1181 #endif
1182 
1183         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1184                 return;
1185         
1186         if (I_IXOFF(tty))
1187                 info->x_char = STOP_CHAR(tty);
1188 
1189         /* Turn off RTS line */
1190         cli();
1191         info->curregs[5] &= ~RTS;
1192         info->pendregs[5] &= ~RTS;
1193         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1194         sti();
1195 }
1196 
1197 static void rs_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1198 {
1199         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1200 #ifdef SERIAL_DEBUG_THROTTLE
1201         char    buf[64];
1202         
1203         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1204                tty->ldisc.chars_in_buffer(tty));
1205 #endif
1206 
1207         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1208                 return;
1209         
1210         if (I_IXOFF(tty)) {
1211                 if (info->x_char)
1212                         info->x_char = 0;
1213                 else
1214                         info->x_char = START_CHAR(tty);
1215         }
1216 
1217         /* Assert RTS line */
1218         cli();
1219         info->curregs[5] |= RTS;
1220         info->pendregs[5] |= RTS;
1221         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1222         sti();
1223 }
1224 
1225 /*
1226  * ------------------------------------------------------------
1227  * rs_ioctl() and friends
1228  * ------------------------------------------------------------
1229  */
1230 
1231 static int get_serial_info(struct sun_serial * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1232                            struct serial_struct * retinfo)
1233 {
1234         struct serial_struct tmp;
1235   
1236         if (!retinfo)
1237                 return -EFAULT;
1238         memset(&tmp, 0, sizeof(tmp));
1239         tmp.type = info->type;
1240         tmp.line = info->line;
1241         tmp.port = info->port;
1242         tmp.irq = info->irq;
1243         tmp.flags = info->flags;
1244         tmp.baud_base = info->baud_base;
1245         tmp.close_delay = info->close_delay;
1246         tmp.closing_wait = info->closing_wait;
1247         tmp.custom_divisor = info->custom_divisor;
1248         memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1249         return 0;
1250 }
1251 
1252 static int set_serial_info(struct sun_serial * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1253                            struct serial_struct * new_info)
1254 {
1255         struct serial_struct new_serial;
1256         struct sun_serial old_info;
1257         int                     retval = 0;
1258 
1259         if (!new_info)
1260                 return -EFAULT;
1261         memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1262         old_info = *info;
1263 
1264         if (!suser()) {
1265                 if ((new_serial.baud_base != info->baud_base) ||
1266                     (new_serial.type != info->type) ||
1267                     (new_serial.close_delay != info->close_delay) ||
1268                     ((new_serial.flags & ~ZILOG_USR_MASK) !=
1269                      (info->flags & ~ZILOG_USR_MASK)))
1270                         return -EPERM;
1271                 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1272                                (new_serial.flags & ZILOG_USR_MASK));
1273                 info->custom_divisor = new_serial.custom_divisor;
1274                 goto check_and_exit;
1275         }
1276 
1277         if (info->count > 1)
1278                 return -EBUSY;
1279 
1280         /*
1281          * OK, past this point, all the error checking has been done.
1282          * At this point, we start making changes.....
1283          */
1284 
1285         info->baud_base = new_serial.baud_base;
1286         info->flags = ((info->flags & ~ZILOG_FLAGS) |
1287                         (new_serial.flags & ZILOG_FLAGS));
1288         info->type = new_serial.type;
1289         info->close_delay = new_serial.close_delay;
1290         info->closing_wait = new_serial.closing_wait;
1291 
1292 check_and_exit:
1293         retval = startup(info);
1294         return retval;
1295 }
1296 
1297 /*
1298  * get_lsr_info - get line status register info
1299  *
1300  * Purpose: Let user call ioctl() to get info when the UART physically
1301  *          is emptied.  On bus types like RS485, the transmitter must
1302  *          release the bus after transmitting. This must be done when
1303  *          the transmit shift register is empty, not be done when the
1304  *          transmit holding register is empty.  This functionality
1305  *          allows RS485 driver to be written in user space. 
1306  */
1307 static int get_lsr_info(struct sun_serial * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1308 {
1309         unsigned char status;
1310 
1311         cli();
1312         status = info->zs_channel->control;
1313         sti();
1314         put_user(status,value);
1315         return 0;
1316 }
1317 
1318 /*
1319  * This routine sends a break character out the serial port.
1320  */
1321 static void send_break( struct sun_serial * info, int duration)
     /* [previous][next][first][last][top][bottom][index][help] */
1322 {
1323         if (!info->port)
1324                 return;
1325         current->state = TASK_INTERRUPTIBLE;
1326         current->timeout = jiffies + duration;
1327         cli();
1328         write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
1329         schedule();
1330         write_zsreg(info->zs_channel, 5, info->curregs[5]);
1331         sti();
1332 }
1333 
1334 static int rs_ioctl(struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1335                     unsigned int cmd, unsigned long arg)
1336 {
1337         int error;
1338         struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1339         int retval;
1340 
1341         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1342                 return -ENODEV;
1343 
1344         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1345             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1346             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1347                 if (tty->flags & (1 << TTY_IO_ERROR))
1348                     return -EIO;
1349         }
1350         
1351         switch (cmd) {
1352                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1353                         retval = tty_check_change(tty);
1354                         if (retval)
1355                                 return retval;
1356                         tty_wait_until_sent(tty, 0);
1357                         if (!arg)
1358                                 send_break(info, HZ/4); /* 1/4 second */
1359                         return 0;
1360                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1361                         retval = tty_check_change(tty);
1362                         if (retval)
1363                                 return retval;
1364                         tty_wait_until_sent(tty, 0);
1365                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1366                         return 0;
1367                 case TIOCGSOFTCAR:
1368                         error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1369                         if (error)
1370                                 return error;
1371                         put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1372                                     (unsigned long *) arg);
1373                         return 0;
1374                 case TIOCSSOFTCAR:
1375                         arg = get_fs_long((unsigned long *) arg);
1376                         tty->termios->c_cflag =
1377                                 ((tty->termios->c_cflag & ~CLOCAL) |
1378                                  (arg ? CLOCAL : 0));
1379                         return 0;
1380                 case TIOCGSERIAL:
1381                         error = verify_area(VERIFY_WRITE, (void *) arg,
1382                                                 sizeof(struct serial_struct));
1383                         if (error)
1384                                 return error;
1385                         return get_serial_info(info,
1386                                                (struct serial_struct *) arg);
1387                 case TIOCSSERIAL:
1388                         return set_serial_info(info,
1389                                                (struct serial_struct *) arg);
1390                 case TIOCSERGETLSR: /* Get line status register */
1391                         error = verify_area(VERIFY_WRITE, (void *) arg,
1392                                 sizeof(unsigned int));
1393                         if (error)
1394                                 return error;
1395                         else
1396                             return get_lsr_info(info, (unsigned int *) arg);
1397 
1398                 case TIOCSERGSTRUCT:
1399                         error = verify_area(VERIFY_WRITE, (void *) arg,
1400                                                 sizeof(struct sun_serial));
1401                         if (error)
1402                                 return error;
1403                         memcpy_tofs((struct sun_serial *) arg,
1404                                     info, sizeof(struct sun_serial));
1405                         return 0;
1406                         
1407                 default:
1408                         return -ENOIOCTLCMD;
1409                 }
1410         return 0;
1411 }
1412 
1413 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
1414 {
1415         struct sun_serial *info = (struct sun_serial *)tty->driver_data;
1416 
1417         if (tty->termios->c_cflag == old_termios->c_cflag)
1418                 return;
1419 
1420         change_speed(info);
1421 
1422         if ((old_termios->c_cflag & CRTSCTS) &&
1423             !(tty->termios->c_cflag & CRTSCTS)) {
1424                 tty->hw_stopped = 0;
1425                 rs_start(tty);
1426         }
1427 }
1428 
1429 /*
1430  * ------------------------------------------------------------
1431  * rs_close()
1432  * 
1433  * This routine is called when the serial port gets closed.  First, we
1434  * wait for the last remaining data to be sent.  Then, we unlink its
1435  * ZILOG structure from the interrupt chain if necessary, and we free
1436  * that IRQ if nothing is left in the chain.
1437  * ------------------------------------------------------------
1438  */
1439 static void rs_close(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1440 {
1441         struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1442         unsigned long flags;
1443 
1444         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1445                 return;
1446         
1447         save_flags(flags); cli();
1448         
1449         if (tty_hung_up_p(filp)) {
1450                 restore_flags(flags);
1451                 return;
1452         }
1453         
1454 #ifdef SERIAL_DEBUG_OPEN
1455         printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1456 #endif
1457         if ((tty->count == 1) && (info->count != 1)) {
1458                 /*
1459                  * Uh, oh.  tty->count is 1, which means that the tty
1460                  * structure will be freed.  Info->count should always
1461                  * be one in these conditions.  If it's greater than
1462                  * one, we've got real problems, since it means the
1463                  * serial port won't be shutdown.
1464                  */
1465                 printk("rs_close: bad serial port count; tty->count is 1, "
1466                        "info->count is %d\n", info->count);
1467                 info->count = 1;
1468         }
1469         if (--info->count < 0) {
1470                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1471                        info->line, info->count);
1472                 info->count = 0;
1473         }
1474         if (info->count) {
1475                 restore_flags(flags);
1476                 return;
1477         }
1478         info->flags |= ZILOG_CLOSING;
1479         /*
1480          * Save the termios structure, since this port may have
1481          * separate termios for callout and dialin.
1482          */
1483         if (info->flags & ZILOG_NORMAL_ACTIVE)
1484                 info->normal_termios = *tty->termios;
1485         if (info->flags & ZILOG_CALLOUT_ACTIVE)
1486                 info->callout_termios = *tty->termios;
1487         /*
1488          * Now we wait for the transmit buffer to clear; and we notify 
1489          * the line discipline to only process XON/XOFF characters.
1490          */
1491         tty->closing = 1;
1492         if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1493                 tty_wait_until_sent(tty, info->closing_wait);
1494         /*
1495          * At this point we stop accepting input.  To do this, we
1496          * disable the receive line status interrupts, and tell the
1497          * interrupt driver to stop checking the data ready bit in the
1498          * line status register.
1499          */
1500         /** if (!info->iscons) ... **/
1501         info->curregs[3] &= ~RxENABLE;
1502         info->pendregs[3] = info->curregs[3];
1503         write_zsreg(info->zs_channel, 3, info->curregs[3]);
1504         info->curregs[1] &= ~(0x18);
1505         info->pendregs[1] = info->curregs[1];
1506         write_zsreg(info->zs_channel, 1, info->curregs[1]);
1507         ZS_CLEARFIFO(info->zs_channel);
1508 
1509         shutdown(info);
1510         if (tty->driver.flush_buffer)
1511                 tty->driver.flush_buffer(tty);
1512         if (tty->ldisc.flush_buffer)
1513                 tty->ldisc.flush_buffer(tty);
1514         tty->closing = 0;
1515         info->event = 0;
1516         info->tty = 0;
1517         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1518                 if (tty->ldisc.close)
1519                         (tty->ldisc.close)(tty);
1520                 tty->ldisc = ldiscs[N_TTY];
1521                 tty->termios->c_line = N_TTY;
1522                 if (tty->ldisc.open)
1523                         (tty->ldisc.open)(tty);
1524         }
1525         if (info->blocked_open) {
1526                 if (info->close_delay) {
1527                         current->state = TASK_INTERRUPTIBLE;
1528                         current->timeout = jiffies + info->close_delay;
1529                         schedule();
1530                 }
1531                 wake_up_interruptible(&info->open_wait);
1532         }
1533         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1534                          ZILOG_CLOSING);
1535         wake_up_interruptible(&info->close_wait);
1536         restore_flags(flags);
1537 }
1538 
1539 /*
1540  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1541  */
1542 void rs_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1543 {
1544         struct sun_serial * info = (struct sun_serial *)tty->driver_data;
1545         
1546         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1547                 return;
1548         
1549         rs_flush_buffer(tty);
1550         shutdown(info);
1551         info->event = 0;
1552         info->count = 0;
1553         info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1554         info->tty = 0;
1555         wake_up_interruptible(&info->open_wait);
1556 }
1557 
1558 /*
1559  * ------------------------------------------------------------
1560  * rs_open() and friends
1561  * ------------------------------------------------------------
1562  */
1563 static int block_til_ready(struct tty_struct *tty, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
1564                            struct sun_serial *info)
1565 {
1566         struct wait_queue wait = { current, NULL };
1567         int             retval;
1568         int             do_clocal = 0;
1569 
1570         /*
1571          * If the device is in the middle of being closed, then block
1572          * until it's done, and then try again.
1573          */
1574         if (info->flags & ZILOG_CLOSING) {
1575                 interruptible_sleep_on(&info->close_wait);
1576 #ifdef SERIAL_DO_RESTART
1577                 if (info->flags & ZILOG_HUP_NOTIFY)
1578                         return -EAGAIN;
1579                 else
1580                         return -ERESTARTSYS;
1581 #else
1582                 return -EAGAIN;
1583 #endif
1584         }
1585 
1586         /*
1587          * If this is a callout device, then just make sure the normal
1588          * device isn't being used.
1589          */
1590         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1591                 if (info->flags & ZILOG_NORMAL_ACTIVE)
1592                         return -EBUSY;
1593                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1594                     (info->flags & ZILOG_SESSION_LOCKOUT) &&
1595                     (info->session != current->session))
1596                     return -EBUSY;
1597                 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1598                     (info->flags & ZILOG_PGRP_LOCKOUT) &&
1599                     (info->pgrp != current->pgrp))
1600                     return -EBUSY;
1601                 info->flags |= ZILOG_CALLOUT_ACTIVE;
1602                 return 0;
1603         }
1604         
1605         /*
1606          * If non-blocking mode is set, or the port is not enabled,
1607          * then make the check up front and then exit.
1608          */
1609         if ((filp->f_flags & O_NONBLOCK) ||
1610             (tty->flags & (1 << TTY_IO_ERROR))) {
1611                 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1612                         return -EBUSY;
1613                 info->flags |= ZILOG_NORMAL_ACTIVE;
1614                 return 0;
1615         }
1616 
1617         if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1618                 if (info->normal_termios.c_cflag & CLOCAL)
1619                         do_clocal = 1;
1620         } else {
1621                 if (tty->termios->c_cflag & CLOCAL)
1622                         do_clocal = 1;
1623         }
1624         
1625         /*
1626          * Block waiting for the carrier detect and the line to become
1627          * free (i.e., not in use by the callout).  While we are in
1628          * this loop, info->count is dropped by one, so that
1629          * rs_close() knows when to free things.  We restore it upon
1630          * exit, either normal or abnormal.
1631          */
1632         retval = 0;
1633         add_wait_queue(&info->open_wait, &wait);
1634 #ifdef SERIAL_DEBUG_OPEN
1635         printk("block_til_ready before block: ttys%d, count = %d\n",
1636                info->line, info->count);
1637 #endif
1638         info->count--;
1639         info->blocked_open++;
1640         while (1) {
1641                 cli();
1642                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
1643                         zs_rtsdtr(info, 1);
1644                 sti();
1645                 current->state = TASK_INTERRUPTIBLE;
1646                 if (tty_hung_up_p(filp) ||
1647                     !(info->flags & ZILOG_INITIALIZED)) {
1648 #ifdef SERIAL_DO_RESTART
1649                         if (info->flags & ZILOG_HUP_NOTIFY)
1650                                 retval = -EAGAIN;
1651                         else
1652                                 retval = -ERESTARTSYS;  
1653 #else
1654                         retval = -EAGAIN;
1655 #endif
1656                         break;
1657                 }
1658                 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1659                     !(info->flags & ZILOG_CLOSING) && do_clocal)
1660                         break;
1661                 if (current->signal & ~current->blocked) {
1662                         retval = -ERESTARTSYS;
1663                         break;
1664                 }
1665 #ifdef SERIAL_DEBUG_OPEN
1666                 printk("block_til_ready blocking: ttys%d, count = %d\n",
1667                        info->line, info->count);
1668 #endif
1669                 schedule();
1670         }
1671         current->state = TASK_RUNNING;
1672         remove_wait_queue(&info->open_wait, &wait);
1673         if (!tty_hung_up_p(filp))
1674                 info->count++;
1675         info->blocked_open--;
1676 #ifdef SERIAL_DEBUG_OPEN
1677         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1678                info->line, info->count);
1679 #endif
1680         if (retval)
1681                 return retval;
1682         info->flags |= ZILOG_NORMAL_ACTIVE;
1683         return 0;
1684 }       
1685 
1686 /*
1687  * This routine is called whenever a serial port is opened.  It
1688  * enables interrupts for a serial port, linking in its ZILOG structure into
1689  * the IRQ chain.   It also performs the serial-specific
1690  * initialization for the tty structure.
1691  */
1692 int rs_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1693 {
1694         struct sun_serial       *info;
1695         int                     retval, line;
1696 
1697         line = MINOR(tty->device) - tty->driver.minor_start;
1698         /* The zilog lines for the mouse/keyboard must be
1699          * opened using their respective drivers.
1700          */
1701         if ((line < 0) || (line >= NUM_CHANNELS))
1702                 return -ENODEV;
1703         if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
1704                 return -ENODEV;
1705         info = zs_soft + line;
1706         /* Is the kgdb running over this line? */
1707         if (info->kgdb_channel)
1708                 return -ENODEV;
1709         if (serial_paranoia_check(info, tty->device, "rs_open"))
1710                 return -ENODEV;
1711 #ifdef SERIAL_DEBUG_OPEN
1712         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1713                info->count);
1714 #endif
1715         info->count++;
1716         tty->driver_data = info;
1717         info->tty = tty;
1718 
1719         /*
1720          * Start up serial port
1721          */
1722         retval = startup(info);
1723         if (retval)
1724                 return retval;
1725 
1726         retval = block_til_ready(tty, filp, info);
1727         if (retval) {
1728 #ifdef SERIAL_DEBUG_OPEN
1729                 printk("rs_open returning after block_til_ready with %d\n",
1730                        retval);
1731 #endif
1732                 return retval;
1733         }
1734 
1735         if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1736                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1737                         *tty->termios = info->normal_termios;
1738                 else 
1739                         *tty->termios = info->callout_termios;
1740                 change_speed(info);
1741         }
1742 
1743         info->session = current->session;
1744         info->pgrp = current->pgrp;
1745 
1746 #ifdef SERIAL_DEBUG_OPEN
1747         printk("rs_open ttys%d successful...", info->line);
1748 #endif
1749         return 0;
1750 }
1751 
1752 /* Finally, routines used to initialize the serial driver. */
1753 
1754 static void show_serial_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1755 {
1756         printk("Sparc Zilog8530 serial driver version 1.00\n");
1757 }
1758 
1759 /* Probe the PROM for the request zs chip number. */
1760 static inline struct sun_zslayout *get_zs(int chip)
     /* [previous][next][first][last][top][bottom][index][help] */
1761 {
1762         struct linux_prom_irqs tmp_irq;
1763         unsigned long paddr = 0;
1764         unsigned long vaddr = 0;
1765         int zsnode, tmpnode, iospace, slave;
1766         static int irq = 0;
1767 
1768 #if CONFIG_AP1000
1769         printk("No zs chip\n");
1770         return NULL;
1771 #endif
1772 
1773         iospace = 0;
1774         if(chip < 0 || chip >= NUM_SERIAL)
1775                 panic("get_zs bogon zs chip number");
1776 
1777         if(sparc_cpu_model == sun4) {
1778                 /* Grrr, these have to be hardcoded aieee */
1779                 switch(chip) {
1780                 case 0:
1781                         paddr = 0xf1000000;
1782                         break;
1783                 case 1:
1784                         paddr = 0xf0000000;
1785                         break;
1786                 };
1787                 iospace = 0;
1788                 zs_nodes[chip] = 0;
1789                 if(!irq)
1790                         zilog_irq = irq = 12;
1791                 vaddr = (unsigned long)
1792                         sparc_alloc_io((char *) paddr, 0, 8,
1793                                        "Zilog Serial", iospace, 0);
1794         } else {
1795                 /* Can use the prom for other machine types */
1796                 zsnode = prom_getchild(prom_root_node);
1797                 tmpnode = prom_searchsiblings(zsnode, "obio");
1798                 if(tmpnode)
1799                         zsnode = prom_getchild(tmpnode);
1800                 if(!zsnode)
1801                         panic("get_zs no zs serial prom node");
1802                 while(zsnode) {
1803                         zsnode = prom_searchsiblings(zsnode, "zs");
1804                         slave = prom_getintdefault(zsnode, "slave", -1);
1805                         if(slave==chip) {
1806                                 /* The one we want */
1807                                 vaddr = (unsigned long)
1808                                         prom_getintdefault(zsnode, "address",
1809                                                                0xdeadbeef);
1810                                 if(vaddr == 0xdeadbeef)
1811                                         prom_halt();
1812                                 zs_nodes[chip] = zsnode;
1813                                 prom_getproperty(zsnode, "intr",
1814                                                  (char *) &tmp_irq,
1815                                                  sizeof(tmp_irq));
1816 #ifdef OLD_STYLE_IRQ
1817                                 tmp_irq.pri &= 0xf;
1818 #endif
1819                                 if(!irq) {
1820                                         irq = zilog_irq = tmp_irq.pri;
1821                                 } else {
1822                                         if(tmp_irq.pri != irq)
1823                                                 panic("zilog: bogon irqs");
1824                                 }
1825                                 break;
1826                         }
1827                         zsnode = prom_getsibling(zsnode);
1828                 }
1829                 if(!zsnode)
1830                         panic("get_zs whee chip not found");
1831         }
1832         if(!vaddr)
1833                 panic("get_zs whee no serial chip mappable");
1834 
1835         return (struct sun_zslayout *) vaddr;
1836 
1837 }
1838 
1839 
1840 extern void register_console(void (*proc)(const char *));
1841 
1842 static inline void
1843 rs_cons_check(struct sun_serial *ss, int channel)
     /* [previous][next][first][last][top][bottom][index][help] */
1844 {
1845         int i, o, io;
1846         static consout_registered = 0;
1847         static msg_printed = 0;
1848 
1849         i = o = io = 0;
1850 
1851         /* Is this one of the serial console lines? */
1852         if((zs_cons_chanout != channel) &&
1853            (zs_cons_chanin != channel))
1854                 return;
1855         zs_conschan = ss->zs_channel;
1856         zs_consinfo = ss;
1857 
1858         /* Register the console output putchar, if necessary */
1859         if((zs_cons_chanout == channel)) {
1860                 o = 1;
1861                 /* double whee.. */
1862                 if(!consout_registered) {
1863                         register_console(zs_console_print);
1864                         consout_registered = 1;
1865                 }
1866         }
1867 
1868         /* If this is console input, we handle the break received
1869          * status interrupt on this line to mean prom_halt().
1870          */
1871         if(zs_cons_chanin == channel) {
1872                 ss->break_abort = 1;
1873                 i = 1;
1874         }
1875         if(o && i)
1876                 io = 1;
1877         if(ss->zs_baud != 9600)
1878                 panic("Console baud rate weirdness");
1879 
1880         /* Set flag variable for this port so that it cannot be
1881          * opened for other uses by accident.
1882          */
1883         ss->is_cons = 1;
1884 
1885         if(io) {
1886                 if(!msg_printed) {
1887                         printk("zs%d: console I/O\n", ((channel>>1)&1));
1888                         msg_printed = 1;
1889                 }
1890         } else {
1891                 printk("zs%d: console %s\n", ((channel>>1)&1),
1892                        (i==1 ? "input" : (o==1 ? "output" : "WEIRD")));
1893         }
1894 }
1895 
1896 volatile int test_done;
1897 extern void keyboard_zsinit(void);
1898 extern void sun_mouse_zsinit(void);
1899 
1900 /* rs_init inits the driver */
1901 int rs_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1902 {
1903         int chip, channel, i, flags;
1904         struct sun_serial *info;
1905 
1906 #if CONFIG_AP1000
1907         printk("not doing rs_init()\n");
1908         return 0;
1909 #endif
1910 
1911         /* Setup base handler, and timer table. */
1912         init_bh(SERIAL_BH, do_serial_bh);
1913         timer_table[RS_TIMER].fn = rs_timer;
1914         timer_table[RS_TIMER].expires = 0;
1915 
1916         show_serial_version();
1917 
1918         /* Initialize the tty_driver structure */
1919         /* SPARC: Not all of this is exactly right for us. */
1920         
1921         memset(&serial_driver, 0, sizeof(struct tty_driver));
1922         serial_driver.magic = TTY_DRIVER_MAGIC;
1923         serial_driver.name = "ttyS";
1924         serial_driver.major = TTY_MAJOR;
1925         serial_driver.minor_start = 64;
1926         serial_driver.num = NUM_CHANNELS;
1927         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1928         serial_driver.subtype = SERIAL_TYPE_NORMAL;
1929         serial_driver.init_termios = tty_std_termios;
1930 
1931         serial_driver.init_termios.c_cflag =
1932                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1933         serial_driver.flags = TTY_DRIVER_REAL_RAW;
1934         serial_driver.refcount = &serial_refcount;
1935         serial_driver.table = serial_table;
1936         serial_driver.termios = serial_termios;
1937         serial_driver.termios_locked = serial_termios_locked;
1938 
1939         serial_driver.open = rs_open;
1940         serial_driver.close = rs_close;
1941         serial_driver.write = rs_write;
1942         serial_driver.flush_chars = rs_flush_chars;
1943         serial_driver.write_room = rs_write_room;
1944         serial_driver.chars_in_buffer = rs_chars_in_buffer;
1945         serial_driver.flush_buffer = rs_flush_buffer;
1946         serial_driver.ioctl = rs_ioctl;
1947         serial_driver.throttle = rs_throttle;
1948         serial_driver.unthrottle = rs_unthrottle;
1949         serial_driver.set_termios = rs_set_termios;
1950         serial_driver.stop = rs_stop;
1951         serial_driver.start = rs_start;
1952         serial_driver.hangup = rs_hangup;
1953 
1954         /*
1955          * The callout device is just like normal device except for
1956          * major number and the subtype code.
1957          */
1958         callout_driver = serial_driver;
1959         callout_driver.name = "cua";
1960         callout_driver.major = TTYAUX_MAJOR;
1961         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1962 
1963         if (tty_register_driver(&serial_driver))
1964                 panic("Couldn't register serial driver\n");
1965         if (tty_register_driver(&callout_driver))
1966                 panic("Couldn't register callout driver\n");
1967         
1968         save_flags(flags); cli();
1969 
1970         /* Set up our interrupt linked list */
1971         zs_chain = &zs_soft[0];
1972         zs_soft[0].zs_next = &zs_soft[1];
1973         zs_soft[1].zs_next = &zs_soft[2];
1974         zs_soft[2].zs_next = &zs_soft[3];
1975         zs_soft[3].zs_next = 0;
1976 
1977         for(chip = 0; chip < NUM_SERIAL; chip++) {
1978                 /* If we are doing kgdb over one of the channels on
1979                  * chip zero, kgdb_channel will be set to 1 by the
1980                  * rs_kgdb_hook() routine below.
1981                  */
1982                 if(!zs_chips[chip]) {
1983                         zs_chips[chip] = get_zs(chip);
1984                         /* Two channels per chip */
1985                         zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
1986                         zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
1987                         zs_soft[(chip*2)].kgdb_channel = 0;
1988                         zs_soft[(chip*2)+1].kgdb_channel = 0;
1989                 }
1990                 /* First, set up channel A on this chip. */
1991                 channel = chip * 2;
1992                 zs_soft[channel].zs_channel = zs_channels[channel];
1993                 zs_soft[channel].change_needed = 0;
1994                 zs_soft[channel].clk_divisor = 16;
1995                 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1996                 zs_soft[channel].cons_mouse = 0;
1997                 /* If not keyboard/mouse and is console serial
1998                  * line, then enable receiver interrupts.
1999                  */
2000                 if((channel<KEYBOARD_LINE) && (zs_soft[channel].is_cons)) {
2001                         write_zsreg(zs_soft[channel].zs_channel, R1,
2002                                     (EXT_INT_ENAB | INT_ALL_Rx));
2003                         write_zsreg(zs_soft[channel].zs_channel, R9, (NV | MIE));
2004                         write_zsreg(zs_soft[channel].zs_channel, R10, (NRZ));
2005                         write_zsreg(zs_soft[channel].zs_channel, R3, (Rx8|RxENABLE));
2006                         write_zsreg(zs_soft[channel].zs_channel, R5, (Tx8 | TxENAB));
2007                 }
2008                 /* If this is the kgdb line, enable interrupts because we
2009                  * now want to receive the 'control-c' character from the
2010                  * client attached to us asynchronously.
2011                  */
2012                 if(zs_soft[channel].kgdb_channel)
2013                         kgdb_chaninit(&zs_soft[channel], 1, zs_soft[channel].zs_baud);
2014 
2015                 if(channel == KEYBOARD_LINE) {
2016                         /* Tell keyboard driver about our presence. */
2017                         if(zs_soft[channel].zs_baud != 1200)
2018                                 panic("Weird keyboard serial baud rate");
2019                         zs_soft[channel].cons_keyb = 1;
2020                         zs_kbdchan = zs_soft[channel].zs_channel;
2021                         /* Enable Rx/Tx, IRQs, and inform kbd driver */
2022                         write_zsreg(zs_soft[channel].zs_channel, R1,
2023                                     (EXT_INT_ENAB | INT_ALL_Rx));
2024                         write_zsreg(zs_soft[channel].zs_channel, R4,
2025                                     (PAR_EVEN | X16CLK | SB1));
2026                         write_zsreg(zs_soft[channel].zs_channel, R9, (NV|MIE));
2027                         write_zsreg(zs_soft[channel].zs_channel, R10, (NRZ));
2028                         write_zsreg(zs_soft[channel].zs_channel, R11,
2029                                     (TCBR | RCBR));
2030                         write_zsreg(zs_soft[channel].zs_channel, R14,
2031                                     (BRSRC | BRENABL));
2032                         write_zsreg(zs_soft[channel].zs_channel, R3, (Rx8|RxENABLE));
2033                         write_zsreg(zs_soft[channel].zs_channel, R5,
2034                                     (Tx8 | TxENAB | DTR | RTS));
2035 #if 0
2036                         write_zsreg(zs_soft[channel].zs_channel, R15,
2037                                     (DCDIE | CTSIE | TxUIE | BRKIE));
2038 #endif
2039                         ZS_CLEARERR(zs_soft[channel].zs_channel);
2040                         ZS_CLEARFIFO(zs_soft[channel].zs_channel);
2041                 }
2042 
2043                 /* Now, channel B */
2044                 channel++;
2045                 zs_soft[channel].zs_channel = zs_channels[channel];
2046                 zs_soft[channel].change_needed = 0;
2047                 zs_soft[channel].clk_divisor = 16;
2048                 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2049                 zs_soft[channel].cons_keyb = 0;
2050                 /* If not keyboard/mouse and is console serial
2051                  * line, then enable receiver interrupts.
2052                  */
2053                 if(channel<KEYBOARD_LINE && zs_soft[channel].is_cons) {
2054                         write_zsreg(zs_soft[channel].zs_channel, R1,
2055                                     (EXT_INT_ENAB | INT_ALL_Rx));
2056                         write_zsreg(zs_soft[channel].zs_channel, R9,
2057                                     (NV | MIE));
2058                         write_zsreg(zs_soft[channel].zs_channel, R10,
2059                                     (NRZ));
2060                         write_zsreg(zs_soft[channel].zs_channel, R3,
2061                                     (Rx8|RxENABLE));
2062                         write_zsreg(zs_soft[channel].zs_channel, R5,
2063                                     (Tx8 | TxENAB | RTS | DTR));
2064                 }
2065                 if(channel == MOUSE_LINE) {
2066                         /* Tell mouse driver about our presence. */
2067                         if(zs_soft[channel].zs_baud != 1200)
2068                                 panic("Weird mouse serial baud rate");
2069                         zs_soft[channel].cons_mouse = 1;
2070                         zs_mousechan = zs_soft[channel].zs_channel;
2071                         /* Enable Rx, IRQs, and inform mouse driver */
2072                         write_zsreg(zs_soft[channel].zs_channel, R1, (INT_ALL_Rx));
2073                         write_zsreg(zs_soft[channel].zs_channel, R9, (NV|MIE));
2074                         write_zsreg(zs_soft[channel].zs_channel, R3, (Rx8|RxENABLE));
2075 #if 0 /* XXX hangs sun4c's sometimes */
2076                         write_zsreg(zs_soft[channel].zs_channel, R15,
2077                                     (DCDIE | CTSIE | TxUIE | BRKIE));
2078 #endif
2079                         sun_mouse_zsinit();
2080                 } else {
2081                         zs_soft[channel].cons_mouse = 0;
2082                 }
2083         }
2084 
2085         for(info=zs_chain, i=0; info; info = info->zs_next, i++)
2086         {
2087                 info->magic = SERIAL_MAGIC;
2088                 info->port = (int) info->zs_channel;
2089                 info->line = i;
2090                 info->tty = 0;
2091                 info->irq = zilog_irq;
2092                 info->custom_divisor = 16;
2093                 info->close_delay = 50;
2094                 info->closing_wait = 3000;
2095                 info->x_char = 0;
2096                 info->event = 0;
2097                 info->count = 0;
2098                 info->blocked_open = 0;
2099                 info->tqueue.routine = do_softint;
2100                 info->tqueue.data = info;
2101                 info->tqueue_hangup.routine = do_serial_hangup;
2102                 info->tqueue_hangup.data = info;
2103                 info->callout_termios =callout_driver.init_termios;
2104                 info->normal_termios = serial_driver.init_termios;
2105                 info->open_wait = 0;
2106                 info->close_wait = 0;
2107                 printk("tty%02d at 0x%04x (irq = %d)", info->line, 
2108                        info->port, info->irq);
2109                 printk(" is a Zilog8530\n");
2110         }
2111 
2112         if (request_irq(zilog_irq,
2113                         rs_interrupt,
2114                         (SA_INTERRUPT | SA_STATIC_ALLOC),
2115                         "Zilog8530", NULL))
2116                 panic("Unable to attach zs intr\n");
2117         restore_flags(flags);
2118 
2119         keyboard_zsinit();
2120 
2121         return 0;
2122 }
2123 
2124 /*
2125  * register_serial and unregister_serial allows for serial ports to be
2126  * configured at run-time, to support PCMCIA modems.
2127  */
2128 /* SPARC: Unused at this time, just here to make things link. */
2129 int register_serial(struct serial_struct *req)
     /* [previous][next][first][last][top][bottom][index][help] */
2130 {
2131         return -1;
2132 }
2133 
2134 void unregister_serial(int line)
     /* [previous][next][first][last][top][bottom][index][help] */
2135 {
2136         return;
2137 }
2138 
2139 /* Hooks for running a serial console.  con_init() calls this if the
2140  * console is being run over one of the ttya/ttyb serial ports.
2141  * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
2142  * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
2143  * are addressed backwards, channel B is first, then channel A.
2144  */
2145 void
2146 rs_cons_hook(int chip, int out, int channel)
     /* [previous][next][first][last][top][bottom][index][help] */
2147 {
2148         if(chip)
2149                 panic("rs_cons_hook called with chip not zero");
2150         if(!zs_chips[chip]) {
2151                 zs_chips[chip] = get_zs(chip);
2152                 /* Two channels per chip */
2153                 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2154                 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2155         }
2156         zs_soft[channel].zs_channel = zs_channels[channel];
2157         zs_soft[channel].change_needed = 0;
2158         zs_soft[channel].clk_divisor = 16;
2159         zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
2160         rs_cons_check(&zs_soft[channel], channel);
2161         if(out)
2162                 zs_cons_chanout = ((chip * 2) + channel);
2163         else
2164                 zs_cons_chanin = ((chip * 2) + channel);
2165 
2166 }
2167 
2168 /* This is called at boot time to prime the kgdb serial debugging
2169  * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2170  * for /dev/ttyb which is determined in setup_arch() from the
2171  * boot command line flags.
2172  */
2173 void
2174 rs_kgdb_hook(int tty_num)
     /* [previous][next][first][last][top][bottom][index][help] */
2175 {
2176         int chip = 0;
2177 
2178         if(!zs_chips[chip]) {
2179                 zs_chips[chip] = get_zs(chip);
2180                 /* Two channels per chip */
2181                 zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
2182                 zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
2183         }
2184         zs_soft[tty_num].zs_channel = zs_channels[tty_num];
2185         zs_kgdbchan = zs_soft[tty_num].zs_channel;
2186         zs_soft[tty_num].change_needed = 0;
2187         zs_soft[tty_num].clk_divisor = 16;
2188         zs_soft[tty_num].zs_baud = get_zsbaud(&zs_soft[tty_num]);
2189         zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
2190         zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2191         /* Turn on transmitter/receiver at 8-bits/char */
2192         kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
2193         ZS_CLEARERR(zs_kgdbchan);
2194         udelay(5);
2195         ZS_CLEARFIFO(zs_kgdbchan);
2196 }

/* [previous][next][first][last][top][bottom][index][help] */