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