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