blob: aad4012bbb30510a66c2f7983f07473a9885f5d4 [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
87/* serial subtype definitions */
88#define SERIAL_TYPE_NORMAL 1
89
90/* number of characters left in xmit buffer before we ask for more */
91#define WAKEUP_CHARS 256
92
93/* Debugging... DEBUG_INTR is bad to use when one of the zs
94 * lines is your console ;(
95 */
96#undef SERIAL_DEBUG_INTR
97#undef SERIAL_DEBUG_OPEN
98#undef SERIAL_DEBUG_FLOW
99
100#define RS_ISR_PASS_LIMIT 256
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static void change_speed(struct m68k_serial *info);
103
104/*
105 * Setup for console. Argument comes from the boot command line.
106 */
107
108#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
109#define CONSOLE_BAUD_RATE 115200
110#define DEFAULT_CBAUD B115200
111#else
112 /* (es) */
113 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
114 #ifdef CONFIG_M68VZ328
115 #define CONSOLE_BAUD_RATE 19200
116 #define DEFAULT_CBAUD B19200
117 #endif
118 /* (/es) */
119#endif
120
121#ifndef CONSOLE_BAUD_RATE
122#define CONSOLE_BAUD_RATE 9600
123#define DEFAULT_CBAUD B9600
124#endif
125
126
127static int m68328_console_initted = 0;
128static int m68328_console_baud = CONSOLE_BAUD_RATE;
129static int m68328_console_cbaud = DEFAULT_CBAUD;
130
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static inline int serial_paranoia_check(struct m68k_serial *info,
133 char *name, const char *routine)
134{
135#ifdef SERIAL_PARANOIA_CHECK
136 static const char *badmagic =
137 "Warning: bad magic number for serial struct %s in %s\n";
138 static const char *badinfo =
139 "Warning: null m68k_serial for %s in %s\n";
140
141 if (!info) {
142 printk(badinfo, name, routine);
143 return 1;
144 }
145 if (info->magic != SERIAL_MAGIC) {
146 printk(badmagic, name, routine);
147 return 1;
148 }
149#endif
150 return 0;
151}
152
153/*
154 * This is used to figure out the divisor speeds and the timeouts
155 */
156static int baud_table[] = {
157 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
158 9600, 19200, 38400, 57600, 115200, 0 };
159
160#define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
161
162/* Sets or clears DTR/RTS on the requested line */
163static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
164{
165 if (set) {
166 /* set the RTS/CTS line */
167 } else {
168 /* clear it */
169 }
170 return;
171}
172
173/* Utility routines */
174static inline int get_baud(struct m68k_serial *ss)
175{
176 unsigned long result = 115200;
177 unsigned short int baud = uart_addr[ss->line].ubaud;
178 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
179 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
180
181 return result;
182}
183
184/*
185 * ------------------------------------------------------------
186 * rs_stop() and rs_start()
187 *
188 * This routines are called before setting or resetting tty->stopped.
189 * They enable or disable transmitter interrupts, as necessary.
190 * ------------------------------------------------------------
191 */
192static void rs_stop(struct tty_struct *tty)
193{
194 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
195 m68328_uart *uart = &uart_addr[info->line];
196 unsigned long flags;
197
198 if (serial_paranoia_check(info, tty->name, "rs_stop"))
199 return;
200
Greg Ungerer727dda82006-06-28 15:54:22 +1000201 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000203 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static void rs_put_char(char ch)
207{
208 int flags, loops = 0;
209
Greg Ungerer727dda82006-06-28 15:54:22 +1000210 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
213 loops++;
214 udelay(5);
215 }
216
217 UTX_TXDATA = ch;
218 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000219 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222static void rs_start(struct tty_struct *tty)
223{
224 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
225 m68328_uart *uart = &uart_addr[info->line];
226 unsigned long flags;
227
228 if (serial_paranoia_check(info, tty->name, "rs_start"))
229 return;
230
Greg Ungerer727dda82006-06-28 15:54:22 +1000231 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
233#ifdef USE_INTS
234 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
235#else
236 uart->ustcnt |= USTCNT_TXEN;
237#endif
238 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000239 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/* Drop into either the boot monitor or kadb upon receiving a break
243 * from keyboard/console input.
244 */
245static void batten_down_hatches(void)
246{
247 /* Drop into the debugger */
248}
249
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800250static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252#if 0
253 if(status & DCD) {
254 if((info->tty->termios->c_cflag & CRTSCTS) &&
255 ((info->curregs[3] & AUTO_ENAB)==0)) {
256 info->curregs[3] |= AUTO_ENAB;
257 info->pendregs[3] |= AUTO_ENAB;
258 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
259 }
260 } else {
261 if((info->curregs[3] & AUTO_ENAB)) {
262 info->curregs[3] &= ~AUTO_ENAB;
263 info->pendregs[3] &= ~AUTO_ENAB;
264 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
265 }
266 }
267#endif
268 /* If this is console input and this is a
269 * 'break asserted' status change interrupt
270 * see if we can drop into the debugger
271 */
272 if((status & URX_BREAK) && info->break_abort)
273 batten_down_hatches();
274 return;
275}
276
David Howells7d12e782006-10-05 14:55:46 +0100277static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct tty_struct *tty = info->tty;
280 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800281 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /*
284 * This do { } while() loop will get ALL chars out of Rx FIFO
285 */
286#ifndef CONFIG_XCOPILOT_BUGS
287 do {
288#endif
289 ch = GET_FIELD(rx, URX_RXDATA);
290
291 if(info->is_cons) {
292 if(URX_BREAK & rx) { /* whee, break received */
293 status_handle(info, rx);
294 return;
295#ifdef CONFIG_MAGIC_SYSRQ
296 } else if (ch == 0x10) { /* ^P */
297 show_state();
298 show_free_areas();
299 show_buffers();
300/* show_net_buffers(); */
301 return;
302 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600303 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return;
305#endif /* CONFIG_MAGIC_SYSRQ */
306 }
307 /* It is a 'keyboard interrupt' ;-) */
308#ifdef CONFIG_CONSOLE
309 wake_up(&keypress_wait);
310#endif
311 }
312
313 if(!tty)
314 goto clear_and_exit;
315
Alan Cox33f0f882006-01-09 20:54:13 -0800316 flag = TTY_NORMAL;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800319 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 status_handle(info, rx);
321 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800322 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 status_handle(info, rx);
324 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800325 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Alan Cox33f0f882006-01-09 20:54:13 -0800328 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#ifndef CONFIG_XCOPILOT_BUGS
330 } while((rx = uart->urx.w) & URX_DATA_READY);
331#endif
332
Greg Ungerer8e63e662006-02-08 09:19:17 +1000333 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335clear_and_exit:
336 return;
337}
338
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800339static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 m68328_uart *uart = &uart_addr[info->line];
342
343 if (info->x_char) {
344 /* Send next char */
345 uart->utx.b.txdata = info->x_char;
346 info->x_char = 0;
347 goto clear_and_return;
348 }
349
350 if((info->xmit_cnt <= 0) || info->tty->stopped) {
351 /* That's peculiar... TX ints off */
352 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
353 goto clear_and_return;
354 }
355
356 /* Send char */
357 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
358 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
359 info->xmit_cnt--;
360
361 if (info->xmit_cnt < WAKEUP_CHARS)
362 schedule_work(&info->tqueue);
363
364 if(info->xmit_cnt <= 0) {
365 /* All done for now... TX ints off */
366 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
367 goto clear_and_return;
368 }
369
370clear_and_return:
371 /* Clear interrupt (should be auto)*/
372 return;
373}
374
375/*
376 * This is the serial driver's generic interrupt routine
377 */
David Howells7d12e782006-10-05 14:55:46 +0100378irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct m68k_serial * info;
381 m68328_uart *uart;
382 unsigned short rx;
383 unsigned short tx;
384
385 info = IRQ_ports[irq];
386 if(!info)
387 return IRQ_NONE;
388
389 uart = &uart_addr[info->line];
390 rx = uart->urx.w;
391
392#ifdef USE_INTS
393 tx = uart->utx.w;
394
David Howells7d12e782006-10-05 14:55:46 +0100395 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (tx & UTX_TX_AVAIL) transmit_chars(info);
397#else
David Howells7d12e782006-10-05 14:55:46 +0100398 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#endif
400 return IRQ_HANDLED;
401}
402
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000403static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000405 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct tty_struct *tty;
407
408 tty = info->tty;
409 if (!tty)
410 return;
411#if 0
412 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
413 tty_wakeup(tty);
414 }
415#endif
416}
417
418/*
419 * This routine is called from the scheduler tqueue when the interrupt
420 * routine has signalled that a hangup has occurred. The path of
421 * hangup processing is:
422 *
423 * serial interrupt routine -> (scheduler tqueue) ->
424 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
425 *
426 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000427static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000429 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct tty_struct *tty;
431
432 tty = info->tty;
433 if (!tty)
434 return;
435
436 tty_hangup(tty);
437}
438
439
440static int startup(struct m68k_serial * info)
441{
442 m68328_uart *uart = &uart_addr[info->line];
443 unsigned long flags;
444
445 if (info->flags & S_INITIALIZED)
446 return 0;
447
448 if (!info->xmit_buf) {
449 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
450 if (!info->xmit_buf)
451 return -ENOMEM;
452 }
453
Greg Ungerer727dda82006-06-28 15:54:22 +1000454 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /*
457 * Clear the FIFO buffers and disable them
458 * (they will be reenabled in change_speed())
459 */
460
461 uart->ustcnt = USTCNT_UEN;
462 info->xmit_fifo_size = 1;
463 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
464 (void)uart->urx.w;
465
466 /*
467 * Finally, enable sequencing and interrupts
468 */
469#ifdef USE_INTS
470 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
471 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
472#else
473 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
474#endif
475
476 if (info->tty)
477 clear_bit(TTY_IO_ERROR, &info->tty->flags);
478 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
479
480 /*
481 * and set the speed of the serial port
482 */
483
484 change_speed(info);
485
486 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000487 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 0;
489}
490
491/*
492 * This routine will shutdown a serial port; interrupts are disabled, and
493 * DTR is dropped if the hangup on close termio flag is on.
494 */
495static void shutdown(struct m68k_serial * info)
496{
497 m68328_uart *uart = &uart_addr[info->line];
498 unsigned long flags;
499
500 uart->ustcnt = 0; /* All off! */
501 if (!(info->flags & S_INITIALIZED))
502 return;
503
Greg Ungerer727dda82006-06-28 15:54:22 +1000504 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (info->xmit_buf) {
507 free_page((unsigned long) info->xmit_buf);
508 info->xmit_buf = 0;
509 }
510
511 if (info->tty)
512 set_bit(TTY_IO_ERROR, &info->tty->flags);
513
514 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000515 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518struct {
519 int divisor, prescale;
520}
521#ifndef CONFIG_M68VZ328
522 hw_baud_table[18] = {
523 {0,0}, /* 0 */
524 {0,0}, /* 50 */
525 {0,0}, /* 75 */
526 {0,0}, /* 110 */
527 {0,0}, /* 134 */
528 {0,0}, /* 150 */
529 {0,0}, /* 200 */
530 {7,0x26}, /* 300 */
531 {6,0x26}, /* 600 */
532 {5,0x26}, /* 1200 */
533 {0,0}, /* 1800 */
534 {4,0x26}, /* 2400 */
535 {3,0x26}, /* 4800 */
536 {2,0x26}, /* 9600 */
537 {1,0x26}, /* 19200 */
538 {0,0x26}, /* 38400 */
539 {1,0x38}, /* 57600 */
540 {0,0x38}, /* 115200 */
541};
542#else
543 hw_baud_table[18] = {
544 {0,0}, /* 0 */
545 {0,0}, /* 50 */
546 {0,0}, /* 75 */
547 {0,0}, /* 110 */
548 {0,0}, /* 134 */
549 {0,0}, /* 150 */
550 {0,0}, /* 200 */
551 {0,0}, /* 300 */
552 {7,0x26}, /* 600 */
553 {6,0x26}, /* 1200 */
554 {0,0}, /* 1800 */
555 {5,0x26}, /* 2400 */
556 {4,0x26}, /* 4800 */
557 {3,0x26}, /* 9600 */
558 {2,0x26}, /* 19200 */
559 {1,0x26}, /* 38400 */
560 {0,0x26}, /* 57600 */
561 {1,0x38}, /* 115200 */
562};
563#endif
564/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
565
566/*
567 * This routine is called to set the UART divisor registers to match
568 * the specified baud rate for a serial port.
569 */
570static void change_speed(struct m68k_serial *info)
571{
572 m68328_uart *uart = &uart_addr[info->line];
573 unsigned short port;
574 unsigned short ustcnt;
575 unsigned cflag;
576 int i;
577
578 if (!info->tty || !info->tty->termios)
579 return;
580 cflag = info->tty->termios->c_cflag;
581 if (!(port = info->port))
582 return;
583
584 ustcnt = uart->ustcnt;
585 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
586
587 i = cflag & CBAUD;
588 if (i & CBAUDEX) {
589 i = (i & ~CBAUDEX) + B38400;
590 }
591
592 info->baud = baud_table[i];
593 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
594 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
595
596 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
597
598 if ((cflag & CSIZE) == CS8)
599 ustcnt |= USTCNT_8_7;
600
601 if (cflag & CSTOPB)
602 ustcnt |= USTCNT_STOP;
603
604 if (cflag & PARENB)
605 ustcnt |= USTCNT_PARITYEN;
606 if (cflag & PARODD)
607 ustcnt |= USTCNT_ODD_EVEN;
608
609#ifdef CONFIG_SERIAL_68328_RTS_CTS
610 if (cflag & CRTSCTS) {
611 uart->utx.w &= ~ UTX_NOCTS;
612 } else {
613 uart->utx.w |= UTX_NOCTS;
614 }
615#endif
616
617 ustcnt |= USTCNT_TXEN;
618
619 uart->ustcnt = ustcnt;
620 return;
621}
622
623/*
624 * Fair output driver allows a process to speak.
625 */
626static void rs_fair_output(void)
627{
628 int left; /* Output no more than that */
629 unsigned long flags;
630 struct m68k_serial *info = &m68k_soft[0];
631 char c;
632
633 if (info == 0) return;
634 if (info->xmit_buf == 0) return;
635
Greg Ungerer727dda82006-06-28 15:54:22 +1000636 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 left = info->xmit_cnt;
638 while (left != 0) {
639 c = info->xmit_buf[info->xmit_tail];
640 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
641 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000642 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 rs_put_char(c);
645
Greg Ungerer727dda82006-06-28 15:54:22 +1000646 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 left = min(info->xmit_cnt, left-1);
648 }
649
650 /* Last character is being transmitted now (hopefully). */
651 udelay(5);
652
Greg Ungerer727dda82006-06-28 15:54:22 +1000653 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return;
655}
656
657/*
658 * m68k_console_print is registered for printk.
659 */
660void console_print_68328(const char *p)
661{
662 char c;
663
664 while((c=*(p++)) != 0) {
665 if(c == '\n')
666 rs_put_char('\r');
667 rs_put_char(c);
668 }
669
670 /* Comment this if you want to have a strict interrupt-driven output */
671 rs_fair_output();
672
673 return;
674}
675
676static void rs_set_ldisc(struct tty_struct *tty)
677{
678 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
679
680 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
681 return;
682
683 info->is_cons = (tty->termios->c_line == N_TTY);
684
685 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
686}
687
688static void rs_flush_chars(struct tty_struct *tty)
689{
690 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
691 m68328_uart *uart = &uart_addr[info->line];
692 unsigned long flags;
693
694 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
695 return;
696#ifndef USE_INTS
697 for(;;) {
698#endif
699
700 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000701 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
704 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000705 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
707 }
708
709#ifdef USE_INTS
710 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
711#else
712 uart->ustcnt |= USTCNT_TXEN;
713#endif
714
715#ifdef USE_INTS
716 if (uart->utx.w & UTX_TX_AVAIL) {
717#else
718 if (1) {
719#endif
720 /* Send char */
721 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
722 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
723 info->xmit_cnt--;
724 }
725
726#ifndef USE_INTS
727 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
728 }
729#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000730 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733extern void console_printn(const char * b, int count);
734
735static int rs_write(struct tty_struct * tty,
736 const unsigned char *buf, int count)
737{
738 int c, total = 0;
739 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
740 m68328_uart *uart = &uart_addr[info->line];
741 unsigned long flags;
742
743 if (serial_paranoia_check(info, tty->name, "rs_write"))
744 return 0;
745
746 if (!tty || !info->xmit_buf)
747 return 0;
748
Greg Ungerer727dda82006-06-28 15:54:22 +1000749 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000751 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
753 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000754 local_irq_restore(flags);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (c <= 0)
757 break;
758
759 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000760
761 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
763 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000764 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 buf += c;
766 count -= c;
767 total += c;
768 }
769
770 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
771 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000772 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#ifndef USE_INTS
774 while(info->xmit_cnt) {
775#endif
776
777 uart->ustcnt |= USTCNT_TXEN;
778#ifdef USE_INTS
779 uart->ustcnt |= USTCNT_TX_INTR_MASK;
780#else
781 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
782#endif
783 if (uart->utx.w & UTX_TX_AVAIL) {
784 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
785 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
786 info->xmit_cnt--;
787 }
788
789#ifndef USE_INTS
790 }
791#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000792 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return total;
796}
797
798static int rs_write_room(struct tty_struct *tty)
799{
800 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
801 int ret;
802
803 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
804 return 0;
805 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
806 if (ret < 0)
807 ret = 0;
808 return ret;
809}
810
811static int rs_chars_in_buffer(struct tty_struct *tty)
812{
813 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
814
815 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
816 return 0;
817 return info->xmit_cnt;
818}
819
820static void rs_flush_buffer(struct tty_struct *tty)
821{
822 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000823 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
826 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000827 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000829 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 tty_wakeup(tty);
831}
832
833/*
834 * ------------------------------------------------------------
835 * rs_throttle()
836 *
837 * This routine is called by the upper-layer tty layer to signal that
838 * incoming characters should be throttled.
839 * ------------------------------------------------------------
840 */
841static void rs_throttle(struct tty_struct * tty)
842{
843 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
844
845 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
846 return;
847
848 if (I_IXOFF(tty))
849 info->x_char = STOP_CHAR(tty);
850
851 /* Turn off RTS line (do this atomic) */
852}
853
854static void rs_unthrottle(struct tty_struct * tty)
855{
856 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
857
858 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
859 return;
860
861 if (I_IXOFF(tty)) {
862 if (info->x_char)
863 info->x_char = 0;
864 else
865 info->x_char = START_CHAR(tty);
866 }
867
868 /* Assert RTS line (do this atomic) */
869}
870
871/*
872 * ------------------------------------------------------------
873 * rs_ioctl() and friends
874 * ------------------------------------------------------------
875 */
876
877static int get_serial_info(struct m68k_serial * info,
878 struct serial_struct * retinfo)
879{
880 struct serial_struct tmp;
881
882 if (!retinfo)
883 return -EFAULT;
884 memset(&tmp, 0, sizeof(tmp));
885 tmp.type = info->type;
886 tmp.line = info->line;
887 tmp.port = info->port;
888 tmp.irq = info->irq;
889 tmp.flags = info->flags;
890 tmp.baud_base = info->baud_base;
891 tmp.close_delay = info->close_delay;
892 tmp.closing_wait = info->closing_wait;
893 tmp.custom_divisor = info->custom_divisor;
894 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
895 return 0;
896}
897
898static int set_serial_info(struct m68k_serial * info,
899 struct serial_struct * new_info)
900{
901 struct serial_struct new_serial;
902 struct m68k_serial old_info;
903 int retval = 0;
904
905 if (!new_info)
906 return -EFAULT;
907 copy_from_user(&new_serial,new_info,sizeof(new_serial));
908 old_info = *info;
909
910 if (!capable(CAP_SYS_ADMIN)) {
911 if ((new_serial.baud_base != info->baud_base) ||
912 (new_serial.type != info->type) ||
913 (new_serial.close_delay != info->close_delay) ||
914 ((new_serial.flags & ~S_USR_MASK) !=
915 (info->flags & ~S_USR_MASK)))
916 return -EPERM;
917 info->flags = ((info->flags & ~S_USR_MASK) |
918 (new_serial.flags & S_USR_MASK));
919 info->custom_divisor = new_serial.custom_divisor;
920 goto check_and_exit;
921 }
922
923 if (info->count > 1)
924 return -EBUSY;
925
926 /*
927 * OK, past this point, all the error checking has been done.
928 * At this point, we start making changes.....
929 */
930
931 info->baud_base = new_serial.baud_base;
932 info->flags = ((info->flags & ~S_FLAGS) |
933 (new_serial.flags & S_FLAGS));
934 info->type = new_serial.type;
935 info->close_delay = new_serial.close_delay;
936 info->closing_wait = new_serial.closing_wait;
937
938check_and_exit:
939 retval = startup(info);
940 return retval;
941}
942
943/*
944 * get_lsr_info - get line status register info
945 *
946 * Purpose: Let user call ioctl() to get info when the UART physically
947 * is emptied. On bus types like RS485, the transmitter must
948 * release the bus after transmitting. This must be done when
949 * the transmit shift register is empty, not be done when the
950 * transmit holding register is empty. This functionality
951 * allows an RS485 driver to be written in user space.
952 */
953static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
954{
955#ifdef CONFIG_SERIAL_68328_RTS_CTS
956 m68328_uart *uart = &uart_addr[info->line];
957#endif
958 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000959 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Greg Ungerer727dda82006-06-28 15:54:22 +1000961 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef CONFIG_SERIAL_68328_RTS_CTS
963 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
964#else
965 status = 0;
966#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000967 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 put_user(status,value);
969 return 0;
970}
971
972/*
973 * This routine sends a break character out the serial port.
974 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700975static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 m68328_uart *uart = &uart_addr[info->line];
978 unsigned long flags;
979 if (!info->port)
980 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000981 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#ifdef USE_INTS
983 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700984 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 uart->utx.w &= ~UTX_SEND_BREAK;
986#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000987 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990static int rs_ioctl(struct tty_struct *tty, struct file * file,
991 unsigned int cmd, unsigned long arg)
992{
993 int error;
994 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
995 int retval;
996
997 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
998 return -ENODEV;
999
1000 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1001 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1002 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1003 if (tty->flags & (1 << TTY_IO_ERROR))
1004 return -EIO;
1005 }
1006
1007 switch (cmd) {
1008 case TCSBRK: /* SVID version: non-zero arg --> no break */
1009 retval = tty_check_change(tty);
1010 if (retval)
1011 return retval;
1012 tty_wait_until_sent(tty, 0);
1013 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001014 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016 case TCSBRKP: /* support for POSIX tcsendbreak() */
1017 retval = tty_check_change(tty);
1018 if (retval)
1019 return retval;
1020 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001021 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023 case TIOCGSOFTCAR:
1024 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1025 (unsigned long *) arg);
1026 if (error)
1027 return error;
1028 return 0;
1029 case TIOCSSOFTCAR:
1030 get_user(arg, (unsigned long *) arg);
1031 tty->termios->c_cflag =
1032 ((tty->termios->c_cflag & ~CLOCAL) |
1033 (arg ? CLOCAL : 0));
1034 return 0;
1035 case TIOCGSERIAL:
1036 if (access_ok(VERIFY_WRITE, (void *) arg,
1037 sizeof(struct serial_struct)))
1038 return get_serial_info(info,
1039 (struct serial_struct *) arg);
1040 return -EFAULT;
1041 case TIOCSSERIAL:
1042 return set_serial_info(info,
1043 (struct serial_struct *) arg);
1044 case TIOCSERGETLSR: /* Get line status register */
1045 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001046 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return get_lsr_info(info, (unsigned int *) arg);
1048 return -EFAULT;
1049 case TIOCSERGSTRUCT:
1050 if (!access_ok(VERIFY_WRITE, (void *) arg,
1051 sizeof(struct m68k_serial)))
1052 return -EFAULT;
1053 copy_to_user((struct m68k_serial *) arg,
1054 info, sizeof(struct m68k_serial));
1055 return 0;
1056
1057 default:
1058 return -ENOIOCTLCMD;
1059 }
1060 return 0;
1061}
1062
Alan Cox606d0992006-12-08 02:38:45 -08001063static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1066
1067 if (tty->termios->c_cflag == old_termios->c_cflag)
1068 return;
1069
1070 change_speed(info);
1071
1072 if ((old_termios->c_cflag & CRTSCTS) &&
1073 !(tty->termios->c_cflag & CRTSCTS)) {
1074 tty->hw_stopped = 0;
1075 rs_start(tty);
1076 }
1077
1078}
1079
1080/*
1081 * ------------------------------------------------------------
1082 * rs_close()
1083 *
1084 * This routine is called when the serial port gets closed. First, we
1085 * wait for the last remaining data to be sent. Then, we unlink its
1086 * S structure from the interrupt chain if necessary, and we free
1087 * that IRQ if nothing is left in the chain.
1088 * ------------------------------------------------------------
1089 */
1090static void rs_close(struct tty_struct *tty, struct file * filp)
1091{
1092 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1093 m68328_uart *uart = &uart_addr[info->line];
1094 unsigned long flags;
1095
1096 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1097 return;
1098
Greg Ungerer727dda82006-06-28 15:54:22 +10001099 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105
1106 if ((tty->count == 1) && (info->count != 1)) {
1107 /*
1108 * Uh, oh. tty->count is 1, which means that the tty
1109 * structure will be freed. Info->count should always
1110 * be one in these conditions. If it's greater than
1111 * one, we've got real problems, since it means the
1112 * serial port won't be shutdown.
1113 */
1114 printk("rs_close: bad serial port count; tty->count is 1, "
1115 "info->count is %d\n", info->count);
1116 info->count = 1;
1117 }
1118 if (--info->count < 0) {
1119 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1120 info->line, info->count);
1121 info->count = 0;
1122 }
1123 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001124 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return;
1126 }
1127 info->flags |= S_CLOSING;
1128 /*
1129 * Now we wait for the transmit buffer to clear; and we notify
1130 * the line discipline to only process XON/XOFF characters.
1131 */
1132 tty->closing = 1;
1133 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1134 tty_wait_until_sent(tty, info->closing_wait);
1135 /*
1136 * At this point we stop accepting input. To do this, we
1137 * disable the receive line status interrupts, and tell the
1138 * interrupt driver to stop checking the data ready bit in the
1139 * line status register.
1140 */
1141
1142 uart->ustcnt &= ~USTCNT_RXEN;
1143 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1144
1145 shutdown(info);
1146 if (tty->driver->flush_buffer)
1147 tty->driver->flush_buffer(tty);
1148
1149 tty_ldisc_flush(tty);
1150 tty->closing = 0;
1151 info->event = 0;
1152 info->tty = 0;
1153#warning "This is not and has never been valid so fix it"
1154#if 0
1155 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1156 if (tty->ldisc.close)
1157 (tty->ldisc.close)(tty);
1158 tty->ldisc = ldiscs[N_TTY];
1159 tty->termios->c_line = N_TTY;
1160 if (tty->ldisc.open)
1161 (tty->ldisc.open)(tty);
1162 }
1163#endif
1164 if (info->blocked_open) {
1165 if (info->close_delay) {
1166 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1167 }
1168 wake_up_interruptible(&info->open_wait);
1169 }
1170 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1171 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001172 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175/*
1176 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1177 */
1178void rs_hangup(struct tty_struct *tty)
1179{
1180 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1181
1182 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1183 return;
1184
1185 rs_flush_buffer(tty);
1186 shutdown(info);
1187 info->event = 0;
1188 info->count = 0;
1189 info->flags &= ~S_NORMAL_ACTIVE;
1190 info->tty = 0;
1191 wake_up_interruptible(&info->open_wait);
1192}
1193
1194/*
1195 * ------------------------------------------------------------
1196 * rs_open() and friends
1197 * ------------------------------------------------------------
1198 */
1199static int block_til_ready(struct tty_struct *tty, struct file * filp,
1200 struct m68k_serial *info)
1201{
1202 DECLARE_WAITQUEUE(wait, current);
1203 int retval;
1204 int do_clocal = 0;
1205
1206 /*
1207 * If the device is in the middle of being closed, then block
1208 * until it's done, and then try again.
1209 */
1210 if (info->flags & S_CLOSING) {
1211 interruptible_sleep_on(&info->close_wait);
1212#ifdef SERIAL_DO_RESTART
1213 if (info->flags & S_HUP_NOTIFY)
1214 return -EAGAIN;
1215 else
1216 return -ERESTARTSYS;
1217#else
1218 return -EAGAIN;
1219#endif
1220 }
1221
1222 /*
1223 * If non-blocking mode is set, or the port is not enabled,
1224 * then make the check up front and then exit.
1225 */
1226 if ((filp->f_flags & O_NONBLOCK) ||
1227 (tty->flags & (1 << TTY_IO_ERROR))) {
1228 info->flags |= S_NORMAL_ACTIVE;
1229 return 0;
1230 }
1231
1232 if (tty->termios->c_cflag & CLOCAL)
1233 do_clocal = 1;
1234
1235 /*
1236 * Block waiting for the carrier detect and the line to become
1237 * free (i.e., not in use by the callout). While we are in
1238 * this loop, info->count is dropped by one, so that
1239 * rs_close() knows when to free things. We restore it upon
1240 * exit, either normal or abnormal.
1241 */
1242 retval = 0;
1243 add_wait_queue(&info->open_wait, &wait);
1244
1245 info->count--;
1246 info->blocked_open++;
1247 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001248 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001250 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 current->state = TASK_INTERRUPTIBLE;
1252 if (tty_hung_up_p(filp) ||
1253 !(info->flags & S_INITIALIZED)) {
1254#ifdef SERIAL_DO_RESTART
1255 if (info->flags & S_HUP_NOTIFY)
1256 retval = -EAGAIN;
1257 else
1258 retval = -ERESTARTSYS;
1259#else
1260 retval = -EAGAIN;
1261#endif
1262 break;
1263 }
1264 if (!(info->flags & S_CLOSING) && do_clocal)
1265 break;
1266 if (signal_pending(current)) {
1267 retval = -ERESTARTSYS;
1268 break;
1269 }
1270 schedule();
1271 }
1272 current->state = TASK_RUNNING;
1273 remove_wait_queue(&info->open_wait, &wait);
1274 if (!tty_hung_up_p(filp))
1275 info->count++;
1276 info->blocked_open--;
1277
1278 if (retval)
1279 return retval;
1280 info->flags |= S_NORMAL_ACTIVE;
1281 return 0;
1282}
1283
1284/*
1285 * This routine is called whenever a serial port is opened. It
1286 * enables interrupts for a serial port, linking in its S structure into
1287 * the IRQ chain. It also performs the serial-specific
1288 * initialization for the tty structure.
1289 */
1290int rs_open(struct tty_struct *tty, struct file * filp)
1291{
1292 struct m68k_serial *info;
1293 int retval, line;
1294
1295 line = tty->index;
1296
1297 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1298 return -ENODEV;
1299
1300 info = &m68k_soft[line];
1301
1302 if (serial_paranoia_check(info, tty->name, "rs_open"))
1303 return -ENODEV;
1304
1305 info->count++;
1306 tty->driver_data = info;
1307 info->tty = tty;
1308
1309 /*
1310 * Start up serial port
1311 */
1312 retval = startup(info);
1313 if (retval)
1314 return retval;
1315
1316 return block_til_ready(tty, filp, info);
1317}
1318
1319/* Finally, routines used to initialize the serial driver. */
1320
1321static void show_serial_version(void)
1322{
1323 printk("MC68328 serial driver version 1.00\n");
1324}
1325
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001326static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 .open = rs_open,
1328 .close = rs_close,
1329 .write = rs_write,
1330 .flush_chars = rs_flush_chars,
1331 .write_room = rs_write_room,
1332 .chars_in_buffer = rs_chars_in_buffer,
1333 .flush_buffer = rs_flush_buffer,
1334 .ioctl = rs_ioctl,
1335 .throttle = rs_throttle,
1336 .unthrottle = rs_unthrottle,
1337 .set_termios = rs_set_termios,
1338 .stop = rs_stop,
1339 .start = rs_start,
1340 .hangup = rs_hangup,
1341 .set_ldisc = rs_set_ldisc,
1342};
1343
1344/* rs_init inits the driver */
1345static int __init
1346rs68328_init(void)
1347{
1348 int flags, i;
1349 struct m68k_serial *info;
1350
1351 serial_driver = alloc_tty_driver(NR_PORTS);
1352 if (!serial_driver)
1353 return -ENOMEM;
1354
1355 show_serial_version();
1356
1357 /* Initialize the tty_driver structure */
1358 /* SPARC: Not all of this is exactly right for us. */
1359
1360 serial_driver->name = "ttyS";
1361 serial_driver->major = TTY_MAJOR;
1362 serial_driver->minor_start = 64;
1363 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1364 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1365 serial_driver->init_termios = tty_std_termios;
1366 serial_driver->init_termios.c_cflag =
1367 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1368 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1369 tty_set_operations(serial_driver, &rs_ops);
1370
1371 if (tty_register_driver(serial_driver)) {
1372 put_tty_driver(serial_driver);
1373 printk(KERN_ERR "Couldn't register serial driver\n");
1374 return -ENOMEM;
1375 }
1376
Greg Ungerer727dda82006-06-28 15:54:22 +10001377 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 for(i=0;i<NR_PORTS;i++) {
1380
1381 info = &m68k_soft[i];
1382 info->magic = SERIAL_MAGIC;
1383 info->port = (int) &uart_addr[i];
1384 info->tty = 0;
1385 info->irq = uart_irqs[i];
1386 info->custom_divisor = 16;
1387 info->close_delay = 50;
1388 info->closing_wait = 3000;
1389 info->x_char = 0;
1390 info->event = 0;
1391 info->count = 0;
1392 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001393 INIT_WORK(&info->tqueue, do_softint);
1394 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 init_waitqueue_head(&info->open_wait);
1396 init_waitqueue_head(&info->close_wait);
1397 info->line = i;
1398 info->is_cons = 1; /* Means shortcuts work */
1399
1400 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1401 info->port, info->irq);
1402 printk(" is a builtin MC68328 UART\n");
1403
1404 IRQ_ports[info->irq] = info; /* waste of space */
1405
1406#ifdef CONFIG_M68VZ328
1407 if (i > 0 )
1408 PJSEL &= 0xCF; /* PSW enable second port output */
1409#endif
1410
1411 if (request_irq(uart_irqs[i],
1412 rs_interrupt,
1413 IRQ_FLG_STD,
1414 "M68328_UART", NULL))
1415 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001417 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421module_init(rs68328_init);
1422
1423
1424
1425static void m68328_set_baud(void)
1426{
1427 unsigned short ustcnt;
1428 int i;
1429
1430 ustcnt = USTCNT;
1431 USTCNT = ustcnt & ~USTCNT_TXEN;
1432
1433again:
1434 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1435 if (baud_table[i] == m68328_console_baud)
1436 break;
1437 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1438 m68328_console_baud = 9600;
1439 goto again;
1440 }
1441
1442 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1443 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1444 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1445 ustcnt |= USTCNT_8_7;
1446 ustcnt |= USTCNT_TXEN;
1447 USTCNT = ustcnt;
1448 m68328_console_initted = 1;
1449 return;
1450}
1451
1452
1453int m68328_console_setup(struct console *cp, char *arg)
1454{
1455 int i, n = CONSOLE_BAUD_RATE;
1456
1457 if (!cp)
1458 return(-1);
1459
1460 if (arg)
1461 n = simple_strtoul(arg,NULL,0);
1462
1463 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1464 if (baud_table[i] == n)
1465 break;
1466 if (i < BAUD_TABLE_SIZE) {
1467 m68328_console_baud = n;
1468 m68328_console_cbaud = 0;
1469 if (i > 15) {
1470 m68328_console_cbaud |= CBAUDEX;
1471 i -= 15;
1472 }
1473 m68328_console_cbaud |= i;
1474 }
1475
1476 m68328_set_baud(); /* make sure baud rate changes */
1477 return(0);
1478}
1479
1480
1481static struct tty_driver *m68328_console_device(struct console *c, int *index)
1482{
1483 *index = c->index;
1484 return serial_driver;
1485}
1486
1487
1488void m68328_console_write (struct console *co, const char *str,
1489 unsigned int count)
1490{
1491 if (!m68328_console_initted)
1492 m68328_set_baud();
1493 while (count--) {
1494 if (*str == '\n')
1495 rs_put_char('\r');
1496 rs_put_char( *str++ );
1497 }
1498}
1499
1500
1501static struct console m68328_driver = {
1502 .name = "ttyS",
1503 .write = m68328_console_write,
1504 .device = m68328_console_device,
1505 .setup = m68328_console_setup,
1506 .flags = CON_PRINTBUFFER,
1507 .index = -1,
1508};
1509
1510
1511static int __init m68328_console_init(void)
1512{
1513 register_console(&m68328_driver);
1514 return 0;
1515}
1516
1517console_initcall(m68328_console_init);