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