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