Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / vme_scc.c
blob8938a30942c70ad0dd4ff153ea5b6dd4beb7d7fc
1 /*
2 * drivers/char/vme_scc.c: MVME147, MVME162, BVME6000 SCC serial ports
3 * implementation.
4 * Copyright 1999 Richard Hirst <richard@sleepie.demon.co.uk>
6 * Based on atari_SCC.c which was
7 * Copyright 1994-95 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
8 * Partially based on PC-Linux serial.c by Linus Torvalds and Theodore Ts'o
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
12 * for more details.
16 #include <linux/module.h>
17 #include <linux/config.h>
18 #include <linux/kdev_t.h>
19 #include <asm/io.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/ioport.h>
23 #include <linux/interrupt.h>
24 #include <linux/errno.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/mm.h>
28 #include <linux/serial.h>
29 #include <linux/fcntl.h>
30 #include <linux/major.h>
31 #include <linux/delay.h>
32 #include <linux/version.h>
33 #include <linux/slab.h>
34 #include <linux/miscdevice.h>
35 #include <linux/console.h>
36 #include <linux/init.h>
37 #include <asm/setup.h>
38 #include <asm/bootinfo.h>
40 #ifdef CONFIG_MVME147_SCC
41 #include <asm/mvme147hw.h>
42 #endif
43 #ifdef CONFIG_MVME162_SCC
44 #include <asm/mvme16xhw.h>
45 #endif
46 #ifdef CONFIG_BVME6000_SCC
47 #include <asm/bvme6000hw.h>
48 #endif
50 #include <linux/generic_serial.h>
51 #include "scc.h"
54 #define CHANNEL_A 0
55 #define CHANNEL_B 1
57 #define SCC_MINOR_BASE 64
59 /* Shadows for all SCC write registers */
60 static unsigned char scc_shadow[2][16];
62 /* Location to access for SCC register access delay */
63 static volatile unsigned char *scc_del = NULL;
65 /* To keep track of STATUS_REG state for detection of Ext/Status int source */
66 static unsigned char scc_last_status_reg[2];
68 /***************************** Prototypes *****************************/
70 /* Function prototypes */
71 static void scc_disable_tx_interrupts(void * ptr);
72 static void scc_enable_tx_interrupts(void * ptr);
73 static void scc_disable_rx_interrupts(void * ptr);
74 static void scc_enable_rx_interrupts(void * ptr);
75 static int scc_get_CD(void * ptr);
76 static void scc_shutdown_port(void * ptr);
77 static int scc_set_real_termios(void *ptr);
78 static void scc_hungup(void *ptr);
79 static void scc_close(void *ptr);
80 static int scc_chars_in_buffer(void * ptr);
81 static int scc_open(struct tty_struct * tty, struct file * filp);
82 static int scc_ioctl(struct tty_struct * tty, struct file * filp,
83 unsigned int cmd, unsigned long arg);
84 static void scc_throttle(struct tty_struct *tty);
85 static void scc_unthrottle(struct tty_struct *tty);
86 static irqreturn_t scc_tx_int(int irq, void *data, struct pt_regs *fp);
87 static irqreturn_t scc_rx_int(int irq, void *data, struct pt_regs *fp);
88 static irqreturn_t scc_stat_int(int irq, void *data, struct pt_regs *fp);
89 static irqreturn_t scc_spcond_int(int irq, void *data, struct pt_regs *fp);
90 static void scc_setsignals(struct scc_port *port, int dtr, int rts);
91 static void scc_break_ctl(struct tty_struct *tty, int break_state);
93 static struct tty_driver *scc_driver;
95 struct scc_port scc_ports[2];
97 int scc_initialized = 0;
99 /*---------------------------------------------------------------------------
100 * Interface from generic_serial.c back here
101 *--------------------------------------------------------------------------*/
103 static struct real_driver scc_real_driver = {
104 scc_disable_tx_interrupts,
105 scc_enable_tx_interrupts,
106 scc_disable_rx_interrupts,
107 scc_enable_rx_interrupts,
108 scc_get_CD,
109 scc_shutdown_port,
110 scc_set_real_termios,
111 scc_chars_in_buffer,
112 scc_close,
113 scc_hungup,
114 NULL
118 static struct tty_operations scc_ops = {
119 .open = scc_open,
120 .close = gs_close,
121 .write = gs_write,
122 .put_char = gs_put_char,
123 .flush_chars = gs_flush_chars,
124 .write_room = gs_write_room,
125 .chars_in_buffer = gs_chars_in_buffer,
126 .flush_buffer = gs_flush_buffer,
127 .ioctl = scc_ioctl,
128 .throttle = scc_throttle,
129 .unthrottle = scc_unthrottle,
130 .set_termios = gs_set_termios,
131 .stop = gs_stop,
132 .start = gs_start,
133 .hangup = gs_hangup,
134 .break_ctl = scc_break_ctl,
137 /*----------------------------------------------------------------------------
138 * vme_scc_init() and support functions
139 *---------------------------------------------------------------------------*/
141 static int scc_init_drivers(void)
143 int error;
145 scc_driver = alloc_tty_driver(2);
146 if (!scc_driver)
147 return -ENOMEM;
148 scc_driver->owner = THIS_MODULE;
149 scc_driver->driver_name = "scc";
150 scc_driver->name = "ttyS";
151 scc_driver->devfs_name = "tts/";
152 scc_driver->major = TTY_MAJOR;
153 scc_driver->minor_start = SCC_MINOR_BASE;
154 scc_driver->type = TTY_DRIVER_TYPE_SERIAL;
155 scc_driver->subtype = SERIAL_TYPE_NORMAL;
156 scc_driver->init_termios = tty_std_termios;
157 scc_driver->init_termios.c_cflag =
158 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
159 scc_driver->flags = TTY_DRIVER_REAL_RAW;
160 tty_set_operations(scc_driver, &scc_ops);
162 if ((error = tty_register_driver(scc_driver))) {
163 printk(KERN_ERR "scc: Couldn't register scc driver, error = %d\n",
164 error);
165 put_tty_driver(scc_driver);
166 return 1;
169 return 0;
173 /* ports[] array is indexed by line no (i.e. [0] for ttyS0, [1] for ttyS1).
176 static void scc_init_portstructs(void)
178 struct scc_port *port;
179 int i;
181 for (i = 0; i < 2; i++) {
182 port = scc_ports + i;
183 port->gs.magic = SCC_MAGIC;
184 port->gs.close_delay = HZ/2;
185 port->gs.closing_wait = 30 * HZ;
186 port->gs.rd = &scc_real_driver;
187 #ifdef NEW_WRITE_LOCKING
188 port->gs.port_write_sem = MUTEX;
189 #endif
190 init_waitqueue_head(&port->gs.open_wait);
191 init_waitqueue_head(&port->gs.close_wait);
196 #ifdef CONFIG_MVME147_SCC
197 static int mvme147_scc_init(void)
199 struct scc_port *port;
201 printk(KERN_INFO "SCC: MVME147 Serial Driver\n");
202 /* Init channel A */
203 port = &scc_ports[0];
204 port->channel = CHANNEL_A;
205 port->ctrlp = (volatile unsigned char *)M147_SCC_A_ADDR;
206 port->datap = port->ctrlp + 1;
207 port->port_a = &scc_ports[0];
208 port->port_b = &scc_ports[1];
209 request_irq(MVME147_IRQ_SCCA_TX, scc_tx_int, SA_INTERRUPT,
210 "SCC-A TX", port);
211 request_irq(MVME147_IRQ_SCCA_STAT, scc_stat_int, SA_INTERRUPT,
212 "SCC-A status", port);
213 request_irq(MVME147_IRQ_SCCA_RX, scc_rx_int, SA_INTERRUPT,
214 "SCC-A RX", port);
215 request_irq(MVME147_IRQ_SCCA_SPCOND, scc_spcond_int, SA_INTERRUPT,
216 "SCC-A special cond", port);
218 SCC_ACCESS_INIT(port);
220 /* disable interrupts for this channel */
221 SCCwrite(INT_AND_DMA_REG, 0);
222 /* Set the interrupt vector */
223 SCCwrite(INT_VECTOR_REG, MVME147_IRQ_SCC_BASE);
224 /* Interrupt parameters: vector includes status, status low */
225 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
226 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
229 /* Init channel B */
230 port = &scc_ports[1];
231 port->channel = CHANNEL_B;
232 port->ctrlp = (volatile unsigned char *)M147_SCC_B_ADDR;
233 port->datap = port->ctrlp + 1;
234 port->port_a = &scc_ports[0];
235 port->port_b = &scc_ports[1];
236 request_irq(MVME147_IRQ_SCCB_TX, scc_tx_int, SA_INTERRUPT,
237 "SCC-B TX", port);
238 request_irq(MVME147_IRQ_SCCB_STAT, scc_stat_int, SA_INTERRUPT,
239 "SCC-B status", port);
240 request_irq(MVME147_IRQ_SCCB_RX, scc_rx_int, SA_INTERRUPT,
241 "SCC-B RX", port);
242 request_irq(MVME147_IRQ_SCCB_SPCOND, scc_spcond_int, SA_INTERRUPT,
243 "SCC-B special cond", port);
245 SCC_ACCESS_INIT(port);
247 /* disable interrupts for this channel */
248 SCCwrite(INT_AND_DMA_REG, 0);
251 /* Ensure interrupts are enabled in the PCC chip */
252 m147_pcc->serial_cntrl=PCC_LEVEL_SERIAL|PCC_INT_ENAB;
254 /* Initialise the tty driver structures and register */
255 scc_init_portstructs();
256 scc_init_drivers();
258 return 0;
260 #endif
263 #ifdef CONFIG_MVME162_SCC
264 static int mvme162_scc_init(void)
266 struct scc_port *port;
268 if (!(mvme16x_config & MVME16x_CONFIG_GOT_SCCA))
269 return (-ENODEV);
271 printk(KERN_INFO "SCC: MVME162 Serial Driver\n");
272 /* Init channel A */
273 port = &scc_ports[0];
274 port->channel = CHANNEL_A;
275 port->ctrlp = (volatile unsigned char *)MVME_SCC_A_ADDR;
276 port->datap = port->ctrlp + 2;
277 port->port_a = &scc_ports[0];
278 port->port_b = &scc_ports[1];
279 request_irq(MVME162_IRQ_SCCA_TX, scc_tx_int, SA_INTERRUPT,
280 "SCC-A TX", port);
281 request_irq(MVME162_IRQ_SCCA_STAT, scc_stat_int, SA_INTERRUPT,
282 "SCC-A status", port);
283 request_irq(MVME162_IRQ_SCCA_RX, scc_rx_int, SA_INTERRUPT,
284 "SCC-A RX", port);
285 request_irq(MVME162_IRQ_SCCA_SPCOND, scc_spcond_int, SA_INTERRUPT,
286 "SCC-A special cond", port);
288 SCC_ACCESS_INIT(port);
290 /* disable interrupts for this channel */
291 SCCwrite(INT_AND_DMA_REG, 0);
292 /* Set the interrupt vector */
293 SCCwrite(INT_VECTOR_REG, MVME162_IRQ_SCC_BASE);
294 /* Interrupt parameters: vector includes status, status low */
295 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
296 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
299 /* Init channel B */
300 port = &scc_ports[1];
301 port->channel = CHANNEL_B;
302 port->ctrlp = (volatile unsigned char *)MVME_SCC_B_ADDR;
303 port->datap = port->ctrlp + 2;
304 port->port_a = &scc_ports[0];
305 port->port_b = &scc_ports[1];
306 request_irq(MVME162_IRQ_SCCB_TX, scc_tx_int, SA_INTERRUPT,
307 "SCC-B TX", port);
308 request_irq(MVME162_IRQ_SCCB_STAT, scc_stat_int, SA_INTERRUPT,
309 "SCC-B status", port);
310 request_irq(MVME162_IRQ_SCCB_RX, scc_rx_int, SA_INTERRUPT,
311 "SCC-B RX", port);
312 request_irq(MVME162_IRQ_SCCB_SPCOND, scc_spcond_int, SA_INTERRUPT,
313 "SCC-B special cond", port);
316 SCC_ACCESS_INIT(port); /* Either channel will do */
318 /* disable interrupts for this channel */
319 SCCwrite(INT_AND_DMA_REG, 0);
322 /* Ensure interrupts are enabled in the MC2 chip */
323 *(volatile char *)0xfff4201d = 0x14;
325 /* Initialise the tty driver structures and register */
326 scc_init_portstructs();
327 scc_init_drivers();
329 return 0;
331 #endif
334 #ifdef CONFIG_BVME6000_SCC
335 static int bvme6000_scc_init(void)
337 struct scc_port *port;
339 printk(KERN_INFO "SCC: BVME6000 Serial Driver\n");
340 /* Init channel A */
341 port = &scc_ports[0];
342 port->channel = CHANNEL_A;
343 port->ctrlp = (volatile unsigned char *)BVME_SCC_A_ADDR;
344 port->datap = port->ctrlp + 4;
345 port->port_a = &scc_ports[0];
346 port->port_b = &scc_ports[1];
347 request_irq(BVME_IRQ_SCCA_TX, scc_tx_int, SA_INTERRUPT,
348 "SCC-A TX", port);
349 request_irq(BVME_IRQ_SCCA_STAT, scc_stat_int, SA_INTERRUPT,
350 "SCC-A status", port);
351 request_irq(BVME_IRQ_SCCA_RX, scc_rx_int, SA_INTERRUPT,
352 "SCC-A RX", port);
353 request_irq(BVME_IRQ_SCCA_SPCOND, scc_spcond_int, SA_INTERRUPT,
354 "SCC-A special cond", port);
356 SCC_ACCESS_INIT(port);
358 /* disable interrupts for this channel */
359 SCCwrite(INT_AND_DMA_REG, 0);
360 /* Set the interrupt vector */
361 SCCwrite(INT_VECTOR_REG, BVME_IRQ_SCC_BASE);
362 /* Interrupt parameters: vector includes status, status low */
363 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
364 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
367 /* Init channel B */
368 port = &scc_ports[1];
369 port->channel = CHANNEL_B;
370 port->ctrlp = (volatile unsigned char *)BVME_SCC_B_ADDR;
371 port->datap = port->ctrlp + 4;
372 port->port_a = &scc_ports[0];
373 port->port_b = &scc_ports[1];
374 request_irq(BVME_IRQ_SCCB_TX, scc_tx_int, SA_INTERRUPT,
375 "SCC-B TX", port);
376 request_irq(BVME_IRQ_SCCB_STAT, scc_stat_int, SA_INTERRUPT,
377 "SCC-B status", port);
378 request_irq(BVME_IRQ_SCCB_RX, scc_rx_int, SA_INTERRUPT,
379 "SCC-B RX", port);
380 request_irq(BVME_IRQ_SCCB_SPCOND, scc_spcond_int, SA_INTERRUPT,
381 "SCC-B special cond", port);
384 SCC_ACCESS_INIT(port); /* Either channel will do */
386 /* disable interrupts for this channel */
387 SCCwrite(INT_AND_DMA_REG, 0);
390 /* Initialise the tty driver structures and register */
391 scc_init_portstructs();
392 scc_init_drivers();
394 return 0;
396 #endif
399 int vme_scc_init(void)
401 int res = -ENODEV;
402 static int called = 0;
404 if (called)
405 return res;
406 called = 1;
407 #ifdef CONFIG_MVME147_SCC
408 if (MACH_IS_MVME147)
409 res = mvme147_scc_init();
410 #endif
411 #ifdef CONFIG_MVME162_SCC
412 if (MACH_IS_MVME16x)
413 res = mvme162_scc_init();
414 #endif
415 #ifdef CONFIG_BVME6000_SCC
416 if (MACH_IS_BVME6000)
417 res = bvme6000_scc_init();
418 #endif
419 return res;
423 /*---------------------------------------------------------------------------
424 * Interrupt handlers
425 *--------------------------------------------------------------------------*/
427 static irqreturn_t scc_rx_int(int irq, void *data, struct pt_regs *fp)
429 unsigned char ch;
430 struct scc_port *port = data;
431 struct tty_struct *tty = port->gs.tty;
432 SCC_ACCESS_INIT(port);
434 ch = SCCread_NB(RX_DATA_REG);
435 if (!tty) {
436 printk(KERN_WARNING "scc_rx_int with NULL tty!\n");
437 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
438 return IRQ_HANDLED;
440 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
441 *tty->flip.char_buf_ptr = ch;
442 *tty->flip.flag_buf_ptr = 0;
443 tty->flip.flag_buf_ptr++;
444 tty->flip.char_buf_ptr++;
445 tty->flip.count++;
448 /* Check if another character is already ready; in that case, the
449 * spcond_int() function must be used, because this character may have an
450 * error condition that isn't signalled by the interrupt vector used!
452 if (SCCread(INT_PENDING_REG) &
453 (port->channel == CHANNEL_A ? IPR_A_RX : IPR_B_RX)) {
454 scc_spcond_int (irq, data, fp);
455 return IRQ_HANDLED;
458 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
460 tty_flip_buffer_push(tty);
461 return IRQ_HANDLED;
465 static irqreturn_t scc_spcond_int(int irq, void *data, struct pt_regs *fp)
467 struct scc_port *port = data;
468 struct tty_struct *tty = port->gs.tty;
469 unsigned char stat, ch, err;
470 int int_pending_mask = port->channel == CHANNEL_A ?
471 IPR_A_RX : IPR_B_RX;
472 SCC_ACCESS_INIT(port);
474 if (!tty) {
475 printk(KERN_WARNING "scc_spcond_int with NULL tty!\n");
476 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
477 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
478 return IRQ_HANDLED;
480 do {
481 stat = SCCread(SPCOND_STATUS_REG);
482 ch = SCCread_NB(RX_DATA_REG);
484 if (stat & SCSR_RX_OVERRUN)
485 err = TTY_OVERRUN;
486 else if (stat & SCSR_PARITY_ERR)
487 err = TTY_PARITY;
488 else if (stat & SCSR_CRC_FRAME_ERR)
489 err = TTY_FRAME;
490 else
491 err = 0;
493 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
494 *tty->flip.char_buf_ptr = ch;
495 *tty->flip.flag_buf_ptr = err;
496 tty->flip.flag_buf_ptr++;
497 tty->flip.char_buf_ptr++;
498 tty->flip.count++;
501 /* ++TeSche: *All* errors have to be cleared manually,
502 * else the condition persists for the next chars
504 if (err)
505 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
507 } while(SCCread(INT_PENDING_REG) & int_pending_mask);
509 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
511 tty_flip_buffer_push(tty);
512 return IRQ_HANDLED;
516 static irqreturn_t scc_tx_int(int irq, void *data, struct pt_regs *fp)
518 struct scc_port *port = data;
519 SCC_ACCESS_INIT(port);
521 if (!port->gs.tty) {
522 printk(KERN_WARNING "scc_tx_int with NULL tty!\n");
523 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
524 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET);
525 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
526 return IRQ_HANDLED;
528 while ((SCCread_NB(STATUS_REG) & SR_TX_BUF_EMPTY)) {
529 if (port->x_char) {
530 SCCwrite(TX_DATA_REG, port->x_char);
531 port->x_char = 0;
533 else if ((port->gs.xmit_cnt <= 0) || port->gs.tty->stopped ||
534 port->gs.tty->hw_stopped)
535 break;
536 else {
537 SCCwrite(TX_DATA_REG, port->gs.xmit_buf[port->gs.xmit_tail++]);
538 port->gs.xmit_tail = port->gs.xmit_tail & (SERIAL_XMIT_SIZE-1);
539 if (--port->gs.xmit_cnt <= 0)
540 break;
543 if ((port->gs.xmit_cnt <= 0) || port->gs.tty->stopped ||
544 port->gs.tty->hw_stopped) {
545 /* disable tx interrupts */
546 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
547 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET); /* disable tx_int on next tx underrun? */
548 port->gs.flags &= ~GS_TX_INTEN;
550 if (port->gs.tty && port->gs.xmit_cnt <= port->gs.wakeup_chars) {
551 if ((port->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
552 port->gs.tty->ldisc.write_wakeup)
553 (port->gs.tty->ldisc.write_wakeup)(port->gs.tty);
554 wake_up_interruptible(&port->gs.tty->write_wait);
557 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
558 return IRQ_HANDLED;
562 static irqreturn_t scc_stat_int(int irq, void *data, struct pt_regs *fp)
564 struct scc_port *port = data;
565 unsigned channel = port->channel;
566 unsigned char last_sr, sr, changed;
567 SCC_ACCESS_INIT(port);
569 last_sr = scc_last_status_reg[channel];
570 sr = scc_last_status_reg[channel] = SCCread_NB(STATUS_REG);
571 changed = last_sr ^ sr;
573 if (changed & SR_DCD) {
574 port->c_dcd = !!(sr & SR_DCD);
575 if (!(port->gs.flags & ASYNC_CHECK_CD))
576 ; /* Don't report DCD changes */
577 else if (port->c_dcd) {
578 wake_up_interruptible(&port->gs.open_wait);
580 else {
581 if (port->gs.tty)
582 tty_hangup (port->gs.tty);
585 SCCwrite(COMMAND_REG, CR_EXTSTAT_RESET);
586 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
587 return IRQ_HANDLED;
591 /*---------------------------------------------------------------------------
592 * generic_serial.c callback funtions
593 *--------------------------------------------------------------------------*/
595 static void scc_disable_tx_interrupts(void *ptr)
597 struct scc_port *port = ptr;
598 unsigned long flags;
599 SCC_ACCESS_INIT(port);
601 local_irq_save(flags);
602 SCCmod(INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
603 port->gs.flags &= ~GS_TX_INTEN;
604 local_irq_restore(flags);
608 static void scc_enable_tx_interrupts(void *ptr)
610 struct scc_port *port = ptr;
611 unsigned long flags;
612 SCC_ACCESS_INIT(port);
614 local_irq_save(flags);
615 SCCmod(INT_AND_DMA_REG, 0xff, IDR_TX_INT_ENAB);
616 /* restart the transmitter */
617 scc_tx_int (0, port, 0);
618 local_irq_restore(flags);
622 static void scc_disable_rx_interrupts(void *ptr)
624 struct scc_port *port = ptr;
625 unsigned long flags;
626 SCC_ACCESS_INIT(port);
628 local_irq_save(flags);
629 SCCmod(INT_AND_DMA_REG,
630 ~(IDR_RX_INT_MASK|IDR_PARERR_AS_SPCOND|IDR_EXTSTAT_INT_ENAB), 0);
631 local_irq_restore(flags);
635 static void scc_enable_rx_interrupts(void *ptr)
637 struct scc_port *port = ptr;
638 unsigned long flags;
639 SCC_ACCESS_INIT(port);
641 local_irq_save(flags);
642 SCCmod(INT_AND_DMA_REG, 0xff,
643 IDR_EXTSTAT_INT_ENAB|IDR_PARERR_AS_SPCOND|IDR_RX_INT_ALL);
644 local_irq_restore(flags);
648 static int scc_get_CD(void *ptr)
650 struct scc_port *port = ptr;
651 unsigned channel = port->channel;
653 return !!(scc_last_status_reg[channel] & SR_DCD);
657 static void scc_shutdown_port(void *ptr)
659 struct scc_port *port = ptr;
661 port->gs.flags &= ~ GS_ACTIVE;
662 if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
663 scc_setsignals (port, 0, 0);
668 static int scc_set_real_termios (void *ptr)
670 /* the SCC has char sizes 5,7,6,8 in that order! */
671 static int chsize_map[4] = { 0, 2, 1, 3 };
672 unsigned cflag, baud, chsize, channel, brgval = 0;
673 unsigned long flags;
674 struct scc_port *port = ptr;
675 SCC_ACCESS_INIT(port);
677 if (!port->gs.tty || !port->gs.tty->termios) return 0;
679 channel = port->channel;
681 if (channel == CHANNEL_A)
682 return 0; /* Settings controlled by boot PROM */
684 cflag = port->gs.tty->termios->c_cflag;
685 baud = port->gs.baud;
686 chsize = (cflag & CSIZE) >> 4;
688 if (baud == 0) {
689 /* speed == 0 -> drop DTR */
690 local_irq_save(flags);
691 SCCmod(TX_CTRL_REG, ~TCR_DTR, 0);
692 local_irq_restore(flags);
693 return 0;
695 else if ((MACH_IS_MVME16x && (baud < 50 || baud > 38400)) ||
696 (MACH_IS_MVME147 && (baud < 50 || baud > 19200)) ||
697 (MACH_IS_BVME6000 &&(baud < 50 || baud > 76800))) {
698 printk(KERN_NOTICE "SCC: Bad speed requested, %d\n", baud);
699 return 0;
702 if (cflag & CLOCAL)
703 port->gs.flags &= ~ASYNC_CHECK_CD;
704 else
705 port->gs.flags |= ASYNC_CHECK_CD;
707 #ifdef CONFIG_MVME147_SCC
708 if (MACH_IS_MVME147)
709 brgval = (M147_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
710 #endif
711 #ifdef CONFIG_MVME162_SCC
712 if (MACH_IS_MVME16x)
713 brgval = (MVME_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
714 #endif
715 #ifdef CONFIG_BVME6000_SCC
716 if (MACH_IS_BVME6000)
717 brgval = (BVME_SCC_RTxC + baud/2) / (16 * 2 * baud) - 2;
718 #endif
719 /* Now we have all parameters and can go to set them: */
720 local_irq_save(flags);
722 /* receiver's character size and auto-enables */
723 SCCmod(RX_CTRL_REG, ~(RCR_CHSIZE_MASK|RCR_AUTO_ENAB_MODE),
724 (chsize_map[chsize] << 6) |
725 ((cflag & CRTSCTS) ? RCR_AUTO_ENAB_MODE : 0));
726 /* parity and stop bits (both, Tx and Rx), clock mode never changes */
727 SCCmod (AUX1_CTRL_REG,
728 ~(A1CR_PARITY_MASK | A1CR_MODE_MASK),
729 ((cflag & PARENB
730 ? (cflag & PARODD ? A1CR_PARITY_ODD : A1CR_PARITY_EVEN)
731 : A1CR_PARITY_NONE)
732 | (cflag & CSTOPB ? A1CR_MODE_ASYNC_2 : A1CR_MODE_ASYNC_1)));
733 /* sender's character size, set DTR for valid baud rate */
734 SCCmod(TX_CTRL_REG, ~TCR_CHSIZE_MASK, chsize_map[chsize] << 5 | TCR_DTR);
735 /* clock sources never change */
736 /* disable BRG before changing the value */
737 SCCmod(DPLL_CTRL_REG, ~DCR_BRG_ENAB, 0);
738 /* BRG value */
739 SCCwrite(TIMER_LOW_REG, brgval & 0xff);
740 SCCwrite(TIMER_HIGH_REG, (brgval >> 8) & 0xff);
741 /* BRG enable, and clock source never changes */
742 SCCmod(DPLL_CTRL_REG, 0xff, DCR_BRG_ENAB);
744 local_irq_restore(flags);
746 return 0;
750 static int scc_chars_in_buffer (void *ptr)
752 struct scc_port *port = ptr;
753 SCC_ACCESS_INIT(port);
755 return (SCCread (SPCOND_STATUS_REG) & SCSR_ALL_SENT) ? 0 : 1;
759 /* Comment taken from sx.c (2.4.0):
760 I haven't the foggiest why the decrement use count has to happen
761 here. The whole linux serial drivers stuff needs to be redesigned.
762 My guess is that this is a hack to minimize the impact of a bug
763 elsewhere. Thinking about it some more. (try it sometime) Try
764 running minicom on a serial port that is driven by a modularized
765 driver. Have the modem hangup. Then remove the driver module. Then
766 exit minicom. I expect an "oops". -- REW */
768 static void scc_hungup(void *ptr)
770 scc_disable_tx_interrupts(ptr);
771 scc_disable_rx_interrupts(ptr);
775 static void scc_close(void *ptr)
777 scc_disable_tx_interrupts(ptr);
778 scc_disable_rx_interrupts(ptr);
782 /*---------------------------------------------------------------------------
783 * Internal support functions
784 *--------------------------------------------------------------------------*/
786 static void scc_setsignals(struct scc_port *port, int dtr, int rts)
788 unsigned long flags;
789 unsigned char t;
790 SCC_ACCESS_INIT(port);
792 local_irq_save(flags);
793 t = SCCread(TX_CTRL_REG);
794 if (dtr >= 0) t = dtr? (t | TCR_DTR): (t & ~TCR_DTR);
795 if (rts >= 0) t = rts? (t | TCR_RTS): (t & ~TCR_RTS);
796 SCCwrite(TX_CTRL_REG, t);
797 local_irq_restore(flags);
801 static void scc_send_xchar(struct tty_struct *tty, char ch)
803 struct scc_port *port = (struct scc_port *)tty->driver_data;
805 port->x_char = ch;
806 if (ch)
807 scc_enable_tx_interrupts(port);
811 /*---------------------------------------------------------------------------
812 * Driver entrypoints referenced from above
813 *--------------------------------------------------------------------------*/
815 static int scc_open (struct tty_struct * tty, struct file * filp)
817 int line = tty->index;
818 int retval;
819 struct scc_port *port = &scc_ports[line];
820 int i, channel = port->channel;
821 unsigned long flags;
822 SCC_ACCESS_INIT(port);
823 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_MVME147_SCC)
824 static const struct {
825 unsigned reg, val;
826 } mvme_init_tab[] = {
827 /* Values for MVME162 and MVME147 */
828 /* no parity, 1 stop bit, async, 1:16 */
829 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
830 /* parity error is special cond, ints disabled, no DMA */
831 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
832 /* Rx 8 bits/char, no auto enable, Rx off */
833 { RX_CTRL_REG, RCR_CHSIZE_8 },
834 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
835 { TX_CTRL_REG, TCR_CHSIZE_8 },
836 /* special features off */
837 { AUX2_CTRL_REG, 0 },
838 { CLK_CTRL_REG, CCR_RXCLK_BRG | CCR_TXCLK_BRG },
839 { DPLL_CTRL_REG, DCR_BRG_ENAB | DCR_BRG_USE_PCLK },
840 /* Start Rx */
841 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
842 /* Start Tx */
843 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
844 /* Ext/Stat ints: DCD only */
845 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
846 /* Reset Ext/Stat ints */
847 { COMMAND_REG, CR_EXTSTAT_RESET },
848 /* ...again */
849 { COMMAND_REG, CR_EXTSTAT_RESET },
851 #endif
852 #if defined(CONFIG_BVME6000_SCC)
853 static const struct {
854 unsigned reg, val;
855 } bvme_init_tab[] = {
856 /* Values for BVME6000 */
857 /* no parity, 1 stop bit, async, 1:16 */
858 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
859 /* parity error is special cond, ints disabled, no DMA */
860 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
861 /* Rx 8 bits/char, no auto enable, Rx off */
862 { RX_CTRL_REG, RCR_CHSIZE_8 },
863 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
864 { TX_CTRL_REG, TCR_CHSIZE_8 },
865 /* special features off */
866 { AUX2_CTRL_REG, 0 },
867 { CLK_CTRL_REG, CCR_RTxC_XTAL | CCR_RXCLK_BRG | CCR_TXCLK_BRG },
868 { DPLL_CTRL_REG, DCR_BRG_ENAB },
869 /* Start Rx */
870 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
871 /* Start Tx */
872 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
873 /* Ext/Stat ints: DCD only */
874 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
875 /* Reset Ext/Stat ints */
876 { COMMAND_REG, CR_EXTSTAT_RESET },
877 /* ...again */
878 { COMMAND_REG, CR_EXTSTAT_RESET },
880 #endif
881 if (!(port->gs.flags & ASYNC_INITIALIZED)) {
882 local_irq_save(flags);
883 #if defined(CONFIG_MVME147_SCC) || defined(CONFIG_MVME162_SCC)
884 if (MACH_IS_MVME147 || MACH_IS_MVME16x) {
885 for (i=0; i<sizeof(mvme_init_tab)/sizeof(*mvme_init_tab); ++i)
886 SCCwrite(mvme_init_tab[i].reg, mvme_init_tab[i].val);
888 #endif
889 #if defined(CONFIG_BVME6000_SCC)
890 if (MACH_IS_BVME6000) {
891 for (i=0; i<sizeof(bvme_init_tab)/sizeof(*bvme_init_tab); ++i)
892 SCCwrite(bvme_init_tab[i].reg, bvme_init_tab[i].val);
894 #endif
896 /* remember status register for detection of DCD and CTS changes */
897 scc_last_status_reg[channel] = SCCread(STATUS_REG);
899 port->c_dcd = 0; /* Prevent initial 1->0 interrupt */
900 scc_setsignals (port, 1,1);
901 local_irq_restore(flags);
904 tty->driver_data = port;
905 port->gs.tty = tty;
906 port->gs.count++;
907 retval = gs_init_port(&port->gs);
908 if (retval) {
909 port->gs.count--;
910 return retval;
912 port->gs.flags |= GS_ACTIVE;
913 retval = gs_block_til_ready(port, filp);
915 if (retval) {
916 port->gs.count--;
917 return retval;
920 port->c_dcd = scc_get_CD (port);
922 scc_enable_rx_interrupts(port);
924 return 0;
928 static void scc_throttle (struct tty_struct * tty)
930 struct scc_port *port = (struct scc_port *)tty->driver_data;
931 unsigned long flags;
932 SCC_ACCESS_INIT(port);
934 if (tty->termios->c_cflag & CRTSCTS) {
935 local_irq_save(flags);
936 SCCmod(TX_CTRL_REG, ~TCR_RTS, 0);
937 local_irq_restore(flags);
939 if (I_IXOFF(tty))
940 scc_send_xchar(tty, STOP_CHAR(tty));
944 static void scc_unthrottle (struct tty_struct * tty)
946 struct scc_port *port = (struct scc_port *)tty->driver_data;
947 unsigned long flags;
948 SCC_ACCESS_INIT(port);
950 if (tty->termios->c_cflag & CRTSCTS) {
951 local_irq_save(flags);
952 SCCmod(TX_CTRL_REG, 0xff, TCR_RTS);
953 local_irq_restore(flags);
955 if (I_IXOFF(tty))
956 scc_send_xchar(tty, START_CHAR(tty));
960 static int scc_ioctl(struct tty_struct *tty, struct file *file,
961 unsigned int cmd, unsigned long arg)
963 return -ENOIOCTLCMD;
967 static void scc_break_ctl(struct tty_struct *tty, int break_state)
969 struct scc_port *port = (struct scc_port *)tty->driver_data;
970 unsigned long flags;
971 SCC_ACCESS_INIT(port);
973 local_irq_save(flags);
974 SCCmod(TX_CTRL_REG, ~TCR_SEND_BREAK,
975 break_state ? TCR_SEND_BREAK : 0);
976 local_irq_restore(flags);
980 /*---------------------------------------------------------------------------
981 * Serial console stuff...
982 *--------------------------------------------------------------------------*/
984 #define scc_delay() do { __asm__ __volatile__ (" nop; nop"); } while (0)
986 static void scc_ch_write (char ch)
988 volatile char *p = NULL;
990 #ifdef CONFIG_MVME147_SCC
991 if (MACH_IS_MVME147)
992 p = (volatile char *)M147_SCC_A_ADDR;
993 #endif
994 #ifdef CONFIG_MVME162_SCC
995 if (MACH_IS_MVME16x)
996 p = (volatile char *)MVME_SCC_A_ADDR;
997 #endif
998 #ifdef CONFIG_BVME6000_SCC
999 if (MACH_IS_BVME6000)
1000 p = (volatile char *)BVME_SCC_A_ADDR;
1001 #endif
1003 do {
1004 scc_delay();
1006 while (!(*p & 4));
1007 scc_delay();
1008 *p = 8;
1009 scc_delay();
1010 *p = ch;
1013 /* The console must be locked when we get here. */
1015 static void scc_console_write (struct console *co, const char *str, unsigned count)
1017 unsigned long flags;
1019 local_irq_save(flags);
1021 while (count--)
1023 if (*str == '\n')
1024 scc_ch_write ('\r');
1025 scc_ch_write (*str++);
1027 local_irq_restore(flags);
1030 static struct tty_driver *scc_console_device(struct console *c, int *index)
1032 *index = c->index;
1033 return scc_driver;
1037 static int __init scc_console_setup(struct console *co, char *options)
1039 return 0;
1043 static struct console sercons = {
1044 .name = "ttyS",
1045 .write = scc_console_write,
1046 .device = scc_console_device,
1047 .setup = scc_console_setup,
1048 .flags = CON_PRINTBUFFER,
1049 .index = -1,
1053 static int __init vme_scc_console_init(void)
1055 if (vme_brdtype == VME_TYPE_MVME147 ||
1056 vme_brdtype == VME_TYPE_MVME162 ||
1057 vme_brdtype == VME_TYPE_MVME172 ||
1058 vme_brdtype == VME_TYPE_BVME4000 ||
1059 vme_brdtype == VME_TYPE_BVME6000)
1060 register_console(&sercons);
1061 return 0;
1063 console_initcall(vme_scc_console_init);