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