blob: d935b2d04f93345c9cc577373dd9a89e4357188c [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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
71
72/* multiple ports are contiguous in memory */
73m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
74
75struct tty_struct m68k_ttys;
76struct m68k_serial *m68k_consinfo = 0;
77
78#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
79
80#ifdef CONFIG_CONSOLE
81extern wait_queue_head_t keypress_wait;
82#endif
83
84struct tty_driver *serial_driver;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* number of characters left in xmit buffer before we ask for more */
87#define WAKEUP_CHARS 256
88
89/* Debugging... DEBUG_INTR is bad to use when one of the zs
90 * lines is your console ;(
91 */
92#undef SERIAL_DEBUG_INTR
93#undef SERIAL_DEBUG_OPEN
94#undef SERIAL_DEBUG_FLOW
95
96#define RS_ISR_PASS_LIMIT 256
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void change_speed(struct m68k_serial *info);
99
100/*
101 * Setup for console. Argument comes from the boot command line.
102 */
103
104#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
105#define CONSOLE_BAUD_RATE 115200
106#define DEFAULT_CBAUD B115200
107#else
108 /* (es) */
109 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
110 #ifdef CONFIG_M68VZ328
111 #define CONSOLE_BAUD_RATE 19200
112 #define DEFAULT_CBAUD B19200
113 #endif
114 /* (/es) */
115#endif
116
117#ifndef CONSOLE_BAUD_RATE
118#define CONSOLE_BAUD_RATE 9600
119#define DEFAULT_CBAUD B9600
120#endif
121
122
123static int m68328_console_initted = 0;
124static int m68328_console_baud = CONSOLE_BAUD_RATE;
125static int m68328_console_cbaud = DEFAULT_CBAUD;
126
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static inline int serial_paranoia_check(struct m68k_serial *info,
129 char *name, const char *routine)
130{
131#ifdef SERIAL_PARANOIA_CHECK
132 static const char *badmagic =
133 "Warning: bad magic number for serial struct %s in %s\n";
134 static const char *badinfo =
135 "Warning: null m68k_serial for %s in %s\n";
136
137 if (!info) {
138 printk(badinfo, name, routine);
139 return 1;
140 }
141 if (info->magic != SERIAL_MAGIC) {
142 printk(badmagic, name, routine);
143 return 1;
144 }
145#endif
146 return 0;
147}
148
149/*
150 * This is used to figure out the divisor speeds and the timeouts
151 */
152static int baud_table[] = {
153 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
154 9600, 19200, 38400, 57600, 115200, 0 };
155
156#define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
157
158/* Sets or clears DTR/RTS on the requested line */
159static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
160{
161 if (set) {
162 /* set the RTS/CTS line */
163 } else {
164 /* clear it */
165 }
166 return;
167}
168
169/* Utility routines */
170static inline int get_baud(struct m68k_serial *ss)
171{
172 unsigned long result = 115200;
173 unsigned short int baud = uart_addr[ss->line].ubaud;
174 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
175 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
176
177 return result;
178}
179
180/*
181 * ------------------------------------------------------------
182 * rs_stop() and rs_start()
183 *
184 * This routines are called before setting or resetting tty->stopped.
185 * They enable or disable transmitter interrupts, as necessary.
186 * ------------------------------------------------------------
187 */
188static void rs_stop(struct tty_struct *tty)
189{
190 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
191 m68328_uart *uart = &uart_addr[info->line];
192 unsigned long flags;
193
194 if (serial_paranoia_check(info, tty->name, "rs_stop"))
195 return;
196
Greg Ungerer727dda82006-06-28 15:54:22 +1000197 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000199 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Alan Cox09a6ffa2008-04-30 00:54:01 -0700202static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int flags, loops = 0;
205
Greg Ungerer727dda82006-06-28 15:54:22 +1000206 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
209 loops++;
210 udelay(5);
211 }
212
213 UTX_TXDATA = ch;
214 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000215 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700216 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static void rs_start(struct tty_struct *tty)
220{
221 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
222 m68328_uart *uart = &uart_addr[info->line];
223 unsigned long flags;
224
225 if (serial_paranoia_check(info, tty->name, "rs_start"))
226 return;
227
Greg Ungerer727dda82006-06-28 15:54:22 +1000228 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
230#ifdef USE_INTS
231 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
232#else
233 uart->ustcnt |= USTCNT_TXEN;
234#endif
235 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000236 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/* Drop into either the boot monitor or kadb upon receiving a break
240 * from keyboard/console input.
241 */
242static void batten_down_hatches(void)
243{
244 /* Drop into the debugger */
245}
246
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800247static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249#if 0
250 if(status & DCD) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100251 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ((info->curregs[3] & AUTO_ENAB)==0)) {
253 info->curregs[3] |= AUTO_ENAB;
254 info->pendregs[3] |= AUTO_ENAB;
255 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
256 }
257 } else {
258 if((info->curregs[3] & AUTO_ENAB)) {
259 info->curregs[3] &= ~AUTO_ENAB;
260 info->pendregs[3] &= ~AUTO_ENAB;
261 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
262 }
263 }
264#endif
265 /* If this is console input and this is a
266 * 'break asserted' status change interrupt
267 * see if we can drop into the debugger
268 */
269 if((status & URX_BREAK) && info->break_abort)
270 batten_down_hatches();
271 return;
272}
273
David Howells7d12e782006-10-05 14:55:46 +0100274static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100276 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800278 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /*
281 * This do { } while() loop will get ALL chars out of Rx FIFO
282 */
283#ifndef CONFIG_XCOPILOT_BUGS
284 do {
285#endif
286 ch = GET_FIELD(rx, URX_RXDATA);
287
288 if(info->is_cons) {
289 if(URX_BREAK & rx) { /* whee, break received */
290 status_handle(info, rx);
291 return;
292#ifdef CONFIG_MAGIC_SYSRQ
293 } else if (ch == 0x10) { /* ^P */
294 show_state();
295 show_free_areas();
296 show_buffers();
297/* show_net_buffers(); */
298 return;
299 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600300 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return;
302#endif /* CONFIG_MAGIC_SYSRQ */
303 }
304 /* It is a 'keyboard interrupt' ;-) */
305#ifdef CONFIG_CONSOLE
306 wake_up(&keypress_wait);
307#endif
308 }
309
310 if(!tty)
311 goto clear_and_exit;
312
Alan Cox33f0f882006-01-09 20:54:13 -0800313 flag = TTY_NORMAL;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800316 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 status_handle(info, rx);
318 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800319 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 status_handle(info, rx);
321 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800322 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Alan Cox33f0f882006-01-09 20:54:13 -0800325 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#ifndef CONFIG_XCOPILOT_BUGS
327 } while((rx = uart->urx.w) & URX_DATA_READY);
328#endif
329
Greg Ungerer8e63e662006-02-08 09:19:17 +1000330 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332clear_and_exit:
333 return;
334}
335
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800336static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 m68328_uart *uart = &uart_addr[info->line];
339
340 if (info->x_char) {
341 /* Send next char */
342 uart->utx.b.txdata = info->x_char;
343 info->x_char = 0;
344 goto clear_and_return;
345 }
346
Takashi Iwaia88487c2008-07-16 21:54:42 +0100347 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* That's peculiar... TX ints off */
349 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
350 goto clear_and_return;
351 }
352
353 /* Send char */
354 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
355 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
356 info->xmit_cnt--;
357
358 if (info->xmit_cnt < WAKEUP_CHARS)
359 schedule_work(&info->tqueue);
360
361 if(info->xmit_cnt <= 0) {
362 /* All done for now... TX ints off */
363 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
364 goto clear_and_return;
365 }
366
367clear_and_return:
368 /* Clear interrupt (should be auto)*/
369 return;
370}
371
372/*
373 * This is the serial driver's generic interrupt routine
374 */
David Howells7d12e782006-10-05 14:55:46 +0100375irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Alan Cox25db8ad2008-08-19 20:49:40 -0700377 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 m68328_uart *uart;
379 unsigned short rx;
380 unsigned short tx;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 uart = &uart_addr[info->line];
383 rx = uart->urx.w;
384
385#ifdef USE_INTS
386 tx = uart->utx.w;
387
David Howells7d12e782006-10-05 14:55:46 +0100388 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (tx & UTX_TX_AVAIL) transmit_chars(info);
390#else
David Howells7d12e782006-10-05 14:55:46 +0100391 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif
393 return IRQ_HANDLED;
394}
395
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000396static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000398 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct tty_struct *tty;
400
Takashi Iwaia88487c2008-07-16 21:54:42 +0100401 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!tty)
403 return;
404#if 0
405 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
406 tty_wakeup(tty);
407 }
408#endif
409}
410
411/*
412 * This routine is called from the scheduler tqueue when the interrupt
413 * routine has signalled that a hangup has occurred. The path of
414 * hangup processing is:
415 *
416 * serial interrupt routine -> (scheduler tqueue) ->
417 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
418 *
419 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000420static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000422 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct tty_struct *tty;
424
Takashi Iwaia88487c2008-07-16 21:54:42 +0100425 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!tty)
427 return;
428
429 tty_hangup(tty);
430}
431
432
433static int startup(struct m68k_serial * info)
434{
435 m68328_uart *uart = &uart_addr[info->line];
436 unsigned long flags;
437
438 if (info->flags & S_INITIALIZED)
439 return 0;
440
441 if (!info->xmit_buf) {
442 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
443 if (!info->xmit_buf)
444 return -ENOMEM;
445 }
446
Greg Ungerer727dda82006-06-28 15:54:22 +1000447 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /*
450 * Clear the FIFO buffers and disable them
451 * (they will be reenabled in change_speed())
452 */
453
454 uart->ustcnt = USTCNT_UEN;
455 info->xmit_fifo_size = 1;
456 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
457 (void)uart->urx.w;
458
459 /*
460 * Finally, enable sequencing and interrupts
461 */
462#ifdef USE_INTS
463 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
464 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
465#else
466 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
467#endif
468
Takashi Iwaia88487c2008-07-16 21:54:42 +0100469 if (info->port.tty)
470 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
472
473 /*
474 * and set the speed of the serial port
475 */
476
477 change_speed(info);
478
479 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000480 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482}
483
484/*
485 * This routine will shutdown a serial port; interrupts are disabled, and
486 * DTR is dropped if the hangup on close termio flag is on.
487 */
488static void shutdown(struct m68k_serial * info)
489{
490 m68328_uart *uart = &uart_addr[info->line];
491 unsigned long flags;
492
493 uart->ustcnt = 0; /* All off! */
494 if (!(info->flags & S_INITIALIZED))
495 return;
496
Greg Ungerer727dda82006-06-28 15:54:22 +1000497 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (info->xmit_buf) {
500 free_page((unsigned long) info->xmit_buf);
501 info->xmit_buf = 0;
502 }
503
Takashi Iwaia88487c2008-07-16 21:54:42 +0100504 if (info->port.tty)
505 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000508 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511struct {
512 int divisor, prescale;
513}
514#ifndef CONFIG_M68VZ328
515 hw_baud_table[18] = {
516 {0,0}, /* 0 */
517 {0,0}, /* 50 */
518 {0,0}, /* 75 */
519 {0,0}, /* 110 */
520 {0,0}, /* 134 */
521 {0,0}, /* 150 */
522 {0,0}, /* 200 */
523 {7,0x26}, /* 300 */
524 {6,0x26}, /* 600 */
525 {5,0x26}, /* 1200 */
526 {0,0}, /* 1800 */
527 {4,0x26}, /* 2400 */
528 {3,0x26}, /* 4800 */
529 {2,0x26}, /* 9600 */
530 {1,0x26}, /* 19200 */
531 {0,0x26}, /* 38400 */
532 {1,0x38}, /* 57600 */
533 {0,0x38}, /* 115200 */
534};
535#else
536 hw_baud_table[18] = {
537 {0,0}, /* 0 */
538 {0,0}, /* 50 */
539 {0,0}, /* 75 */
540 {0,0}, /* 110 */
541 {0,0}, /* 134 */
542 {0,0}, /* 150 */
543 {0,0}, /* 200 */
544 {0,0}, /* 300 */
545 {7,0x26}, /* 600 */
546 {6,0x26}, /* 1200 */
547 {0,0}, /* 1800 */
548 {5,0x26}, /* 2400 */
549 {4,0x26}, /* 4800 */
550 {3,0x26}, /* 9600 */
551 {2,0x26}, /* 19200 */
552 {1,0x26}, /* 38400 */
553 {0,0x26}, /* 57600 */
554 {1,0x38}, /* 115200 */
555};
556#endif
557/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
558
559/*
560 * This routine is called to set the UART divisor registers to match
561 * the specified baud rate for a serial port.
562 */
563static void change_speed(struct m68k_serial *info)
564{
565 m68328_uart *uart = &uart_addr[info->line];
566 unsigned short port;
567 unsigned short ustcnt;
568 unsigned cflag;
569 int i;
570
Takashi Iwaia88487c2008-07-16 21:54:42 +0100571 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100573 cflag = info->port.tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!(port = info->port))
575 return;
576
577 ustcnt = uart->ustcnt;
578 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
579
580 i = cflag & CBAUD;
581 if (i & CBAUDEX) {
582 i = (i & ~CBAUDEX) + B38400;
583 }
584
585 info->baud = baud_table[i];
586 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
587 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
588
589 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
590
591 if ((cflag & CSIZE) == CS8)
592 ustcnt |= USTCNT_8_7;
593
594 if (cflag & CSTOPB)
595 ustcnt |= USTCNT_STOP;
596
597 if (cflag & PARENB)
598 ustcnt |= USTCNT_PARITYEN;
599 if (cflag & PARODD)
600 ustcnt |= USTCNT_ODD_EVEN;
601
602#ifdef CONFIG_SERIAL_68328_RTS_CTS
603 if (cflag & CRTSCTS) {
604 uart->utx.w &= ~ UTX_NOCTS;
605 } else {
606 uart->utx.w |= UTX_NOCTS;
607 }
608#endif
609
610 ustcnt |= USTCNT_TXEN;
611
612 uart->ustcnt = ustcnt;
613 return;
614}
615
616/*
617 * Fair output driver allows a process to speak.
618 */
619static void rs_fair_output(void)
620{
621 int left; /* Output no more than that */
622 unsigned long flags;
623 struct m68k_serial *info = &m68k_soft[0];
624 char c;
625
626 if (info == 0) return;
627 if (info->xmit_buf == 0) return;
628
Greg Ungerer727dda82006-06-28 15:54:22 +1000629 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 left = info->xmit_cnt;
631 while (left != 0) {
632 c = info->xmit_buf[info->xmit_tail];
633 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
634 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000635 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 rs_put_char(c);
638
Greg Ungerer727dda82006-06-28 15:54:22 +1000639 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 left = min(info->xmit_cnt, left-1);
641 }
642
643 /* Last character is being transmitted now (hopefully). */
644 udelay(5);
645
Greg Ungerer727dda82006-06-28 15:54:22 +1000646 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return;
648}
649
650/*
651 * m68k_console_print is registered for printk.
652 */
653void console_print_68328(const char *p)
654{
655 char c;
656
657 while((c=*(p++)) != 0) {
658 if(c == '\n')
659 rs_put_char('\r');
660 rs_put_char(c);
661 }
662
663 /* Comment this if you want to have a strict interrupt-driven output */
664 rs_fair_output();
665
666 return;
667}
668
669static void rs_set_ldisc(struct tty_struct *tty)
670{
671 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
672
673 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
674 return;
675
676 info->is_cons = (tty->termios->c_line == N_TTY);
677
678 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
679}
680
681static void rs_flush_chars(struct tty_struct *tty)
682{
683 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
684 m68328_uart *uart = &uart_addr[info->line];
685 unsigned long flags;
686
687 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
688 return;
689#ifndef USE_INTS
690 for(;;) {
691#endif
692
693 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000694 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
697 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000698 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return;
700 }
701
702#ifdef USE_INTS
703 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
704#else
705 uart->ustcnt |= USTCNT_TXEN;
706#endif
707
708#ifdef USE_INTS
709 if (uart->utx.w & UTX_TX_AVAIL) {
710#else
711 if (1) {
712#endif
713 /* Send char */
714 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
715 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
716 info->xmit_cnt--;
717 }
718
719#ifndef USE_INTS
720 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
721 }
722#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000723 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726extern void console_printn(const char * b, int count);
727
728static int rs_write(struct tty_struct * tty,
729 const unsigned char *buf, int count)
730{
731 int c, total = 0;
732 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
733 m68328_uart *uart = &uart_addr[info->line];
734 unsigned long flags;
735
736 if (serial_paranoia_check(info, tty->name, "rs_write"))
737 return 0;
738
739 if (!tty || !info->xmit_buf)
740 return 0;
741
Greg Ungerer727dda82006-06-28 15:54:22 +1000742 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000744 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
746 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000747 local_irq_restore(flags);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (c <= 0)
750 break;
751
752 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000753
754 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
756 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000757 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 buf += c;
759 count -= c;
760 total += c;
761 }
762
763 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
764 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000765 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766#ifndef USE_INTS
767 while(info->xmit_cnt) {
768#endif
769
770 uart->ustcnt |= USTCNT_TXEN;
771#ifdef USE_INTS
772 uart->ustcnt |= USTCNT_TX_INTR_MASK;
773#else
774 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
775#endif
776 if (uart->utx.w & UTX_TX_AVAIL) {
777 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
778 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
779 info->xmit_cnt--;
780 }
781
782#ifndef USE_INTS
783 }
784#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000785 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return total;
789}
790
791static int rs_write_room(struct tty_struct *tty)
792{
793 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
794 int ret;
795
796 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
797 return 0;
798 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
799 if (ret < 0)
800 ret = 0;
801 return ret;
802}
803
804static int rs_chars_in_buffer(struct tty_struct *tty)
805{
806 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
807
808 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
809 return 0;
810 return info->xmit_cnt;
811}
812
813static void rs_flush_buffer(struct tty_struct *tty)
814{
815 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000816 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
819 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000820 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000822 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 tty_wakeup(tty);
824}
825
826/*
827 * ------------------------------------------------------------
828 * rs_throttle()
829 *
830 * This routine is called by the upper-layer tty layer to signal that
831 * incoming characters should be throttled.
832 * ------------------------------------------------------------
833 */
834static void rs_throttle(struct tty_struct * tty)
835{
836 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
837
838 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
839 return;
840
841 if (I_IXOFF(tty))
842 info->x_char = STOP_CHAR(tty);
843
844 /* Turn off RTS line (do this atomic) */
845}
846
847static void rs_unthrottle(struct tty_struct * tty)
848{
849 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
850
851 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
852 return;
853
854 if (I_IXOFF(tty)) {
855 if (info->x_char)
856 info->x_char = 0;
857 else
858 info->x_char = START_CHAR(tty);
859 }
860
861 /* Assert RTS line (do this atomic) */
862}
863
864/*
865 * ------------------------------------------------------------
866 * rs_ioctl() and friends
867 * ------------------------------------------------------------
868 */
869
870static int get_serial_info(struct m68k_serial * info,
871 struct serial_struct * retinfo)
872{
873 struct serial_struct tmp;
874
875 if (!retinfo)
876 return -EFAULT;
877 memset(&tmp, 0, sizeof(tmp));
878 tmp.type = info->type;
879 tmp.line = info->line;
880 tmp.port = info->port;
881 tmp.irq = info->irq;
882 tmp.flags = info->flags;
883 tmp.baud_base = info->baud_base;
884 tmp.close_delay = info->close_delay;
885 tmp.closing_wait = info->closing_wait;
886 tmp.custom_divisor = info->custom_divisor;
887 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
888 return 0;
889}
890
891static int set_serial_info(struct m68k_serial * info,
892 struct serial_struct * new_info)
893{
894 struct serial_struct new_serial;
895 struct m68k_serial old_info;
896 int retval = 0;
897
898 if (!new_info)
899 return -EFAULT;
900 copy_from_user(&new_serial,new_info,sizeof(new_serial));
901 old_info = *info;
902
903 if (!capable(CAP_SYS_ADMIN)) {
904 if ((new_serial.baud_base != info->baud_base) ||
905 (new_serial.type != info->type) ||
906 (new_serial.close_delay != info->close_delay) ||
907 ((new_serial.flags & ~S_USR_MASK) !=
908 (info->flags & ~S_USR_MASK)))
909 return -EPERM;
910 info->flags = ((info->flags & ~S_USR_MASK) |
911 (new_serial.flags & S_USR_MASK));
912 info->custom_divisor = new_serial.custom_divisor;
913 goto check_and_exit;
914 }
915
916 if (info->count > 1)
917 return -EBUSY;
918
919 /*
920 * OK, past this point, all the error checking has been done.
921 * At this point, we start making changes.....
922 */
923
924 info->baud_base = new_serial.baud_base;
925 info->flags = ((info->flags & ~S_FLAGS) |
926 (new_serial.flags & S_FLAGS));
927 info->type = new_serial.type;
928 info->close_delay = new_serial.close_delay;
929 info->closing_wait = new_serial.closing_wait;
930
931check_and_exit:
932 retval = startup(info);
933 return retval;
934}
935
936/*
937 * get_lsr_info - get line status register info
938 *
939 * Purpose: Let user call ioctl() to get info when the UART physically
940 * is emptied. On bus types like RS485, the transmitter must
941 * release the bus after transmitting. This must be done when
942 * the transmit shift register is empty, not be done when the
943 * transmit holding register is empty. This functionality
944 * allows an RS485 driver to be written in user space.
945 */
946static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
947{
948#ifdef CONFIG_SERIAL_68328_RTS_CTS
949 m68328_uart *uart = &uart_addr[info->line];
950#endif
951 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000952 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Greg Ungerer727dda82006-06-28 15:54:22 +1000954 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955#ifdef CONFIG_SERIAL_68328_RTS_CTS
956 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
957#else
958 status = 0;
959#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000960 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 put_user(status,value);
962 return 0;
963}
964
965/*
966 * This routine sends a break character out the serial port.
967 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700968static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 m68328_uart *uart = &uart_addr[info->line];
971 unsigned long flags;
972 if (!info->port)
973 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000974 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975#ifdef USE_INTS
976 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700977 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 uart->utx.w &= ~UTX_SEND_BREAK;
979#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000980 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983static int rs_ioctl(struct tty_struct *tty, struct file * file,
984 unsigned int cmd, unsigned long arg)
985{
986 int error;
987 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
988 int retval;
989
990 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
991 return -ENODEV;
992
993 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
994 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
995 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
996 if (tty->flags & (1 << TTY_IO_ERROR))
997 return -EIO;
998 }
999
1000 switch (cmd) {
1001 case TCSBRK: /* SVID version: non-zero arg --> no break */
1002 retval = tty_check_change(tty);
1003 if (retval)
1004 return retval;
1005 tty_wait_until_sent(tty, 0);
1006 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001007 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009 case TCSBRKP: /* support for POSIX tcsendbreak() */
1010 retval = tty_check_change(tty);
1011 if (retval)
1012 return retval;
1013 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001014 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 case TIOCGSERIAL:
1017 if (access_ok(VERIFY_WRITE, (void *) arg,
1018 sizeof(struct serial_struct)))
1019 return get_serial_info(info,
1020 (struct serial_struct *) arg);
1021 return -EFAULT;
1022 case TIOCSSERIAL:
1023 return set_serial_info(info,
1024 (struct serial_struct *) arg);
1025 case TIOCSERGETLSR: /* Get line status register */
1026 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001027 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return get_lsr_info(info, (unsigned int *) arg);
1029 return -EFAULT;
1030 case TIOCSERGSTRUCT:
1031 if (!access_ok(VERIFY_WRITE, (void *) arg,
1032 sizeof(struct m68k_serial)))
1033 return -EFAULT;
1034 copy_to_user((struct m68k_serial *) arg,
1035 info, sizeof(struct m68k_serial));
1036 return 0;
1037
1038 default:
1039 return -ENOIOCTLCMD;
1040 }
1041 return 0;
1042}
1043
Alan Cox606d0992006-12-08 02:38:45 -08001044static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 change_speed(info);
1049
1050 if ((old_termios->c_cflag & CRTSCTS) &&
1051 !(tty->termios->c_cflag & CRTSCTS)) {
1052 tty->hw_stopped = 0;
1053 rs_start(tty);
1054 }
1055
1056}
1057
1058/*
1059 * ------------------------------------------------------------
1060 * rs_close()
1061 *
1062 * This routine is called when the serial port gets closed. First, we
1063 * wait for the last remaining data to be sent. Then, we unlink its
1064 * S structure from the interrupt chain if necessary, and we free
1065 * that IRQ if nothing is left in the chain.
1066 * ------------------------------------------------------------
1067 */
1068static void rs_close(struct tty_struct *tty, struct file * filp)
1069{
1070 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1071 m68328_uart *uart = &uart_addr[info->line];
1072 unsigned long flags;
1073
1074 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1075 return;
1076
Greg Ungerer727dda82006-06-28 15:54:22 +10001077 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001080 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return;
1082 }
1083
1084 if ((tty->count == 1) && (info->count != 1)) {
1085 /*
1086 * Uh, oh. tty->count is 1, which means that the tty
1087 * structure will be freed. Info->count should always
1088 * be one in these conditions. If it's greater than
1089 * one, we've got real problems, since it means the
1090 * serial port won't be shutdown.
1091 */
1092 printk("rs_close: bad serial port count; tty->count is 1, "
1093 "info->count is %d\n", info->count);
1094 info->count = 1;
1095 }
1096 if (--info->count < 0) {
1097 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1098 info->line, info->count);
1099 info->count = 0;
1100 }
1101 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105 info->flags |= S_CLOSING;
1106 /*
1107 * Now we wait for the transmit buffer to clear; and we notify
1108 * the line discipline to only process XON/XOFF characters.
1109 */
1110 tty->closing = 1;
1111 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1112 tty_wait_until_sent(tty, info->closing_wait);
1113 /*
1114 * At this point we stop accepting input. To do this, we
1115 * disable the receive line status interrupts, and tell the
1116 * interrupt driver to stop checking the data ready bit in the
1117 * line status register.
1118 */
1119
1120 uart->ustcnt &= ~USTCNT_RXEN;
1121 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1122
1123 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001124 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 tty_ldisc_flush(tty);
1127 tty->closing = 0;
1128 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001129 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#warning "This is not and has never been valid so fix it"
1131#if 0
1132 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1133 if (tty->ldisc.close)
1134 (tty->ldisc.close)(tty);
1135 tty->ldisc = ldiscs[N_TTY];
1136 tty->termios->c_line = N_TTY;
1137 if (tty->ldisc.open)
1138 (tty->ldisc.open)(tty);
1139 }
1140#endif
1141 if (info->blocked_open) {
1142 if (info->close_delay) {
1143 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1144 }
1145 wake_up_interruptible(&info->open_wait);
1146 }
1147 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1148 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001149 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152/*
1153 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1154 */
1155void rs_hangup(struct tty_struct *tty)
1156{
1157 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1158
1159 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1160 return;
1161
1162 rs_flush_buffer(tty);
1163 shutdown(info);
1164 info->event = 0;
1165 info->count = 0;
1166 info->flags &= ~S_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001167 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 wake_up_interruptible(&info->open_wait);
1169}
1170
1171/*
1172 * ------------------------------------------------------------
1173 * rs_open() and friends
1174 * ------------------------------------------------------------
1175 */
1176static int block_til_ready(struct tty_struct *tty, struct file * filp,
1177 struct m68k_serial *info)
1178{
1179 DECLARE_WAITQUEUE(wait, current);
1180 int retval;
1181 int do_clocal = 0;
1182
1183 /*
1184 * If the device is in the middle of being closed, then block
1185 * until it's done, and then try again.
1186 */
1187 if (info->flags & S_CLOSING) {
1188 interruptible_sleep_on(&info->close_wait);
1189#ifdef SERIAL_DO_RESTART
1190 if (info->flags & S_HUP_NOTIFY)
1191 return -EAGAIN;
1192 else
1193 return -ERESTARTSYS;
1194#else
1195 return -EAGAIN;
1196#endif
1197 }
1198
1199 /*
1200 * If non-blocking mode is set, or the port is not enabled,
1201 * then make the check up front and then exit.
1202 */
1203 if ((filp->f_flags & O_NONBLOCK) ||
1204 (tty->flags & (1 << TTY_IO_ERROR))) {
1205 info->flags |= S_NORMAL_ACTIVE;
1206 return 0;
1207 }
1208
1209 if (tty->termios->c_cflag & CLOCAL)
1210 do_clocal = 1;
1211
1212 /*
1213 * Block waiting for the carrier detect and the line to become
1214 * free (i.e., not in use by the callout). While we are in
1215 * this loop, info->count is dropped by one, so that
1216 * rs_close() knows when to free things. We restore it upon
1217 * exit, either normal or abnormal.
1218 */
1219 retval = 0;
1220 add_wait_queue(&info->open_wait, &wait);
1221
1222 info->count--;
1223 info->blocked_open++;
1224 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001225 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001227 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 current->state = TASK_INTERRUPTIBLE;
1229 if (tty_hung_up_p(filp) ||
1230 !(info->flags & S_INITIALIZED)) {
1231#ifdef SERIAL_DO_RESTART
1232 if (info->flags & S_HUP_NOTIFY)
1233 retval = -EAGAIN;
1234 else
1235 retval = -ERESTARTSYS;
1236#else
1237 retval = -EAGAIN;
1238#endif
1239 break;
1240 }
1241 if (!(info->flags & S_CLOSING) && do_clocal)
1242 break;
1243 if (signal_pending(current)) {
1244 retval = -ERESTARTSYS;
1245 break;
1246 }
1247 schedule();
1248 }
1249 current->state = TASK_RUNNING;
1250 remove_wait_queue(&info->open_wait, &wait);
1251 if (!tty_hung_up_p(filp))
1252 info->count++;
1253 info->blocked_open--;
1254
1255 if (retval)
1256 return retval;
1257 info->flags |= S_NORMAL_ACTIVE;
1258 return 0;
1259}
1260
1261/*
1262 * This routine is called whenever a serial port is opened. It
1263 * enables interrupts for a serial port, linking in its S structure into
1264 * the IRQ chain. It also performs the serial-specific
1265 * initialization for the tty structure.
1266 */
1267int rs_open(struct tty_struct *tty, struct file * filp)
1268{
1269 struct m68k_serial *info;
1270 int retval, line;
1271
1272 line = tty->index;
1273
1274 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1275 return -ENODEV;
1276
1277 info = &m68k_soft[line];
1278
1279 if (serial_paranoia_check(info, tty->name, "rs_open"))
1280 return -ENODEV;
1281
1282 info->count++;
1283 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001284 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
1287 * Start up serial port
1288 */
1289 retval = startup(info);
1290 if (retval)
1291 return retval;
1292
1293 return block_til_ready(tty, filp, info);
1294}
1295
1296/* Finally, routines used to initialize the serial driver. */
1297
1298static void show_serial_version(void)
1299{
1300 printk("MC68328 serial driver version 1.00\n");
1301}
1302
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001303static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 .open = rs_open,
1305 .close = rs_close,
1306 .write = rs_write,
1307 .flush_chars = rs_flush_chars,
1308 .write_room = rs_write_room,
1309 .chars_in_buffer = rs_chars_in_buffer,
1310 .flush_buffer = rs_flush_buffer,
1311 .ioctl = rs_ioctl,
1312 .throttle = rs_throttle,
1313 .unthrottle = rs_unthrottle,
1314 .set_termios = rs_set_termios,
1315 .stop = rs_stop,
1316 .start = rs_start,
1317 .hangup = rs_hangup,
1318 .set_ldisc = rs_set_ldisc,
1319};
1320
1321/* rs_init inits the driver */
1322static int __init
1323rs68328_init(void)
1324{
1325 int flags, i;
1326 struct m68k_serial *info;
1327
1328 serial_driver = alloc_tty_driver(NR_PORTS);
1329 if (!serial_driver)
1330 return -ENOMEM;
1331
1332 show_serial_version();
1333
1334 /* Initialize the tty_driver structure */
1335 /* SPARC: Not all of this is exactly right for us. */
1336
1337 serial_driver->name = "ttyS";
1338 serial_driver->major = TTY_MAJOR;
1339 serial_driver->minor_start = 64;
1340 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1341 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1342 serial_driver->init_termios = tty_std_termios;
1343 serial_driver->init_termios.c_cflag =
1344 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1345 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1346 tty_set_operations(serial_driver, &rs_ops);
1347
1348 if (tty_register_driver(serial_driver)) {
1349 put_tty_driver(serial_driver);
1350 printk(KERN_ERR "Couldn't register serial driver\n");
1351 return -ENOMEM;
1352 }
1353
Greg Ungerer727dda82006-06-28 15:54:22 +10001354 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 for(i=0;i<NR_PORTS;i++) {
1357
1358 info = &m68k_soft[i];
1359 info->magic = SERIAL_MAGIC;
1360 info->port = (int) &uart_addr[i];
Takashi Iwaia88487c2008-07-16 21:54:42 +01001361 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 info->irq = uart_irqs[i];
1363 info->custom_divisor = 16;
1364 info->close_delay = 50;
1365 info->closing_wait = 3000;
1366 info->x_char = 0;
1367 info->event = 0;
1368 info->count = 0;
1369 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001370 INIT_WORK(&info->tqueue, do_softint);
1371 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 init_waitqueue_head(&info->open_wait);
1373 init_waitqueue_head(&info->close_wait);
1374 info->line = i;
1375 info->is_cons = 1; /* Means shortcuts work */
1376
1377 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1378 info->port, info->irq);
1379 printk(" is a builtin MC68328 UART\n");
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381#ifdef CONFIG_M68VZ328
1382 if (i > 0 )
1383 PJSEL &= 0xCF; /* PSW enable second port output */
1384#endif
1385
1386 if (request_irq(uart_irqs[i],
1387 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001388 IRQF_DISABLED,
Alan Cox25db8ad2008-08-19 20:49:40 -07001389 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001392 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396module_init(rs68328_init);
1397
1398
1399
1400static void m68328_set_baud(void)
1401{
1402 unsigned short ustcnt;
1403 int i;
1404
1405 ustcnt = USTCNT;
1406 USTCNT = ustcnt & ~USTCNT_TXEN;
1407
1408again:
1409 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1410 if (baud_table[i] == m68328_console_baud)
1411 break;
1412 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1413 m68328_console_baud = 9600;
1414 goto again;
1415 }
1416
1417 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1418 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1419 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1420 ustcnt |= USTCNT_8_7;
1421 ustcnt |= USTCNT_TXEN;
1422 USTCNT = ustcnt;
1423 m68328_console_initted = 1;
1424 return;
1425}
1426
1427
1428int m68328_console_setup(struct console *cp, char *arg)
1429{
1430 int i, n = CONSOLE_BAUD_RATE;
1431
1432 if (!cp)
1433 return(-1);
1434
1435 if (arg)
1436 n = simple_strtoul(arg,NULL,0);
1437
1438 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1439 if (baud_table[i] == n)
1440 break;
1441 if (i < BAUD_TABLE_SIZE) {
1442 m68328_console_baud = n;
1443 m68328_console_cbaud = 0;
1444 if (i > 15) {
1445 m68328_console_cbaud |= CBAUDEX;
1446 i -= 15;
1447 }
1448 m68328_console_cbaud |= i;
1449 }
1450
1451 m68328_set_baud(); /* make sure baud rate changes */
1452 return(0);
1453}
1454
1455
1456static struct tty_driver *m68328_console_device(struct console *c, int *index)
1457{
1458 *index = c->index;
1459 return serial_driver;
1460}
1461
1462
1463void m68328_console_write (struct console *co, const char *str,
1464 unsigned int count)
1465{
1466 if (!m68328_console_initted)
1467 m68328_set_baud();
1468 while (count--) {
1469 if (*str == '\n')
1470 rs_put_char('\r');
1471 rs_put_char( *str++ );
1472 }
1473}
1474
1475
1476static struct console m68328_driver = {
1477 .name = "ttyS",
1478 .write = m68328_console_write,
1479 .device = m68328_console_device,
1480 .setup = m68328_console_setup,
1481 .flags = CON_PRINTBUFFER,
1482 .index = -1,
1483};
1484
1485
1486static int __init m68328_console_init(void)
1487{
1488 register_console(&m68328_driver);
1489 return 0;
1490}
1491
1492console_initcall(m68328_console_init);