blob: 993a702422ec6e2e22175ea67e6c9bacb55ab093 [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
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800278static void receive_chars(struct m68k_serial *info, struct pt_regs *regs,
279 unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct tty_struct *tty = info->tty;
282 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800283 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /*
286 * This do { } while() loop will get ALL chars out of Rx FIFO
287 */
288#ifndef CONFIG_XCOPILOT_BUGS
289 do {
290#endif
291 ch = GET_FIELD(rx, URX_RXDATA);
292
293 if(info->is_cons) {
294 if(URX_BREAK & rx) { /* whee, break received */
295 status_handle(info, rx);
296 return;
297#ifdef CONFIG_MAGIC_SYSRQ
298 } else if (ch == 0x10) { /* ^P */
299 show_state();
300 show_free_areas();
301 show_buffers();
302/* show_net_buffers(); */
303 return;
304 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600305 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return;
307#endif /* CONFIG_MAGIC_SYSRQ */
308 }
309 /* It is a 'keyboard interrupt' ;-) */
310#ifdef CONFIG_CONSOLE
311 wake_up(&keypress_wait);
312#endif
313 }
314
315 if(!tty)
316 goto clear_and_exit;
317
Alan Cox33f0f882006-01-09 20:54:13 -0800318 flag = TTY_NORMAL;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800321 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 status_handle(info, rx);
323 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800324 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 status_handle(info, rx);
326 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800327 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Alan Cox33f0f882006-01-09 20:54:13 -0800330 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#ifndef CONFIG_XCOPILOT_BUGS
332 } while((rx = uart->urx.w) & URX_DATA_READY);
333#endif
334
Greg Ungerer8e63e662006-02-08 09:19:17 +1000335 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337clear_and_exit:
338 return;
339}
340
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800341static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 m68328_uart *uart = &uart_addr[info->line];
344
345 if (info->x_char) {
346 /* Send next char */
347 uart->utx.b.txdata = info->x_char;
348 info->x_char = 0;
349 goto clear_and_return;
350 }
351
352 if((info->xmit_cnt <= 0) || info->tty->stopped) {
353 /* That's peculiar... TX ints off */
354 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
355 goto clear_and_return;
356 }
357
358 /* Send char */
359 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
360 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
361 info->xmit_cnt--;
362
363 if (info->xmit_cnt < WAKEUP_CHARS)
364 schedule_work(&info->tqueue);
365
366 if(info->xmit_cnt <= 0) {
367 /* All done for now... TX ints off */
368 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
369 goto clear_and_return;
370 }
371
372clear_and_return:
373 /* Clear interrupt (should be auto)*/
374 return;
375}
376
377/*
378 * This is the serial driver's generic interrupt routine
379 */
380irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
381{
382 struct m68k_serial * info;
383 m68328_uart *uart;
384 unsigned short rx;
385 unsigned short tx;
386
387 info = IRQ_ports[irq];
388 if(!info)
389 return IRQ_NONE;
390
391 uart = &uart_addr[info->line];
392 rx = uart->urx.w;
393
394#ifdef USE_INTS
395 tx = uart->utx.w;
396
397 if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
398 if (tx & UTX_TX_AVAIL) transmit_chars(info);
399#else
400 receive_chars(info, regs, rx);
401#endif
402 return IRQ_HANDLED;
403}
404
405static void do_softint(void *private)
406{
407 struct m68k_serial *info = (struct m68k_serial *) private;
408 struct tty_struct *tty;
409
410 tty = info->tty;
411 if (!tty)
412 return;
413#if 0
414 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
415 tty_wakeup(tty);
416 }
417#endif
418}
419
420/*
421 * This routine is called from the scheduler tqueue when the interrupt
422 * routine has signalled that a hangup has occurred. The path of
423 * hangup processing is:
424 *
425 * serial interrupt routine -> (scheduler tqueue) ->
426 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
427 *
428 */
429static void do_serial_hangup(void *private)
430{
431 struct m68k_serial *info = (struct m68k_serial *) private;
432 struct tty_struct *tty;
433
434 tty = info->tty;
435 if (!tty)
436 return;
437
438 tty_hangup(tty);
439}
440
441
442static int startup(struct m68k_serial * info)
443{
444 m68328_uart *uart = &uart_addr[info->line];
445 unsigned long flags;
446
447 if (info->flags & S_INITIALIZED)
448 return 0;
449
450 if (!info->xmit_buf) {
451 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
452 if (!info->xmit_buf)
453 return -ENOMEM;
454 }
455
Greg Ungerer727dda82006-06-28 15:54:22 +1000456 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /*
459 * Clear the FIFO buffers and disable them
460 * (they will be reenabled in change_speed())
461 */
462
463 uart->ustcnt = USTCNT_UEN;
464 info->xmit_fifo_size = 1;
465 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
466 (void)uart->urx.w;
467
468 /*
469 * Finally, enable sequencing and interrupts
470 */
471#ifdef USE_INTS
472 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
473 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
474#else
475 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
476#endif
477
478 if (info->tty)
479 clear_bit(TTY_IO_ERROR, &info->tty->flags);
480 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
481
482 /*
483 * and set the speed of the serial port
484 */
485
486 change_speed(info);
487
488 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000489 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491}
492
493/*
494 * This routine will shutdown a serial port; interrupts are disabled, and
495 * DTR is dropped if the hangup on close termio flag is on.
496 */
497static void shutdown(struct m68k_serial * info)
498{
499 m68328_uart *uart = &uart_addr[info->line];
500 unsigned long flags;
501
502 uart->ustcnt = 0; /* All off! */
503 if (!(info->flags & S_INITIALIZED))
504 return;
505
Greg Ungerer727dda82006-06-28 15:54:22 +1000506 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (info->xmit_buf) {
509 free_page((unsigned long) info->xmit_buf);
510 info->xmit_buf = 0;
511 }
512
513 if (info->tty)
514 set_bit(TTY_IO_ERROR, &info->tty->flags);
515
516 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000517 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520struct {
521 int divisor, prescale;
522}
523#ifndef CONFIG_M68VZ328
524 hw_baud_table[18] = {
525 {0,0}, /* 0 */
526 {0,0}, /* 50 */
527 {0,0}, /* 75 */
528 {0,0}, /* 110 */
529 {0,0}, /* 134 */
530 {0,0}, /* 150 */
531 {0,0}, /* 200 */
532 {7,0x26}, /* 300 */
533 {6,0x26}, /* 600 */
534 {5,0x26}, /* 1200 */
535 {0,0}, /* 1800 */
536 {4,0x26}, /* 2400 */
537 {3,0x26}, /* 4800 */
538 {2,0x26}, /* 9600 */
539 {1,0x26}, /* 19200 */
540 {0,0x26}, /* 38400 */
541 {1,0x38}, /* 57600 */
542 {0,0x38}, /* 115200 */
543};
544#else
545 hw_baud_table[18] = {
546 {0,0}, /* 0 */
547 {0,0}, /* 50 */
548 {0,0}, /* 75 */
549 {0,0}, /* 110 */
550 {0,0}, /* 134 */
551 {0,0}, /* 150 */
552 {0,0}, /* 200 */
553 {0,0}, /* 300 */
554 {7,0x26}, /* 600 */
555 {6,0x26}, /* 1200 */
556 {0,0}, /* 1800 */
557 {5,0x26}, /* 2400 */
558 {4,0x26}, /* 4800 */
559 {3,0x26}, /* 9600 */
560 {2,0x26}, /* 19200 */
561 {1,0x26}, /* 38400 */
562 {0,0x26}, /* 57600 */
563 {1,0x38}, /* 115200 */
564};
565#endif
566/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
567
568/*
569 * This routine is called to set the UART divisor registers to match
570 * the specified baud rate for a serial port.
571 */
572static void change_speed(struct m68k_serial *info)
573{
574 m68328_uart *uart = &uart_addr[info->line];
575 unsigned short port;
576 unsigned short ustcnt;
577 unsigned cflag;
578 int i;
579
580 if (!info->tty || !info->tty->termios)
581 return;
582 cflag = info->tty->termios->c_cflag;
583 if (!(port = info->port))
584 return;
585
586 ustcnt = uart->ustcnt;
587 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
588
589 i = cflag & CBAUD;
590 if (i & CBAUDEX) {
591 i = (i & ~CBAUDEX) + B38400;
592 }
593
594 info->baud = baud_table[i];
595 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
596 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
597
598 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
599
600 if ((cflag & CSIZE) == CS8)
601 ustcnt |= USTCNT_8_7;
602
603 if (cflag & CSTOPB)
604 ustcnt |= USTCNT_STOP;
605
606 if (cflag & PARENB)
607 ustcnt |= USTCNT_PARITYEN;
608 if (cflag & PARODD)
609 ustcnt |= USTCNT_ODD_EVEN;
610
611#ifdef CONFIG_SERIAL_68328_RTS_CTS
612 if (cflag & CRTSCTS) {
613 uart->utx.w &= ~ UTX_NOCTS;
614 } else {
615 uart->utx.w |= UTX_NOCTS;
616 }
617#endif
618
619 ustcnt |= USTCNT_TXEN;
620
621 uart->ustcnt = ustcnt;
622 return;
623}
624
625/*
626 * Fair output driver allows a process to speak.
627 */
628static void rs_fair_output(void)
629{
630 int left; /* Output no more than that */
631 unsigned long flags;
632 struct m68k_serial *info = &m68k_soft[0];
633 char c;
634
635 if (info == 0) return;
636 if (info->xmit_buf == 0) return;
637
Greg Ungerer727dda82006-06-28 15:54:22 +1000638 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 left = info->xmit_cnt;
640 while (left != 0) {
641 c = info->xmit_buf[info->xmit_tail];
642 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
643 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000644 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 rs_put_char(c);
647
Greg Ungerer727dda82006-06-28 15:54:22 +1000648 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 left = min(info->xmit_cnt, left-1);
650 }
651
652 /* Last character is being transmitted now (hopefully). */
653 udelay(5);
654
Greg Ungerer727dda82006-06-28 15:54:22 +1000655 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657}
658
659/*
660 * m68k_console_print is registered for printk.
661 */
662void console_print_68328(const char *p)
663{
664 char c;
665
666 while((c=*(p++)) != 0) {
667 if(c == '\n')
668 rs_put_char('\r');
669 rs_put_char(c);
670 }
671
672 /* Comment this if you want to have a strict interrupt-driven output */
673 rs_fair_output();
674
675 return;
676}
677
678static void rs_set_ldisc(struct tty_struct *tty)
679{
680 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
681
682 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
683 return;
684
685 info->is_cons = (tty->termios->c_line == N_TTY);
686
687 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
688}
689
690static void rs_flush_chars(struct tty_struct *tty)
691{
692 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
693 m68328_uart *uart = &uart_addr[info->line];
694 unsigned long flags;
695
696 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
697 return;
698#ifndef USE_INTS
699 for(;;) {
700#endif
701
702 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000703 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
706 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000707 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return;
709 }
710
711#ifdef USE_INTS
712 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
713#else
714 uart->ustcnt |= USTCNT_TXEN;
715#endif
716
717#ifdef USE_INTS
718 if (uart->utx.w & UTX_TX_AVAIL) {
719#else
720 if (1) {
721#endif
722 /* Send char */
723 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
724 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
725 info->xmit_cnt--;
726 }
727
728#ifndef USE_INTS
729 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
730 }
731#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000732 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735extern void console_printn(const char * b, int count);
736
737static int rs_write(struct tty_struct * tty,
738 const unsigned char *buf, int count)
739{
740 int c, total = 0;
741 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
742 m68328_uart *uart = &uart_addr[info->line];
743 unsigned long flags;
744
745 if (serial_paranoia_check(info, tty->name, "rs_write"))
746 return 0;
747
748 if (!tty || !info->xmit_buf)
749 return 0;
750
Greg Ungerer727dda82006-06-28 15:54:22 +1000751 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000753 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
755 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000756 local_irq_restore(flags);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (c <= 0)
759 break;
760
761 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000762
763 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
765 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000766 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 buf += c;
768 count -= c;
769 total += c;
770 }
771
772 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
773 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000774 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#ifndef USE_INTS
776 while(info->xmit_cnt) {
777#endif
778
779 uart->ustcnt |= USTCNT_TXEN;
780#ifdef USE_INTS
781 uart->ustcnt |= USTCNT_TX_INTR_MASK;
782#else
783 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
784#endif
785 if (uart->utx.w & UTX_TX_AVAIL) {
786 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
787 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
788 info->xmit_cnt--;
789 }
790
791#ifndef USE_INTS
792 }
793#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000794 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return total;
798}
799
800static int rs_write_room(struct tty_struct *tty)
801{
802 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
803 int ret;
804
805 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
806 return 0;
807 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
808 if (ret < 0)
809 ret = 0;
810 return ret;
811}
812
813static int rs_chars_in_buffer(struct tty_struct *tty)
814{
815 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
816
817 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
818 return 0;
819 return info->xmit_cnt;
820}
821
822static void rs_flush_buffer(struct tty_struct *tty)
823{
824 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000825 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
828 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000829 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000831 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tty_wakeup(tty);
833}
834
835/*
836 * ------------------------------------------------------------
837 * rs_throttle()
838 *
839 * This routine is called by the upper-layer tty layer to signal that
840 * incoming characters should be throttled.
841 * ------------------------------------------------------------
842 */
843static void rs_throttle(struct tty_struct * tty)
844{
845 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
846
847 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
848 return;
849
850 if (I_IXOFF(tty))
851 info->x_char = STOP_CHAR(tty);
852
853 /* Turn off RTS line (do this atomic) */
854}
855
856static void rs_unthrottle(struct tty_struct * tty)
857{
858 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
859
860 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
861 return;
862
863 if (I_IXOFF(tty)) {
864 if (info->x_char)
865 info->x_char = 0;
866 else
867 info->x_char = START_CHAR(tty);
868 }
869
870 /* Assert RTS line (do this atomic) */
871}
872
873/*
874 * ------------------------------------------------------------
875 * rs_ioctl() and friends
876 * ------------------------------------------------------------
877 */
878
879static int get_serial_info(struct m68k_serial * info,
880 struct serial_struct * retinfo)
881{
882 struct serial_struct tmp;
883
884 if (!retinfo)
885 return -EFAULT;
886 memset(&tmp, 0, sizeof(tmp));
887 tmp.type = info->type;
888 tmp.line = info->line;
889 tmp.port = info->port;
890 tmp.irq = info->irq;
891 tmp.flags = info->flags;
892 tmp.baud_base = info->baud_base;
893 tmp.close_delay = info->close_delay;
894 tmp.closing_wait = info->closing_wait;
895 tmp.custom_divisor = info->custom_divisor;
896 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
897 return 0;
898}
899
900static int set_serial_info(struct m68k_serial * info,
901 struct serial_struct * new_info)
902{
903 struct serial_struct new_serial;
904 struct m68k_serial old_info;
905 int retval = 0;
906
907 if (!new_info)
908 return -EFAULT;
909 copy_from_user(&new_serial,new_info,sizeof(new_serial));
910 old_info = *info;
911
912 if (!capable(CAP_SYS_ADMIN)) {
913 if ((new_serial.baud_base != info->baud_base) ||
914 (new_serial.type != info->type) ||
915 (new_serial.close_delay != info->close_delay) ||
916 ((new_serial.flags & ~S_USR_MASK) !=
917 (info->flags & ~S_USR_MASK)))
918 return -EPERM;
919 info->flags = ((info->flags & ~S_USR_MASK) |
920 (new_serial.flags & S_USR_MASK));
921 info->custom_divisor = new_serial.custom_divisor;
922 goto check_and_exit;
923 }
924
925 if (info->count > 1)
926 return -EBUSY;
927
928 /*
929 * OK, past this point, all the error checking has been done.
930 * At this point, we start making changes.....
931 */
932
933 info->baud_base = new_serial.baud_base;
934 info->flags = ((info->flags & ~S_FLAGS) |
935 (new_serial.flags & S_FLAGS));
936 info->type = new_serial.type;
937 info->close_delay = new_serial.close_delay;
938 info->closing_wait = new_serial.closing_wait;
939
940check_and_exit:
941 retval = startup(info);
942 return retval;
943}
944
945/*
946 * get_lsr_info - get line status register info
947 *
948 * Purpose: Let user call ioctl() to get info when the UART physically
949 * is emptied. On bus types like RS485, the transmitter must
950 * release the bus after transmitting. This must be done when
951 * the transmit shift register is empty, not be done when the
952 * transmit holding register is empty. This functionality
953 * allows an RS485 driver to be written in user space.
954 */
955static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
956{
957#ifdef CONFIG_SERIAL_68328_RTS_CTS
958 m68328_uart *uart = &uart_addr[info->line];
959#endif
960 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000961 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Greg Ungerer727dda82006-06-28 15:54:22 +1000963 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964#ifdef CONFIG_SERIAL_68328_RTS_CTS
965 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
966#else
967 status = 0;
968#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000969 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 put_user(status,value);
971 return 0;
972}
973
974/*
975 * This routine sends a break character out the serial port.
976 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700977static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 m68328_uart *uart = &uart_addr[info->line];
980 unsigned long flags;
981 if (!info->port)
982 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000983 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984#ifdef USE_INTS
985 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700986 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 uart->utx.w &= ~UTX_SEND_BREAK;
988#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000989 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static int rs_ioctl(struct tty_struct *tty, struct file * file,
993 unsigned int cmd, unsigned long arg)
994{
995 int error;
996 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
997 int retval;
998
999 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1000 return -ENODEV;
1001
1002 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1003 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1004 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1005 if (tty->flags & (1 << TTY_IO_ERROR))
1006 return -EIO;
1007 }
1008
1009 switch (cmd) {
1010 case TCSBRK: /* SVID version: non-zero arg --> no break */
1011 retval = tty_check_change(tty);
1012 if (retval)
1013 return retval;
1014 tty_wait_until_sent(tty, 0);
1015 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001016 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018 case TCSBRKP: /* support for POSIX tcsendbreak() */
1019 retval = tty_check_change(tty);
1020 if (retval)
1021 return retval;
1022 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001023 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025 case TIOCGSOFTCAR:
1026 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1027 (unsigned long *) arg);
1028 if (error)
1029 return error;
1030 return 0;
1031 case TIOCSSOFTCAR:
1032 get_user(arg, (unsigned long *) arg);
1033 tty->termios->c_cflag =
1034 ((tty->termios->c_cflag & ~CLOCAL) |
1035 (arg ? CLOCAL : 0));
1036 return 0;
1037 case TIOCGSERIAL:
1038 if (access_ok(VERIFY_WRITE, (void *) arg,
1039 sizeof(struct serial_struct)))
1040 return get_serial_info(info,
1041 (struct serial_struct *) arg);
1042 return -EFAULT;
1043 case TIOCSSERIAL:
1044 return set_serial_info(info,
1045 (struct serial_struct *) arg);
1046 case TIOCSERGETLSR: /* Get line status register */
1047 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001048 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return get_lsr_info(info, (unsigned int *) arg);
1050 return -EFAULT;
1051 case TIOCSERGSTRUCT:
1052 if (!access_ok(VERIFY_WRITE, (void *) arg,
1053 sizeof(struct m68k_serial)))
1054 return -EFAULT;
1055 copy_to_user((struct m68k_serial *) arg,
1056 info, sizeof(struct m68k_serial));
1057 return 0;
1058
1059 default:
1060 return -ENOIOCTLCMD;
1061 }
1062 return 0;
1063}
1064
1065static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1066{
1067 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1068
1069 if (tty->termios->c_cflag == old_termios->c_cflag)
1070 return;
1071
1072 change_speed(info);
1073
1074 if ((old_termios->c_cflag & CRTSCTS) &&
1075 !(tty->termios->c_cflag & CRTSCTS)) {
1076 tty->hw_stopped = 0;
1077 rs_start(tty);
1078 }
1079
1080}
1081
1082/*
1083 * ------------------------------------------------------------
1084 * rs_close()
1085 *
1086 * This routine is called when the serial port gets closed. First, we
1087 * wait for the last remaining data to be sent. Then, we unlink its
1088 * S structure from the interrupt chain if necessary, and we free
1089 * that IRQ if nothing is left in the chain.
1090 * ------------------------------------------------------------
1091 */
1092static void rs_close(struct tty_struct *tty, struct file * filp)
1093{
1094 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1095 m68328_uart *uart = &uart_addr[info->line];
1096 unsigned long flags;
1097
1098 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1099 return;
1100
Greg Ungerer727dda82006-06-28 15:54:22 +10001101 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001104 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return;
1106 }
1107
1108 if ((tty->count == 1) && (info->count != 1)) {
1109 /*
1110 * Uh, oh. tty->count is 1, which means that the tty
1111 * structure will be freed. Info->count should always
1112 * be one in these conditions. If it's greater than
1113 * one, we've got real problems, since it means the
1114 * serial port won't be shutdown.
1115 */
1116 printk("rs_close: bad serial port count; tty->count is 1, "
1117 "info->count is %d\n", info->count);
1118 info->count = 1;
1119 }
1120 if (--info->count < 0) {
1121 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1122 info->line, info->count);
1123 info->count = 0;
1124 }
1125 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001126 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128 }
1129 info->flags |= S_CLOSING;
1130 /*
1131 * Now we wait for the transmit buffer to clear; and we notify
1132 * the line discipline to only process XON/XOFF characters.
1133 */
1134 tty->closing = 1;
1135 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1136 tty_wait_until_sent(tty, info->closing_wait);
1137 /*
1138 * At this point we stop accepting input. To do this, we
1139 * disable the receive line status interrupts, and tell the
1140 * interrupt driver to stop checking the data ready bit in the
1141 * line status register.
1142 */
1143
1144 uart->ustcnt &= ~USTCNT_RXEN;
1145 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1146
1147 shutdown(info);
1148 if (tty->driver->flush_buffer)
1149 tty->driver->flush_buffer(tty);
1150
1151 tty_ldisc_flush(tty);
1152 tty->closing = 0;
1153 info->event = 0;
1154 info->tty = 0;
1155#warning "This is not and has never been valid so fix it"
1156#if 0
1157 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1158 if (tty->ldisc.close)
1159 (tty->ldisc.close)(tty);
1160 tty->ldisc = ldiscs[N_TTY];
1161 tty->termios->c_line = N_TTY;
1162 if (tty->ldisc.open)
1163 (tty->ldisc.open)(tty);
1164 }
1165#endif
1166 if (info->blocked_open) {
1167 if (info->close_delay) {
1168 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1169 }
1170 wake_up_interruptible(&info->open_wait);
1171 }
1172 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1173 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001174 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/*
1178 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1179 */
1180void rs_hangup(struct tty_struct *tty)
1181{
1182 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1183
1184 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1185 return;
1186
1187 rs_flush_buffer(tty);
1188 shutdown(info);
1189 info->event = 0;
1190 info->count = 0;
1191 info->flags &= ~S_NORMAL_ACTIVE;
1192 info->tty = 0;
1193 wake_up_interruptible(&info->open_wait);
1194}
1195
1196/*
1197 * ------------------------------------------------------------
1198 * rs_open() and friends
1199 * ------------------------------------------------------------
1200 */
1201static int block_til_ready(struct tty_struct *tty, struct file * filp,
1202 struct m68k_serial *info)
1203{
1204 DECLARE_WAITQUEUE(wait, current);
1205 int retval;
1206 int do_clocal = 0;
1207
1208 /*
1209 * If the device is in the middle of being closed, then block
1210 * until it's done, and then try again.
1211 */
1212 if (info->flags & S_CLOSING) {
1213 interruptible_sleep_on(&info->close_wait);
1214#ifdef SERIAL_DO_RESTART
1215 if (info->flags & S_HUP_NOTIFY)
1216 return -EAGAIN;
1217 else
1218 return -ERESTARTSYS;
1219#else
1220 return -EAGAIN;
1221#endif
1222 }
1223
1224 /*
1225 * If non-blocking mode is set, or the port is not enabled,
1226 * then make the check up front and then exit.
1227 */
1228 if ((filp->f_flags & O_NONBLOCK) ||
1229 (tty->flags & (1 << TTY_IO_ERROR))) {
1230 info->flags |= S_NORMAL_ACTIVE;
1231 return 0;
1232 }
1233
1234 if (tty->termios->c_cflag & CLOCAL)
1235 do_clocal = 1;
1236
1237 /*
1238 * Block waiting for the carrier detect and the line to become
1239 * free (i.e., not in use by the callout). While we are in
1240 * this loop, info->count is dropped by one, so that
1241 * rs_close() knows when to free things. We restore it upon
1242 * exit, either normal or abnormal.
1243 */
1244 retval = 0;
1245 add_wait_queue(&info->open_wait, &wait);
1246
1247 info->count--;
1248 info->blocked_open++;
1249 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001250 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001252 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 current->state = TASK_INTERRUPTIBLE;
1254 if (tty_hung_up_p(filp) ||
1255 !(info->flags & S_INITIALIZED)) {
1256#ifdef SERIAL_DO_RESTART
1257 if (info->flags & S_HUP_NOTIFY)
1258 retval = -EAGAIN;
1259 else
1260 retval = -ERESTARTSYS;
1261#else
1262 retval = -EAGAIN;
1263#endif
1264 break;
1265 }
1266 if (!(info->flags & S_CLOSING) && do_clocal)
1267 break;
1268 if (signal_pending(current)) {
1269 retval = -ERESTARTSYS;
1270 break;
1271 }
1272 schedule();
1273 }
1274 current->state = TASK_RUNNING;
1275 remove_wait_queue(&info->open_wait, &wait);
1276 if (!tty_hung_up_p(filp))
1277 info->count++;
1278 info->blocked_open--;
1279
1280 if (retval)
1281 return retval;
1282 info->flags |= S_NORMAL_ACTIVE;
1283 return 0;
1284}
1285
1286/*
1287 * This routine is called whenever a serial port is opened. It
1288 * enables interrupts for a serial port, linking in its S structure into
1289 * the IRQ chain. It also performs the serial-specific
1290 * initialization for the tty structure.
1291 */
1292int rs_open(struct tty_struct *tty, struct file * filp)
1293{
1294 struct m68k_serial *info;
1295 int retval, line;
1296
1297 line = tty->index;
1298
1299 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1300 return -ENODEV;
1301
1302 info = &m68k_soft[line];
1303
1304 if (serial_paranoia_check(info, tty->name, "rs_open"))
1305 return -ENODEV;
1306
1307 info->count++;
1308 tty->driver_data = info;
1309 info->tty = tty;
1310
1311 /*
1312 * Start up serial port
1313 */
1314 retval = startup(info);
1315 if (retval)
1316 return retval;
1317
1318 return block_til_ready(tty, filp, info);
1319}
1320
1321/* Finally, routines used to initialize the serial driver. */
1322
1323static void show_serial_version(void)
1324{
1325 printk("MC68328 serial driver version 1.00\n");
1326}
1327
Jeff Garzikbca73e42005-11-13 16:06:25 -08001328#ifdef CONFIG_PM_LEGACY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/* Serial Power management
1330 * The console (currently fixed at line 0) is a special case for power
1331 * management because the kernel is so chatty. The console will be
1332 * explicitly disabled my our power manager as the last minute, so we won't
1333 * mess with it here.
1334 */
1335static struct pm_dev *serial_pm[NR_PORTS];
1336
1337static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
1338{
1339 struct m68k_serial *info = (struct m68k_serial *)dev->data;
1340
1341 if(info == NULL)
1342 return -1;
1343
1344 /* special case for line 0 - pm restores it */
1345 if(info->line == 0)
1346 return 0;
1347
1348 switch (request) {
1349 case PM_SUSPEND:
1350 shutdown(info);
1351 break;
1352
1353 case PM_RESUME:
1354 startup(info);
1355 break;
1356 }
1357 return 0;
1358}
1359
1360void shutdown_console(void)
1361{
1362 struct m68k_serial *info = &m68k_soft[0];
1363
1364 /* HACK: wait a bit for any pending printk's to be dumped */
1365 {
1366 int i = 10000;
1367 while(i--);
1368 }
1369
1370 shutdown(info);
1371}
1372
1373void startup_console(void)
1374{
1375 struct m68k_serial *info = &m68k_soft[0];
1376 startup(info);
1377}
Jeff Garzikbca73e42005-11-13 16:06:25 -08001378#endif /* CONFIG_PM_LEGACY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380
1381static struct tty_operations rs_ops = {
1382 .open = rs_open,
1383 .close = rs_close,
1384 .write = rs_write,
1385 .flush_chars = rs_flush_chars,
1386 .write_room = rs_write_room,
1387 .chars_in_buffer = rs_chars_in_buffer,
1388 .flush_buffer = rs_flush_buffer,
1389 .ioctl = rs_ioctl,
1390 .throttle = rs_throttle,
1391 .unthrottle = rs_unthrottle,
1392 .set_termios = rs_set_termios,
1393 .stop = rs_stop,
1394 .start = rs_start,
1395 .hangup = rs_hangup,
1396 .set_ldisc = rs_set_ldisc,
1397};
1398
1399/* rs_init inits the driver */
1400static int __init
1401rs68328_init(void)
1402{
1403 int flags, i;
1404 struct m68k_serial *info;
1405
1406 serial_driver = alloc_tty_driver(NR_PORTS);
1407 if (!serial_driver)
1408 return -ENOMEM;
1409
1410 show_serial_version();
1411
1412 /* Initialize the tty_driver structure */
1413 /* SPARC: Not all of this is exactly right for us. */
1414
1415 serial_driver->name = "ttyS";
1416 serial_driver->major = TTY_MAJOR;
1417 serial_driver->minor_start = 64;
1418 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1419 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1420 serial_driver->init_termios = tty_std_termios;
1421 serial_driver->init_termios.c_cflag =
1422 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1423 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1424 tty_set_operations(serial_driver, &rs_ops);
1425
1426 if (tty_register_driver(serial_driver)) {
1427 put_tty_driver(serial_driver);
1428 printk(KERN_ERR "Couldn't register serial driver\n");
1429 return -ENOMEM;
1430 }
1431
Greg Ungerer727dda82006-06-28 15:54:22 +10001432 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 for(i=0;i<NR_PORTS;i++) {
1435
1436 info = &m68k_soft[i];
1437 info->magic = SERIAL_MAGIC;
1438 info->port = (int) &uart_addr[i];
1439 info->tty = 0;
1440 info->irq = uart_irqs[i];
1441 info->custom_divisor = 16;
1442 info->close_delay = 50;
1443 info->closing_wait = 3000;
1444 info->x_char = 0;
1445 info->event = 0;
1446 info->count = 0;
1447 info->blocked_open = 0;
1448 INIT_WORK(&info->tqueue, do_softint, info);
1449 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1450 init_waitqueue_head(&info->open_wait);
1451 init_waitqueue_head(&info->close_wait);
1452 info->line = i;
1453 info->is_cons = 1; /* Means shortcuts work */
1454
1455 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1456 info->port, info->irq);
1457 printk(" is a builtin MC68328 UART\n");
1458
1459 IRQ_ports[info->irq] = info; /* waste of space */
1460
1461#ifdef CONFIG_M68VZ328
1462 if (i > 0 )
1463 PJSEL &= 0xCF; /* PSW enable second port output */
1464#endif
1465
1466 if (request_irq(uart_irqs[i],
1467 rs_interrupt,
1468 IRQ_FLG_STD,
1469 "M68328_UART", NULL))
1470 panic("Unable to attach 68328 serial interrupt\n");
Jeff Garzikbca73e42005-11-13 16:06:25 -08001471#ifdef CONFIG_PM_LEGACY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
1473 if (serial_pm[i])
1474 serial_pm[i]->data = info;
1475#endif
1476 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001477 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
1479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481module_init(rs68328_init);
1482
1483
1484
1485static void m68328_set_baud(void)
1486{
1487 unsigned short ustcnt;
1488 int i;
1489
1490 ustcnt = USTCNT;
1491 USTCNT = ustcnt & ~USTCNT_TXEN;
1492
1493again:
1494 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1495 if (baud_table[i] == m68328_console_baud)
1496 break;
1497 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1498 m68328_console_baud = 9600;
1499 goto again;
1500 }
1501
1502 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1503 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1504 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1505 ustcnt |= USTCNT_8_7;
1506 ustcnt |= USTCNT_TXEN;
1507 USTCNT = ustcnt;
1508 m68328_console_initted = 1;
1509 return;
1510}
1511
1512
1513int m68328_console_setup(struct console *cp, char *arg)
1514{
1515 int i, n = CONSOLE_BAUD_RATE;
1516
1517 if (!cp)
1518 return(-1);
1519
1520 if (arg)
1521 n = simple_strtoul(arg,NULL,0);
1522
1523 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1524 if (baud_table[i] == n)
1525 break;
1526 if (i < BAUD_TABLE_SIZE) {
1527 m68328_console_baud = n;
1528 m68328_console_cbaud = 0;
1529 if (i > 15) {
1530 m68328_console_cbaud |= CBAUDEX;
1531 i -= 15;
1532 }
1533 m68328_console_cbaud |= i;
1534 }
1535
1536 m68328_set_baud(); /* make sure baud rate changes */
1537 return(0);
1538}
1539
1540
1541static struct tty_driver *m68328_console_device(struct console *c, int *index)
1542{
1543 *index = c->index;
1544 return serial_driver;
1545}
1546
1547
1548void m68328_console_write (struct console *co, const char *str,
1549 unsigned int count)
1550{
1551 if (!m68328_console_initted)
1552 m68328_set_baud();
1553 while (count--) {
1554 if (*str == '\n')
1555 rs_put_char('\r');
1556 rs_put_char( *str++ );
1557 }
1558}
1559
1560
1561static struct console m68328_driver = {
1562 .name = "ttyS",
1563 .write = m68328_console_write,
1564 .device = m68328_console_device,
1565 .setup = m68328_console_setup,
1566 .flags = CON_PRINTBUFFER,
1567 .index = -1,
1568};
1569
1570
1571static int __init m68328_console_init(void)
1572{
1573 register_console(&m68328_driver);
1574 return 0;
1575}
1576
1577console_initcall(m68328_console_init);