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