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