irq: use kcalloc() instead of the bootmem allocator
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / vme_scc.c
blob994e1a58b987c0616b71fa485e7a8ea608959a39
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/kdev_t.h>
18 #include <asm/io.h>
19 #include <linux/kernel.h>
20 #include <linux/ioport.h>
21 #include <linux/interrupt.h>
22 #include <linux/errno.h>
23 #include <linux/tty.h>
24 #include <linux/tty_flip.h>
25 #include <linux/mm.h>
26 #include <linux/serial.h>
27 #include <linux/fcntl.h>
28 #include <linux/major.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/miscdevice.h>
32 #include <linux/console.h>
33 #include <linux/init.h>
34 #include <asm/setup.h>
35 #include <asm/bootinfo.h>
37 #ifdef CONFIG_MVME147_SCC
38 #include <asm/mvme147hw.h>
39 #endif
40 #ifdef CONFIG_MVME162_SCC
41 #include <asm/mvme16xhw.h>
42 #endif
43 #ifdef CONFIG_BVME6000_SCC
44 #include <asm/bvme6000hw.h>
45 #endif
47 #include <linux/generic_serial.h>
48 #include "scc.h"
51 #define CHANNEL_A 0
52 #define CHANNEL_B 1
54 #define SCC_MINOR_BASE 64
56 /* Shadows for all SCC write registers */
57 static unsigned char scc_shadow[2][16];
59 /* Location to access for SCC register access delay */
60 static volatile unsigned char *scc_del = NULL;
62 /* To keep track of STATUS_REG state for detection of Ext/Status int source */
63 static unsigned char scc_last_status_reg[2];
65 /***************************** Prototypes *****************************/
67 /* Function prototypes */
68 static void scc_disable_tx_interrupts(void * ptr);
69 static void scc_enable_tx_interrupts(void * ptr);
70 static void scc_disable_rx_interrupts(void * ptr);
71 static void scc_enable_rx_interrupts(void * ptr);
72 static int scc_carrier_raised(struct tty_port *port);
73 static void scc_shutdown_port(void * ptr);
74 static int scc_set_real_termios(void *ptr);
75 static void scc_hungup(void *ptr);
76 static void scc_close(void *ptr);
77 static int scc_chars_in_buffer(void * ptr);
78 static int scc_open(struct tty_struct * tty, struct file * filp);
79 static int scc_ioctl(struct tty_struct * tty, struct file * filp,
80 unsigned int cmd, unsigned long arg);
81 static void scc_throttle(struct tty_struct *tty);
82 static void scc_unthrottle(struct tty_struct *tty);
83 static irqreturn_t scc_tx_int(int irq, void *data);
84 static irqreturn_t scc_rx_int(int irq, void *data);
85 static irqreturn_t scc_stat_int(int irq, void *data);
86 static irqreturn_t scc_spcond_int(int irq, void *data);
87 static void scc_setsignals(struct scc_port *port, int dtr, int rts);
88 static int scc_break_ctl(struct tty_struct *tty, int break_state);
90 static struct tty_driver *scc_driver;
92 static struct scc_port scc_ports[2];
94 /*---------------------------------------------------------------------------
95 * Interface from generic_serial.c back here
96 *--------------------------------------------------------------------------*/
98 static struct real_driver scc_real_driver = {
99 scc_disable_tx_interrupts,
100 scc_enable_tx_interrupts,
101 scc_disable_rx_interrupts,
102 scc_enable_rx_interrupts,
103 scc_shutdown_port,
104 scc_set_real_termios,
105 scc_chars_in_buffer,
106 scc_close,
107 scc_hungup,
108 NULL
112 static const struct tty_operations scc_ops = {
113 .open = scc_open,
114 .close = gs_close,
115 .write = gs_write,
116 .put_char = gs_put_char,
117 .flush_chars = gs_flush_chars,
118 .write_room = gs_write_room,
119 .chars_in_buffer = gs_chars_in_buffer,
120 .flush_buffer = gs_flush_buffer,
121 .ioctl = scc_ioctl,
122 .throttle = scc_throttle,
123 .unthrottle = scc_unthrottle,
124 .set_termios = gs_set_termios,
125 .stop = gs_stop,
126 .start = gs_start,
127 .hangup = gs_hangup,
128 .break_ctl = scc_break_ctl,
131 static const struct tty_port_operations scc_port_ops = {
132 .carrier_raised = scc_carrier_raised,
135 /*----------------------------------------------------------------------------
136 * vme_scc_init() and support functions
137 *---------------------------------------------------------------------------*/
139 static int scc_init_drivers(void)
141 int error;
143 scc_driver = alloc_tty_driver(2);
144 if (!scc_driver)
145 return -ENOMEM;
146 scc_driver->owner = THIS_MODULE;
147 scc_driver->driver_name = "scc";
148 scc_driver->name = "ttyS";
149 scc_driver->major = TTY_MAJOR;
150 scc_driver->minor_start = SCC_MINOR_BASE;
151 scc_driver->type = TTY_DRIVER_TYPE_SERIAL;
152 scc_driver->subtype = SERIAL_TYPE_NORMAL;
153 scc_driver->init_termios = tty_std_termios;
154 scc_driver->init_termios.c_cflag =
155 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
156 scc_driver->init_termios.c_ispeed = 9600;
157 scc_driver->init_termios.c_ospeed = 9600;
158 scc_driver->flags = TTY_DRIVER_REAL_RAW;
159 tty_set_operations(scc_driver, &scc_ops);
161 if ((error = tty_register_driver(scc_driver))) {
162 printk(KERN_ERR "scc: Couldn't register scc driver, error = %d\n",
163 error);
164 put_tty_driver(scc_driver);
165 return 1;
168 return 0;
172 /* ports[] array is indexed by line no (i.e. [0] for ttyS0, [1] for ttyS1).
175 static void scc_init_portstructs(void)
177 struct scc_port *port;
178 int i;
180 for (i = 0; i < 2; i++) {
181 port = scc_ports + i;
182 tty_port_init(&port->gs.port);
183 port->gs.port.ops = &scc_port_ops;
184 port->gs.magic = SCC_MAGIC;
185 port->gs.close_delay = HZ/2;
186 port->gs.closing_wait = 30 * HZ;
187 port->gs.rd = &scc_real_driver;
188 #ifdef NEW_WRITE_LOCKING
189 port->gs.port_write_mutex = MUTEX;
190 #endif
191 init_waitqueue_head(&port->gs.port.open_wait);
192 init_waitqueue_head(&port->gs.port.close_wait);
197 #ifdef CONFIG_MVME147_SCC
198 static int mvme147_scc_init(void)
200 struct scc_port *port;
201 int error;
203 printk(KERN_INFO "SCC: MVME147 Serial Driver\n");
204 /* Init channel A */
205 port = &scc_ports[0];
206 port->channel = CHANNEL_A;
207 port->ctrlp = (volatile unsigned char *)M147_SCC_A_ADDR;
208 port->datap = port->ctrlp + 1;
209 port->port_a = &scc_ports[0];
210 port->port_b = &scc_ports[1];
211 error = request_irq(MVME147_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
212 "SCC-A TX", port);
213 if (error)
214 goto fail;
215 error = request_irq(MVME147_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
216 "SCC-A status", port);
217 if (error)
218 goto fail_free_a_tx;
219 error = request_irq(MVME147_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
220 "SCC-A RX", port);
221 if (error)
222 goto fail_free_a_stat;
223 error = request_irq(MVME147_IRQ_SCCA_SPCOND, scc_spcond_int,
224 IRQF_DISABLED, "SCC-A special cond", port);
225 if (error)
226 goto fail_free_a_rx;
229 SCC_ACCESS_INIT(port);
231 /* disable interrupts for this channel */
232 SCCwrite(INT_AND_DMA_REG, 0);
233 /* Set the interrupt vector */
234 SCCwrite(INT_VECTOR_REG, MVME147_IRQ_SCC_BASE);
235 /* Interrupt parameters: vector includes status, status low */
236 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
237 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
240 /* Init channel B */
241 port = &scc_ports[1];
242 port->channel = CHANNEL_B;
243 port->ctrlp = (volatile unsigned char *)M147_SCC_B_ADDR;
244 port->datap = port->ctrlp + 1;
245 port->port_a = &scc_ports[0];
246 port->port_b = &scc_ports[1];
247 error = request_irq(MVME147_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
248 "SCC-B TX", port);
249 if (error)
250 goto fail_free_a_spcond;
251 error = request_irq(MVME147_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
252 "SCC-B status", port);
253 if (error)
254 goto fail_free_b_tx;
255 error = request_irq(MVME147_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
256 "SCC-B RX", port);
257 if (error)
258 goto fail_free_b_stat;
259 error = request_irq(MVME147_IRQ_SCCB_SPCOND, scc_spcond_int,
260 IRQF_DISABLED, "SCC-B special cond", port);
261 if (error)
262 goto fail_free_b_rx;
265 SCC_ACCESS_INIT(port);
267 /* disable interrupts for this channel */
268 SCCwrite(INT_AND_DMA_REG, 0);
271 /* Ensure interrupts are enabled in the PCC chip */
272 m147_pcc->serial_cntrl=PCC_LEVEL_SERIAL|PCC_INT_ENAB;
274 /* Initialise the tty driver structures and register */
275 scc_init_portstructs();
276 scc_init_drivers();
278 return 0;
280 fail_free_b_rx:
281 free_irq(MVME147_IRQ_SCCB_RX, port);
282 fail_free_b_stat:
283 free_irq(MVME147_IRQ_SCCB_STAT, port);
284 fail_free_b_tx:
285 free_irq(MVME147_IRQ_SCCB_TX, port);
286 fail_free_a_spcond:
287 free_irq(MVME147_IRQ_SCCA_SPCOND, port);
288 fail_free_a_rx:
289 free_irq(MVME147_IRQ_SCCA_RX, port);
290 fail_free_a_stat:
291 free_irq(MVME147_IRQ_SCCA_STAT, port);
292 fail_free_a_tx:
293 free_irq(MVME147_IRQ_SCCA_TX, port);
294 fail:
295 return error;
297 #endif
300 #ifdef CONFIG_MVME162_SCC
301 static int mvme162_scc_init(void)
303 struct scc_port *port;
304 int error;
306 if (!(mvme16x_config & MVME16x_CONFIG_GOT_SCCA))
307 return (-ENODEV);
309 printk(KERN_INFO "SCC: MVME162 Serial Driver\n");
310 /* Init channel A */
311 port = &scc_ports[0];
312 port->channel = CHANNEL_A;
313 port->ctrlp = (volatile unsigned char *)MVME_SCC_A_ADDR;
314 port->datap = port->ctrlp + 2;
315 port->port_a = &scc_ports[0];
316 port->port_b = &scc_ports[1];
317 error = request_irq(MVME162_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
318 "SCC-A TX", port);
319 if (error)
320 goto fail;
321 error = request_irq(MVME162_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
322 "SCC-A status", port);
323 if (error)
324 goto fail_free_a_tx;
325 error = request_irq(MVME162_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
326 "SCC-A RX", port);
327 if (error)
328 goto fail_free_a_stat;
329 error = request_irq(MVME162_IRQ_SCCA_SPCOND, scc_spcond_int,
330 IRQF_DISABLED, "SCC-A special cond", port);
331 if (error)
332 goto fail_free_a_rx;
335 SCC_ACCESS_INIT(port);
337 /* disable interrupts for this channel */
338 SCCwrite(INT_AND_DMA_REG, 0);
339 /* Set the interrupt vector */
340 SCCwrite(INT_VECTOR_REG, MVME162_IRQ_SCC_BASE);
341 /* Interrupt parameters: vector includes status, status low */
342 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
343 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
346 /* Init channel B */
347 port = &scc_ports[1];
348 port->channel = CHANNEL_B;
349 port->ctrlp = (volatile unsigned char *)MVME_SCC_B_ADDR;
350 port->datap = port->ctrlp + 2;
351 port->port_a = &scc_ports[0];
352 port->port_b = &scc_ports[1];
353 error = request_irq(MVME162_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
354 "SCC-B TX", port);
355 if (error)
356 goto fail_free_a_spcond;
357 error = request_irq(MVME162_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
358 "SCC-B status", port);
359 if (error)
360 goto fail_free_b_tx;
361 error = request_irq(MVME162_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
362 "SCC-B RX", port);
363 if (error)
364 goto fail_free_b_stat;
365 error = request_irq(MVME162_IRQ_SCCB_SPCOND, scc_spcond_int,
366 IRQF_DISABLED, "SCC-B special cond", port);
367 if (error)
368 goto fail_free_b_rx;
371 SCC_ACCESS_INIT(port); /* Either channel will do */
373 /* disable interrupts for this channel */
374 SCCwrite(INT_AND_DMA_REG, 0);
377 /* Ensure interrupts are enabled in the MC2 chip */
378 *(volatile char *)0xfff4201d = 0x14;
380 /* Initialise the tty driver structures and register */
381 scc_init_portstructs();
382 scc_init_drivers();
384 return 0;
386 fail_free_b_rx:
387 free_irq(MVME162_IRQ_SCCB_RX, port);
388 fail_free_b_stat:
389 free_irq(MVME162_IRQ_SCCB_STAT, port);
390 fail_free_b_tx:
391 free_irq(MVME162_IRQ_SCCB_TX, port);
392 fail_free_a_spcond:
393 free_irq(MVME162_IRQ_SCCA_SPCOND, port);
394 fail_free_a_rx:
395 free_irq(MVME162_IRQ_SCCA_RX, port);
396 fail_free_a_stat:
397 free_irq(MVME162_IRQ_SCCA_STAT, port);
398 fail_free_a_tx:
399 free_irq(MVME162_IRQ_SCCA_TX, port);
400 fail:
401 return error;
403 #endif
406 #ifdef CONFIG_BVME6000_SCC
407 static int bvme6000_scc_init(void)
409 struct scc_port *port;
410 int error;
412 printk(KERN_INFO "SCC: BVME6000 Serial Driver\n");
413 /* Init channel A */
414 port = &scc_ports[0];
415 port->channel = CHANNEL_A;
416 port->ctrlp = (volatile unsigned char *)BVME_SCC_A_ADDR;
417 port->datap = port->ctrlp + 4;
418 port->port_a = &scc_ports[0];
419 port->port_b = &scc_ports[1];
420 error = request_irq(BVME_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
421 "SCC-A TX", port);
422 if (error)
423 goto fail;
424 error = request_irq(BVME_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
425 "SCC-A status", port);
426 if (error)
427 goto fail_free_a_tx;
428 error = request_irq(BVME_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
429 "SCC-A RX", port);
430 if (error)
431 goto fail_free_a_stat;
432 error = request_irq(BVME_IRQ_SCCA_SPCOND, scc_spcond_int,
433 IRQF_DISABLED, "SCC-A special cond", port);
434 if (error)
435 goto fail_free_a_rx;
438 SCC_ACCESS_INIT(port);
440 /* disable interrupts for this channel */
441 SCCwrite(INT_AND_DMA_REG, 0);
442 /* Set the interrupt vector */
443 SCCwrite(INT_VECTOR_REG, BVME_IRQ_SCC_BASE);
444 /* Interrupt parameters: vector includes status, status low */
445 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
446 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
449 /* Init channel B */
450 port = &scc_ports[1];
451 port->channel = CHANNEL_B;
452 port->ctrlp = (volatile unsigned char *)BVME_SCC_B_ADDR;
453 port->datap = port->ctrlp + 4;
454 port->port_a = &scc_ports[0];
455 port->port_b = &scc_ports[1];
456 error = request_irq(BVME_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
457 "SCC-B TX", port);
458 if (error)
459 goto fail_free_a_spcond;
460 error = request_irq(BVME_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
461 "SCC-B status", port);
462 if (error)
463 goto fail_free_b_tx;
464 error = request_irq(BVME_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
465 "SCC-B RX", port);
466 if (error)
467 goto fail_free_b_stat;
468 error = request_irq(BVME_IRQ_SCCB_SPCOND, scc_spcond_int,
469 IRQF_DISABLED, "SCC-B special cond", port);
470 if (error)
471 goto fail_free_b_rx;
474 SCC_ACCESS_INIT(port); /* Either channel will do */
476 /* disable interrupts for this channel */
477 SCCwrite(INT_AND_DMA_REG, 0);
480 /* Initialise the tty driver structures and register */
481 scc_init_portstructs();
482 scc_init_drivers();
484 return 0;
486 fail:
487 free_irq(BVME_IRQ_SCCA_STAT, port);
488 fail_free_a_tx:
489 free_irq(BVME_IRQ_SCCA_RX, port);
490 fail_free_a_stat:
491 free_irq(BVME_IRQ_SCCA_SPCOND, port);
492 fail_free_a_rx:
493 free_irq(BVME_IRQ_SCCB_TX, port);
494 fail_free_a_spcond:
495 free_irq(BVME_IRQ_SCCB_STAT, port);
496 fail_free_b_tx:
497 free_irq(BVME_IRQ_SCCB_RX, port);
498 fail_free_b_stat:
499 free_irq(BVME_IRQ_SCCB_SPCOND, port);
500 fail_free_b_rx:
501 return error;
503 #endif
506 static int vme_scc_init(void)
508 int res = -ENODEV;
510 #ifdef CONFIG_MVME147_SCC
511 if (MACH_IS_MVME147)
512 res = mvme147_scc_init();
513 #endif
514 #ifdef CONFIG_MVME162_SCC
515 if (MACH_IS_MVME16x)
516 res = mvme162_scc_init();
517 #endif
518 #ifdef CONFIG_BVME6000_SCC
519 if (MACH_IS_BVME6000)
520 res = bvme6000_scc_init();
521 #endif
522 return res;
525 module_init(vme_scc_init);
528 /*---------------------------------------------------------------------------
529 * Interrupt handlers
530 *--------------------------------------------------------------------------*/
532 static irqreturn_t scc_rx_int(int irq, void *data)
534 unsigned char ch;
535 struct scc_port *port = data;
536 struct tty_struct *tty = port->gs.port.tty;
537 SCC_ACCESS_INIT(port);
539 ch = SCCread_NB(RX_DATA_REG);
540 if (!tty) {
541 printk(KERN_WARNING "scc_rx_int with NULL tty!\n");
542 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
543 return IRQ_HANDLED;
545 tty_insert_flip_char(tty, ch, 0);
547 /* Check if another character is already ready; in that case, the
548 * spcond_int() function must be used, because this character may have an
549 * error condition that isn't signalled by the interrupt vector used!
551 if (SCCread(INT_PENDING_REG) &
552 (port->channel == CHANNEL_A ? IPR_A_RX : IPR_B_RX)) {
553 scc_spcond_int (irq, data);
554 return IRQ_HANDLED;
557 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
559 tty_flip_buffer_push(tty);
560 return IRQ_HANDLED;
564 static irqreturn_t scc_spcond_int(int irq, void *data)
566 struct scc_port *port = data;
567 struct tty_struct *tty = port->gs.port.tty;
568 unsigned char stat, ch, err;
569 int int_pending_mask = port->channel == CHANNEL_A ?
570 IPR_A_RX : IPR_B_RX;
571 SCC_ACCESS_INIT(port);
573 if (!tty) {
574 printk(KERN_WARNING "scc_spcond_int with NULL tty!\n");
575 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
576 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
577 return IRQ_HANDLED;
579 do {
580 stat = SCCread(SPCOND_STATUS_REG);
581 ch = SCCread_NB(RX_DATA_REG);
583 if (stat & SCSR_RX_OVERRUN)
584 err = TTY_OVERRUN;
585 else if (stat & SCSR_PARITY_ERR)
586 err = TTY_PARITY;
587 else if (stat & SCSR_CRC_FRAME_ERR)
588 err = TTY_FRAME;
589 else
590 err = 0;
592 tty_insert_flip_char(tty, ch, err);
594 /* ++TeSche: *All* errors have to be cleared manually,
595 * else the condition persists for the next chars
597 if (err)
598 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
600 } while(SCCread(INT_PENDING_REG) & int_pending_mask);
602 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
604 tty_flip_buffer_push(tty);
605 return IRQ_HANDLED;
609 static irqreturn_t scc_tx_int(int irq, void *data)
611 struct scc_port *port = data;
612 SCC_ACCESS_INIT(port);
614 if (!port->gs.port.tty) {
615 printk(KERN_WARNING "scc_tx_int with NULL tty!\n");
616 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
617 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET);
618 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
619 return IRQ_HANDLED;
621 while ((SCCread_NB(STATUS_REG) & SR_TX_BUF_EMPTY)) {
622 if (port->x_char) {
623 SCCwrite(TX_DATA_REG, port->x_char);
624 port->x_char = 0;
626 else if ((port->gs.xmit_cnt <= 0) ||
627 port->gs.port.tty->stopped ||
628 port->gs.port.tty->hw_stopped)
629 break;
630 else {
631 SCCwrite(TX_DATA_REG, port->gs.xmit_buf[port->gs.xmit_tail++]);
632 port->gs.xmit_tail = port->gs.xmit_tail & (SERIAL_XMIT_SIZE-1);
633 if (--port->gs.xmit_cnt <= 0)
634 break;
637 if ((port->gs.xmit_cnt <= 0) || port->gs.port.tty->stopped ||
638 port->gs.port.tty->hw_stopped) {
639 /* disable tx interrupts */
640 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
641 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET); /* disable tx_int on next tx underrun? */
642 port->gs.port.flags &= ~GS_TX_INTEN;
644 if (port->gs.port.tty && port->gs.xmit_cnt <= port->gs.wakeup_chars)
645 tty_wakeup(port->gs.port.tty);
647 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
648 return IRQ_HANDLED;
652 static irqreturn_t scc_stat_int(int irq, void *data)
654 struct scc_port *port = data;
655 unsigned channel = port->channel;
656 unsigned char last_sr, sr, changed;
657 SCC_ACCESS_INIT(port);
659 last_sr = scc_last_status_reg[channel];
660 sr = scc_last_status_reg[channel] = SCCread_NB(STATUS_REG);
661 changed = last_sr ^ sr;
663 if (changed & SR_DCD) {
664 port->c_dcd = !!(sr & SR_DCD);
665 if (!(port->gs.port.flags & ASYNC_CHECK_CD))
666 ; /* Don't report DCD changes */
667 else if (port->c_dcd) {
668 wake_up_interruptible(&port->gs.port.open_wait);
670 else {
671 if (port->gs.port.tty)
672 tty_hangup (port->gs.port.tty);
675 SCCwrite(COMMAND_REG, CR_EXTSTAT_RESET);
676 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
677 return IRQ_HANDLED;
681 /*---------------------------------------------------------------------------
682 * generic_serial.c callback funtions
683 *--------------------------------------------------------------------------*/
685 static void scc_disable_tx_interrupts(void *ptr)
687 struct scc_port *port = ptr;
688 unsigned long flags;
689 SCC_ACCESS_INIT(port);
691 local_irq_save(flags);
692 SCCmod(INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
693 port->gs.port.flags &= ~GS_TX_INTEN;
694 local_irq_restore(flags);
698 static void scc_enable_tx_interrupts(void *ptr)
700 struct scc_port *port = ptr;
701 unsigned long flags;
702 SCC_ACCESS_INIT(port);
704 local_irq_save(flags);
705 SCCmod(INT_AND_DMA_REG, 0xff, IDR_TX_INT_ENAB);
706 /* restart the transmitter */
707 scc_tx_int (0, port);
708 local_irq_restore(flags);
712 static void scc_disable_rx_interrupts(void *ptr)
714 struct scc_port *port = ptr;
715 unsigned long flags;
716 SCC_ACCESS_INIT(port);
718 local_irq_save(flags);
719 SCCmod(INT_AND_DMA_REG,
720 ~(IDR_RX_INT_MASK|IDR_PARERR_AS_SPCOND|IDR_EXTSTAT_INT_ENAB), 0);
721 local_irq_restore(flags);
725 static void scc_enable_rx_interrupts(void *ptr)
727 struct scc_port *port = ptr;
728 unsigned long flags;
729 SCC_ACCESS_INIT(port);
731 local_irq_save(flags);
732 SCCmod(INT_AND_DMA_REG, 0xff,
733 IDR_EXTSTAT_INT_ENAB|IDR_PARERR_AS_SPCOND|IDR_RX_INT_ALL);
734 local_irq_restore(flags);
738 static int scc_carrier_raised(struct tty_port *port)
740 struct scc_port *sc = container_of(port, struct scc_port, gs.port);
741 unsigned channel = sc->channel;
743 return !!(scc_last_status_reg[channel] & SR_DCD);
747 static void scc_shutdown_port(void *ptr)
749 struct scc_port *port = ptr;
751 port->gs.port.flags &= ~ GS_ACTIVE;
752 if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
753 scc_setsignals (port, 0, 0);
758 static int scc_set_real_termios (void *ptr)
760 /* the SCC has char sizes 5,7,6,8 in that order! */
761 static int chsize_map[4] = { 0, 2, 1, 3 };
762 unsigned cflag, baud, chsize, channel, brgval = 0;
763 unsigned long flags;
764 struct scc_port *port = ptr;
765 SCC_ACCESS_INIT(port);
767 if (!port->gs.port.tty || !port->gs.port.tty->termios) return 0;
769 channel = port->channel;
771 if (channel == CHANNEL_A)
772 return 0; /* Settings controlled by boot PROM */
774 cflag = port->gs.port.tty->termios->c_cflag;
775 baud = port->gs.baud;
776 chsize = (cflag & CSIZE) >> 4;
778 if (baud == 0) {
779 /* speed == 0 -> drop DTR */
780 local_irq_save(flags);
781 SCCmod(TX_CTRL_REG, ~TCR_DTR, 0);
782 local_irq_restore(flags);
783 return 0;
785 else if ((MACH_IS_MVME16x && (baud < 50 || baud > 38400)) ||
786 (MACH_IS_MVME147 && (baud < 50 || baud > 19200)) ||
787 (MACH_IS_BVME6000 &&(baud < 50 || baud > 76800))) {
788 printk(KERN_NOTICE "SCC: Bad speed requested, %d\n", baud);
789 return 0;
792 if (cflag & CLOCAL)
793 port->gs.port.flags &= ~ASYNC_CHECK_CD;
794 else
795 port->gs.port.flags |= ASYNC_CHECK_CD;
797 #ifdef CONFIG_MVME147_SCC
798 if (MACH_IS_MVME147)
799 brgval = (M147_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
800 #endif
801 #ifdef CONFIG_MVME162_SCC
802 if (MACH_IS_MVME16x)
803 brgval = (MVME_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
804 #endif
805 #ifdef CONFIG_BVME6000_SCC
806 if (MACH_IS_BVME6000)
807 brgval = (BVME_SCC_RTxC + baud/2) / (16 * 2 * baud) - 2;
808 #endif
809 /* Now we have all parameters and can go to set them: */
810 local_irq_save(flags);
812 /* receiver's character size and auto-enables */
813 SCCmod(RX_CTRL_REG, ~(RCR_CHSIZE_MASK|RCR_AUTO_ENAB_MODE),
814 (chsize_map[chsize] << 6) |
815 ((cflag & CRTSCTS) ? RCR_AUTO_ENAB_MODE : 0));
816 /* parity and stop bits (both, Tx and Rx), clock mode never changes */
817 SCCmod (AUX1_CTRL_REG,
818 ~(A1CR_PARITY_MASK | A1CR_MODE_MASK),
819 ((cflag & PARENB
820 ? (cflag & PARODD ? A1CR_PARITY_ODD : A1CR_PARITY_EVEN)
821 : A1CR_PARITY_NONE)
822 | (cflag & CSTOPB ? A1CR_MODE_ASYNC_2 : A1CR_MODE_ASYNC_1)));
823 /* sender's character size, set DTR for valid baud rate */
824 SCCmod(TX_CTRL_REG, ~TCR_CHSIZE_MASK, chsize_map[chsize] << 5 | TCR_DTR);
825 /* clock sources never change */
826 /* disable BRG before changing the value */
827 SCCmod(DPLL_CTRL_REG, ~DCR_BRG_ENAB, 0);
828 /* BRG value */
829 SCCwrite(TIMER_LOW_REG, brgval & 0xff);
830 SCCwrite(TIMER_HIGH_REG, (brgval >> 8) & 0xff);
831 /* BRG enable, and clock source never changes */
832 SCCmod(DPLL_CTRL_REG, 0xff, DCR_BRG_ENAB);
834 local_irq_restore(flags);
836 return 0;
840 static int scc_chars_in_buffer (void *ptr)
842 struct scc_port *port = ptr;
843 SCC_ACCESS_INIT(port);
845 return (SCCread (SPCOND_STATUS_REG) & SCSR_ALL_SENT) ? 0 : 1;
849 /* Comment taken from sx.c (2.4.0):
850 I haven't the foggiest why the decrement use count has to happen
851 here. The whole linux serial drivers stuff needs to be redesigned.
852 My guess is that this is a hack to minimize the impact of a bug
853 elsewhere. Thinking about it some more. (try it sometime) Try
854 running minicom on a serial port that is driven by a modularized
855 driver. Have the modem hangup. Then remove the driver module. Then
856 exit minicom. I expect an "oops". -- REW */
858 static void scc_hungup(void *ptr)
860 scc_disable_tx_interrupts(ptr);
861 scc_disable_rx_interrupts(ptr);
865 static void scc_close(void *ptr)
867 scc_disable_tx_interrupts(ptr);
868 scc_disable_rx_interrupts(ptr);
872 /*---------------------------------------------------------------------------
873 * Internal support functions
874 *--------------------------------------------------------------------------*/
876 static void scc_setsignals(struct scc_port *port, int dtr, int rts)
878 unsigned long flags;
879 unsigned char t;
880 SCC_ACCESS_INIT(port);
882 local_irq_save(flags);
883 t = SCCread(TX_CTRL_REG);
884 if (dtr >= 0) t = dtr? (t | TCR_DTR): (t & ~TCR_DTR);
885 if (rts >= 0) t = rts? (t | TCR_RTS): (t & ~TCR_RTS);
886 SCCwrite(TX_CTRL_REG, t);
887 local_irq_restore(flags);
891 static void scc_send_xchar(struct tty_struct *tty, char ch)
893 struct scc_port *port = tty->driver_data;
895 port->x_char = ch;
896 if (ch)
897 scc_enable_tx_interrupts(port);
901 /*---------------------------------------------------------------------------
902 * Driver entrypoints referenced from above
903 *--------------------------------------------------------------------------*/
905 static int scc_open (struct tty_struct * tty, struct file * filp)
907 int line = tty->index;
908 int retval;
909 struct scc_port *port = &scc_ports[line];
910 int i, channel = port->channel;
911 unsigned long flags;
912 SCC_ACCESS_INIT(port);
913 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_MVME147_SCC)
914 static const struct {
915 unsigned reg, val;
916 } mvme_init_tab[] = {
917 /* Values for MVME162 and MVME147 */
918 /* no parity, 1 stop bit, async, 1:16 */
919 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
920 /* parity error is special cond, ints disabled, no DMA */
921 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
922 /* Rx 8 bits/char, no auto enable, Rx off */
923 { RX_CTRL_REG, RCR_CHSIZE_8 },
924 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
925 { TX_CTRL_REG, TCR_CHSIZE_8 },
926 /* special features off */
927 { AUX2_CTRL_REG, 0 },
928 { CLK_CTRL_REG, CCR_RXCLK_BRG | CCR_TXCLK_BRG },
929 { DPLL_CTRL_REG, DCR_BRG_ENAB | DCR_BRG_USE_PCLK },
930 /* Start Rx */
931 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
932 /* Start Tx */
933 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
934 /* Ext/Stat ints: DCD only */
935 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
936 /* Reset Ext/Stat ints */
937 { COMMAND_REG, CR_EXTSTAT_RESET },
938 /* ...again */
939 { COMMAND_REG, CR_EXTSTAT_RESET },
941 #endif
942 #if defined(CONFIG_BVME6000_SCC)
943 static const struct {
944 unsigned reg, val;
945 } bvme_init_tab[] = {
946 /* Values for BVME6000 */
947 /* no parity, 1 stop bit, async, 1:16 */
948 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
949 /* parity error is special cond, ints disabled, no DMA */
950 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
951 /* Rx 8 bits/char, no auto enable, Rx off */
952 { RX_CTRL_REG, RCR_CHSIZE_8 },
953 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
954 { TX_CTRL_REG, TCR_CHSIZE_8 },
955 /* special features off */
956 { AUX2_CTRL_REG, 0 },
957 { CLK_CTRL_REG, CCR_RTxC_XTAL | CCR_RXCLK_BRG | CCR_TXCLK_BRG },
958 { DPLL_CTRL_REG, DCR_BRG_ENAB },
959 /* Start Rx */
960 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
961 /* Start Tx */
962 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
963 /* Ext/Stat ints: DCD only */
964 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
965 /* Reset Ext/Stat ints */
966 { COMMAND_REG, CR_EXTSTAT_RESET },
967 /* ...again */
968 { COMMAND_REG, CR_EXTSTAT_RESET },
970 #endif
971 if (!(port->gs.port.flags & ASYNC_INITIALIZED)) {
972 local_irq_save(flags);
973 #if defined(CONFIG_MVME147_SCC) || defined(CONFIG_MVME162_SCC)
974 if (MACH_IS_MVME147 || MACH_IS_MVME16x) {
975 for (i = 0; i < ARRAY_SIZE(mvme_init_tab); ++i)
976 SCCwrite(mvme_init_tab[i].reg, mvme_init_tab[i].val);
978 #endif
979 #if defined(CONFIG_BVME6000_SCC)
980 if (MACH_IS_BVME6000) {
981 for (i = 0; i < ARRAY_SIZE(bvme_init_tab); ++i)
982 SCCwrite(bvme_init_tab[i].reg, bvme_init_tab[i].val);
984 #endif
986 /* remember status register for detection of DCD and CTS changes */
987 scc_last_status_reg[channel] = SCCread(STATUS_REG);
989 port->c_dcd = 0; /* Prevent initial 1->0 interrupt */
990 scc_setsignals (port, 1,1);
991 local_irq_restore(flags);
994 tty->driver_data = port;
995 port->gs.port.tty = tty;
996 port->gs.port.count++;
997 retval = gs_init_port(&port->gs);
998 if (retval) {
999 port->gs.port.count--;
1000 return retval;
1002 port->gs.port.flags |= GS_ACTIVE;
1003 retval = gs_block_til_ready(port, filp);
1005 if (retval) {
1006 port->gs.port.count--;
1007 return retval;
1010 port->c_dcd = tty_port_carrier_raised(&port->gs.port);
1012 scc_enable_rx_interrupts(port);
1014 return 0;
1018 static void scc_throttle (struct tty_struct * tty)
1020 struct scc_port *port = tty->driver_data;
1021 unsigned long flags;
1022 SCC_ACCESS_INIT(port);
1024 if (tty->termios->c_cflag & CRTSCTS) {
1025 local_irq_save(flags);
1026 SCCmod(TX_CTRL_REG, ~TCR_RTS, 0);
1027 local_irq_restore(flags);
1029 if (I_IXOFF(tty))
1030 scc_send_xchar(tty, STOP_CHAR(tty));
1034 static void scc_unthrottle (struct tty_struct * tty)
1036 struct scc_port *port = tty->driver_data;
1037 unsigned long flags;
1038 SCC_ACCESS_INIT(port);
1040 if (tty->termios->c_cflag & CRTSCTS) {
1041 local_irq_save(flags);
1042 SCCmod(TX_CTRL_REG, 0xff, TCR_RTS);
1043 local_irq_restore(flags);
1045 if (I_IXOFF(tty))
1046 scc_send_xchar(tty, START_CHAR(tty));
1050 static int scc_ioctl(struct tty_struct *tty, struct file *file,
1051 unsigned int cmd, unsigned long arg)
1053 return -ENOIOCTLCMD;
1057 static int scc_break_ctl(struct tty_struct *tty, int break_state)
1059 struct scc_port *port = tty->driver_data;
1060 unsigned long flags;
1061 SCC_ACCESS_INIT(port);
1063 local_irq_save(flags);
1064 SCCmod(TX_CTRL_REG, ~TCR_SEND_BREAK,
1065 break_state ? TCR_SEND_BREAK : 0);
1066 local_irq_restore(flags);
1067 return 0;
1071 /*---------------------------------------------------------------------------
1072 * Serial console stuff...
1073 *--------------------------------------------------------------------------*/
1075 #define scc_delay() do { __asm__ __volatile__ (" nop; nop"); } while (0)
1077 static void scc_ch_write (char ch)
1079 volatile char *p = NULL;
1081 #ifdef CONFIG_MVME147_SCC
1082 if (MACH_IS_MVME147)
1083 p = (volatile char *)M147_SCC_A_ADDR;
1084 #endif
1085 #ifdef CONFIG_MVME162_SCC
1086 if (MACH_IS_MVME16x)
1087 p = (volatile char *)MVME_SCC_A_ADDR;
1088 #endif
1089 #ifdef CONFIG_BVME6000_SCC
1090 if (MACH_IS_BVME6000)
1091 p = (volatile char *)BVME_SCC_A_ADDR;
1092 #endif
1094 do {
1095 scc_delay();
1097 while (!(*p & 4));
1098 scc_delay();
1099 *p = 8;
1100 scc_delay();
1101 *p = ch;
1104 /* The console must be locked when we get here. */
1106 static void scc_console_write (struct console *co, const char *str, unsigned count)
1108 unsigned long flags;
1110 local_irq_save(flags);
1112 while (count--)
1114 if (*str == '\n')
1115 scc_ch_write ('\r');
1116 scc_ch_write (*str++);
1118 local_irq_restore(flags);
1121 static struct tty_driver *scc_console_device(struct console *c, int *index)
1123 *index = c->index;
1124 return scc_driver;
1127 static struct console sercons = {
1128 .name = "ttyS",
1129 .write = scc_console_write,
1130 .device = scc_console_device,
1131 .flags = CON_PRINTBUFFER,
1132 .index = -1,
1136 static int __init vme_scc_console_init(void)
1138 if (vme_brdtype == VME_TYPE_MVME147 ||
1139 vme_brdtype == VME_TYPE_MVME162 ||
1140 vme_brdtype == VME_TYPE_MVME172 ||
1141 vme_brdtype == VME_TYPE_BVME4000 ||
1142 vme_brdtype == VME_TYPE_BVME6000)
1143 register_console(&sercons);
1144 return 0;
1146 console_initcall(vme_scc_console_init);