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