sgiserial.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:59k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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