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