tty: now phase out the ioctl file pointer for good
[linux-2.6-block.git] / drivers / tty / serial / 68328serial.c
1 /* 68328serial.c: Serial port driver for 68328 microcontroller
2  *
3  * Copyright (C) 1995       David S. Miller    <davem@caip.rutgers.edu>
4  * Copyright (C) 1998       Kenneth Albanowski <kjahds@kjahds.com>
5  * Copyright (C) 1998, 1999 D. Jeff Dionne     <jeff@uclinux.org>
6  * Copyright (C) 1999       Vladimir Gurevich  <vgurevic@cisco.com>
7  * Copyright (C) 2002-2003  David McCullough   <davidm@snapgear.com>
8  * Copyright (C) 2002       Greg Ungerer       <gerg@snapgear.com>
9  *
10  * VZ Support/Fixes             Evan Stawnyczy <e@lineo.ca>
11  * Multiple UART support        Daniel Potts <danielp@cse.unsw.edu.au>
12  * Power management support     Daniel Potts <danielp@cse.unsw.edu.au>
13  * VZ Second Serial Port enable Phil Wilshire
14  * 2.4/2.5 port                 David McCullough
15  */
16
17 #include <asm/dbg.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/kernel.h>
31 #include <linux/console.h>
32 #include <linux/reboot.h>
33 #include <linux/keyboard.h>
34 #include <linux/init.h>
35 #include <linux/pm.h>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
38 #include <linux/gfp.h>
39
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/system.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
45
46 /* (es) */
47 /* note: perhaps we can murge these files, so that you can just
48  *       define 1 of them, and they can sort that out for themselves
49  */
50 #if defined(CONFIG_M68EZ328)
51 #include <asm/MC68EZ328.h>
52 #else
53 #if defined(CONFIG_M68VZ328)
54 #include <asm/MC68VZ328.h>
55 #else
56 #include <asm/MC68328.h>
57 #endif /* CONFIG_M68VZ328 */
58 #endif /* CONFIG_M68EZ328 */
59
60 #include "68328serial.h"
61
62 /* Turn off usage of real serial interrupt code, to "support" Copilot */
63 #ifdef CONFIG_XCOPILOT_BUGS
64 #undef USE_INTS
65 #else
66 #define USE_INTS
67 #endif
68
69 static struct m68k_serial m68k_soft[NR_PORTS];
70
71 static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72
73 /* multiple ports are contiguous in memory */
74 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75
76 struct tty_struct m68k_ttys;
77 struct m68k_serial *m68k_consinfo = 0;
78
79 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80
81 struct tty_driver *serial_driver;
82
83 /* number of characters left in xmit buffer before we ask for more */
84 #define WAKEUP_CHARS 256
85
86 /* Debugging... DEBUG_INTR is bad to use when one of the zs
87  * lines is your console ;(
88  */
89 #undef SERIAL_DEBUG_INTR
90 #undef SERIAL_DEBUG_OPEN
91 #undef SERIAL_DEBUG_FLOW
92
93 #define RS_ISR_PASS_LIMIT 256
94
95 static void change_speed(struct m68k_serial *info);
96
97 /*
98  *      Setup for console. Argument comes from the boot command line.
99  */
100
101 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102 #ifdef CONFIG_M68VZ328
103 #define CONSOLE_BAUD_RATE       19200
104 #define DEFAULT_CBAUD           B19200
105 #endif
106
107
108 #ifndef CONSOLE_BAUD_RATE
109 #define CONSOLE_BAUD_RATE       9600
110 #define DEFAULT_CBAUD           B9600
111 #endif
112
113
114 static int m68328_console_initted = 0;
115 static int m68328_console_baud    = CONSOLE_BAUD_RATE;
116 static int m68328_console_cbaud   = DEFAULT_CBAUD;
117
118
119 static inline int serial_paranoia_check(struct m68k_serial *info,
120                                         char *name, const char *routine)
121 {
122 #ifdef SERIAL_PARANOIA_CHECK
123         static const char *badmagic =
124                 "Warning: bad magic number for serial struct %s in %s\n";
125         static const char *badinfo =
126                 "Warning: null m68k_serial for %s in %s\n";
127
128         if (!info) {
129                 printk(badinfo, name, routine);
130                 return 1;
131         }
132         if (info->magic != SERIAL_MAGIC) {
133                 printk(badmagic, name, routine);
134                 return 1;
135         }
136 #endif
137         return 0;
138 }
139
140 /*
141  * This is used to figure out the divisor speeds and the timeouts
142  */
143 static int baud_table[] = {
144         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145         9600, 19200, 38400, 57600, 115200, 0 };
146
147 /* Sets or clears DTR/RTS on the requested line */
148 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149 {
150         if (set) {
151                 /* set the RTS/CTS line */
152         } else {
153                 /* clear it */
154         }
155         return;
156 }
157
158 /* Utility routines */
159 static inline int get_baud(struct m68k_serial *ss)
160 {
161         unsigned long result = 115200;
162         unsigned short int baud = uart_addr[ss->line].ubaud;
163         if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164         result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165
166         return result;
167 }
168
169 /*
170  * ------------------------------------------------------------
171  * rs_stop() and rs_start()
172  *
173  * This routines are called before setting or resetting tty->stopped.
174  * They enable or disable transmitter interrupts, as necessary.
175  * ------------------------------------------------------------
176  */
177 static void rs_stop(struct tty_struct *tty)
178 {
179         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180         m68328_uart *uart = &uart_addr[info->line];
181         unsigned long flags;
182
183         if (serial_paranoia_check(info, tty->name, "rs_stop"))
184                 return;
185         
186         local_irq_save(flags);
187         uart->ustcnt &= ~USTCNT_TXEN;
188         local_irq_restore(flags);
189 }
190
191 static int rs_put_char(char ch)
192 {
193         int flags, loops = 0;
194
195         local_irq_save(flags);
196
197         while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
198                 loops++;
199                 udelay(5);
200         }
201
202         UTX_TXDATA = ch;
203         udelay(5);
204         local_irq_restore(flags);
205         return 1;
206 }
207
208 static void rs_start(struct tty_struct *tty)
209 {
210         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211         m68328_uart *uart = &uart_addr[info->line];
212         unsigned long flags;
213         
214         if (serial_paranoia_check(info, tty->name, "rs_start"))
215                 return;
216         
217         local_irq_save(flags);
218         if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219 #ifdef USE_INTS
220                 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221 #else
222                 uart->ustcnt |= USTCNT_TXEN;
223 #endif
224         }
225         local_irq_restore(flags);
226 }
227
228 /* Drop into either the boot monitor or kadb upon receiving a break
229  * from keyboard/console input.
230  */
231 static void batten_down_hatches(void)
232 {
233         /* Drop into the debugger */
234 }
235
236 static void status_handle(struct m68k_serial *info, unsigned short status)
237 {
238 #if 0
239         if(status & DCD) {
240                 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
241                    ((info->curregs[3] & AUTO_ENAB)==0)) {
242                         info->curregs[3] |= AUTO_ENAB;
243                         info->pendregs[3] |= AUTO_ENAB;
244                         write_zsreg(info->m68k_channel, 3, info->curregs[3]);
245                 }
246         } else {
247                 if((info->curregs[3] & AUTO_ENAB)) {
248                         info->curregs[3] &= ~AUTO_ENAB;
249                         info->pendregs[3] &= ~AUTO_ENAB;
250                         write_zsreg(info->m68k_channel, 3, info->curregs[3]);
251                 }
252         }
253 #endif
254         /* If this is console input and this is a
255          * 'break asserted' status change interrupt
256          * see if we can drop into the debugger
257          */
258         if((status & URX_BREAK) && info->break_abort)
259                 batten_down_hatches();
260         return;
261 }
262
263 static void receive_chars(struct m68k_serial *info, unsigned short rx)
264 {
265         struct tty_struct *tty = info->port.tty;
266         m68328_uart *uart = &uart_addr[info->line];
267         unsigned char ch, flag;
268
269         /*
270          * This do { } while() loop will get ALL chars out of Rx FIFO 
271          */
272 #ifndef CONFIG_XCOPILOT_BUGS
273         do {
274 #endif  
275                 ch = GET_FIELD(rx, URX_RXDATA);
276         
277                 if(info->is_cons) {
278                         if(URX_BREAK & rx) { /* whee, break received */
279                                 status_handle(info, rx);
280                                 return;
281 #ifdef CONFIG_MAGIC_SYSRQ
282                         } else if (ch == 0x10) { /* ^P */
283                                 show_state();
284                                 show_free_areas();
285                                 show_buffers();
286 /*                              show_net_buffers(); */
287                                 return;
288                         } else if (ch == 0x12) { /* ^R */
289                                 emergency_restart();
290                                 return;
291 #endif /* CONFIG_MAGIC_SYSRQ */
292                         }
293                 }
294
295                 if(!tty)
296                         goto clear_and_exit;
297                 
298                 flag = TTY_NORMAL;
299
300                 if(rx & URX_PARITY_ERROR) {
301                         flag = TTY_PARITY;
302                         status_handle(info, rx);
303                 } else if(rx & URX_OVRUN) {
304                         flag = TTY_OVERRUN;
305                         status_handle(info, rx);
306                 } else if(rx & URX_FRAME_ERROR) {
307                         flag = TTY_FRAME;
308                         status_handle(info, rx);
309                 }
310                 tty_insert_flip_char(tty, ch, flag);
311 #ifndef CONFIG_XCOPILOT_BUGS
312         } while((rx = uart->urx.w) & URX_DATA_READY);
313 #endif
314
315         tty_schedule_flip(tty);
316
317 clear_and_exit:
318         return;
319 }
320
321 static void transmit_chars(struct m68k_serial *info)
322 {
323         m68328_uart *uart = &uart_addr[info->line];
324
325         if (info->x_char) {
326                 /* Send next char */
327                 uart->utx.b.txdata = info->x_char;
328                 info->x_char = 0;
329                 goto clear_and_return;
330         }
331
332         if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
333                 /* That's peculiar... TX ints off */
334                 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335                 goto clear_and_return;
336         }
337
338         /* Send char */
339         uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
340         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
341         info->xmit_cnt--;
342
343         if (info->xmit_cnt < WAKEUP_CHARS)
344                 schedule_work(&info->tqueue);
345
346         if(info->xmit_cnt <= 0) {
347                 /* All done for now... TX ints off */
348                 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349                 goto clear_and_return;
350         }
351
352 clear_and_return:
353         /* Clear interrupt (should be auto)*/
354         return;
355 }
356
357 /*
358  * This is the serial driver's generic interrupt routine
359  */
360 irqreturn_t rs_interrupt(int irq, void *dev_id)
361 {
362         struct m68k_serial *info = dev_id;
363         m68328_uart *uart;
364         unsigned short rx;
365         unsigned short tx;
366
367         uart = &uart_addr[info->line];
368         rx = uart->urx.w;
369
370 #ifdef USE_INTS
371         tx = uart->utx.w;
372
373         if (rx & URX_DATA_READY) receive_chars(info, rx);
374         if (tx & UTX_TX_AVAIL)   transmit_chars(info);
375 #else
376         receive_chars(info, rx);                
377 #endif
378         return IRQ_HANDLED;
379 }
380
381 static void do_softint(struct work_struct *work)
382 {
383         struct m68k_serial      *info = container_of(work, struct m68k_serial, tqueue);
384         struct tty_struct       *tty;
385         
386         tty = info->port.tty;
387         if (!tty)
388                 return;
389 #if 0
390         if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
391                 tty_wakeup(tty);
392         }
393 #endif   
394 }
395
396 static int startup(struct m68k_serial * info)
397 {
398         m68328_uart *uart = &uart_addr[info->line];
399         unsigned long flags;
400         
401         if (info->flags & S_INITIALIZED)
402                 return 0;
403
404         if (!info->xmit_buf) {
405                 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
406                 if (!info->xmit_buf)
407                         return -ENOMEM;
408         }
409
410         local_irq_save(flags);
411
412         /*
413          * Clear the FIFO buffers and disable them
414          * (they will be reenabled in change_speed())
415          */
416
417         uart->ustcnt = USTCNT_UEN;
418         info->xmit_fifo_size = 1;
419         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
420         (void)uart->urx.w;
421
422         /*
423          * Finally, enable sequencing and interrupts
424          */
425 #ifdef USE_INTS
426         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | 
427                  USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
428 #else
429         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
430 #endif
431
432         if (info->port.tty)
433                 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
434         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
435
436         /*
437          * and set the speed of the serial port
438          */
439
440         change_speed(info);
441
442         info->flags |= S_INITIALIZED;
443         local_irq_restore(flags);
444         return 0;
445 }
446
447 /*
448  * This routine will shutdown a serial port; interrupts are disabled, and
449  * DTR is dropped if the hangup on close termio flag is on.
450  */
451 static void shutdown(struct m68k_serial * info)
452 {
453         m68328_uart *uart = &uart_addr[info->line];
454         unsigned long   flags;
455
456         uart->ustcnt = 0; /* All off! */
457         if (!(info->flags & S_INITIALIZED))
458                 return;
459
460         local_irq_save(flags);
461         
462         if (info->xmit_buf) {
463                 free_page((unsigned long) info->xmit_buf);
464                 info->xmit_buf = 0;
465         }
466
467         if (info->port.tty)
468                 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
469         
470         info->flags &= ~S_INITIALIZED;
471         local_irq_restore(flags);
472 }
473
474 struct {
475         int divisor, prescale;
476 }
477 #ifndef CONFIG_M68VZ328
478  hw_baud_table[18] = {
479         {0,0}, /* 0 */
480         {0,0}, /* 50 */
481         {0,0}, /* 75 */
482         {0,0}, /* 110 */
483         {0,0}, /* 134 */
484         {0,0}, /* 150 */
485         {0,0}, /* 200 */
486         {7,0x26}, /* 300 */
487         {6,0x26}, /* 600 */
488         {5,0x26}, /* 1200 */
489         {0,0}, /* 1800 */
490         {4,0x26}, /* 2400 */
491         {3,0x26}, /* 4800 */
492         {2,0x26}, /* 9600 */
493         {1,0x26}, /* 19200 */
494         {0,0x26}, /* 38400 */
495         {1,0x38}, /* 57600 */
496         {0,0x38}, /* 115200 */
497 };
498 #else
499  hw_baud_table[18] = {
500                  {0,0}, /* 0 */
501                  {0,0}, /* 50 */
502                  {0,0}, /* 75 */
503                  {0,0}, /* 110 */
504                  {0,0}, /* 134 */
505                  {0,0}, /* 150 */
506                  {0,0}, /* 200 */
507                  {0,0}, /* 300 */
508                  {7,0x26}, /* 600 */
509                  {6,0x26}, /* 1200 */
510                  {0,0}, /* 1800 */
511                  {5,0x26}, /* 2400 */
512                  {4,0x26}, /* 4800 */
513                  {3,0x26}, /* 9600 */
514                  {2,0x26}, /* 19200 */
515                  {1,0x26}, /* 38400 */
516                  {0,0x26}, /* 57600 */
517                  {1,0x38}, /* 115200 */
518 }; 
519 #endif
520 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
521
522 /*
523  * This routine is called to set the UART divisor registers to match
524  * the specified baud rate for a serial port.
525  */
526 static void change_speed(struct m68k_serial *info)
527 {
528         m68328_uart *uart = &uart_addr[info->line];
529         unsigned short port;
530         unsigned short ustcnt;
531         unsigned cflag;
532         int     i;
533
534         if (!info->port.tty || !info->port.tty->termios)
535                 return;
536         cflag = info->port.tty->termios->c_cflag;
537         if (!(port = info->port))
538                 return;
539
540         ustcnt = uart->ustcnt;
541         uart->ustcnt = ustcnt & ~USTCNT_TXEN;
542
543         i = cflag & CBAUD;
544         if (i & CBAUDEX) {
545                 i = (i & ~CBAUDEX) + B38400;
546         }
547
548         info->baud = baud_table[i];
549         uart->ubaud = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) | 
550                 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
551
552         ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
553         
554         if ((cflag & CSIZE) == CS8)
555                 ustcnt |= USTCNT_8_7;
556                 
557         if (cflag & CSTOPB)
558                 ustcnt |= USTCNT_STOP;
559
560         if (cflag & PARENB)
561                 ustcnt |= USTCNT_PARITYEN;
562         if (cflag & PARODD)
563                 ustcnt |= USTCNT_ODD_EVEN;
564         
565 #ifdef CONFIG_SERIAL_68328_RTS_CTS
566         if (cflag & CRTSCTS) {
567                 uart->utx.w &= ~ UTX_NOCTS;
568         } else {
569                 uart->utx.w |= UTX_NOCTS;
570         }
571 #endif
572
573         ustcnt |= USTCNT_TXEN;
574         
575         uart->ustcnt = ustcnt;
576         return;
577 }
578
579 /*
580  * Fair output driver allows a process to speak.
581  */
582 static void rs_fair_output(void)
583 {
584         int left;               /* Output no more than that */
585         unsigned long flags;
586         struct m68k_serial *info = &m68k_soft[0];
587         char c;
588
589         if (info == 0) return;
590         if (info->xmit_buf == 0) return;
591
592         local_irq_save(flags);
593         left = info->xmit_cnt;
594         while (left != 0) {
595                 c = info->xmit_buf[info->xmit_tail];
596                 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
597                 info->xmit_cnt--;
598                 local_irq_restore(flags);
599
600                 rs_put_char(c);
601
602                 local_irq_save(flags);
603                 left = min(info->xmit_cnt, left-1);
604         }
605
606         /* Last character is being transmitted now (hopefully). */
607         udelay(5);
608
609         local_irq_restore(flags);
610         return;
611 }
612
613 /*
614  * m68k_console_print is registered for printk.
615  */
616 void console_print_68328(const char *p)
617 {
618         char c;
619         
620         while((c=*(p++)) != 0) {
621                 if(c == '\n')
622                         rs_put_char('\r');
623                 rs_put_char(c);
624         }
625
626         /* Comment this if you want to have a strict interrupt-driven output */
627         rs_fair_output();
628
629         return;
630 }
631
632 static void rs_set_ldisc(struct tty_struct *tty)
633 {
634         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
635
636         if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
637                 return;
638
639         info->is_cons = (tty->termios->c_line == N_TTY);
640         
641         printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
642 }
643
644 static void rs_flush_chars(struct tty_struct *tty)
645 {
646         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
647         m68328_uart *uart = &uart_addr[info->line];
648         unsigned long flags;
649
650         if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
651                 return;
652 #ifndef USE_INTS
653         for(;;) {
654 #endif
655
656         /* Enable transmitter */
657         local_irq_save(flags);
658
659         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
660                         !info->xmit_buf) {
661                 local_irq_restore(flags);
662                 return;
663         }
664
665 #ifdef USE_INTS
666         uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
667 #else
668         uart->ustcnt |= USTCNT_TXEN;
669 #endif
670
671 #ifdef USE_INTS
672         if (uart->utx.w & UTX_TX_AVAIL) {
673 #else
674         if (1) {
675 #endif
676                 /* Send char */
677                 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
678                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
679                 info->xmit_cnt--;
680         }
681
682 #ifndef USE_INTS
683         while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
684         }
685 #endif
686         local_irq_restore(flags);
687 }
688
689 extern void console_printn(const char * b, int count);
690
691 static int rs_write(struct tty_struct * tty,
692                     const unsigned char *buf, int count)
693 {
694         int     c, total = 0;
695         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
696         m68328_uart *uart = &uart_addr[info->line];
697         unsigned long flags;
698
699         if (serial_paranoia_check(info, tty->name, "rs_write"))
700                 return 0;
701
702         if (!tty || !info->xmit_buf)
703                 return 0;
704
705         local_save_flags(flags);
706         while (1) {
707                 local_irq_disable();            
708                 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
709                                    SERIAL_XMIT_SIZE - info->xmit_head));
710                 local_irq_restore(flags);
711
712                 if (c <= 0)
713                         break;
714
715                 memcpy(info->xmit_buf + info->xmit_head, buf, c);
716
717                 local_irq_disable();
718                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
719                 info->xmit_cnt += c;
720                 local_irq_restore(flags);
721                 buf += c;
722                 count -= c;
723                 total += c;
724         }
725
726         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
727                 /* Enable transmitter */
728                 local_irq_disable();            
729 #ifndef USE_INTS
730                 while(info->xmit_cnt) {
731 #endif
732
733                 uart->ustcnt |= USTCNT_TXEN;
734 #ifdef USE_INTS
735                 uart->ustcnt |= USTCNT_TX_INTR_MASK;
736 #else
737                 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
738 #endif
739                 if (uart->utx.w & UTX_TX_AVAIL) {
740                         uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
741                         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
742                         info->xmit_cnt--;
743                 }
744
745 #ifndef USE_INTS
746                 }
747 #endif
748                 local_irq_restore(flags);
749         }
750
751         return total;
752 }
753
754 static int rs_write_room(struct tty_struct *tty)
755 {
756         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
757         int     ret;
758                                 
759         if (serial_paranoia_check(info, tty->name, "rs_write_room"))
760                 return 0;
761         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
762         if (ret < 0)
763                 ret = 0;
764         return ret;
765 }
766
767 static int rs_chars_in_buffer(struct tty_struct *tty)
768 {
769         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
770                                 
771         if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
772                 return 0;
773         return info->xmit_cnt;
774 }
775
776 static void rs_flush_buffer(struct tty_struct *tty)
777 {
778         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779         unsigned long flags;
780                                 
781         if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
782                 return;
783         local_irq_save(flags);
784         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
785         local_irq_restore(flags);
786         tty_wakeup(tty);
787 }
788
789 /*
790  * ------------------------------------------------------------
791  * rs_throttle()
792  * 
793  * This routine is called by the upper-layer tty layer to signal that
794  * incoming characters should be throttled.
795  * ------------------------------------------------------------
796  */
797 static void rs_throttle(struct tty_struct * tty)
798 {
799         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
800
801         if (serial_paranoia_check(info, tty->name, "rs_throttle"))
802                 return;
803         
804         if (I_IXOFF(tty))
805                 info->x_char = STOP_CHAR(tty);
806
807         /* Turn off RTS line (do this atomic) */
808 }
809
810 static void rs_unthrottle(struct tty_struct * tty)
811 {
812         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
813
814         if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
815                 return;
816         
817         if (I_IXOFF(tty)) {
818                 if (info->x_char)
819                         info->x_char = 0;
820                 else
821                         info->x_char = START_CHAR(tty);
822         }
823
824         /* Assert RTS line (do this atomic) */
825 }
826
827 /*
828  * ------------------------------------------------------------
829  * rs_ioctl() and friends
830  * ------------------------------------------------------------
831  */
832
833 static int get_serial_info(struct m68k_serial * info,
834                            struct serial_struct * retinfo)
835 {
836         struct serial_struct tmp;
837   
838         if (!retinfo)
839                 return -EFAULT;
840         memset(&tmp, 0, sizeof(tmp));
841         tmp.type = info->type;
842         tmp.line = info->line;
843         tmp.port = info->port;
844         tmp.irq = info->irq;
845         tmp.flags = info->flags;
846         tmp.baud_base = info->baud_base;
847         tmp.close_delay = info->close_delay;
848         tmp.closing_wait = info->closing_wait;
849         tmp.custom_divisor = info->custom_divisor;
850         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
851                 return -EFAULT;
852
853         return 0;
854 }
855
856 static int set_serial_info(struct m68k_serial * info,
857                            struct serial_struct * new_info)
858 {
859         struct serial_struct new_serial;
860         struct m68k_serial old_info;
861         int                     retval = 0;
862
863         if (!new_info)
864                 return -EFAULT;
865         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
866                 return -EFAULT;
867         old_info = *info;
868
869         if (!capable(CAP_SYS_ADMIN)) {
870                 if ((new_serial.baud_base != info->baud_base) ||
871                     (new_serial.type != info->type) ||
872                     (new_serial.close_delay != info->close_delay) ||
873                     ((new_serial.flags & ~S_USR_MASK) !=
874                      (info->flags & ~S_USR_MASK)))
875                         return -EPERM;
876                 info->flags = ((info->flags & ~S_USR_MASK) |
877                                (new_serial.flags & S_USR_MASK));
878                 info->custom_divisor = new_serial.custom_divisor;
879                 goto check_and_exit;
880         }
881
882         if (info->count > 1)
883                 return -EBUSY;
884
885         /*
886          * OK, past this point, all the error checking has been done.
887          * At this point, we start making changes.....
888          */
889
890         info->baud_base = new_serial.baud_base;
891         info->flags = ((info->flags & ~S_FLAGS) |
892                         (new_serial.flags & S_FLAGS));
893         info->type = new_serial.type;
894         info->close_delay = new_serial.close_delay;
895         info->closing_wait = new_serial.closing_wait;
896
897 check_and_exit:
898         retval = startup(info);
899         return retval;
900 }
901
902 /*
903  * get_lsr_info - get line status register info
904  *
905  * Purpose: Let user call ioctl() to get info when the UART physically
906  *          is emptied.  On bus types like RS485, the transmitter must
907  *          release the bus after transmitting. This must be done when
908  *          the transmit shift register is empty, not be done when the
909  *          transmit holding register is empty.  This functionality
910  *          allows an RS485 driver to be written in user space. 
911  */
912 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
913 {
914 #ifdef CONFIG_SERIAL_68328_RTS_CTS
915         m68328_uart *uart = &uart_addr[info->line];
916 #endif
917         unsigned char status;
918         unsigned long flags;
919
920         local_irq_save(flags);
921 #ifdef CONFIG_SERIAL_68328_RTS_CTS
922         status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
923 #else
924         status = 0;
925 #endif
926         local_irq_restore(flags);
927         return put_user(status, value);
928 }
929
930 /*
931  * This routine sends a break character out the serial port.
932  */
933 static void send_break(struct m68k_serial * info, unsigned int duration)
934 {
935         m68328_uart *uart = &uart_addr[info->line];
936         unsigned long flags;
937         if (!info->port)
938                 return;
939         local_irq_save(flags);
940 #ifdef USE_INTS 
941         uart->utx.w |= UTX_SEND_BREAK;
942         msleep_interruptible(duration);
943         uart->utx.w &= ~UTX_SEND_BREAK;
944 #endif          
945         local_irq_restore(flags);
946 }
947
948 static int rs_ioctl(struct tty_struct *tty,
949                     unsigned int cmd, unsigned long arg)
950 {
951         int error;
952         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
953         int retval;
954
955         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
956                 return -ENODEV;
957
958         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
959             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
960             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
961                 if (tty->flags & (1 << TTY_IO_ERROR))
962                     return -EIO;
963         }
964         
965         switch (cmd) {
966                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
967                         retval = tty_check_change(tty);
968                         if (retval)
969                                 return retval;
970                         tty_wait_until_sent(tty, 0);
971                         if (!arg)
972                                 send_break(info, 250);  /* 1/4 second */
973                         return 0;
974                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
975                         retval = tty_check_change(tty);
976                         if (retval)
977                                 return retval;
978                         tty_wait_until_sent(tty, 0);
979                         send_break(info, arg ? arg*(100) : 250);
980                         return 0;
981                 case TIOCGSERIAL:
982                         return get_serial_info(info,
983                                        (struct serial_struct *) arg);
984                 case TIOCSSERIAL:
985                         return set_serial_info(info,
986                                                (struct serial_struct *) arg);
987                 case TIOCSERGETLSR: /* Get line status register */
988                         return get_lsr_info(info, (unsigned int *) arg);
989                 case TIOCSERGSTRUCT:
990                         if (copy_to_user((struct m68k_serial *) arg,
991                                     info, sizeof(struct m68k_serial)))
992                                 return -EFAULT;
993                         return 0;
994                 default:
995                         return -ENOIOCTLCMD;
996                 }
997         return 0;
998 }
999
1000 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1001 {
1002         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1003
1004         change_speed(info);
1005
1006         if ((old_termios->c_cflag & CRTSCTS) &&
1007             !(tty->termios->c_cflag & CRTSCTS)) {
1008                 tty->hw_stopped = 0;
1009                 rs_start(tty);
1010         }
1011         
1012 }
1013
1014 /*
1015  * ------------------------------------------------------------
1016  * rs_close()
1017  * 
1018  * This routine is called when the serial port gets closed.  First, we
1019  * wait for the last remaining data to be sent.  Then, we unlink its
1020  * S structure from the interrupt chain if necessary, and we free
1021  * that IRQ if nothing is left in the chain.
1022  * ------------------------------------------------------------
1023  */
1024 static void rs_close(struct tty_struct *tty, struct file * filp)
1025 {
1026         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1027         m68328_uart *uart = &uart_addr[info->line];
1028         unsigned long flags;
1029
1030         if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1031                 return;
1032         
1033         local_irq_save(flags);
1034         
1035         if (tty_hung_up_p(filp)) {
1036                 local_irq_restore(flags);
1037                 return;
1038         }
1039         
1040         if ((tty->count == 1) && (info->count != 1)) {
1041                 /*
1042                  * Uh, oh.  tty->count is 1, which means that the tty
1043                  * structure will be freed.  Info->count should always
1044                  * be one in these conditions.  If it's greater than
1045                  * one, we've got real problems, since it means the
1046                  * serial port won't be shutdown.
1047                  */
1048                 printk("rs_close: bad serial port count; tty->count is 1, "
1049                        "info->count is %d\n", info->count);
1050                 info->count = 1;
1051         }
1052         if (--info->count < 0) {
1053                 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1054                        info->line, info->count);
1055                 info->count = 0;
1056         }
1057         if (info->count) {
1058                 local_irq_restore(flags);
1059                 return;
1060         }
1061         info->flags |= S_CLOSING;
1062         /*
1063          * Now we wait for the transmit buffer to clear; and we notify 
1064          * the line discipline to only process XON/XOFF characters.
1065          */
1066         tty->closing = 1;
1067         if (info->closing_wait != S_CLOSING_WAIT_NONE)
1068                 tty_wait_until_sent(tty, info->closing_wait);
1069         /*
1070          * At this point we stop accepting input.  To do this, we
1071          * disable the receive line status interrupts, and tell the
1072          * interrupt driver to stop checking the data ready bit in the
1073          * line status register.
1074          */
1075
1076         uart->ustcnt &= ~USTCNT_RXEN;
1077         uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1078
1079         shutdown(info);
1080         rs_flush_buffer(tty);
1081                 
1082         tty_ldisc_flush(tty);
1083         tty->closing = 0;
1084         info->event = 0;
1085         info->port.tty = NULL;
1086 #warning "This is not and has never been valid so fix it"       
1087 #if 0
1088         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1089                 if (tty->ldisc.close)
1090                         (tty->ldisc.close)(tty);
1091                 tty->ldisc = ldiscs[N_TTY];
1092                 tty->termios->c_line = N_TTY;
1093                 if (tty->ldisc.open)
1094                         (tty->ldisc.open)(tty);
1095         }
1096 #endif  
1097         if (info->blocked_open) {
1098                 if (info->close_delay) {
1099                         msleep_interruptible(jiffies_to_msecs(info->close_delay));
1100                 }
1101                 wake_up_interruptible(&info->open_wait);
1102         }
1103         info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1104         wake_up_interruptible(&info->close_wait);
1105         local_irq_restore(flags);
1106 }
1107
1108 /*
1109  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1110  */
1111 void rs_hangup(struct tty_struct *tty)
1112 {
1113         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1114         
1115         if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1116                 return;
1117         
1118         rs_flush_buffer(tty);
1119         shutdown(info);
1120         info->event = 0;
1121         info->count = 0;
1122         info->flags &= ~S_NORMAL_ACTIVE;
1123         info->port.tty = NULL;
1124         wake_up_interruptible(&info->open_wait);
1125 }
1126
1127 /*
1128  * ------------------------------------------------------------
1129  * rs_open() and friends
1130  * ------------------------------------------------------------
1131  */
1132 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1133                            struct m68k_serial *info)
1134 {
1135         DECLARE_WAITQUEUE(wait, current);
1136         int             retval;
1137         int             do_clocal = 0;
1138
1139         /*
1140          * If the device is in the middle of being closed, then block
1141          * until it's done, and then try again.
1142          */
1143         if (info->flags & S_CLOSING) {
1144                 interruptible_sleep_on(&info->close_wait);
1145 #ifdef SERIAL_DO_RESTART
1146                 if (info->flags & S_HUP_NOTIFY)
1147                         return -EAGAIN;
1148                 else
1149                         return -ERESTARTSYS;
1150 #else
1151                 return -EAGAIN;
1152 #endif
1153         }
1154         
1155         /*
1156          * If non-blocking mode is set, or the port is not enabled,
1157          * then make the check up front and then exit.
1158          */
1159         if ((filp->f_flags & O_NONBLOCK) ||
1160             (tty->flags & (1 << TTY_IO_ERROR))) {
1161                 info->flags |= S_NORMAL_ACTIVE;
1162                 return 0;
1163         }
1164
1165         if (tty->termios->c_cflag & CLOCAL)
1166                 do_clocal = 1;
1167
1168         /*
1169          * Block waiting for the carrier detect and the line to become
1170          * free (i.e., not in use by the callout).  While we are in
1171          * this loop, info->count is dropped by one, so that
1172          * rs_close() knows when to free things.  We restore it upon
1173          * exit, either normal or abnormal.
1174          */
1175         retval = 0;
1176         add_wait_queue(&info->open_wait, &wait);
1177
1178         info->count--;
1179         info->blocked_open++;
1180         while (1) {
1181                 local_irq_disable();
1182                 m68k_rtsdtr(info, 1);
1183                 local_irq_enable();
1184                 current->state = TASK_INTERRUPTIBLE;
1185                 if (tty_hung_up_p(filp) ||
1186                     !(info->flags & S_INITIALIZED)) {
1187 #ifdef SERIAL_DO_RESTART
1188                         if (info->flags & S_HUP_NOTIFY)
1189                                 retval = -EAGAIN;
1190                         else
1191                                 retval = -ERESTARTSYS;  
1192 #else
1193                         retval = -EAGAIN;
1194 #endif
1195                         break;
1196                 }
1197                 if (!(info->flags & S_CLOSING) && do_clocal)
1198                         break;
1199                 if (signal_pending(current)) {
1200                         retval = -ERESTARTSYS;
1201                         break;
1202                 }
1203                 tty_unlock();
1204                 schedule();
1205                 tty_lock();
1206         }
1207         current->state = TASK_RUNNING;
1208         remove_wait_queue(&info->open_wait, &wait);
1209         if (!tty_hung_up_p(filp))
1210                 info->count++;
1211         info->blocked_open--;
1212
1213         if (retval)
1214                 return retval;
1215         info->flags |= S_NORMAL_ACTIVE;
1216         return 0;
1217 }       
1218
1219 /*
1220  * This routine is called whenever a serial port is opened.  It
1221  * enables interrupts for a serial port, linking in its S structure into
1222  * the IRQ chain.   It also performs the serial-specific
1223  * initialization for the tty structure.
1224  */
1225 int rs_open(struct tty_struct *tty, struct file * filp)
1226 {
1227         struct m68k_serial      *info;
1228         int                     retval, line;
1229
1230         line = tty->index;
1231         
1232         if (line >= NR_PORTS || line < 0) /* we have exactly one */
1233                 return -ENODEV;
1234
1235         info = &m68k_soft[line];
1236
1237         if (serial_paranoia_check(info, tty->name, "rs_open"))
1238                 return -ENODEV;
1239
1240         info->count++;
1241         tty->driver_data = info;
1242         info->port.tty = tty;
1243
1244         /*
1245          * Start up serial port
1246          */
1247         retval = startup(info);
1248         if (retval)
1249                 return retval;
1250
1251         return block_til_ready(tty, filp, info);
1252 }
1253
1254 /* Finally, routines used to initialize the serial driver. */
1255
1256 static void show_serial_version(void)
1257 {
1258         printk("MC68328 serial driver version 1.00\n");
1259 }
1260
1261 static const struct tty_operations rs_ops = {
1262         .open = rs_open,
1263         .close = rs_close,
1264         .write = rs_write,
1265         .flush_chars = rs_flush_chars,
1266         .write_room = rs_write_room,
1267         .chars_in_buffer = rs_chars_in_buffer,
1268         .flush_buffer = rs_flush_buffer,
1269         .ioctl = rs_ioctl,
1270         .throttle = rs_throttle,
1271         .unthrottle = rs_unthrottle,
1272         .set_termios = rs_set_termios,
1273         .stop = rs_stop,
1274         .start = rs_start,
1275         .hangup = rs_hangup,
1276         .set_ldisc = rs_set_ldisc,
1277 };
1278
1279 /* rs_init inits the driver */
1280 static int __init
1281 rs68328_init(void)
1282 {
1283         int flags, i;
1284         struct m68k_serial *info;
1285
1286         serial_driver = alloc_tty_driver(NR_PORTS);
1287         if (!serial_driver)
1288                 return -ENOMEM;
1289
1290         show_serial_version();
1291
1292         /* Initialize the tty_driver structure */
1293         /* SPARC: Not all of this is exactly right for us. */
1294         
1295         serial_driver->name = "ttyS";
1296         serial_driver->major = TTY_MAJOR;
1297         serial_driver->minor_start = 64;
1298         serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1299         serial_driver->subtype = SERIAL_TYPE_NORMAL;
1300         serial_driver->init_termios = tty_std_termios;
1301         serial_driver->init_termios.c_cflag = 
1302                         m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1303         serial_driver->flags = TTY_DRIVER_REAL_RAW;
1304         tty_set_operations(serial_driver, &rs_ops);
1305
1306         if (tty_register_driver(serial_driver)) {
1307                 put_tty_driver(serial_driver);
1308                 printk(KERN_ERR "Couldn't register serial driver\n");
1309                 return -ENOMEM;
1310         }
1311
1312         local_irq_save(flags);
1313
1314         for(i=0;i<NR_PORTS;i++) {
1315
1316             info = &m68k_soft[i];
1317             info->magic = SERIAL_MAGIC;
1318             info->port = (int) &uart_addr[i];
1319             info->port.tty = NULL;
1320             info->irq = uart_irqs[i];
1321             info->custom_divisor = 16;
1322             info->close_delay = 50;
1323             info->closing_wait = 3000;
1324             info->x_char = 0;
1325             info->event = 0;
1326             info->count = 0;
1327             info->blocked_open = 0;
1328             INIT_WORK(&info->tqueue, do_softint);
1329             init_waitqueue_head(&info->open_wait);
1330             init_waitqueue_head(&info->close_wait);
1331             info->line = i;
1332             info->is_cons = 1; /* Means shortcuts work */
1333             
1334             printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line, 
1335                    info->port, info->irq);
1336             printk(" is a builtin MC68328 UART\n");
1337             
1338 #ifdef CONFIG_M68VZ328
1339                 if (i > 0 )
1340                         PJSEL &= 0xCF;  /* PSW enable second port output */
1341 #endif
1342
1343             if (request_irq(uart_irqs[i],
1344                             rs_interrupt,
1345                             IRQF_DISABLED,
1346                             "M68328_UART", info))
1347                 panic("Unable to attach 68328 serial interrupt\n");
1348         }
1349         local_irq_restore(flags);
1350         return 0;
1351 }
1352
1353 module_init(rs68328_init);
1354
1355
1356
1357 static void m68328_set_baud(void)
1358 {
1359         unsigned short ustcnt;
1360         int     i;
1361
1362         ustcnt = USTCNT;
1363         USTCNT = ustcnt & ~USTCNT_TXEN;
1364
1365 again:
1366         for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1367                 if (baud_table[i] == m68328_console_baud)
1368                         break;
1369         if (i >= ARRAY_SIZE(baud_table)) {
1370                 m68328_console_baud = 9600;
1371                 goto again;
1372         }
1373
1374         UBAUD = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) | 
1375                 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1376         ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1377         ustcnt |= USTCNT_8_7;
1378         ustcnt |= USTCNT_TXEN;
1379         USTCNT = ustcnt;
1380         m68328_console_initted = 1;
1381         return;
1382 }
1383
1384
1385 int m68328_console_setup(struct console *cp, char *arg)
1386 {
1387         int             i, n = CONSOLE_BAUD_RATE;
1388
1389         if (!cp)
1390                 return(-1);
1391
1392         if (arg)
1393                 n = simple_strtoul(arg,NULL,0);
1394
1395         for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1396                 if (baud_table[i] == n)
1397                         break;
1398         if (i < ARRAY_SIZE(baud_table)) {
1399                 m68328_console_baud = n;
1400                 m68328_console_cbaud = 0;
1401                 if (i > 15) {
1402                         m68328_console_cbaud |= CBAUDEX;
1403                         i -= 15;
1404                 }
1405                 m68328_console_cbaud |= i;
1406         }
1407
1408         m68328_set_baud(); /* make sure baud rate changes */
1409         return(0);
1410 }
1411
1412
1413 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1414 {
1415         *index = c->index;
1416         return serial_driver;
1417 }
1418
1419
1420 void m68328_console_write (struct console *co, const char *str,
1421                            unsigned int count)
1422 {
1423         if (!m68328_console_initted)
1424                 m68328_set_baud();
1425     while (count--) {
1426         if (*str == '\n')
1427            rs_put_char('\r');
1428         rs_put_char( *str++ );
1429     }
1430 }
1431
1432
1433 static struct console m68328_driver = {
1434         .name           = "ttyS",
1435         .write          = m68328_console_write,
1436         .device         = m68328_console_device,
1437         .setup          = m68328_console_setup,
1438         .flags          = CON_PRINTBUFFER,
1439         .index          = -1,
1440 };
1441
1442
1443 static int __init m68328_console_init(void)
1444 {
1445         register_console(&m68328_driver);
1446         return 0;
1447 }
1448
1449 console_initcall(m68328_console_init);