Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc-merge
[linux-2.6] / 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/pm_legacy.h>
38 #include <linux/bitops.h>
39 #include <linux/delay.h>
40
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/system.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, flag;
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                                 emergency_restart();
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_request_buffer_room(tty, 1) == 0) {
336                         schedule_work(&tty->flip.work);
337                         return;
338                 }
339
340                 flag = TTY_NORMAL;
341
342                 if(rx & URX_PARITY_ERROR) {
343                         flag = TTY_PARITY;
344                         status_handle(info, rx);
345                 } else if(rx & URX_OVRUN) {
346                         flag = TTY_OVERRUN;
347                         status_handle(info, rx);
348                 } else if(rx & URX_FRAME_ERROR) {
349                         flag = TTY_FRAME;
350                         status_handle(info, rx);
351                 }
352                 tty_insert_flip_char(tty, ch, flag);
353 #ifndef CONFIG_XCOPILOT_BUGS
354         } while((rx = uart->urx.w) & URX_DATA_READY);
355 #endif
356
357         schedule_work(&tty->flip.work);
358
359 clear_and_exit:
360         return;
361 }
362
363 static _INLINE_ void transmit_chars(struct m68k_serial *info)
364 {
365         m68328_uart *uart = &uart_addr[info->line];
366
367         if (info->x_char) {
368                 /* Send next char */
369                 uart->utx.b.txdata = info->x_char;
370                 info->x_char = 0;
371                 goto clear_and_return;
372         }
373
374         if((info->xmit_cnt <= 0) || info->tty->stopped) {
375                 /* That's peculiar... TX ints off */
376                 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
377                 goto clear_and_return;
378         }
379
380         /* Send char */
381         uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
382         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
383         info->xmit_cnt--;
384
385         if (info->xmit_cnt < WAKEUP_CHARS)
386                 schedule_work(&info->tqueue);
387
388         if(info->xmit_cnt <= 0) {
389                 /* All done for now... TX ints off */
390                 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
391                 goto clear_and_return;
392         }
393
394 clear_and_return:
395         /* Clear interrupt (should be auto)*/
396         return;
397 }
398
399 /*
400  * This is the serial driver's generic interrupt routine
401  */
402 irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
403 {
404         struct m68k_serial * info;
405         m68328_uart *uart;
406         unsigned short rx;
407         unsigned short tx;
408
409         info = IRQ_ports[irq];
410         if(!info)
411             return IRQ_NONE;
412
413         uart = &uart_addr[info->line];
414         rx = uart->urx.w;
415
416 #ifdef USE_INTS
417         tx = uart->utx.w;
418
419         if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
420         if (tx & UTX_TX_AVAIL)   transmit_chars(info);
421 #else
422         receive_chars(info, regs, rx);          
423 #endif
424         return IRQ_HANDLED;
425 }
426
427 static void do_softint(void *private)
428 {
429         struct m68k_serial      *info = (struct m68k_serial *) private;
430         struct tty_struct       *tty;
431         
432         tty = info->tty;
433         if (!tty)
434                 return;
435 #if 0
436         if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
437                 tty_wakeup(tty);
438         }
439 #endif   
440 }
441
442 /*
443  * This routine is called from the scheduler tqueue when the interrupt
444  * routine has signalled that a hangup has occurred.  The path of
445  * hangup processing is:
446  *
447  *      serial interrupt routine -> (scheduler tqueue) ->
448  *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
449  * 
450  */
451 static void do_serial_hangup(void *private)
452 {
453         struct m68k_serial      *info = (struct m68k_serial *) private;
454         struct tty_struct       *tty;
455         
456         tty = info->tty;
457         if (!tty)
458                 return;
459
460         tty_hangup(tty);
461 }
462
463
464 static int startup(struct m68k_serial * info)
465 {
466         m68328_uart *uart = &uart_addr[info->line];
467         unsigned long flags;
468         
469         if (info->flags & S_INITIALIZED)
470                 return 0;
471
472         if (!info->xmit_buf) {
473                 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
474                 if (!info->xmit_buf)
475                         return -ENOMEM;
476         }
477
478         save_flags(flags); cli();
479
480         /*
481          * Clear the FIFO buffers and disable them
482          * (they will be reenabled in change_speed())
483          */
484
485         uart->ustcnt = USTCNT_UEN;
486         info->xmit_fifo_size = 1;
487         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
488         (void)uart->urx.w;
489
490         /*
491          * Finally, enable sequencing and interrupts
492          */
493 #ifdef USE_INTS
494         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | 
495                  USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
496 #else
497         uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
498 #endif
499
500         if (info->tty)
501                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
502         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
503
504         /*
505          * and set the speed of the serial port
506          */
507
508         change_speed(info);
509
510         info->flags |= S_INITIALIZED;
511         restore_flags(flags);
512         return 0;
513 }
514
515 /*
516  * This routine will shutdown a serial port; interrupts are disabled, and
517  * DTR is dropped if the hangup on close termio flag is on.
518  */
519 static void shutdown(struct m68k_serial * info)
520 {
521         m68328_uart *uart = &uart_addr[info->line];
522         unsigned long   flags;
523
524         uart->ustcnt = 0; /* All off! */
525         if (!(info->flags & S_INITIALIZED))
526                 return;
527
528         save_flags(flags); cli(); /* Disable interrupts */
529         
530         if (info->xmit_buf) {
531                 free_page((unsigned long) info->xmit_buf);
532                 info->xmit_buf = 0;
533         }
534
535         if (info->tty)
536                 set_bit(TTY_IO_ERROR, &info->tty->flags);
537         
538         info->flags &= ~S_INITIALIZED;
539         restore_flags(flags);
540 }
541
542 struct {
543         int divisor, prescale;
544 }
545 #ifndef CONFIG_M68VZ328
546  hw_baud_table[18] = {
547         {0,0}, /* 0 */
548         {0,0}, /* 50 */
549         {0,0}, /* 75 */
550         {0,0}, /* 110 */
551         {0,0}, /* 134 */
552         {0,0}, /* 150 */
553         {0,0}, /* 200 */
554         {7,0x26}, /* 300 */
555         {6,0x26}, /* 600 */
556         {5,0x26}, /* 1200 */
557         {0,0}, /* 1800 */
558         {4,0x26}, /* 2400 */
559         {3,0x26}, /* 4800 */
560         {2,0x26}, /* 9600 */
561         {1,0x26}, /* 19200 */
562         {0,0x26}, /* 38400 */
563         {1,0x38}, /* 57600 */
564         {0,0x38}, /* 115200 */
565 };
566 #else
567  hw_baud_table[18] = {
568                  {0,0}, /* 0 */
569                  {0,0}, /* 50 */
570                  {0,0}, /* 75 */
571                  {0,0}, /* 110 */
572                  {0,0}, /* 134 */
573                  {0,0}, /* 150 */
574                  {0,0}, /* 200 */
575                  {0,0}, /* 300 */
576                  {7,0x26}, /* 600 */
577                  {6,0x26}, /* 1200 */
578                  {0,0}, /* 1800 */
579                  {5,0x26}, /* 2400 */
580                  {4,0x26}, /* 4800 */
581                  {3,0x26}, /* 9600 */
582                  {2,0x26}, /* 19200 */
583                  {1,0x26}, /* 38400 */
584                  {0,0x26}, /* 57600 */
585                  {1,0x38}, /* 115200 */
586 }; 
587 #endif
588 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
589
590 /*
591  * This routine is called to set the UART divisor registers to match
592  * the specified baud rate for a serial port.
593  */
594 static void change_speed(struct m68k_serial *info)
595 {
596         m68328_uart *uart = &uart_addr[info->line];
597         unsigned short port;
598         unsigned short ustcnt;
599         unsigned cflag;
600         int     i;
601
602         if (!info->tty || !info->tty->termios)
603                 return;
604         cflag = info->tty->termios->c_cflag;
605         if (!(port = info->port))
606                 return;
607
608         ustcnt = uart->ustcnt;
609         uart->ustcnt = ustcnt & ~USTCNT_TXEN;
610
611         i = cflag & CBAUD;
612         if (i & CBAUDEX) {
613                 i = (i & ~CBAUDEX) + B38400;
614         }
615
616         info->baud = baud_table[i];
617         uart->ubaud = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) | 
618                 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
619
620         ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
621         
622         if ((cflag & CSIZE) == CS8)
623                 ustcnt |= USTCNT_8_7;
624                 
625         if (cflag & CSTOPB)
626                 ustcnt |= USTCNT_STOP;
627
628         if (cflag & PARENB)
629                 ustcnt |= USTCNT_PARITYEN;
630         if (cflag & PARODD)
631                 ustcnt |= USTCNT_ODD_EVEN;
632         
633 #ifdef CONFIG_SERIAL_68328_RTS_CTS
634         if (cflag & CRTSCTS) {
635                 uart->utx.w &= ~ UTX_NOCTS;
636         } else {
637                 uart->utx.w |= UTX_NOCTS;
638         }
639 #endif
640
641         ustcnt |= USTCNT_TXEN;
642         
643         uart->ustcnt = ustcnt;
644         return;
645 }
646
647 /*
648  * Fair output driver allows a process to speak.
649  */
650 static void rs_fair_output(void)
651 {
652         int left;               /* Output no more than that */
653         unsigned long flags;
654         struct m68k_serial *info = &m68k_soft[0];
655         char c;
656
657         if (info == 0) return;
658         if (info->xmit_buf == 0) return;
659
660         save_flags(flags);  cli();
661         left = info->xmit_cnt;
662         while (left != 0) {
663                 c = info->xmit_buf[info->xmit_tail];
664                 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
665                 info->xmit_cnt--;
666                 restore_flags(flags);
667
668                 rs_put_char(c);
669
670                 save_flags(flags);  cli();
671                 left = min(info->xmit_cnt, left-1);
672         }
673
674         /* Last character is being transmitted now (hopefully). */
675         udelay(5);
676
677         restore_flags(flags);
678         return;
679 }
680
681 /*
682  * m68k_console_print is registered for printk.
683  */
684 void console_print_68328(const char *p)
685 {
686         char c;
687         
688         while((c=*(p++)) != 0) {
689                 if(c == '\n')
690                         rs_put_char('\r');
691                 rs_put_char(c);
692         }
693
694         /* Comment this if you want to have a strict interrupt-driven output */
695         rs_fair_output();
696
697         return;
698 }
699
700 static void rs_set_ldisc(struct tty_struct *tty)
701 {
702         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
703
704         if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
705                 return;
706
707         info->is_cons = (tty->termios->c_line == N_TTY);
708         
709         printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
710 }
711
712 static void rs_flush_chars(struct tty_struct *tty)
713 {
714         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
715         m68328_uart *uart = &uart_addr[info->line];
716         unsigned long flags;
717
718         if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
719                 return;
720 #ifndef USE_INTS
721         for(;;) {
722 #endif
723
724         /* Enable transmitter */
725         save_flags(flags); cli();
726
727         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
728                         !info->xmit_buf) {
729                 restore_flags(flags);
730                 return;
731         }
732
733 #ifdef USE_INTS
734         uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
735 #else
736         uart->ustcnt |= USTCNT_TXEN;
737 #endif
738
739 #ifdef USE_INTS
740         if (uart->utx.w & UTX_TX_AVAIL) {
741 #else
742         if (1) {
743 #endif
744                 /* Send char */
745                 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
746                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
747                 info->xmit_cnt--;
748         }
749
750 #ifndef USE_INTS
751         while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
752         }
753 #endif
754         restore_flags(flags);
755 }
756
757 extern void console_printn(const char * b, int count);
758
759 static int rs_write(struct tty_struct * tty,
760                     const unsigned char *buf, int count)
761 {
762         int     c, total = 0;
763         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
764         m68328_uart *uart = &uart_addr[info->line];
765         unsigned long flags;
766
767         if (serial_paranoia_check(info, tty->name, "rs_write"))
768                 return 0;
769
770         if (!tty || !info->xmit_buf)
771                 return 0;
772
773         save_flags(flags);
774         while (1) {
775                 cli();          
776                 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
777                                    SERIAL_XMIT_SIZE - info->xmit_head));
778                 if (c <= 0)
779                         break;
780
781                 memcpy(info->xmit_buf + info->xmit_head, buf, c);
782                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
783                 info->xmit_cnt += c;
784                 restore_flags(flags);
785                 buf += c;
786                 count -= c;
787                 total += c;
788         }
789
790         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
791                 /* Enable transmitter */
792                 cli();          
793 #ifndef USE_INTS
794                 while(info->xmit_cnt) {
795 #endif
796
797                 uart->ustcnt |= USTCNT_TXEN;
798 #ifdef USE_INTS
799                 uart->ustcnt |= USTCNT_TX_INTR_MASK;
800 #else
801                 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
802 #endif
803                 if (uart->utx.w & UTX_TX_AVAIL) {
804                         uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
805                         info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
806                         info->xmit_cnt--;
807                 }
808
809 #ifndef USE_INTS
810                 }
811 #endif
812                 restore_flags(flags);
813         }
814         restore_flags(flags);
815         return total;
816 }
817
818 static int rs_write_room(struct tty_struct *tty)
819 {
820         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
821         int     ret;
822                                 
823         if (serial_paranoia_check(info, tty->name, "rs_write_room"))
824                 return 0;
825         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
826         if (ret < 0)
827                 ret = 0;
828         return ret;
829 }
830
831 static int rs_chars_in_buffer(struct tty_struct *tty)
832 {
833         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
834                                 
835         if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
836                 return 0;
837         return info->xmit_cnt;
838 }
839
840 static void rs_flush_buffer(struct tty_struct *tty)
841 {
842         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
843                                 
844         if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
845                 return;
846         cli();
847         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
848         sti();
849         tty_wakeup(tty);
850 }
851
852 /*
853  * ------------------------------------------------------------
854  * rs_throttle()
855  * 
856  * This routine is called by the upper-layer tty layer to signal that
857  * incoming characters should be throttled.
858  * ------------------------------------------------------------
859  */
860 static void rs_throttle(struct tty_struct * tty)
861 {
862         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
863
864         if (serial_paranoia_check(info, tty->name, "rs_throttle"))
865                 return;
866         
867         if (I_IXOFF(tty))
868                 info->x_char = STOP_CHAR(tty);
869
870         /* Turn off RTS line (do this atomic) */
871 }
872
873 static void rs_unthrottle(struct tty_struct * tty)
874 {
875         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
876
877         if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
878                 return;
879         
880         if (I_IXOFF(tty)) {
881                 if (info->x_char)
882                         info->x_char = 0;
883                 else
884                         info->x_char = START_CHAR(tty);
885         }
886
887         /* Assert RTS line (do this atomic) */
888 }
889
890 /*
891  * ------------------------------------------------------------
892  * rs_ioctl() and friends
893  * ------------------------------------------------------------
894  */
895
896 static int get_serial_info(struct m68k_serial * info,
897                            struct serial_struct * retinfo)
898 {
899         struct serial_struct tmp;
900   
901         if (!retinfo)
902                 return -EFAULT;
903         memset(&tmp, 0, sizeof(tmp));
904         tmp.type = info->type;
905         tmp.line = info->line;
906         tmp.port = info->port;
907         tmp.irq = info->irq;
908         tmp.flags = info->flags;
909         tmp.baud_base = info->baud_base;
910         tmp.close_delay = info->close_delay;
911         tmp.closing_wait = info->closing_wait;
912         tmp.custom_divisor = info->custom_divisor;
913         copy_to_user(retinfo,&tmp,sizeof(*retinfo));
914         return 0;
915 }
916
917 static int set_serial_info(struct m68k_serial * info,
918                            struct serial_struct * new_info)
919 {
920         struct serial_struct new_serial;
921         struct m68k_serial old_info;
922         int                     retval = 0;
923
924         if (!new_info)
925                 return -EFAULT;
926         copy_from_user(&new_serial,new_info,sizeof(new_serial));
927         old_info = *info;
928
929         if (!capable(CAP_SYS_ADMIN)) {
930                 if ((new_serial.baud_base != info->baud_base) ||
931                     (new_serial.type != info->type) ||
932                     (new_serial.close_delay != info->close_delay) ||
933                     ((new_serial.flags & ~S_USR_MASK) !=
934                      (info->flags & ~S_USR_MASK)))
935                         return -EPERM;
936                 info->flags = ((info->flags & ~S_USR_MASK) |
937                                (new_serial.flags & S_USR_MASK));
938                 info->custom_divisor = new_serial.custom_divisor;
939                 goto check_and_exit;
940         }
941
942         if (info->count > 1)
943                 return -EBUSY;
944
945         /*
946          * OK, past this point, all the error checking has been done.
947          * At this point, we start making changes.....
948          */
949
950         info->baud_base = new_serial.baud_base;
951         info->flags = ((info->flags & ~S_FLAGS) |
952                         (new_serial.flags & S_FLAGS));
953         info->type = new_serial.type;
954         info->close_delay = new_serial.close_delay;
955         info->closing_wait = new_serial.closing_wait;
956
957 check_and_exit:
958         retval = startup(info);
959         return retval;
960 }
961
962 /*
963  * get_lsr_info - get line status register info
964  *
965  * Purpose: Let user call ioctl() to get info when the UART physically
966  *          is emptied.  On bus types like RS485, the transmitter must
967  *          release the bus after transmitting. This must be done when
968  *          the transmit shift register is empty, not be done when the
969  *          transmit holding register is empty.  This functionality
970  *          allows an RS485 driver to be written in user space. 
971  */
972 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
973 {
974 #ifdef CONFIG_SERIAL_68328_RTS_CTS
975         m68328_uart *uart = &uart_addr[info->line];
976 #endif
977         unsigned char status;
978
979         cli();
980 #ifdef CONFIG_SERIAL_68328_RTS_CTS
981         status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
982 #else
983         status = 0;
984 #endif
985         sti();
986         put_user(status,value);
987         return 0;
988 }
989
990 /*
991  * This routine sends a break character out the serial port.
992  */
993 static void send_break(struct m68k_serial * info, unsigned int duration)
994 {
995         m68328_uart *uart = &uart_addr[info->line];
996         unsigned long flags;
997         if (!info->port)
998                 return;
999         save_flags(flags);
1000         cli();
1001 #ifdef USE_INTS 
1002         uart->utx.w |= UTX_SEND_BREAK;
1003         msleep_interruptible(duration);
1004         uart->utx.w &= ~UTX_SEND_BREAK;
1005 #endif          
1006         restore_flags(flags);
1007 }
1008
1009 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1010                     unsigned int cmd, unsigned long arg)
1011 {
1012         int error;
1013         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1014         int retval;
1015
1016         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1017                 return -ENODEV;
1018
1019         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1020             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1021             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1022                 if (tty->flags & (1 << TTY_IO_ERROR))
1023                     return -EIO;
1024         }
1025         
1026         switch (cmd) {
1027                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1028                         retval = tty_check_change(tty);
1029                         if (retval)
1030                                 return retval;
1031                         tty_wait_until_sent(tty, 0);
1032                         if (!arg)
1033                                 send_break(info, 250);  /* 1/4 second */
1034                         return 0;
1035                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1036                         retval = tty_check_change(tty);
1037                         if (retval)
1038                                 return retval;
1039                         tty_wait_until_sent(tty, 0);
1040                         send_break(info, arg ? arg*(100) : 250);
1041                         return 0;
1042                 case TIOCGSOFTCAR:
1043                         error = put_user(C_CLOCAL(tty) ? 1 : 0,
1044                                     (unsigned long *) arg);
1045                         if (error)
1046                                 return error;
1047                         return 0;
1048                 case TIOCSSOFTCAR:
1049                         get_user(arg, (unsigned long *) arg);
1050                         tty->termios->c_cflag =
1051                                 ((tty->termios->c_cflag & ~CLOCAL) |
1052                                  (arg ? CLOCAL : 0));
1053                         return 0;
1054                 case TIOCGSERIAL:
1055                         if (access_ok(VERIFY_WRITE, (void *) arg,
1056                                                 sizeof(struct serial_struct)))
1057                                 return get_serial_info(info,
1058                                                (struct serial_struct *) arg);
1059                         return -EFAULT;
1060                 case TIOCSSERIAL:
1061                         return set_serial_info(info,
1062                                                (struct serial_struct *) arg);
1063                 case TIOCSERGETLSR: /* Get line status register */
1064                         if (access_ok(VERIFY_WRITE, (void *) arg,
1065                                                 sizeof(unsigned int));
1066                                 return get_lsr_info(info, (unsigned int *) arg);
1067                         return -EFAULT;
1068                 case TIOCSERGSTRUCT:
1069                         if (!access_ok(VERIFY_WRITE, (void *) arg,
1070                                                 sizeof(struct m68k_serial)))
1071                                 return -EFAULT;
1072                         copy_to_user((struct m68k_serial *) arg,
1073                                     info, sizeof(struct m68k_serial));
1074                         return 0;
1075                         
1076                 default:
1077                         return -ENOIOCTLCMD;
1078                 }
1079         return 0;
1080 }
1081
1082 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1083 {
1084         struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1085
1086         if (tty->termios->c_cflag == old_termios->c_cflag)
1087                 return;
1088
1089         change_speed(info);
1090
1091         if ((old_termios->c_cflag & CRTSCTS) &&
1092             !(tty->termios->c_cflag & CRTSCTS)) {
1093                 tty->hw_stopped = 0;
1094                 rs_start(tty);
1095         }
1096         
1097 }
1098
1099 /*
1100  * ------------------------------------------------------------
1101  * rs_close()
1102  * 
1103  * This routine is called when the serial port gets closed.  First, we
1104  * wait for the last remaining data to be sent.  Then, we unlink its
1105  * S structure from the interrupt chain if necessary, and we free
1106  * that IRQ if nothing is left in the chain.
1107  * ------------------------------------------------------------
1108  */
1109 static void rs_close(struct tty_struct *tty, struct file * filp)
1110 {
1111         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1112         m68328_uart *uart = &uart_addr[info->line];
1113         unsigned long flags;
1114
1115         if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1116                 return;
1117         
1118         save_flags(flags); cli();
1119         
1120         if (tty_hung_up_p(filp)) {
1121                 restore_flags(flags);
1122                 return;
1123         }
1124         
1125         if ((tty->count == 1) && (info->count != 1)) {
1126                 /*
1127                  * Uh, oh.  tty->count is 1, which means that the tty
1128                  * structure will be freed.  Info->count should always
1129                  * be one in these conditions.  If it's greater than
1130                  * one, we've got real problems, since it means the
1131                  * serial port won't be shutdown.
1132                  */
1133                 printk("rs_close: bad serial port count; tty->count is 1, "
1134                        "info->count is %d\n", info->count);
1135                 info->count = 1;
1136         }
1137         if (--info->count < 0) {
1138                 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1139                        info->line, info->count);
1140                 info->count = 0;
1141         }
1142         if (info->count) {
1143                 restore_flags(flags);
1144                 return;
1145         }
1146         info->flags |= S_CLOSING;
1147         /*
1148          * Now we wait for the transmit buffer to clear; and we notify 
1149          * the line discipline to only process XON/XOFF characters.
1150          */
1151         tty->closing = 1;
1152         if (info->closing_wait != S_CLOSING_WAIT_NONE)
1153                 tty_wait_until_sent(tty, info->closing_wait);
1154         /*
1155          * At this point we stop accepting input.  To do this, we
1156          * disable the receive line status interrupts, and tell the
1157          * interrupt driver to stop checking the data ready bit in the
1158          * line status register.
1159          */
1160
1161         uart->ustcnt &= ~USTCNT_RXEN;
1162         uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1163
1164         shutdown(info);
1165         if (tty->driver->flush_buffer)
1166                 tty->driver->flush_buffer(tty);
1167                 
1168         tty_ldisc_flush(tty);
1169         tty->closing = 0;
1170         info->event = 0;
1171         info->tty = 0;
1172 #warning "This is not and has never been valid so fix it"       
1173 #if 0
1174         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1175                 if (tty->ldisc.close)
1176                         (tty->ldisc.close)(tty);
1177                 tty->ldisc = ldiscs[N_TTY];
1178                 tty->termios->c_line = N_TTY;
1179                 if (tty->ldisc.open)
1180                         (tty->ldisc.open)(tty);
1181         }
1182 #endif  
1183         if (info->blocked_open) {
1184                 if (info->close_delay) {
1185                         msleep_interruptible(jiffies_to_msecs(info->close_delay));
1186                 }
1187                 wake_up_interruptible(&info->open_wait);
1188         }
1189         info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1190         wake_up_interruptible(&info->close_wait);
1191         restore_flags(flags);
1192 }
1193
1194 /*
1195  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1196  */
1197 void rs_hangup(struct tty_struct *tty)
1198 {
1199         struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1200         
1201         if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1202                 return;
1203         
1204         rs_flush_buffer(tty);
1205         shutdown(info);
1206         info->event = 0;
1207         info->count = 0;
1208         info->flags &= ~S_NORMAL_ACTIVE;
1209         info->tty = 0;
1210         wake_up_interruptible(&info->open_wait);
1211 }
1212
1213 /*
1214  * ------------------------------------------------------------
1215  * rs_open() and friends
1216  * ------------------------------------------------------------
1217  */
1218 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1219                            struct m68k_serial *info)
1220 {
1221         DECLARE_WAITQUEUE(wait, current);
1222         int             retval;
1223         int             do_clocal = 0;
1224
1225         /*
1226          * If the device is in the middle of being closed, then block
1227          * until it's done, and then try again.
1228          */
1229         if (info->flags & S_CLOSING) {
1230                 interruptible_sleep_on(&info->close_wait);
1231 #ifdef SERIAL_DO_RESTART
1232                 if (info->flags & S_HUP_NOTIFY)
1233                         return -EAGAIN;
1234                 else
1235                         return -ERESTARTSYS;
1236 #else
1237                 return -EAGAIN;
1238 #endif
1239         }
1240         
1241         /*
1242          * If non-blocking mode is set, or the port is not enabled,
1243          * then make the check up front and then exit.
1244          */
1245         if ((filp->f_flags & O_NONBLOCK) ||
1246             (tty->flags & (1 << TTY_IO_ERROR))) {
1247                 info->flags |= S_NORMAL_ACTIVE;
1248                 return 0;
1249         }
1250
1251         if (tty->termios->c_cflag & CLOCAL)
1252                 do_clocal = 1;
1253
1254         /*
1255          * Block waiting for the carrier detect and the line to become
1256          * free (i.e., not in use by the callout).  While we are in
1257          * this loop, info->count is dropped by one, so that
1258          * rs_close() knows when to free things.  We restore it upon
1259          * exit, either normal or abnormal.
1260          */
1261         retval = 0;
1262         add_wait_queue(&info->open_wait, &wait);
1263
1264         info->count--;
1265         info->blocked_open++;
1266         while (1) {
1267                 cli();
1268                 m68k_rtsdtr(info, 1);
1269                 sti();
1270                 current->state = TASK_INTERRUPTIBLE;
1271                 if (tty_hung_up_p(filp) ||
1272                     !(info->flags & S_INITIALIZED)) {
1273 #ifdef SERIAL_DO_RESTART
1274                         if (info->flags & S_HUP_NOTIFY)
1275                                 retval = -EAGAIN;
1276                         else
1277                                 retval = -ERESTARTSYS;  
1278 #else
1279                         retval = -EAGAIN;
1280 #endif
1281                         break;
1282                 }
1283                 if (!(info->flags & S_CLOSING) && do_clocal)
1284                         break;
1285                 if (signal_pending(current)) {
1286                         retval = -ERESTARTSYS;
1287                         break;
1288                 }
1289                 schedule();
1290         }
1291         current->state = TASK_RUNNING;
1292         remove_wait_queue(&info->open_wait, &wait);
1293         if (!tty_hung_up_p(filp))
1294                 info->count++;
1295         info->blocked_open--;
1296
1297         if (retval)
1298                 return retval;
1299         info->flags |= S_NORMAL_ACTIVE;
1300         return 0;
1301 }       
1302
1303 /*
1304  * This routine is called whenever a serial port is opened.  It
1305  * enables interrupts for a serial port, linking in its S structure into
1306  * the IRQ chain.   It also performs the serial-specific
1307  * initialization for the tty structure.
1308  */
1309 int rs_open(struct tty_struct *tty, struct file * filp)
1310 {
1311         struct m68k_serial      *info;
1312         int                     retval, line;
1313
1314         line = tty->index;
1315         
1316         if (line >= NR_PORTS || line < 0) /* we have exactly one */
1317                 return -ENODEV;
1318
1319         info = &m68k_soft[line];
1320
1321         if (serial_paranoia_check(info, tty->name, "rs_open"))
1322                 return -ENODEV;
1323
1324         info->count++;
1325         tty->driver_data = info;
1326         info->tty = tty;
1327
1328         /*
1329          * Start up serial port
1330          */
1331         retval = startup(info);
1332         if (retval)
1333                 return retval;
1334
1335         return block_til_ready(tty, filp, info);
1336 }
1337
1338 /* Finally, routines used to initialize the serial driver. */
1339
1340 static void show_serial_version(void)
1341 {
1342         printk("MC68328 serial driver version 1.00\n");
1343 }
1344
1345 #ifdef CONFIG_PM_LEGACY
1346 /* Serial Power management
1347  *  The console (currently fixed at line 0) is a special case for power
1348  *  management because the kernel is so chatty. The console will be 
1349  *  explicitly disabled my our power manager as the last minute, so we won't
1350  *  mess with it here.
1351  */
1352 static struct pm_dev *serial_pm[NR_PORTS];
1353
1354 static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
1355 {
1356         struct m68k_serial *info = (struct m68k_serial *)dev->data;
1357
1358         if(info == NULL)
1359                 return -1;
1360
1361         /* special case for line 0 - pm restores it */
1362         if(info->line == 0)
1363                 return 0; 
1364
1365         switch (request) {
1366         case PM_SUSPEND:
1367                 shutdown(info);
1368                 break;
1369
1370         case PM_RESUME:
1371                 startup(info);
1372                 break;
1373         }
1374         return 0;
1375 }
1376
1377 void shutdown_console(void)
1378 {
1379         struct m68k_serial *info = &m68k_soft[0];
1380
1381         /* HACK: wait a bit for any pending printk's to be dumped */
1382         {
1383                 int i = 10000;
1384                 while(i--);
1385         }
1386
1387         shutdown(info);
1388 }
1389
1390 void startup_console(void)
1391 {
1392         struct m68k_serial *info = &m68k_soft[0];
1393         startup(info);
1394 }
1395 #endif /* CONFIG_PM_LEGACY */
1396
1397
1398 static struct tty_operations rs_ops = {
1399         .open = rs_open,
1400         .close = rs_close,
1401         .write = rs_write,
1402         .flush_chars = rs_flush_chars,
1403         .write_room = rs_write_room,
1404         .chars_in_buffer = rs_chars_in_buffer,
1405         .flush_buffer = rs_flush_buffer,
1406         .ioctl = rs_ioctl,
1407         .throttle = rs_throttle,
1408         .unthrottle = rs_unthrottle,
1409         .set_termios = rs_set_termios,
1410         .stop = rs_stop,
1411         .start = rs_start,
1412         .hangup = rs_hangup,
1413         .set_ldisc = rs_set_ldisc,
1414 };
1415
1416 /* rs_init inits the driver */
1417 static int __init
1418 rs68328_init(void)
1419 {
1420         int flags, i;
1421         struct m68k_serial *info;
1422
1423         serial_driver = alloc_tty_driver(NR_PORTS);
1424         if (!serial_driver)
1425                 return -ENOMEM;
1426
1427         show_serial_version();
1428
1429         /* Initialize the tty_driver structure */
1430         /* SPARC: Not all of this is exactly right for us. */
1431         
1432         serial_driver->name = "ttyS";
1433         serial_driver->major = TTY_MAJOR;
1434         serial_driver->minor_start = 64;
1435         serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1436         serial_driver->subtype = SERIAL_TYPE_NORMAL;
1437         serial_driver->init_termios = tty_std_termios;
1438         serial_driver->init_termios.c_cflag = 
1439                         m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1440         serial_driver->flags = TTY_DRIVER_REAL_RAW;
1441         tty_set_operations(serial_driver, &rs_ops);
1442
1443         if (tty_register_driver(serial_driver)) {
1444                 put_tty_driver(serial_driver);
1445                 printk(KERN_ERR "Couldn't register serial driver\n");
1446                 return -ENOMEM;
1447         }
1448
1449         save_flags(flags); cli();
1450
1451         for(i=0;i<NR_PORTS;i++) {
1452
1453             info = &m68k_soft[i];
1454             info->magic = SERIAL_MAGIC;
1455             info->port = (int) &uart_addr[i];
1456             info->tty = 0;
1457             info->irq = uart_irqs[i];
1458             info->custom_divisor = 16;
1459             info->close_delay = 50;
1460             info->closing_wait = 3000;
1461             info->x_char = 0;
1462             info->event = 0;
1463             info->count = 0;
1464             info->blocked_open = 0;
1465             INIT_WORK(&info->tqueue, do_softint, info);
1466             INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1467             init_waitqueue_head(&info->open_wait);
1468             init_waitqueue_head(&info->close_wait);
1469             info->line = i;
1470             info->is_cons = 1; /* Means shortcuts work */
1471             
1472             printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line, 
1473                    info->port, info->irq);
1474             printk(" is a builtin MC68328 UART\n");
1475             
1476             IRQ_ports[info->irq] = info;        /* waste of space */
1477
1478 #ifdef CONFIG_M68VZ328
1479                 if (i > 0 )
1480                         PJSEL &= 0xCF;  /* PSW enable second port output */
1481 #endif
1482
1483             if (request_irq(uart_irqs[i],
1484                             rs_interrupt,
1485                             IRQ_FLG_STD,
1486                             "M68328_UART", NULL))
1487                 panic("Unable to attach 68328 serial interrupt\n");
1488 #ifdef CONFIG_PM_LEGACY
1489             serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
1490             if (serial_pm[i])
1491                     serial_pm[i]->data = info;
1492 #endif
1493         }
1494         restore_flags(flags);
1495         return 0;
1496 }
1497
1498 module_init(rs68328_init);
1499
1500
1501
1502 static void m68328_set_baud(void)
1503 {
1504         unsigned short ustcnt;
1505         int     i;
1506
1507         ustcnt = USTCNT;
1508         USTCNT = ustcnt & ~USTCNT_TXEN;
1509
1510 again:
1511         for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1512                 if (baud_table[i] == m68328_console_baud)
1513                         break;
1514         if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1515                 m68328_console_baud = 9600;
1516                 goto again;
1517         }
1518
1519         UBAUD = PUT_FIELD(UBAUD_DIVIDE,    hw_baud_table[i].divisor) | 
1520                 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1521         ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1522         ustcnt |= USTCNT_8_7;
1523         ustcnt |= USTCNT_TXEN;
1524         USTCNT = ustcnt;
1525         m68328_console_initted = 1;
1526         return;
1527 }
1528
1529
1530 int m68328_console_setup(struct console *cp, char *arg)
1531 {
1532         int             i, n = CONSOLE_BAUD_RATE;
1533
1534         if (!cp)
1535                 return(-1);
1536
1537         if (arg)
1538                 n = simple_strtoul(arg,NULL,0);
1539
1540         for (i = 0; i < BAUD_TABLE_SIZE; i++)
1541                 if (baud_table[i] == n)
1542                         break;
1543         if (i < BAUD_TABLE_SIZE) {
1544                 m68328_console_baud = n;
1545                 m68328_console_cbaud = 0;
1546                 if (i > 15) {
1547                         m68328_console_cbaud |= CBAUDEX;
1548                         i -= 15;
1549                 }
1550                 m68328_console_cbaud |= i;
1551         }
1552
1553         m68328_set_baud(); /* make sure baud rate changes */
1554         return(0);
1555 }
1556
1557
1558 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1559 {
1560         *index = c->index;
1561         return serial_driver;
1562 }
1563
1564
1565 void m68328_console_write (struct console *co, const char *str,
1566                            unsigned int count)
1567 {
1568         if (!m68328_console_initted)
1569                 m68328_set_baud();
1570     while (count--) {
1571         if (*str == '\n')
1572            rs_put_char('\r');
1573         rs_put_char( *str++ );
1574     }
1575 }
1576
1577
1578 static struct console m68328_driver = {
1579         .name           = "ttyS",
1580         .write          = m68328_console_write,
1581         .device         = m68328_console_device,
1582         .setup          = m68328_console_setup,
1583         .flags          = CON_PRINTBUFFER,
1584         .index          = -1,
1585 };
1586
1587
1588 static int __init m68328_console_init(void)
1589 {
1590         register_console(&m68328_driver);
1591         return 0;
1592 }
1593
1594 console_initcall(m68328_console_init);