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