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