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