blob: bbf5bc5892c7be21451fa55c6863af71519ffaff [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
Alan Cox09a6ffa2008-04-30 00:54:01 -0700203static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
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);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700217 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220static void rs_start(struct tty_struct *tty)
221{
222 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
223 m68328_uart *uart = &uart_addr[info->line];
224 unsigned long flags;
225
226 if (serial_paranoia_check(info, tty->name, "rs_start"))
227 return;
228
Greg Ungerer727dda82006-06-28 15:54:22 +1000229 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
231#ifdef USE_INTS
232 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
233#else
234 uart->ustcnt |= USTCNT_TXEN;
235#endif
236 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000237 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/* Drop into either the boot monitor or kadb upon receiving a break
241 * from keyboard/console input.
242 */
243static void batten_down_hatches(void)
244{
245 /* Drop into the debugger */
246}
247
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800248static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250#if 0
251 if(status & DCD) {
252 if((info->tty->termios->c_cflag & CRTSCTS) &&
253 ((info->curregs[3] & AUTO_ENAB)==0)) {
254 info->curregs[3] |= AUTO_ENAB;
255 info->pendregs[3] |= AUTO_ENAB;
256 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
257 }
258 } else {
259 if((info->curregs[3] & AUTO_ENAB)) {
260 info->curregs[3] &= ~AUTO_ENAB;
261 info->pendregs[3] &= ~AUTO_ENAB;
262 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
263 }
264 }
265#endif
266 /* If this is console input and this is a
267 * 'break asserted' status change interrupt
268 * see if we can drop into the debugger
269 */
270 if((status & URX_BREAK) && info->break_abort)
271 batten_down_hatches();
272 return;
273}
274
David Howells7d12e782006-10-05 14:55:46 +0100275static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct tty_struct *tty = info->tty;
278 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800279 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /*
282 * This do { } while() loop will get ALL chars out of Rx FIFO
283 */
284#ifndef CONFIG_XCOPILOT_BUGS
285 do {
286#endif
287 ch = GET_FIELD(rx, URX_RXDATA);
288
289 if(info->is_cons) {
290 if(URX_BREAK & rx) { /* whee, break received */
291 status_handle(info, rx);
292 return;
293#ifdef CONFIG_MAGIC_SYSRQ
294 } else if (ch == 0x10) { /* ^P */
295 show_state();
296 show_free_areas();
297 show_buffers();
298/* show_net_buffers(); */
299 return;
300 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600301 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
303#endif /* CONFIG_MAGIC_SYSRQ */
304 }
305 /* It is a 'keyboard interrupt' ;-) */
306#ifdef CONFIG_CONSOLE
307 wake_up(&keypress_wait);
308#endif
309 }
310
311 if(!tty)
312 goto clear_and_exit;
313
Alan Cox33f0f882006-01-09 20:54:13 -0800314 flag = TTY_NORMAL;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800317 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 status_handle(info, rx);
319 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800320 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 status_handle(info, rx);
322 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800323 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Alan Cox33f0f882006-01-09 20:54:13 -0800326 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifndef CONFIG_XCOPILOT_BUGS
328 } while((rx = uart->urx.w) & URX_DATA_READY);
329#endif
330
Greg Ungerer8e63e662006-02-08 09:19:17 +1000331 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333clear_and_exit:
334 return;
335}
336
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800337static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 m68328_uart *uart = &uart_addr[info->line];
340
341 if (info->x_char) {
342 /* Send next char */
343 uart->utx.b.txdata = info->x_char;
344 info->x_char = 0;
345 goto clear_and_return;
346 }
347
348 if((info->xmit_cnt <= 0) || info->tty->stopped) {
349 /* That's peculiar... TX ints off */
350 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
351 goto clear_and_return;
352 }
353
354 /* Send char */
355 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
356 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
357 info->xmit_cnt--;
358
359 if (info->xmit_cnt < WAKEUP_CHARS)
360 schedule_work(&info->tqueue);
361
362 if(info->xmit_cnt <= 0) {
363 /* All done for now... TX ints off */
364 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
365 goto clear_and_return;
366 }
367
368clear_and_return:
369 /* Clear interrupt (should be auto)*/
370 return;
371}
372
373/*
374 * This is the serial driver's generic interrupt routine
375 */
David Howells7d12e782006-10-05 14:55:46 +0100376irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct m68k_serial * info;
379 m68328_uart *uart;
380 unsigned short rx;
381 unsigned short tx;
382
383 info = IRQ_ports[irq];
384 if(!info)
385 return IRQ_NONE;
386
387 uart = &uart_addr[info->line];
388 rx = uart->urx.w;
389
390#ifdef USE_INTS
391 tx = uart->utx.w;
392
David Howells7d12e782006-10-05 14:55:46 +0100393 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (tx & UTX_TX_AVAIL) transmit_chars(info);
395#else
David Howells7d12e782006-10-05 14:55:46 +0100396 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
398 return IRQ_HANDLED;
399}
400
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000401static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000403 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct tty_struct *tty;
405
406 tty = info->tty;
407 if (!tty)
408 return;
409#if 0
410 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
411 tty_wakeup(tty);
412 }
413#endif
414}
415
416/*
417 * This routine is called from the scheduler tqueue when the interrupt
418 * routine has signalled that a hangup has occurred. The path of
419 * hangup processing is:
420 *
421 * serial interrupt routine -> (scheduler tqueue) ->
422 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
423 *
424 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000425static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000427 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct tty_struct *tty;
429
430 tty = info->tty;
431 if (!tty)
432 return;
433
434 tty_hangup(tty);
435}
436
437
438static int startup(struct m68k_serial * info)
439{
440 m68328_uart *uart = &uart_addr[info->line];
441 unsigned long flags;
442
443 if (info->flags & S_INITIALIZED)
444 return 0;
445
446 if (!info->xmit_buf) {
447 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
448 if (!info->xmit_buf)
449 return -ENOMEM;
450 }
451
Greg Ungerer727dda82006-06-28 15:54:22 +1000452 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /*
455 * Clear the FIFO buffers and disable them
456 * (they will be reenabled in change_speed())
457 */
458
459 uart->ustcnt = USTCNT_UEN;
460 info->xmit_fifo_size = 1;
461 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
462 (void)uart->urx.w;
463
464 /*
465 * Finally, enable sequencing and interrupts
466 */
467#ifdef USE_INTS
468 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
469 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
470#else
471 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
472#endif
473
474 if (info->tty)
475 clear_bit(TTY_IO_ERROR, &info->tty->flags);
476 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
477
478 /*
479 * and set the speed of the serial port
480 */
481
482 change_speed(info);
483
484 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000485 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487}
488
489/*
490 * This routine will shutdown a serial port; interrupts are disabled, and
491 * DTR is dropped if the hangup on close termio flag is on.
492 */
493static void shutdown(struct m68k_serial * info)
494{
495 m68328_uart *uart = &uart_addr[info->line];
496 unsigned long flags;
497
498 uart->ustcnt = 0; /* All off! */
499 if (!(info->flags & S_INITIALIZED))
500 return;
501
Greg Ungerer727dda82006-06-28 15:54:22 +1000502 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (info->xmit_buf) {
505 free_page((unsigned long) info->xmit_buf);
506 info->xmit_buf = 0;
507 }
508
509 if (info->tty)
510 set_bit(TTY_IO_ERROR, &info->tty->flags);
511
512 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000513 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516struct {
517 int divisor, prescale;
518}
519#ifndef CONFIG_M68VZ328
520 hw_baud_table[18] = {
521 {0,0}, /* 0 */
522 {0,0}, /* 50 */
523 {0,0}, /* 75 */
524 {0,0}, /* 110 */
525 {0,0}, /* 134 */
526 {0,0}, /* 150 */
527 {0,0}, /* 200 */
528 {7,0x26}, /* 300 */
529 {6,0x26}, /* 600 */
530 {5,0x26}, /* 1200 */
531 {0,0}, /* 1800 */
532 {4,0x26}, /* 2400 */
533 {3,0x26}, /* 4800 */
534 {2,0x26}, /* 9600 */
535 {1,0x26}, /* 19200 */
536 {0,0x26}, /* 38400 */
537 {1,0x38}, /* 57600 */
538 {0,0x38}, /* 115200 */
539};
540#else
541 hw_baud_table[18] = {
542 {0,0}, /* 0 */
543 {0,0}, /* 50 */
544 {0,0}, /* 75 */
545 {0,0}, /* 110 */
546 {0,0}, /* 134 */
547 {0,0}, /* 150 */
548 {0,0}, /* 200 */
549 {0,0}, /* 300 */
550 {7,0x26}, /* 600 */
551 {6,0x26}, /* 1200 */
552 {0,0}, /* 1800 */
553 {5,0x26}, /* 2400 */
554 {4,0x26}, /* 4800 */
555 {3,0x26}, /* 9600 */
556 {2,0x26}, /* 19200 */
557 {1,0x26}, /* 38400 */
558 {0,0x26}, /* 57600 */
559 {1,0x38}, /* 115200 */
560};
561#endif
562/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
563
564/*
565 * This routine is called to set the UART divisor registers to match
566 * the specified baud rate for a serial port.
567 */
568static void change_speed(struct m68k_serial *info)
569{
570 m68328_uart *uart = &uart_addr[info->line];
571 unsigned short port;
572 unsigned short ustcnt;
573 unsigned cflag;
574 int i;
575
576 if (!info->tty || !info->tty->termios)
577 return;
578 cflag = info->tty->termios->c_cflag;
579 if (!(port = info->port))
580 return;
581
582 ustcnt = uart->ustcnt;
583 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
584
585 i = cflag & CBAUD;
586 if (i & CBAUDEX) {
587 i = (i & ~CBAUDEX) + B38400;
588 }
589
590 info->baud = baud_table[i];
591 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
592 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
593
594 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
595
596 if ((cflag & CSIZE) == CS8)
597 ustcnt |= USTCNT_8_7;
598
599 if (cflag & CSTOPB)
600 ustcnt |= USTCNT_STOP;
601
602 if (cflag & PARENB)
603 ustcnt |= USTCNT_PARITYEN;
604 if (cflag & PARODD)
605 ustcnt |= USTCNT_ODD_EVEN;
606
607#ifdef CONFIG_SERIAL_68328_RTS_CTS
608 if (cflag & CRTSCTS) {
609 uart->utx.w &= ~ UTX_NOCTS;
610 } else {
611 uart->utx.w |= UTX_NOCTS;
612 }
613#endif
614
615 ustcnt |= USTCNT_TXEN;
616
617 uart->ustcnt = ustcnt;
618 return;
619}
620
621/*
622 * Fair output driver allows a process to speak.
623 */
624static void rs_fair_output(void)
625{
626 int left; /* Output no more than that */
627 unsigned long flags;
628 struct m68k_serial *info = &m68k_soft[0];
629 char c;
630
631 if (info == 0) return;
632 if (info->xmit_buf == 0) return;
633
Greg Ungerer727dda82006-06-28 15:54:22 +1000634 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 left = info->xmit_cnt;
636 while (left != 0) {
637 c = info->xmit_buf[info->xmit_tail];
638 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
639 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000640 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 rs_put_char(c);
643
Greg Ungerer727dda82006-06-28 15:54:22 +1000644 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 left = min(info->xmit_cnt, left-1);
646 }
647
648 /* Last character is being transmitted now (hopefully). */
649 udelay(5);
650
Greg Ungerer727dda82006-06-28 15:54:22 +1000651 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return;
653}
654
655/*
656 * m68k_console_print is registered for printk.
657 */
658void console_print_68328(const char *p)
659{
660 char c;
661
662 while((c=*(p++)) != 0) {
663 if(c == '\n')
664 rs_put_char('\r');
665 rs_put_char(c);
666 }
667
668 /* Comment this if you want to have a strict interrupt-driven output */
669 rs_fair_output();
670
671 return;
672}
673
674static void rs_set_ldisc(struct tty_struct *tty)
675{
676 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
677
678 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
679 return;
680
681 info->is_cons = (tty->termios->c_line == N_TTY);
682
683 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
684}
685
686static void rs_flush_chars(struct tty_struct *tty)
687{
688 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
689 m68328_uart *uart = &uart_addr[info->line];
690 unsigned long flags;
691
692 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
693 return;
694#ifndef USE_INTS
695 for(;;) {
696#endif
697
698 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000699 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
702 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000703 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return;
705 }
706
707#ifdef USE_INTS
708 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
709#else
710 uart->ustcnt |= USTCNT_TXEN;
711#endif
712
713#ifdef USE_INTS
714 if (uart->utx.w & UTX_TX_AVAIL) {
715#else
716 if (1) {
717#endif
718 /* Send char */
719 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
720 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
721 info->xmit_cnt--;
722 }
723
724#ifndef USE_INTS
725 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
726 }
727#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000728 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731extern void console_printn(const char * b, int count);
732
733static int rs_write(struct tty_struct * tty,
734 const unsigned char *buf, int count)
735{
736 int c, total = 0;
737 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
738 m68328_uart *uart = &uart_addr[info->line];
739 unsigned long flags;
740
741 if (serial_paranoia_check(info, tty->name, "rs_write"))
742 return 0;
743
744 if (!tty || !info->xmit_buf)
745 return 0;
746
Greg Ungerer727dda82006-06-28 15:54:22 +1000747 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000749 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
751 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000752 local_irq_restore(flags);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (c <= 0)
755 break;
756
757 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000758
759 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
761 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000762 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 buf += c;
764 count -= c;
765 total += c;
766 }
767
768 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
769 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000770 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#ifndef USE_INTS
772 while(info->xmit_cnt) {
773#endif
774
775 uart->ustcnt |= USTCNT_TXEN;
776#ifdef USE_INTS
777 uart->ustcnt |= USTCNT_TX_INTR_MASK;
778#else
779 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
780#endif
781 if (uart->utx.w & UTX_TX_AVAIL) {
782 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
783 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
784 info->xmit_cnt--;
785 }
786
787#ifndef USE_INTS
788 }
789#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000790 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return total;
794}
795
796static int rs_write_room(struct tty_struct *tty)
797{
798 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
799 int ret;
800
801 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
802 return 0;
803 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
804 if (ret < 0)
805 ret = 0;
806 return ret;
807}
808
809static int rs_chars_in_buffer(struct tty_struct *tty)
810{
811 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
812
813 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
814 return 0;
815 return info->xmit_cnt;
816}
817
818static void rs_flush_buffer(struct tty_struct *tty)
819{
820 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000821 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
824 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000825 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000827 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 tty_wakeup(tty);
829}
830
831/*
832 * ------------------------------------------------------------
833 * rs_throttle()
834 *
835 * This routine is called by the upper-layer tty layer to signal that
836 * incoming characters should be throttled.
837 * ------------------------------------------------------------
838 */
839static void rs_throttle(struct tty_struct * tty)
840{
841 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
842
843 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
844 return;
845
846 if (I_IXOFF(tty))
847 info->x_char = STOP_CHAR(tty);
848
849 /* Turn off RTS line (do this atomic) */
850}
851
852static void rs_unthrottle(struct tty_struct * tty)
853{
854 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
855
856 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
857 return;
858
859 if (I_IXOFF(tty)) {
860 if (info->x_char)
861 info->x_char = 0;
862 else
863 info->x_char = START_CHAR(tty);
864 }
865
866 /* Assert RTS line (do this atomic) */
867}
868
869/*
870 * ------------------------------------------------------------
871 * rs_ioctl() and friends
872 * ------------------------------------------------------------
873 */
874
875static int get_serial_info(struct m68k_serial * info,
876 struct serial_struct * retinfo)
877{
878 struct serial_struct tmp;
879
880 if (!retinfo)
881 return -EFAULT;
882 memset(&tmp, 0, sizeof(tmp));
883 tmp.type = info->type;
884 tmp.line = info->line;
885 tmp.port = info->port;
886 tmp.irq = info->irq;
887 tmp.flags = info->flags;
888 tmp.baud_base = info->baud_base;
889 tmp.close_delay = info->close_delay;
890 tmp.closing_wait = info->closing_wait;
891 tmp.custom_divisor = info->custom_divisor;
892 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
893 return 0;
894}
895
896static int set_serial_info(struct m68k_serial * info,
897 struct serial_struct * new_info)
898{
899 struct serial_struct new_serial;
900 struct m68k_serial old_info;
901 int retval = 0;
902
903 if (!new_info)
904 return -EFAULT;
905 copy_from_user(&new_serial,new_info,sizeof(new_serial));
906 old_info = *info;
907
908 if (!capable(CAP_SYS_ADMIN)) {
909 if ((new_serial.baud_base != info->baud_base) ||
910 (new_serial.type != info->type) ||
911 (new_serial.close_delay != info->close_delay) ||
912 ((new_serial.flags & ~S_USR_MASK) !=
913 (info->flags & ~S_USR_MASK)))
914 return -EPERM;
915 info->flags = ((info->flags & ~S_USR_MASK) |
916 (new_serial.flags & S_USR_MASK));
917 info->custom_divisor = new_serial.custom_divisor;
918 goto check_and_exit;
919 }
920
921 if (info->count > 1)
922 return -EBUSY;
923
924 /*
925 * OK, past this point, all the error checking has been done.
926 * At this point, we start making changes.....
927 */
928
929 info->baud_base = new_serial.baud_base;
930 info->flags = ((info->flags & ~S_FLAGS) |
931 (new_serial.flags & S_FLAGS));
932 info->type = new_serial.type;
933 info->close_delay = new_serial.close_delay;
934 info->closing_wait = new_serial.closing_wait;
935
936check_and_exit:
937 retval = startup(info);
938 return retval;
939}
940
941/*
942 * get_lsr_info - get line status register info
943 *
944 * Purpose: Let user call ioctl() to get info when the UART physically
945 * is emptied. On bus types like RS485, the transmitter must
946 * release the bus after transmitting. This must be done when
947 * the transmit shift register is empty, not be done when the
948 * transmit holding register is empty. This functionality
949 * allows an RS485 driver to be written in user space.
950 */
951static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
952{
953#ifdef CONFIG_SERIAL_68328_RTS_CTS
954 m68328_uart *uart = &uart_addr[info->line];
955#endif
956 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000957 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Greg Ungerer727dda82006-06-28 15:54:22 +1000959 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960#ifdef CONFIG_SERIAL_68328_RTS_CTS
961 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
962#else
963 status = 0;
964#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000965 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 put_user(status,value);
967 return 0;
968}
969
970/*
971 * This routine sends a break character out the serial port.
972 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700973static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 m68328_uart *uart = &uart_addr[info->line];
976 unsigned long flags;
977 if (!info->port)
978 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000979 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef USE_INTS
981 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700982 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 uart->utx.w &= ~UTX_SEND_BREAK;
984#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000985 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static int rs_ioctl(struct tty_struct *tty, struct file * file,
989 unsigned int cmd, unsigned long arg)
990{
991 int error;
992 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
993 int retval;
994
995 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
996 return -ENODEV;
997
998 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
999 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1000 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1001 if (tty->flags & (1 << TTY_IO_ERROR))
1002 return -EIO;
1003 }
1004
1005 switch (cmd) {
1006 case TCSBRK: /* SVID version: non-zero arg --> no break */
1007 retval = tty_check_change(tty);
1008 if (retval)
1009 return retval;
1010 tty_wait_until_sent(tty, 0);
1011 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001012 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014 case TCSBRKP: /* support for POSIX tcsendbreak() */
1015 retval = tty_check_change(tty);
1016 if (retval)
1017 return retval;
1018 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001019 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 case TIOCGSERIAL:
1022 if (access_ok(VERIFY_WRITE, (void *) arg,
1023 sizeof(struct serial_struct)))
1024 return get_serial_info(info,
1025 (struct serial_struct *) arg);
1026 return -EFAULT;
1027 case TIOCSSERIAL:
1028 return set_serial_info(info,
1029 (struct serial_struct *) arg);
1030 case TIOCSERGETLSR: /* Get line status register */
1031 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001032 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return get_lsr_info(info, (unsigned int *) arg);
1034 return -EFAULT;
1035 case TIOCSERGSTRUCT:
1036 if (!access_ok(VERIFY_WRITE, (void *) arg,
1037 sizeof(struct m68k_serial)))
1038 return -EFAULT;
1039 copy_to_user((struct m68k_serial *) arg,
1040 info, sizeof(struct m68k_serial));
1041 return 0;
1042
1043 default:
1044 return -ENOIOCTLCMD;
1045 }
1046 return 0;
1047}
1048
Alan Cox606d0992006-12-08 02:38:45 -08001049static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 change_speed(info);
1054
1055 if ((old_termios->c_cflag & CRTSCTS) &&
1056 !(tty->termios->c_cflag & CRTSCTS)) {
1057 tty->hw_stopped = 0;
1058 rs_start(tty);
1059 }
1060
1061}
1062
1063/*
1064 * ------------------------------------------------------------
1065 * rs_close()
1066 *
1067 * This routine is called when the serial port gets closed. First, we
1068 * wait for the last remaining data to be sent. Then, we unlink its
1069 * S structure from the interrupt chain if necessary, and we free
1070 * that IRQ if nothing is left in the chain.
1071 * ------------------------------------------------------------
1072 */
1073static void rs_close(struct tty_struct *tty, struct file * filp)
1074{
1075 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1076 m68328_uart *uart = &uart_addr[info->line];
1077 unsigned long flags;
1078
1079 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1080 return;
1081
Greg Ungerer727dda82006-06-28 15:54:22 +10001082 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001085 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return;
1087 }
1088
1089 if ((tty->count == 1) && (info->count != 1)) {
1090 /*
1091 * Uh, oh. tty->count is 1, which means that the tty
1092 * structure will be freed. Info->count should always
1093 * be one in these conditions. If it's greater than
1094 * one, we've got real problems, since it means the
1095 * serial port won't be shutdown.
1096 */
1097 printk("rs_close: bad serial port count; tty->count is 1, "
1098 "info->count is %d\n", info->count);
1099 info->count = 1;
1100 }
1101 if (--info->count < 0) {
1102 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1103 info->line, info->count);
1104 info->count = 0;
1105 }
1106 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001107 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return;
1109 }
1110 info->flags |= S_CLOSING;
1111 /*
1112 * Now we wait for the transmit buffer to clear; and we notify
1113 * the line discipline to only process XON/XOFF characters.
1114 */
1115 tty->closing = 1;
1116 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1117 tty_wait_until_sent(tty, info->closing_wait);
1118 /*
1119 * At this point we stop accepting input. To do this, we
1120 * disable the receive line status interrupts, and tell the
1121 * interrupt driver to stop checking the data ready bit in the
1122 * line status register.
1123 */
1124
1125 uart->ustcnt &= ~USTCNT_RXEN;
1126 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1127
1128 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001129 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 tty_ldisc_flush(tty);
1132 tty->closing = 0;
1133 info->event = 0;
1134 info->tty = 0;
1135#warning "This is not and has never been valid so fix it"
1136#if 0
1137 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1138 if (tty->ldisc.close)
1139 (tty->ldisc.close)(tty);
1140 tty->ldisc = ldiscs[N_TTY];
1141 tty->termios->c_line = N_TTY;
1142 if (tty->ldisc.open)
1143 (tty->ldisc.open)(tty);
1144 }
1145#endif
1146 if (info->blocked_open) {
1147 if (info->close_delay) {
1148 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1149 }
1150 wake_up_interruptible(&info->open_wait);
1151 }
1152 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1153 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001154 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157/*
1158 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1159 */
1160void rs_hangup(struct tty_struct *tty)
1161{
1162 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1163
1164 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1165 return;
1166
1167 rs_flush_buffer(tty);
1168 shutdown(info);
1169 info->event = 0;
1170 info->count = 0;
1171 info->flags &= ~S_NORMAL_ACTIVE;
1172 info->tty = 0;
1173 wake_up_interruptible(&info->open_wait);
1174}
1175
1176/*
1177 * ------------------------------------------------------------
1178 * rs_open() and friends
1179 * ------------------------------------------------------------
1180 */
1181static int block_til_ready(struct tty_struct *tty, struct file * filp,
1182 struct m68k_serial *info)
1183{
1184 DECLARE_WAITQUEUE(wait, current);
1185 int retval;
1186 int do_clocal = 0;
1187
1188 /*
1189 * If the device is in the middle of being closed, then block
1190 * until it's done, and then try again.
1191 */
1192 if (info->flags & S_CLOSING) {
1193 interruptible_sleep_on(&info->close_wait);
1194#ifdef SERIAL_DO_RESTART
1195 if (info->flags & S_HUP_NOTIFY)
1196 return -EAGAIN;
1197 else
1198 return -ERESTARTSYS;
1199#else
1200 return -EAGAIN;
1201#endif
1202 }
1203
1204 /*
1205 * If non-blocking mode is set, or the port is not enabled,
1206 * then make the check up front and then exit.
1207 */
1208 if ((filp->f_flags & O_NONBLOCK) ||
1209 (tty->flags & (1 << TTY_IO_ERROR))) {
1210 info->flags |= S_NORMAL_ACTIVE;
1211 return 0;
1212 }
1213
1214 if (tty->termios->c_cflag & CLOCAL)
1215 do_clocal = 1;
1216
1217 /*
1218 * Block waiting for the carrier detect and the line to become
1219 * free (i.e., not in use by the callout). While we are in
1220 * this loop, info->count is dropped by one, so that
1221 * rs_close() knows when to free things. We restore it upon
1222 * exit, either normal or abnormal.
1223 */
1224 retval = 0;
1225 add_wait_queue(&info->open_wait, &wait);
1226
1227 info->count--;
1228 info->blocked_open++;
1229 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001230 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001232 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 current->state = TASK_INTERRUPTIBLE;
1234 if (tty_hung_up_p(filp) ||
1235 !(info->flags & S_INITIALIZED)) {
1236#ifdef SERIAL_DO_RESTART
1237 if (info->flags & S_HUP_NOTIFY)
1238 retval = -EAGAIN;
1239 else
1240 retval = -ERESTARTSYS;
1241#else
1242 retval = -EAGAIN;
1243#endif
1244 break;
1245 }
1246 if (!(info->flags & S_CLOSING) && do_clocal)
1247 break;
1248 if (signal_pending(current)) {
1249 retval = -ERESTARTSYS;
1250 break;
1251 }
1252 schedule();
1253 }
1254 current->state = TASK_RUNNING;
1255 remove_wait_queue(&info->open_wait, &wait);
1256 if (!tty_hung_up_p(filp))
1257 info->count++;
1258 info->blocked_open--;
1259
1260 if (retval)
1261 return retval;
1262 info->flags |= S_NORMAL_ACTIVE;
1263 return 0;
1264}
1265
1266/*
1267 * This routine is called whenever a serial port is opened. It
1268 * enables interrupts for a serial port, linking in its S structure into
1269 * the IRQ chain. It also performs the serial-specific
1270 * initialization for the tty structure.
1271 */
1272int rs_open(struct tty_struct *tty, struct file * filp)
1273{
1274 struct m68k_serial *info;
1275 int retval, line;
1276
1277 line = tty->index;
1278
1279 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1280 return -ENODEV;
1281
1282 info = &m68k_soft[line];
1283
1284 if (serial_paranoia_check(info, tty->name, "rs_open"))
1285 return -ENODEV;
1286
1287 info->count++;
1288 tty->driver_data = info;
1289 info->tty = tty;
1290
1291 /*
1292 * Start up serial port
1293 */
1294 retval = startup(info);
1295 if (retval)
1296 return retval;
1297
1298 return block_til_ready(tty, filp, info);
1299}
1300
1301/* Finally, routines used to initialize the serial driver. */
1302
1303static void show_serial_version(void)
1304{
1305 printk("MC68328 serial driver version 1.00\n");
1306}
1307
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001308static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 .open = rs_open,
1310 .close = rs_close,
1311 .write = rs_write,
1312 .flush_chars = rs_flush_chars,
1313 .write_room = rs_write_room,
1314 .chars_in_buffer = rs_chars_in_buffer,
1315 .flush_buffer = rs_flush_buffer,
1316 .ioctl = rs_ioctl,
1317 .throttle = rs_throttle,
1318 .unthrottle = rs_unthrottle,
1319 .set_termios = rs_set_termios,
1320 .stop = rs_stop,
1321 .start = rs_start,
1322 .hangup = rs_hangup,
1323 .set_ldisc = rs_set_ldisc,
1324};
1325
1326/* rs_init inits the driver */
1327static int __init
1328rs68328_init(void)
1329{
1330 int flags, i;
1331 struct m68k_serial *info;
1332
1333 serial_driver = alloc_tty_driver(NR_PORTS);
1334 if (!serial_driver)
1335 return -ENOMEM;
1336
1337 show_serial_version();
1338
1339 /* Initialize the tty_driver structure */
1340 /* SPARC: Not all of this is exactly right for us. */
1341
1342 serial_driver->name = "ttyS";
1343 serial_driver->major = TTY_MAJOR;
1344 serial_driver->minor_start = 64;
1345 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1346 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1347 serial_driver->init_termios = tty_std_termios;
1348 serial_driver->init_termios.c_cflag =
1349 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1350 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1351 tty_set_operations(serial_driver, &rs_ops);
1352
1353 if (tty_register_driver(serial_driver)) {
1354 put_tty_driver(serial_driver);
1355 printk(KERN_ERR "Couldn't register serial driver\n");
1356 return -ENOMEM;
1357 }
1358
Greg Ungerer727dda82006-06-28 15:54:22 +10001359 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 for(i=0;i<NR_PORTS;i++) {
1362
1363 info = &m68k_soft[i];
1364 info->magic = SERIAL_MAGIC;
1365 info->port = (int) &uart_addr[i];
1366 info->tty = 0;
1367 info->irq = uart_irqs[i];
1368 info->custom_divisor = 16;
1369 info->close_delay = 50;
1370 info->closing_wait = 3000;
1371 info->x_char = 0;
1372 info->event = 0;
1373 info->count = 0;
1374 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001375 INIT_WORK(&info->tqueue, do_softint);
1376 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 init_waitqueue_head(&info->open_wait);
1378 init_waitqueue_head(&info->close_wait);
1379 info->line = i;
1380 info->is_cons = 1; /* Means shortcuts work */
1381
1382 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1383 info->port, info->irq);
1384 printk(" is a builtin MC68328 UART\n");
1385
1386 IRQ_ports[info->irq] = info; /* waste of space */
1387
1388#ifdef CONFIG_M68VZ328
1389 if (i > 0 )
1390 PJSEL &= 0xCF; /* PSW enable second port output */
1391#endif
1392
1393 if (request_irq(uart_irqs[i],
1394 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001395 IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 "M68328_UART", NULL))
1397 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001399 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403module_init(rs68328_init);
1404
1405
1406
1407static void m68328_set_baud(void)
1408{
1409 unsigned short ustcnt;
1410 int i;
1411
1412 ustcnt = USTCNT;
1413 USTCNT = ustcnt & ~USTCNT_TXEN;
1414
1415again:
1416 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1417 if (baud_table[i] == m68328_console_baud)
1418 break;
1419 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1420 m68328_console_baud = 9600;
1421 goto again;
1422 }
1423
1424 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1425 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1426 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1427 ustcnt |= USTCNT_8_7;
1428 ustcnt |= USTCNT_TXEN;
1429 USTCNT = ustcnt;
1430 m68328_console_initted = 1;
1431 return;
1432}
1433
1434
1435int m68328_console_setup(struct console *cp, char *arg)
1436{
1437 int i, n = CONSOLE_BAUD_RATE;
1438
1439 if (!cp)
1440 return(-1);
1441
1442 if (arg)
1443 n = simple_strtoul(arg,NULL,0);
1444
1445 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1446 if (baud_table[i] == n)
1447 break;
1448 if (i < BAUD_TABLE_SIZE) {
1449 m68328_console_baud = n;
1450 m68328_console_cbaud = 0;
1451 if (i > 15) {
1452 m68328_console_cbaud |= CBAUDEX;
1453 i -= 15;
1454 }
1455 m68328_console_cbaud |= i;
1456 }
1457
1458 m68328_set_baud(); /* make sure baud rate changes */
1459 return(0);
1460}
1461
1462
1463static struct tty_driver *m68328_console_device(struct console *c, int *index)
1464{
1465 *index = c->index;
1466 return serial_driver;
1467}
1468
1469
1470void m68328_console_write (struct console *co, const char *str,
1471 unsigned int count)
1472{
1473 if (!m68328_console_initted)
1474 m68328_set_baud();
1475 while (count--) {
1476 if (*str == '\n')
1477 rs_put_char('\r');
1478 rs_put_char( *str++ );
1479 }
1480}
1481
1482
1483static struct console m68328_driver = {
1484 .name = "ttyS",
1485 .write = m68328_console_write,
1486 .device = m68328_console_device,
1487 .setup = m68328_console_setup,
1488 .flags = CON_PRINTBUFFER,
1489 .index = -1,
1490};
1491
1492
1493static int __init m68328_console_init(void)
1494{
1495 register_console(&m68328_driver);
1496 return 0;
1497}
1498
1499console_initcall(m68328_console_init);