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