IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
[linux-2.6.git] / drivers / char / vme_scc.c
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>
5  *
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
9  *
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.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/kdev_t.h>
18 #include <asm/io.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/ioport.h>
22 #include <linux/interrupt.h>
23 #include <linux/errno.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/mm.h>
27 #include <linux/serial.h>
28 #include <linux/fcntl.h>
29 #include <linux/major.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/miscdevice.h>
33 #include <linux/console.h>
34 #include <linux/init.h>
35 #include <asm/setup.h>
36 #include <asm/bootinfo.h>
37
38 #ifdef CONFIG_MVME147_SCC
39 #include <asm/mvme147hw.h>
40 #endif
41 #ifdef CONFIG_MVME162_SCC
42 #include <asm/mvme16xhw.h>
43 #endif
44 #ifdef CONFIG_BVME6000_SCC
45 #include <asm/bvme6000hw.h>
46 #endif
47
48 #include <linux/generic_serial.h>
49 #include "scc.h"
50
51
52 #define CHANNEL_A       0
53 #define CHANNEL_B       1
54
55 #define SCC_MINOR_BASE  64
56
57 /* Shadows for all SCC write registers */
58 static unsigned char scc_shadow[2][16];
59
60 /* Location to access for SCC register access delay */
61 static volatile unsigned char *scc_del = NULL;
62
63 /* To keep track of STATUS_REG state for detection of Ext/Status int source */
64 static unsigned char scc_last_status_reg[2];
65
66 /***************************** Prototypes *****************************/
67
68 /* Function prototypes */
69 static void scc_disable_tx_interrupts(void * ptr);
70 static void scc_enable_tx_interrupts(void * ptr);
71 static void scc_disable_rx_interrupts(void * ptr);
72 static void scc_enable_rx_interrupts(void * ptr);
73 static int  scc_get_CD(void * ptr);
74 static void scc_shutdown_port(void * ptr);
75 static int scc_set_real_termios(void  *ptr);
76 static void scc_hungup(void  *ptr);
77 static void scc_close(void  *ptr);
78 static int scc_chars_in_buffer(void * ptr);
79 static int scc_open(struct tty_struct * tty, struct file * filp);
80 static int scc_ioctl(struct tty_struct * tty, struct file * filp,
81                      unsigned int cmd, unsigned long arg);
82 static void scc_throttle(struct tty_struct *tty);
83 static void scc_unthrottle(struct tty_struct *tty);
84 static irqreturn_t scc_tx_int(int irq, void *data);
85 static irqreturn_t scc_rx_int(int irq, void *data);
86 static irqreturn_t scc_stat_int(int irq, void *data);
87 static irqreturn_t scc_spcond_int(int irq, void *data);
88 static void scc_setsignals(struct scc_port *port, int dtr, int rts);
89 static void scc_break_ctl(struct tty_struct *tty, int break_state);
90
91 static struct tty_driver *scc_driver;
92
93 struct scc_port scc_ports[2];
94
95 int scc_initialized = 0;
96
97 /*---------------------------------------------------------------------------
98  * Interface from generic_serial.c back here
99  *--------------------------------------------------------------------------*/
100
101 static struct real_driver scc_real_driver = {
102         scc_disable_tx_interrupts,
103         scc_enable_tx_interrupts,
104         scc_disable_rx_interrupts,
105         scc_enable_rx_interrupts,
106         scc_get_CD,
107         scc_shutdown_port,
108         scc_set_real_termios,
109         scc_chars_in_buffer,
110         scc_close,
111         scc_hungup,
112         NULL
113 };
114
115
116 static const struct tty_operations scc_ops = {
117         .open   = scc_open,
118         .close = gs_close,
119         .write = gs_write,
120         .put_char = gs_put_char,
121         .flush_chars = gs_flush_chars,
122         .write_room = gs_write_room,
123         .chars_in_buffer = gs_chars_in_buffer,
124         .flush_buffer = gs_flush_buffer,
125         .ioctl = scc_ioctl,
126         .throttle = scc_throttle,
127         .unthrottle = scc_unthrottle,
128         .set_termios = gs_set_termios,
129         .stop = gs_stop,
130         .start = gs_start,
131         .hangup = gs_hangup,
132         .break_ctl = scc_break_ctl,
133 };
134
135 /*----------------------------------------------------------------------------
136  * vme_scc_init() and support functions
137  *---------------------------------------------------------------------------*/
138
139 static int scc_init_drivers(void)
140 {
141         int error;
142
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->flags = TTY_DRIVER_REAL_RAW;
157         tty_set_operations(scc_driver, &scc_ops);
158
159         if ((error = tty_register_driver(scc_driver))) {
160                 printk(KERN_ERR "scc: Couldn't register scc driver, error = %d\n",
161                        error);
162                 put_tty_driver(scc_driver);
163                 return 1;
164         }
165
166         return 0;
167 }
168
169
170 /* ports[] array is indexed by line no (i.e. [0] for ttyS0, [1] for ttyS1).
171  */
172
173 static void scc_init_portstructs(void)
174 {
175         struct scc_port *port;
176         int i;
177
178         for (i = 0; i < 2; i++) {
179                 port = scc_ports + i;
180                 port->gs.magic = SCC_MAGIC;
181                 port->gs.close_delay = HZ/2;
182                 port->gs.closing_wait = 30 * HZ;
183                 port->gs.rd = &scc_real_driver;
184 #ifdef NEW_WRITE_LOCKING
185                 port->gs.port_write_mutex = MUTEX;
186 #endif
187                 init_waitqueue_head(&port->gs.open_wait);
188                 init_waitqueue_head(&port->gs.close_wait);
189         }
190 }
191
192
193 #ifdef CONFIG_MVME147_SCC
194 static int mvme147_scc_init(void)
195 {
196         struct scc_port *port;
197
198         printk(KERN_INFO "SCC: MVME147 Serial Driver\n");
199         /* Init channel A */
200         port = &scc_ports[0];
201         port->channel = CHANNEL_A;
202         port->ctrlp = (volatile unsigned char *)M147_SCC_A_ADDR;
203         port->datap = port->ctrlp + 1;
204         port->port_a = &scc_ports[0];
205         port->port_b = &scc_ports[1];
206         request_irq(MVME147_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
207                             "SCC-A TX", port);
208         request_irq(MVME147_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
209                             "SCC-A status", port);
210         request_irq(MVME147_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
211                             "SCC-A RX", port);
212         request_irq(MVME147_IRQ_SCCA_SPCOND, scc_spcond_int, IRQF_DISABLED,
213                             "SCC-A special cond", port);
214         {
215                 SCC_ACCESS_INIT(port);
216
217                 /* disable interrupts for this channel */
218                 SCCwrite(INT_AND_DMA_REG, 0);
219                 /* Set the interrupt vector */
220                 SCCwrite(INT_VECTOR_REG, MVME147_IRQ_SCC_BASE);
221                 /* Interrupt parameters: vector includes status, status low */
222                 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
223                 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
224         }
225
226         /* Init channel B */
227         port = &scc_ports[1];
228         port->channel = CHANNEL_B;
229         port->ctrlp = (volatile unsigned char *)M147_SCC_B_ADDR;
230         port->datap = port->ctrlp + 1;
231         port->port_a = &scc_ports[0];
232         port->port_b = &scc_ports[1];
233         request_irq(MVME147_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
234                             "SCC-B TX", port);
235         request_irq(MVME147_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
236                             "SCC-B status", port);
237         request_irq(MVME147_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
238                             "SCC-B RX", port);
239         request_irq(MVME147_IRQ_SCCB_SPCOND, scc_spcond_int, IRQF_DISABLED,
240                             "SCC-B special cond", port);
241         {
242                 SCC_ACCESS_INIT(port);
243
244                 /* disable interrupts for this channel */
245                 SCCwrite(INT_AND_DMA_REG, 0);
246         }
247
248         /* Ensure interrupts are enabled in the PCC chip */
249         m147_pcc->serial_cntrl=PCC_LEVEL_SERIAL|PCC_INT_ENAB;
250
251         /* Initialise the tty driver structures and register */
252         scc_init_portstructs();
253         scc_init_drivers();
254
255         return 0;
256 }
257 #endif
258
259
260 #ifdef CONFIG_MVME162_SCC
261 static int mvme162_scc_init(void)
262 {
263         struct scc_port *port;
264
265         if (!(mvme16x_config & MVME16x_CONFIG_GOT_SCCA))
266                 return (-ENODEV);
267
268         printk(KERN_INFO "SCC: MVME162 Serial Driver\n");
269         /* Init channel A */
270         port = &scc_ports[0];
271         port->channel = CHANNEL_A;
272         port->ctrlp = (volatile unsigned char *)MVME_SCC_A_ADDR;
273         port->datap = port->ctrlp + 2;
274         port->port_a = &scc_ports[0];
275         port->port_b = &scc_ports[1];
276         request_irq(MVME162_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
277                             "SCC-A TX", port);
278         request_irq(MVME162_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
279                             "SCC-A status", port);
280         request_irq(MVME162_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
281                             "SCC-A RX", port);
282         request_irq(MVME162_IRQ_SCCA_SPCOND, scc_spcond_int, IRQF_DISABLED,
283                             "SCC-A special cond", port);
284         {
285                 SCC_ACCESS_INIT(port);
286
287                 /* disable interrupts for this channel */
288                 SCCwrite(INT_AND_DMA_REG, 0);
289                 /* Set the interrupt vector */
290                 SCCwrite(INT_VECTOR_REG, MVME162_IRQ_SCC_BASE);
291                 /* Interrupt parameters: vector includes status, status low */
292                 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
293                 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
294         }
295
296         /* Init channel B */
297         port = &scc_ports[1];
298         port->channel = CHANNEL_B;
299         port->ctrlp = (volatile unsigned char *)MVME_SCC_B_ADDR;
300         port->datap = port->ctrlp + 2;
301         port->port_a = &scc_ports[0];
302         port->port_b = &scc_ports[1];
303         request_irq(MVME162_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
304                             "SCC-B TX", port);
305         request_irq(MVME162_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
306                             "SCC-B status", port);
307         request_irq(MVME162_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
308                             "SCC-B RX", port);
309         request_irq(MVME162_IRQ_SCCB_SPCOND, scc_spcond_int, IRQF_DISABLED,
310                             "SCC-B special cond", port);
311
312         {
313                 SCC_ACCESS_INIT(port);  /* Either channel will do */
314
315                 /* disable interrupts for this channel */
316                 SCCwrite(INT_AND_DMA_REG, 0);
317         }
318
319         /* Ensure interrupts are enabled in the MC2 chip */
320         *(volatile char *)0xfff4201d = 0x14;
321
322         /* Initialise the tty driver structures and register */
323         scc_init_portstructs();
324         scc_init_drivers();
325
326         return 0;
327 }
328 #endif
329
330
331 #ifdef CONFIG_BVME6000_SCC
332 static int bvme6000_scc_init(void)
333 {
334         struct scc_port *port;
335
336         printk(KERN_INFO "SCC: BVME6000 Serial Driver\n");
337         /* Init channel A */
338         port = &scc_ports[0];
339         port->channel = CHANNEL_A;
340         port->ctrlp = (volatile unsigned char *)BVME_SCC_A_ADDR;
341         port->datap = port->ctrlp + 4;
342         port->port_a = &scc_ports[0];
343         port->port_b = &scc_ports[1];
344         request_irq(BVME_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
345                             "SCC-A TX", port);
346         request_irq(BVME_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
347                             "SCC-A status", port);
348         request_irq(BVME_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
349                             "SCC-A RX", port);
350         request_irq(BVME_IRQ_SCCA_SPCOND, scc_spcond_int, IRQF_DISABLED,
351                             "SCC-A special cond", port);
352         {
353                 SCC_ACCESS_INIT(port);
354
355                 /* disable interrupts for this channel */
356                 SCCwrite(INT_AND_DMA_REG, 0);
357                 /* Set the interrupt vector */
358                 SCCwrite(INT_VECTOR_REG, BVME_IRQ_SCC_BASE);
359                 /* Interrupt parameters: vector includes status, status low */
360                 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
361                 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
362         }
363
364         /* Init channel B */
365         port = &scc_ports[1];
366         port->channel = CHANNEL_B;
367         port->ctrlp = (volatile unsigned char *)BVME_SCC_B_ADDR;
368         port->datap = port->ctrlp + 4;
369         port->port_a = &scc_ports[0];
370         port->port_b = &scc_ports[1];
371         request_irq(BVME_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
372                             "SCC-B TX", port);
373         request_irq(BVME_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
374                             "SCC-B status", port);
375         request_irq(BVME_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
376                             "SCC-B RX", port);
377         request_irq(BVME_IRQ_SCCB_SPCOND, scc_spcond_int, IRQF_DISABLED,
378                             "SCC-B special cond", port);
379
380         {
381                 SCC_ACCESS_INIT(port);  /* Either channel will do */
382
383                 /* disable interrupts for this channel */
384                 SCCwrite(INT_AND_DMA_REG, 0);
385         }
386
387         /* Initialise the tty driver structures and register */
388         scc_init_portstructs();
389         scc_init_drivers();
390
391         return 0;
392 }
393 #endif
394
395
396 static int vme_scc_init(void)
397 {
398         int res = -ENODEV;
399
400 #ifdef CONFIG_MVME147_SCC
401         if (MACH_IS_MVME147)
402                 res = mvme147_scc_init();
403 #endif
404 #ifdef CONFIG_MVME162_SCC
405         if (MACH_IS_MVME16x)
406                 res = mvme162_scc_init();
407 #endif
408 #ifdef CONFIG_BVME6000_SCC
409         if (MACH_IS_BVME6000)
410                 res = bvme6000_scc_init();
411 #endif
412         return res;
413 }
414
415 module_init(vme_scc_init);
416
417
418 /*---------------------------------------------------------------------------
419  * Interrupt handlers
420  *--------------------------------------------------------------------------*/
421
422 static irqreturn_t scc_rx_int(int irq, void *data)
423 {
424         unsigned char   ch;
425         struct scc_port *port = data;
426         struct tty_struct *tty = port->gs.tty;
427         SCC_ACCESS_INIT(port);
428
429         ch = SCCread_NB(RX_DATA_REG);
430         if (!tty) {
431                 printk(KERN_WARNING "scc_rx_int with NULL tty!\n");
432                 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
433                 return IRQ_HANDLED;
434         }
435         tty_insert_flip_char(tty, ch, 0);
436
437         /* Check if another character is already ready; in that case, the
438          * spcond_int() function must be used, because this character may have an
439          * error condition that isn't signalled by the interrupt vector used!
440          */
441         if (SCCread(INT_PENDING_REG) &
442             (port->channel == CHANNEL_A ? IPR_A_RX : IPR_B_RX)) {
443                 scc_spcond_int (irq, data);
444                 return IRQ_HANDLED;
445         }
446
447         SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
448
449         tty_flip_buffer_push(tty);
450         return IRQ_HANDLED;
451 }
452
453
454 static irqreturn_t scc_spcond_int(int irq, void *data)
455 {
456         struct scc_port *port = data;
457         struct tty_struct *tty = port->gs.tty;
458         unsigned char   stat, ch, err;
459         int             int_pending_mask = port->channel == CHANNEL_A ?
460                                            IPR_A_RX : IPR_B_RX;
461         SCC_ACCESS_INIT(port);
462         
463         if (!tty) {
464                 printk(KERN_WARNING "scc_spcond_int with NULL tty!\n");
465                 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
466                 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
467                 return IRQ_HANDLED;
468         }
469         do {
470                 stat = SCCread(SPCOND_STATUS_REG);
471                 ch = SCCread_NB(RX_DATA_REG);
472
473                 if (stat & SCSR_RX_OVERRUN)
474                         err = TTY_OVERRUN;
475                 else if (stat & SCSR_PARITY_ERR)
476                         err = TTY_PARITY;
477                 else if (stat & SCSR_CRC_FRAME_ERR)
478                         err = TTY_FRAME;
479                 else
480                         err = 0;
481
482                 tty_insert_flip_char(tty, ch, err);
483
484                 /* ++TeSche: *All* errors have to be cleared manually,
485                  * else the condition persists for the next chars
486                  */
487                 if (err)
488                   SCCwrite(COMMAND_REG, CR_ERROR_RESET);
489
490         } while(SCCread(INT_PENDING_REG) & int_pending_mask);
491
492         SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
493
494         tty_flip_buffer_push(tty);
495         return IRQ_HANDLED;
496 }
497
498
499 static irqreturn_t scc_tx_int(int irq, void *data)
500 {
501         struct scc_port *port = data;
502         SCC_ACCESS_INIT(port);
503
504         if (!port->gs.tty) {
505                 printk(KERN_WARNING "scc_tx_int with NULL tty!\n");
506                 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
507                 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET);
508                 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
509                 return IRQ_HANDLED;
510         }
511         while ((SCCread_NB(STATUS_REG) & SR_TX_BUF_EMPTY)) {
512                 if (port->x_char) {
513                         SCCwrite(TX_DATA_REG, port->x_char);
514                         port->x_char = 0;
515                 }
516                 else if ((port->gs.xmit_cnt <= 0) || port->gs.tty->stopped ||
517                                 port->gs.tty->hw_stopped)
518                         break;
519                 else {
520                         SCCwrite(TX_DATA_REG, port->gs.xmit_buf[port->gs.xmit_tail++]);
521                         port->gs.xmit_tail = port->gs.xmit_tail & (SERIAL_XMIT_SIZE-1);
522                         if (--port->gs.xmit_cnt <= 0)
523                                 break;
524                 }
525         }
526         if ((port->gs.xmit_cnt <= 0) || port->gs.tty->stopped ||
527                         port->gs.tty->hw_stopped) {
528                 /* disable tx interrupts */
529                 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
530                 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET);   /* disable tx_int on next tx underrun? */
531                 port->gs.flags &= ~GS_TX_INTEN;
532         }
533         if (port->gs.tty && port->gs.xmit_cnt <= port->gs.wakeup_chars)
534                 tty_wakeup(port->gs.tty);
535
536         SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
537         return IRQ_HANDLED;
538 }
539
540
541 static irqreturn_t scc_stat_int(int irq, void *data)
542 {
543         struct scc_port *port = data;
544         unsigned channel = port->channel;
545         unsigned char   last_sr, sr, changed;
546         SCC_ACCESS_INIT(port);
547
548         last_sr = scc_last_status_reg[channel];
549         sr = scc_last_status_reg[channel] = SCCread_NB(STATUS_REG);
550         changed = last_sr ^ sr;
551
552         if (changed & SR_DCD) {
553                 port->c_dcd = !!(sr & SR_DCD);
554                 if (!(port->gs.flags & ASYNC_CHECK_CD))
555                         ;       /* Don't report DCD changes */
556                 else if (port->c_dcd) {
557                         wake_up_interruptible(&port->gs.open_wait);
558                 }
559                 else {
560                         if (port->gs.tty)
561                                 tty_hangup (port->gs.tty);
562                 }
563         }
564         SCCwrite(COMMAND_REG, CR_EXTSTAT_RESET);
565         SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
566         return IRQ_HANDLED;
567 }
568
569
570 /*---------------------------------------------------------------------------
571  * generic_serial.c callback funtions
572  *--------------------------------------------------------------------------*/
573
574 static void scc_disable_tx_interrupts(void *ptr)
575 {
576         struct scc_port *port = ptr;
577         unsigned long   flags;
578         SCC_ACCESS_INIT(port);
579
580         local_irq_save(flags);
581         SCCmod(INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
582         port->gs.flags &= ~GS_TX_INTEN;
583         local_irq_restore(flags);
584 }
585
586
587 static void scc_enable_tx_interrupts(void *ptr)
588 {
589         struct scc_port *port = ptr;
590         unsigned long   flags;
591         SCC_ACCESS_INIT(port);
592
593         local_irq_save(flags);
594         SCCmod(INT_AND_DMA_REG, 0xff, IDR_TX_INT_ENAB);
595         /* restart the transmitter */
596         scc_tx_int (0, port, 0);
597         local_irq_restore(flags);
598 }
599
600
601 static void scc_disable_rx_interrupts(void *ptr)
602 {
603         struct scc_port *port = ptr;
604         unsigned long   flags;
605         SCC_ACCESS_INIT(port);
606
607         local_irq_save(flags);
608         SCCmod(INT_AND_DMA_REG,
609             ~(IDR_RX_INT_MASK|IDR_PARERR_AS_SPCOND|IDR_EXTSTAT_INT_ENAB), 0);
610         local_irq_restore(flags);
611 }
612
613
614 static void scc_enable_rx_interrupts(void *ptr)
615 {
616         struct scc_port *port = ptr;
617         unsigned long   flags;
618         SCC_ACCESS_INIT(port);
619
620         local_irq_save(flags);
621         SCCmod(INT_AND_DMA_REG, 0xff,
622                 IDR_EXTSTAT_INT_ENAB|IDR_PARERR_AS_SPCOND|IDR_RX_INT_ALL);
623         local_irq_restore(flags);
624 }
625
626
627 static int scc_get_CD(void *ptr)
628 {
629         struct scc_port *port = ptr;
630         unsigned channel = port->channel;
631
632         return !!(scc_last_status_reg[channel] & SR_DCD);
633 }
634
635
636 static void scc_shutdown_port(void *ptr)
637 {
638         struct scc_port *port = ptr;
639
640         port->gs.flags &= ~ GS_ACTIVE;
641         if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
642                 scc_setsignals (port, 0, 0);
643         }
644 }
645
646
647 static int scc_set_real_termios (void *ptr)
648 {
649         /* the SCC has char sizes 5,7,6,8 in that order! */
650         static int chsize_map[4] = { 0, 2, 1, 3 };
651         unsigned cflag, baud, chsize, channel, brgval = 0;
652         unsigned long flags;
653         struct scc_port *port = ptr;
654         SCC_ACCESS_INIT(port);
655
656         if (!port->gs.tty || !port->gs.tty->termios) return 0;
657
658         channel = port->channel;
659
660         if (channel == CHANNEL_A)
661                 return 0;               /* Settings controlled by boot PROM */
662
663         cflag  = port->gs.tty->termios->c_cflag;
664         baud = port->gs.baud;
665         chsize = (cflag & CSIZE) >> 4;
666
667         if (baud == 0) {
668                 /* speed == 0 -> drop DTR */
669                 local_irq_save(flags);
670                 SCCmod(TX_CTRL_REG, ~TCR_DTR, 0);
671                 local_irq_restore(flags);
672                 return 0;
673         }
674         else if ((MACH_IS_MVME16x && (baud < 50 || baud > 38400)) ||
675                  (MACH_IS_MVME147 && (baud < 50 || baud > 19200)) ||
676                  (MACH_IS_BVME6000 &&(baud < 50 || baud > 76800))) {
677                 printk(KERN_NOTICE "SCC: Bad speed requested, %d\n", baud);
678                 return 0;
679         }
680
681         if (cflag & CLOCAL)
682                 port->gs.flags &= ~ASYNC_CHECK_CD;
683         else
684                 port->gs.flags |= ASYNC_CHECK_CD;
685
686 #ifdef CONFIG_MVME147_SCC
687         if (MACH_IS_MVME147)
688                 brgval = (M147_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
689 #endif
690 #ifdef CONFIG_MVME162_SCC
691         if (MACH_IS_MVME16x)
692                 brgval = (MVME_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
693 #endif
694 #ifdef CONFIG_BVME6000_SCC
695         if (MACH_IS_BVME6000)
696                 brgval = (BVME_SCC_RTxC + baud/2) / (16 * 2 * baud) - 2;
697 #endif
698         /* Now we have all parameters and can go to set them: */
699         local_irq_save(flags);
700
701         /* receiver's character size and auto-enables */
702         SCCmod(RX_CTRL_REG, ~(RCR_CHSIZE_MASK|RCR_AUTO_ENAB_MODE),
703                         (chsize_map[chsize] << 6) |
704                         ((cflag & CRTSCTS) ? RCR_AUTO_ENAB_MODE : 0));
705         /* parity and stop bits (both, Tx and Rx), clock mode never changes */
706         SCCmod (AUX1_CTRL_REG,
707                 ~(A1CR_PARITY_MASK | A1CR_MODE_MASK),
708                 ((cflag & PARENB
709                   ? (cflag & PARODD ? A1CR_PARITY_ODD : A1CR_PARITY_EVEN)
710                   : A1CR_PARITY_NONE)
711                  | (cflag & CSTOPB ? A1CR_MODE_ASYNC_2 : A1CR_MODE_ASYNC_1)));
712         /* sender's character size, set DTR for valid baud rate */
713         SCCmod(TX_CTRL_REG, ~TCR_CHSIZE_MASK, chsize_map[chsize] << 5 | TCR_DTR);
714         /* clock sources never change */
715         /* disable BRG before changing the value */
716         SCCmod(DPLL_CTRL_REG, ~DCR_BRG_ENAB, 0);
717         /* BRG value */
718         SCCwrite(TIMER_LOW_REG, brgval & 0xff);
719         SCCwrite(TIMER_HIGH_REG, (brgval >> 8) & 0xff);
720         /* BRG enable, and clock source never changes */
721         SCCmod(DPLL_CTRL_REG, 0xff, DCR_BRG_ENAB);
722
723         local_irq_restore(flags);
724
725         return 0;
726 }
727
728
729 static int scc_chars_in_buffer (void *ptr)
730 {
731         struct scc_port *port = ptr;
732         SCC_ACCESS_INIT(port);
733
734         return (SCCread (SPCOND_STATUS_REG) & SCSR_ALL_SENT) ? 0  : 1;
735 }
736
737
738 /* Comment taken from sx.c (2.4.0):
739    I haven't the foggiest why the decrement use count has to happen
740    here. The whole linux serial drivers stuff needs to be redesigned.
741    My guess is that this is a hack to minimize the impact of a bug
742    elsewhere. Thinking about it some more. (try it sometime) Try
743    running minicom on a serial port that is driven by a modularized
744    driver. Have the modem hangup. Then remove the driver module. Then
745    exit minicom.  I expect an "oops".  -- REW */
746
747 static void scc_hungup(void *ptr)
748 {
749         scc_disable_tx_interrupts(ptr);
750         scc_disable_rx_interrupts(ptr);
751 }
752
753
754 static void scc_close(void *ptr)
755 {
756         scc_disable_tx_interrupts(ptr);
757         scc_disable_rx_interrupts(ptr);
758 }
759
760
761 /*---------------------------------------------------------------------------
762  * Internal support functions
763  *--------------------------------------------------------------------------*/
764
765 static void scc_setsignals(struct scc_port *port, int dtr, int rts)
766 {
767         unsigned long flags;
768         unsigned char t;
769         SCC_ACCESS_INIT(port);
770
771         local_irq_save(flags);
772         t = SCCread(TX_CTRL_REG);
773         if (dtr >= 0) t = dtr? (t | TCR_DTR): (t & ~TCR_DTR);
774         if (rts >= 0) t = rts? (t | TCR_RTS): (t & ~TCR_RTS);
775         SCCwrite(TX_CTRL_REG, t);
776         local_irq_restore(flags);
777 }
778
779
780 static void scc_send_xchar(struct tty_struct *tty, char ch)
781 {
782         struct scc_port *port = (struct scc_port *)tty->driver_data;
783
784         port->x_char = ch;
785         if (ch)
786                 scc_enable_tx_interrupts(port);
787 }
788
789
790 /*---------------------------------------------------------------------------
791  * Driver entrypoints referenced from above
792  *--------------------------------------------------------------------------*/
793
794 static int scc_open (struct tty_struct * tty, struct file * filp)
795 {
796         int line = tty->index;
797         int retval;
798         struct scc_port *port = &scc_ports[line];
799         int i, channel = port->channel;
800         unsigned long   flags;
801         SCC_ACCESS_INIT(port);
802 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_MVME147_SCC)
803         static const struct {
804                 unsigned reg, val;
805         } mvme_init_tab[] = {
806                 /* Values for MVME162 and MVME147 */
807                 /* no parity, 1 stop bit, async, 1:16 */
808                 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
809                 /* parity error is special cond, ints disabled, no DMA */
810                 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
811                 /* Rx 8 bits/char, no auto enable, Rx off */
812                 { RX_CTRL_REG, RCR_CHSIZE_8 },
813                 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
814                 { TX_CTRL_REG, TCR_CHSIZE_8 },
815                 /* special features off */
816                 { AUX2_CTRL_REG, 0 },
817                 { CLK_CTRL_REG, CCR_RXCLK_BRG | CCR_TXCLK_BRG },
818                 { DPLL_CTRL_REG, DCR_BRG_ENAB | DCR_BRG_USE_PCLK },
819                 /* Start Rx */
820                 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
821                 /* Start Tx */
822                 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
823                 /* Ext/Stat ints: DCD only */
824                 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
825                 /* Reset Ext/Stat ints */
826                 { COMMAND_REG, CR_EXTSTAT_RESET },
827                 /* ...again */
828                 { COMMAND_REG, CR_EXTSTAT_RESET },
829         };
830 #endif
831 #if defined(CONFIG_BVME6000_SCC)
832         static const struct {
833                 unsigned reg, val;
834         } bvme_init_tab[] = {
835                 /* Values for BVME6000 */
836                 /* no parity, 1 stop bit, async, 1:16 */
837                 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
838                 /* parity error is special cond, ints disabled, no DMA */
839                 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
840                 /* Rx 8 bits/char, no auto enable, Rx off */
841                 { RX_CTRL_REG, RCR_CHSIZE_8 },
842                 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
843                 { TX_CTRL_REG, TCR_CHSIZE_8 },
844                 /* special features off */
845                 { AUX2_CTRL_REG, 0 },
846                 { CLK_CTRL_REG, CCR_RTxC_XTAL | CCR_RXCLK_BRG | CCR_TXCLK_BRG },
847                 { DPLL_CTRL_REG, DCR_BRG_ENAB },
848                 /* Start Rx */
849                 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
850                 /* Start Tx */
851                 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
852                 /* Ext/Stat ints: DCD only */
853                 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
854                 /* Reset Ext/Stat ints */
855                 { COMMAND_REG, CR_EXTSTAT_RESET },
856                 /* ...again */
857                 { COMMAND_REG, CR_EXTSTAT_RESET },
858         };
859 #endif
860         if (!(port->gs.flags & ASYNC_INITIALIZED)) {
861                 local_irq_save(flags);
862 #if defined(CONFIG_MVME147_SCC) || defined(CONFIG_MVME162_SCC)
863                 if (MACH_IS_MVME147 || MACH_IS_MVME16x) {
864                         for (i = 0; i < ARRAY_SIZE(mvme_init_tab); ++i)
865                                 SCCwrite(mvme_init_tab[i].reg, mvme_init_tab[i].val);
866                 }
867 #endif
868 #if defined(CONFIG_BVME6000_SCC)
869                 if (MACH_IS_BVME6000) {
870                         for (i = 0; i < ARRAY_SIZE(bvme_init_tab); ++i)
871                                 SCCwrite(bvme_init_tab[i].reg, bvme_init_tab[i].val);
872                 }
873 #endif
874
875                 /* remember status register for detection of DCD and CTS changes */
876                 scc_last_status_reg[channel] = SCCread(STATUS_REG);
877
878                 port->c_dcd = 0;        /* Prevent initial 1->0 interrupt */
879                 scc_setsignals (port, 1,1);
880                 local_irq_restore(flags);
881         }
882
883         tty->driver_data = port;
884         port->gs.tty = tty;
885         port->gs.count++;
886         retval = gs_init_port(&port->gs);
887         if (retval) {
888                 port->gs.count--;
889                 return retval;
890         }
891         port->gs.flags |= GS_ACTIVE;
892         retval = gs_block_til_ready(port, filp);
893
894         if (retval) {
895                 port->gs.count--;
896                 return retval;
897         }
898
899         port->c_dcd = scc_get_CD (port);
900
901         scc_enable_rx_interrupts(port);
902
903         return 0;
904 }
905
906
907 static void scc_throttle (struct tty_struct * tty)
908 {
909         struct scc_port *port = (struct scc_port *)tty->driver_data;
910         unsigned long   flags;
911         SCC_ACCESS_INIT(port);
912
913         if (tty->termios->c_cflag & CRTSCTS) {
914                 local_irq_save(flags);
915                 SCCmod(TX_CTRL_REG, ~TCR_RTS, 0);
916                 local_irq_restore(flags);
917         }
918         if (I_IXOFF(tty))
919                 scc_send_xchar(tty, STOP_CHAR(tty));
920 }
921
922
923 static void scc_unthrottle (struct tty_struct * tty)
924 {
925         struct scc_port *port = (struct scc_port *)tty->driver_data;
926         unsigned long   flags;
927         SCC_ACCESS_INIT(port);
928
929         if (tty->termios->c_cflag & CRTSCTS) {
930                 local_irq_save(flags);
931                 SCCmod(TX_CTRL_REG, 0xff, TCR_RTS);
932                 local_irq_restore(flags);
933         }
934         if (I_IXOFF(tty))
935                 scc_send_xchar(tty, START_CHAR(tty));
936 }
937
938
939 static int scc_ioctl(struct tty_struct *tty, struct file *file,
940                      unsigned int cmd, unsigned long arg)
941 {
942         return -ENOIOCTLCMD;
943 }
944
945
946 static void scc_break_ctl(struct tty_struct *tty, int break_state)
947 {
948         struct scc_port *port = (struct scc_port *)tty->driver_data;
949         unsigned long   flags;
950         SCC_ACCESS_INIT(port);
951
952         local_irq_save(flags);
953         SCCmod(TX_CTRL_REG, ~TCR_SEND_BREAK, 
954                         break_state ? TCR_SEND_BREAK : 0);
955         local_irq_restore(flags);
956 }
957
958
959 /*---------------------------------------------------------------------------
960  * Serial console stuff...
961  *--------------------------------------------------------------------------*/
962
963 #define scc_delay() do { __asm__ __volatile__ (" nop; nop"); } while (0)
964
965 static void scc_ch_write (char ch)
966 {
967         volatile char *p = NULL;
968         
969 #ifdef CONFIG_MVME147_SCC
970         if (MACH_IS_MVME147)
971                 p = (volatile char *)M147_SCC_A_ADDR;
972 #endif
973 #ifdef CONFIG_MVME162_SCC
974         if (MACH_IS_MVME16x)
975                 p = (volatile char *)MVME_SCC_A_ADDR;
976 #endif
977 #ifdef CONFIG_BVME6000_SCC
978         if (MACH_IS_BVME6000)
979                 p = (volatile char *)BVME_SCC_A_ADDR;
980 #endif
981
982         do {
983                 scc_delay();
984         }
985         while (!(*p & 4));
986         scc_delay();
987         *p = 8;
988         scc_delay();
989         *p = ch;
990 }
991
992 /* The console must be locked when we get here. */
993
994 static void scc_console_write (struct console *co, const char *str, unsigned count)
995 {
996         unsigned long   flags;
997
998         local_irq_save(flags);
999
1000         while (count--)
1001         {
1002                 if (*str == '\n')
1003                         scc_ch_write ('\r');
1004                 scc_ch_write (*str++);
1005         }
1006         local_irq_restore(flags);
1007 }
1008
1009 static struct tty_driver *scc_console_device(struct console *c, int *index)
1010 {
1011         *index = c->index;
1012         return scc_driver;
1013 }
1014
1015
1016 static int __init scc_console_setup(struct console *co, char *options)
1017 {
1018         return 0;
1019 }
1020
1021
1022 static struct console sercons = {
1023         .name           = "ttyS",
1024         .write          = scc_console_write,
1025         .device         = scc_console_device,
1026         .setup          = scc_console_setup,
1027         .flags          = CON_PRINTBUFFER,
1028         .index          = -1,
1029 };
1030
1031
1032 static int __init vme_scc_console_init(void)
1033 {
1034         if (vme_brdtype == VME_TYPE_MVME147 ||
1035                         vme_brdtype == VME_TYPE_MVME162 ||
1036                         vme_brdtype == VME_TYPE_MVME172 ||
1037                         vme_brdtype == VME_TYPE_BVME4000 ||
1038                         vme_brdtype == VME_TYPE_BVME6000)
1039                 register_console(&sercons);
1040         return 0;
1041 }
1042 console_initcall(vme_scc_console_init);