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