blob: ae0251ef6f4ef76b087f119901a46f6d8c5c8b7b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Sets or clears DTR/RTS on the requested line */
157static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
158{
159 if (set) {
160 /* set the RTS/CTS line */
161 } else {
162 /* clear it */
163 }
164 return;
165}
166
167/* Utility routines */
168static inline int get_baud(struct m68k_serial *ss)
169{
170 unsigned long result = 115200;
171 unsigned short int baud = uart_addr[ss->line].ubaud;
172 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
173 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
174
175 return result;
176}
177
178/*
179 * ------------------------------------------------------------
180 * rs_stop() and rs_start()
181 *
182 * This routines are called before setting or resetting tty->stopped.
183 * They enable or disable transmitter interrupts, as necessary.
184 * ------------------------------------------------------------
185 */
186static void rs_stop(struct tty_struct *tty)
187{
188 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
189 m68328_uart *uart = &uart_addr[info->line];
190 unsigned long flags;
191
192 if (serial_paranoia_check(info, tty->name, "rs_stop"))
193 return;
194
Greg Ungerer727dda82006-06-28 15:54:22 +1000195 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000197 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Alan Cox09a6ffa2008-04-30 00:54:01 -0700200static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int flags, loops = 0;
203
Greg Ungerer727dda82006-06-28 15:54:22 +1000204 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
207 loops++;
208 udelay(5);
209 }
210
211 UTX_TXDATA = ch;
212 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000213 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700214 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static void rs_start(struct tty_struct *tty)
218{
219 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
220 m68328_uart *uart = &uart_addr[info->line];
221 unsigned long flags;
222
223 if (serial_paranoia_check(info, tty->name, "rs_start"))
224 return;
225
Greg Ungerer727dda82006-06-28 15:54:22 +1000226 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
228#ifdef USE_INTS
229 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
230#else
231 uart->ustcnt |= USTCNT_TXEN;
232#endif
233 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000234 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/* Drop into either the boot monitor or kadb upon receiving a break
238 * from keyboard/console input.
239 */
240static void batten_down_hatches(void)
241{
242 /* Drop into the debugger */
243}
244
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800245static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247#if 0
248 if(status & DCD) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100249 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ((info->curregs[3] & AUTO_ENAB)==0)) {
251 info->curregs[3] |= AUTO_ENAB;
252 info->pendregs[3] |= AUTO_ENAB;
253 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
254 }
255 } else {
256 if((info->curregs[3] & AUTO_ENAB)) {
257 info->curregs[3] &= ~AUTO_ENAB;
258 info->pendregs[3] &= ~AUTO_ENAB;
259 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
260 }
261 }
262#endif
263 /* If this is console input and this is a
264 * 'break asserted' status change interrupt
265 * see if we can drop into the debugger
266 */
267 if((status & URX_BREAK) && info->break_abort)
268 batten_down_hatches();
269 return;
270}
271
David Howells7d12e782006-10-05 14:55:46 +0100272static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100274 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800276 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /*
279 * This do { } while() loop will get ALL chars out of Rx FIFO
280 */
281#ifndef CONFIG_XCOPILOT_BUGS
282 do {
283#endif
284 ch = GET_FIELD(rx, URX_RXDATA);
285
286 if(info->is_cons) {
287 if(URX_BREAK & rx) { /* whee, break received */
288 status_handle(info, rx);
289 return;
290#ifdef CONFIG_MAGIC_SYSRQ
291 } else if (ch == 0x10) { /* ^P */
292 show_state();
293 show_free_areas();
294 show_buffers();
295/* show_net_buffers(); */
296 return;
297 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600298 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return;
300#endif /* CONFIG_MAGIC_SYSRQ */
301 }
302 /* It is a 'keyboard interrupt' ;-) */
303#ifdef CONFIG_CONSOLE
304 wake_up(&keypress_wait);
305#endif
306 }
307
308 if(!tty)
309 goto clear_and_exit;
310
Alan Cox33f0f882006-01-09 20:54:13 -0800311 flag = TTY_NORMAL;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800314 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 status_handle(info, rx);
316 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800317 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 status_handle(info, rx);
319 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800320 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Alan Cox33f0f882006-01-09 20:54:13 -0800323 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifndef CONFIG_XCOPILOT_BUGS
325 } while((rx = uart->urx.w) & URX_DATA_READY);
326#endif
327
Greg Ungerer8e63e662006-02-08 09:19:17 +1000328 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330clear_and_exit:
331 return;
332}
333
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800334static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 m68328_uart *uart = &uart_addr[info->line];
337
338 if (info->x_char) {
339 /* Send next char */
340 uart->utx.b.txdata = info->x_char;
341 info->x_char = 0;
342 goto clear_and_return;
343 }
344
Takashi Iwaia88487c2008-07-16 21:54:42 +0100345 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* That's peculiar... TX ints off */
347 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
348 goto clear_and_return;
349 }
350
351 /* Send char */
352 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
353 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
354 info->xmit_cnt--;
355
356 if (info->xmit_cnt < WAKEUP_CHARS)
357 schedule_work(&info->tqueue);
358
359 if(info->xmit_cnt <= 0) {
360 /* All done for now... TX ints off */
361 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
362 goto clear_and_return;
363 }
364
365clear_and_return:
366 /* Clear interrupt (should be auto)*/
367 return;
368}
369
370/*
371 * This is the serial driver's generic interrupt routine
372 */
David Howells7d12e782006-10-05 14:55:46 +0100373irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Alan Cox25db8ad2008-08-19 20:49:40 -0700375 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 m68328_uart *uart;
377 unsigned short rx;
378 unsigned short tx;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 uart = &uart_addr[info->line];
381 rx = uart->urx.w;
382
383#ifdef USE_INTS
384 tx = uart->utx.w;
385
David Howells7d12e782006-10-05 14:55:46 +0100386 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (tx & UTX_TX_AVAIL) transmit_chars(info);
388#else
David Howells7d12e782006-10-05 14:55:46 +0100389 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
391 return IRQ_HANDLED;
392}
393
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000394static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000396 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct tty_struct *tty;
398
Takashi Iwaia88487c2008-07-16 21:54:42 +0100399 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!tty)
401 return;
402#if 0
403 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
404 tty_wakeup(tty);
405 }
406#endif
407}
408
409/*
410 * This routine is called from the scheduler tqueue when the interrupt
411 * routine has signalled that a hangup has occurred. The path of
412 * hangup processing is:
413 *
414 * serial interrupt routine -> (scheduler tqueue) ->
415 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
416 *
417 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000418static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000420 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct tty_struct *tty;
422
Takashi Iwaia88487c2008-07-16 21:54:42 +0100423 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!tty)
425 return;
426
427 tty_hangup(tty);
428}
429
430
431static int startup(struct m68k_serial * info)
432{
433 m68328_uart *uart = &uart_addr[info->line];
434 unsigned long flags;
435
436 if (info->flags & S_INITIALIZED)
437 return 0;
438
439 if (!info->xmit_buf) {
440 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
441 if (!info->xmit_buf)
442 return -ENOMEM;
443 }
444
Greg Ungerer727dda82006-06-28 15:54:22 +1000445 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * Clear the FIFO buffers and disable them
449 * (they will be reenabled in change_speed())
450 */
451
452 uart->ustcnt = USTCNT_UEN;
453 info->xmit_fifo_size = 1;
454 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
455 (void)uart->urx.w;
456
457 /*
458 * Finally, enable sequencing and interrupts
459 */
460#ifdef USE_INTS
461 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
462 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
463#else
464 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
465#endif
466
Takashi Iwaia88487c2008-07-16 21:54:42 +0100467 if (info->port.tty)
468 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
470
471 /*
472 * and set the speed of the serial port
473 */
474
475 change_speed(info);
476
477 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000478 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
482/*
483 * This routine will shutdown a serial port; interrupts are disabled, and
484 * DTR is dropped if the hangup on close termio flag is on.
485 */
486static void shutdown(struct m68k_serial * info)
487{
488 m68328_uart *uart = &uart_addr[info->line];
489 unsigned long flags;
490
491 uart->ustcnt = 0; /* All off! */
492 if (!(info->flags & S_INITIALIZED))
493 return;
494
Greg Ungerer727dda82006-06-28 15:54:22 +1000495 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (info->xmit_buf) {
498 free_page((unsigned long) info->xmit_buf);
499 info->xmit_buf = 0;
500 }
501
Takashi Iwaia88487c2008-07-16 21:54:42 +0100502 if (info->port.tty)
503 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000506 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509struct {
510 int divisor, prescale;
511}
512#ifndef CONFIG_M68VZ328
513 hw_baud_table[18] = {
514 {0,0}, /* 0 */
515 {0,0}, /* 50 */
516 {0,0}, /* 75 */
517 {0,0}, /* 110 */
518 {0,0}, /* 134 */
519 {0,0}, /* 150 */
520 {0,0}, /* 200 */
521 {7,0x26}, /* 300 */
522 {6,0x26}, /* 600 */
523 {5,0x26}, /* 1200 */
524 {0,0}, /* 1800 */
525 {4,0x26}, /* 2400 */
526 {3,0x26}, /* 4800 */
527 {2,0x26}, /* 9600 */
528 {1,0x26}, /* 19200 */
529 {0,0x26}, /* 38400 */
530 {1,0x38}, /* 57600 */
531 {0,0x38}, /* 115200 */
532};
533#else
534 hw_baud_table[18] = {
535 {0,0}, /* 0 */
536 {0,0}, /* 50 */
537 {0,0}, /* 75 */
538 {0,0}, /* 110 */
539 {0,0}, /* 134 */
540 {0,0}, /* 150 */
541 {0,0}, /* 200 */
542 {0,0}, /* 300 */
543 {7,0x26}, /* 600 */
544 {6,0x26}, /* 1200 */
545 {0,0}, /* 1800 */
546 {5,0x26}, /* 2400 */
547 {4,0x26}, /* 4800 */
548 {3,0x26}, /* 9600 */
549 {2,0x26}, /* 19200 */
550 {1,0x26}, /* 38400 */
551 {0,0x26}, /* 57600 */
552 {1,0x38}, /* 115200 */
553};
554#endif
555/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
556
557/*
558 * This routine is called to set the UART divisor registers to match
559 * the specified baud rate for a serial port.
560 */
561static void change_speed(struct m68k_serial *info)
562{
563 m68328_uart *uart = &uart_addr[info->line];
564 unsigned short port;
565 unsigned short ustcnt;
566 unsigned cflag;
567 int i;
568
Takashi Iwaia88487c2008-07-16 21:54:42 +0100569 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100571 cflag = info->port.tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!(port = info->port))
573 return;
574
575 ustcnt = uart->ustcnt;
576 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
577
578 i = cflag & CBAUD;
579 if (i & CBAUDEX) {
580 i = (i & ~CBAUDEX) + B38400;
581 }
582
583 info->baud = baud_table[i];
584 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
585 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
586
587 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
588
589 if ((cflag & CSIZE) == CS8)
590 ustcnt |= USTCNT_8_7;
591
592 if (cflag & CSTOPB)
593 ustcnt |= USTCNT_STOP;
594
595 if (cflag & PARENB)
596 ustcnt |= USTCNT_PARITYEN;
597 if (cflag & PARODD)
598 ustcnt |= USTCNT_ODD_EVEN;
599
600#ifdef CONFIG_SERIAL_68328_RTS_CTS
601 if (cflag & CRTSCTS) {
602 uart->utx.w &= ~ UTX_NOCTS;
603 } else {
604 uart->utx.w |= UTX_NOCTS;
605 }
606#endif
607
608 ustcnt |= USTCNT_TXEN;
609
610 uart->ustcnt = ustcnt;
611 return;
612}
613
614/*
615 * Fair output driver allows a process to speak.
616 */
617static void rs_fair_output(void)
618{
619 int left; /* Output no more than that */
620 unsigned long flags;
621 struct m68k_serial *info = &m68k_soft[0];
622 char c;
623
624 if (info == 0) return;
625 if (info->xmit_buf == 0) return;
626
Greg Ungerer727dda82006-06-28 15:54:22 +1000627 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 left = info->xmit_cnt;
629 while (left != 0) {
630 c = info->xmit_buf[info->xmit_tail];
631 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
632 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000633 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 rs_put_char(c);
636
Greg Ungerer727dda82006-06-28 15:54:22 +1000637 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 left = min(info->xmit_cnt, left-1);
639 }
640
641 /* Last character is being transmitted now (hopefully). */
642 udelay(5);
643
Greg Ungerer727dda82006-06-28 15:54:22 +1000644 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
646}
647
648/*
649 * m68k_console_print is registered for printk.
650 */
651void console_print_68328(const char *p)
652{
653 char c;
654
655 while((c=*(p++)) != 0) {
656 if(c == '\n')
657 rs_put_char('\r');
658 rs_put_char(c);
659 }
660
661 /* Comment this if you want to have a strict interrupt-driven output */
662 rs_fair_output();
663
664 return;
665}
666
667static void rs_set_ldisc(struct tty_struct *tty)
668{
669 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
670
671 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
672 return;
673
674 info->is_cons = (tty->termios->c_line == N_TTY);
675
676 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
677}
678
679static void rs_flush_chars(struct tty_struct *tty)
680{
681 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
682 m68328_uart *uart = &uart_addr[info->line];
683 unsigned long flags;
684
685 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
686 return;
687#ifndef USE_INTS
688 for(;;) {
689#endif
690
691 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000692 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
695 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000696 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698 }
699
700#ifdef USE_INTS
701 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
702#else
703 uart->ustcnt |= USTCNT_TXEN;
704#endif
705
706#ifdef USE_INTS
707 if (uart->utx.w & UTX_TX_AVAIL) {
708#else
709 if (1) {
710#endif
711 /* Send char */
712 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
713 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
714 info->xmit_cnt--;
715 }
716
717#ifndef USE_INTS
718 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
719 }
720#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000721 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724extern void console_printn(const char * b, int count);
725
726static int rs_write(struct tty_struct * tty,
727 const unsigned char *buf, int count)
728{
729 int c, total = 0;
730 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
731 m68328_uart *uart = &uart_addr[info->line];
732 unsigned long flags;
733
734 if (serial_paranoia_check(info, tty->name, "rs_write"))
735 return 0;
736
737 if (!tty || !info->xmit_buf)
738 return 0;
739
Greg Ungerer727dda82006-06-28 15:54:22 +1000740 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000742 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
744 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000745 local_irq_restore(flags);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (c <= 0)
748 break;
749
750 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000751
752 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
754 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000755 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 buf += c;
757 count -= c;
758 total += c;
759 }
760
761 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
762 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000763 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#ifndef USE_INTS
765 while(info->xmit_cnt) {
766#endif
767
768 uart->ustcnt |= USTCNT_TXEN;
769#ifdef USE_INTS
770 uart->ustcnt |= USTCNT_TX_INTR_MASK;
771#else
772 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
773#endif
774 if (uart->utx.w & UTX_TX_AVAIL) {
775 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
776 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
777 info->xmit_cnt--;
778 }
779
780#ifndef USE_INTS
781 }
782#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000783 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return total;
787}
788
789static int rs_write_room(struct tty_struct *tty)
790{
791 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
792 int ret;
793
794 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
795 return 0;
796 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
797 if (ret < 0)
798 ret = 0;
799 return ret;
800}
801
802static int rs_chars_in_buffer(struct tty_struct *tty)
803{
804 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
805
806 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
807 return 0;
808 return info->xmit_cnt;
809}
810
811static void rs_flush_buffer(struct tty_struct *tty)
812{
813 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000814 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
817 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000818 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000820 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 tty_wakeup(tty);
822}
823
824/*
825 * ------------------------------------------------------------
826 * rs_throttle()
827 *
828 * This routine is called by the upper-layer tty layer to signal that
829 * incoming characters should be throttled.
830 * ------------------------------------------------------------
831 */
832static void rs_throttle(struct tty_struct * tty)
833{
834 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
835
836 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
837 return;
838
839 if (I_IXOFF(tty))
840 info->x_char = STOP_CHAR(tty);
841
842 /* Turn off RTS line (do this atomic) */
843}
844
845static void rs_unthrottle(struct tty_struct * tty)
846{
847 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
848
849 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
850 return;
851
852 if (I_IXOFF(tty)) {
853 if (info->x_char)
854 info->x_char = 0;
855 else
856 info->x_char = START_CHAR(tty);
857 }
858
859 /* Assert RTS line (do this atomic) */
860}
861
862/*
863 * ------------------------------------------------------------
864 * rs_ioctl() and friends
865 * ------------------------------------------------------------
866 */
867
868static int get_serial_info(struct m68k_serial * info,
869 struct serial_struct * retinfo)
870{
871 struct serial_struct tmp;
872
873 if (!retinfo)
874 return -EFAULT;
875 memset(&tmp, 0, sizeof(tmp));
876 tmp.type = info->type;
877 tmp.line = info->line;
878 tmp.port = info->port;
879 tmp.irq = info->irq;
880 tmp.flags = info->flags;
881 tmp.baud_base = info->baud_base;
882 tmp.close_delay = info->close_delay;
883 tmp.closing_wait = info->closing_wait;
884 tmp.custom_divisor = info->custom_divisor;
885 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
886 return 0;
887}
888
889static int set_serial_info(struct m68k_serial * info,
890 struct serial_struct * new_info)
891{
892 struct serial_struct new_serial;
893 struct m68k_serial old_info;
894 int retval = 0;
895
896 if (!new_info)
897 return -EFAULT;
898 copy_from_user(&new_serial,new_info,sizeof(new_serial));
899 old_info = *info;
900
901 if (!capable(CAP_SYS_ADMIN)) {
902 if ((new_serial.baud_base != info->baud_base) ||
903 (new_serial.type != info->type) ||
904 (new_serial.close_delay != info->close_delay) ||
905 ((new_serial.flags & ~S_USR_MASK) !=
906 (info->flags & ~S_USR_MASK)))
907 return -EPERM;
908 info->flags = ((info->flags & ~S_USR_MASK) |
909 (new_serial.flags & S_USR_MASK));
910 info->custom_divisor = new_serial.custom_divisor;
911 goto check_and_exit;
912 }
913
914 if (info->count > 1)
915 return -EBUSY;
916
917 /*
918 * OK, past this point, all the error checking has been done.
919 * At this point, we start making changes.....
920 */
921
922 info->baud_base = new_serial.baud_base;
923 info->flags = ((info->flags & ~S_FLAGS) |
924 (new_serial.flags & S_FLAGS));
925 info->type = new_serial.type;
926 info->close_delay = new_serial.close_delay;
927 info->closing_wait = new_serial.closing_wait;
928
929check_and_exit:
930 retval = startup(info);
931 return retval;
932}
933
934/*
935 * get_lsr_info - get line status register info
936 *
937 * Purpose: Let user call ioctl() to get info when the UART physically
938 * is emptied. On bus types like RS485, the transmitter must
939 * release the bus after transmitting. This must be done when
940 * the transmit shift register is empty, not be done when the
941 * transmit holding register is empty. This functionality
942 * allows an RS485 driver to be written in user space.
943 */
944static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
945{
946#ifdef CONFIG_SERIAL_68328_RTS_CTS
947 m68328_uart *uart = &uart_addr[info->line];
948#endif
949 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000950 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Greg Ungerer727dda82006-06-28 15:54:22 +1000952 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#ifdef CONFIG_SERIAL_68328_RTS_CTS
954 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
955#else
956 status = 0;
957#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000958 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 put_user(status,value);
960 return 0;
961}
962
963/*
964 * This routine sends a break character out the serial port.
965 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700966static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 m68328_uart *uart = &uart_addr[info->line];
969 unsigned long flags;
970 if (!info->port)
971 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000972 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973#ifdef USE_INTS
974 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700975 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 uart->utx.w &= ~UTX_SEND_BREAK;
977#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000978 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static int rs_ioctl(struct tty_struct *tty, struct file * file,
982 unsigned int cmd, unsigned long arg)
983{
984 int error;
985 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
986 int retval;
987
988 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
989 return -ENODEV;
990
991 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
992 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
993 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
994 if (tty->flags & (1 << TTY_IO_ERROR))
995 return -EIO;
996 }
997
998 switch (cmd) {
999 case TCSBRK: /* SVID version: non-zero arg --> no break */
1000 retval = tty_check_change(tty);
1001 if (retval)
1002 return retval;
1003 tty_wait_until_sent(tty, 0);
1004 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001005 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
1007 case TCSBRKP: /* support for POSIX tcsendbreak() */
1008 retval = tty_check_change(tty);
1009 if (retval)
1010 return retval;
1011 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001012 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 case TIOCGSERIAL:
1015 if (access_ok(VERIFY_WRITE, (void *) arg,
1016 sizeof(struct serial_struct)))
1017 return get_serial_info(info,
1018 (struct serial_struct *) arg);
1019 return -EFAULT;
1020 case TIOCSSERIAL:
1021 return set_serial_info(info,
1022 (struct serial_struct *) arg);
1023 case TIOCSERGETLSR: /* Get line status register */
1024 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001025 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return get_lsr_info(info, (unsigned int *) arg);
1027 return -EFAULT;
1028 case TIOCSERGSTRUCT:
1029 if (!access_ok(VERIFY_WRITE, (void *) arg,
1030 sizeof(struct m68k_serial)))
1031 return -EFAULT;
1032 copy_to_user((struct m68k_serial *) arg,
1033 info, sizeof(struct m68k_serial));
1034 return 0;
1035
1036 default:
1037 return -ENOIOCTLCMD;
1038 }
1039 return 0;
1040}
1041
Alan Cox606d0992006-12-08 02:38:45 -08001042static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 change_speed(info);
1047
1048 if ((old_termios->c_cflag & CRTSCTS) &&
1049 !(tty->termios->c_cflag & CRTSCTS)) {
1050 tty->hw_stopped = 0;
1051 rs_start(tty);
1052 }
1053
1054}
1055
1056/*
1057 * ------------------------------------------------------------
1058 * rs_close()
1059 *
1060 * This routine is called when the serial port gets closed. First, we
1061 * wait for the last remaining data to be sent. Then, we unlink its
1062 * S structure from the interrupt chain if necessary, and we free
1063 * that IRQ if nothing is left in the chain.
1064 * ------------------------------------------------------------
1065 */
1066static void rs_close(struct tty_struct *tty, struct file * filp)
1067{
1068 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1069 m68328_uart *uart = &uart_addr[info->line];
1070 unsigned long flags;
1071
1072 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1073 return;
1074
Greg Ungerer727dda82006-06-28 15:54:22 +10001075 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001078 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return;
1080 }
1081
1082 if ((tty->count == 1) && (info->count != 1)) {
1083 /*
1084 * Uh, oh. tty->count is 1, which means that the tty
1085 * structure will be freed. Info->count should always
1086 * be one in these conditions. If it's greater than
1087 * one, we've got real problems, since it means the
1088 * serial port won't be shutdown.
1089 */
1090 printk("rs_close: bad serial port count; tty->count is 1, "
1091 "info->count is %d\n", info->count);
1092 info->count = 1;
1093 }
1094 if (--info->count < 0) {
1095 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1096 info->line, info->count);
1097 info->count = 0;
1098 }
1099 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001100 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return;
1102 }
1103 info->flags |= S_CLOSING;
1104 /*
1105 * Now we wait for the transmit buffer to clear; and we notify
1106 * the line discipline to only process XON/XOFF characters.
1107 */
1108 tty->closing = 1;
1109 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1110 tty_wait_until_sent(tty, info->closing_wait);
1111 /*
1112 * At this point we stop accepting input. To do this, we
1113 * disable the receive line status interrupts, and tell the
1114 * interrupt driver to stop checking the data ready bit in the
1115 * line status register.
1116 */
1117
1118 uart->ustcnt &= ~USTCNT_RXEN;
1119 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1120
1121 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001122 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 tty_ldisc_flush(tty);
1125 tty->closing = 0;
1126 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001127 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#warning "This is not and has never been valid so fix it"
1129#if 0
1130 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1131 if (tty->ldisc.close)
1132 (tty->ldisc.close)(tty);
1133 tty->ldisc = ldiscs[N_TTY];
1134 tty->termios->c_line = N_TTY;
1135 if (tty->ldisc.open)
1136 (tty->ldisc.open)(tty);
1137 }
1138#endif
1139 if (info->blocked_open) {
1140 if (info->close_delay) {
1141 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1142 }
1143 wake_up_interruptible(&info->open_wait);
1144 }
1145 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1146 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001147 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150/*
1151 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1152 */
1153void rs_hangup(struct tty_struct *tty)
1154{
1155 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1156
1157 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1158 return;
1159
1160 rs_flush_buffer(tty);
1161 shutdown(info);
1162 info->event = 0;
1163 info->count = 0;
1164 info->flags &= ~S_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001165 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 wake_up_interruptible(&info->open_wait);
1167}
1168
1169/*
1170 * ------------------------------------------------------------
1171 * rs_open() and friends
1172 * ------------------------------------------------------------
1173 */
1174static int block_til_ready(struct tty_struct *tty, struct file * filp,
1175 struct m68k_serial *info)
1176{
1177 DECLARE_WAITQUEUE(wait, current);
1178 int retval;
1179 int do_clocal = 0;
1180
1181 /*
1182 * If the device is in the middle of being closed, then block
1183 * until it's done, and then try again.
1184 */
1185 if (info->flags & S_CLOSING) {
1186 interruptible_sleep_on(&info->close_wait);
1187#ifdef SERIAL_DO_RESTART
1188 if (info->flags & S_HUP_NOTIFY)
1189 return -EAGAIN;
1190 else
1191 return -ERESTARTSYS;
1192#else
1193 return -EAGAIN;
1194#endif
1195 }
1196
1197 /*
1198 * If non-blocking mode is set, or the port is not enabled,
1199 * then make the check up front and then exit.
1200 */
1201 if ((filp->f_flags & O_NONBLOCK) ||
1202 (tty->flags & (1 << TTY_IO_ERROR))) {
1203 info->flags |= S_NORMAL_ACTIVE;
1204 return 0;
1205 }
1206
1207 if (tty->termios->c_cflag & CLOCAL)
1208 do_clocal = 1;
1209
1210 /*
1211 * Block waiting for the carrier detect and the line to become
1212 * free (i.e., not in use by the callout). While we are in
1213 * this loop, info->count is dropped by one, so that
1214 * rs_close() knows when to free things. We restore it upon
1215 * exit, either normal or abnormal.
1216 */
1217 retval = 0;
1218 add_wait_queue(&info->open_wait, &wait);
1219
1220 info->count--;
1221 info->blocked_open++;
1222 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001223 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001225 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 current->state = TASK_INTERRUPTIBLE;
1227 if (tty_hung_up_p(filp) ||
1228 !(info->flags & S_INITIALIZED)) {
1229#ifdef SERIAL_DO_RESTART
1230 if (info->flags & S_HUP_NOTIFY)
1231 retval = -EAGAIN;
1232 else
1233 retval = -ERESTARTSYS;
1234#else
1235 retval = -EAGAIN;
1236#endif
1237 break;
1238 }
1239 if (!(info->flags & S_CLOSING) && do_clocal)
1240 break;
1241 if (signal_pending(current)) {
1242 retval = -ERESTARTSYS;
1243 break;
1244 }
1245 schedule();
1246 }
1247 current->state = TASK_RUNNING;
1248 remove_wait_queue(&info->open_wait, &wait);
1249 if (!tty_hung_up_p(filp))
1250 info->count++;
1251 info->blocked_open--;
1252
1253 if (retval)
1254 return retval;
1255 info->flags |= S_NORMAL_ACTIVE;
1256 return 0;
1257}
1258
1259/*
1260 * This routine is called whenever a serial port is opened. It
1261 * enables interrupts for a serial port, linking in its S structure into
1262 * the IRQ chain. It also performs the serial-specific
1263 * initialization for the tty structure.
1264 */
1265int rs_open(struct tty_struct *tty, struct file * filp)
1266{
1267 struct m68k_serial *info;
1268 int retval, line;
1269
1270 line = tty->index;
1271
1272 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1273 return -ENODEV;
1274
1275 info = &m68k_soft[line];
1276
1277 if (serial_paranoia_check(info, tty->name, "rs_open"))
1278 return -ENODEV;
1279
1280 info->count++;
1281 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001282 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /*
1285 * Start up serial port
1286 */
1287 retval = startup(info);
1288 if (retval)
1289 return retval;
1290
1291 return block_til_ready(tty, filp, info);
1292}
1293
1294/* Finally, routines used to initialize the serial driver. */
1295
1296static void show_serial_version(void)
1297{
1298 printk("MC68328 serial driver version 1.00\n");
1299}
1300
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001301static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .open = rs_open,
1303 .close = rs_close,
1304 .write = rs_write,
1305 .flush_chars = rs_flush_chars,
1306 .write_room = rs_write_room,
1307 .chars_in_buffer = rs_chars_in_buffer,
1308 .flush_buffer = rs_flush_buffer,
1309 .ioctl = rs_ioctl,
1310 .throttle = rs_throttle,
1311 .unthrottle = rs_unthrottle,
1312 .set_termios = rs_set_termios,
1313 .stop = rs_stop,
1314 .start = rs_start,
1315 .hangup = rs_hangup,
1316 .set_ldisc = rs_set_ldisc,
1317};
1318
1319/* rs_init inits the driver */
1320static int __init
1321rs68328_init(void)
1322{
1323 int flags, i;
1324 struct m68k_serial *info;
1325
1326 serial_driver = alloc_tty_driver(NR_PORTS);
1327 if (!serial_driver)
1328 return -ENOMEM;
1329
1330 show_serial_version();
1331
1332 /* Initialize the tty_driver structure */
1333 /* SPARC: Not all of this is exactly right for us. */
1334
1335 serial_driver->name = "ttyS";
1336 serial_driver->major = TTY_MAJOR;
1337 serial_driver->minor_start = 64;
1338 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1339 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1340 serial_driver->init_termios = tty_std_termios;
1341 serial_driver->init_termios.c_cflag =
1342 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1343 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1344 tty_set_operations(serial_driver, &rs_ops);
1345
1346 if (tty_register_driver(serial_driver)) {
1347 put_tty_driver(serial_driver);
1348 printk(KERN_ERR "Couldn't register serial driver\n");
1349 return -ENOMEM;
1350 }
1351
Greg Ungerer727dda82006-06-28 15:54:22 +10001352 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 for(i=0;i<NR_PORTS;i++) {
1355
1356 info = &m68k_soft[i];
1357 info->magic = SERIAL_MAGIC;
1358 info->port = (int) &uart_addr[i];
Takashi Iwaia88487c2008-07-16 21:54:42 +01001359 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 info->irq = uart_irqs[i];
1361 info->custom_divisor = 16;
1362 info->close_delay = 50;
1363 info->closing_wait = 3000;
1364 info->x_char = 0;
1365 info->event = 0;
1366 info->count = 0;
1367 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001368 INIT_WORK(&info->tqueue, do_softint);
1369 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 init_waitqueue_head(&info->open_wait);
1371 init_waitqueue_head(&info->close_wait);
1372 info->line = i;
1373 info->is_cons = 1; /* Means shortcuts work */
1374
1375 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1376 info->port, info->irq);
1377 printk(" is a builtin MC68328 UART\n");
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379#ifdef CONFIG_M68VZ328
1380 if (i > 0 )
1381 PJSEL &= 0xCF; /* PSW enable second port output */
1382#endif
1383
1384 if (request_irq(uart_irqs[i],
1385 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001386 IRQF_DISABLED,
Alan Cox25db8ad2008-08-19 20:49:40 -07001387 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001390 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return 0;
1392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394module_init(rs68328_init);
1395
1396
1397
1398static void m68328_set_baud(void)
1399{
1400 unsigned short ustcnt;
1401 int i;
1402
1403 ustcnt = USTCNT;
1404 USTCNT = ustcnt & ~USTCNT_TXEN;
1405
1406again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001407 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (baud_table[i] == m68328_console_baud)
1409 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001410 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 m68328_console_baud = 9600;
1412 goto again;
1413 }
1414
1415 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1416 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1417 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1418 ustcnt |= USTCNT_8_7;
1419 ustcnt |= USTCNT_TXEN;
1420 USTCNT = ustcnt;
1421 m68328_console_initted = 1;
1422 return;
1423}
1424
1425
1426int m68328_console_setup(struct console *cp, char *arg)
1427{
1428 int i, n = CONSOLE_BAUD_RATE;
1429
1430 if (!cp)
1431 return(-1);
1432
1433 if (arg)
1434 n = simple_strtoul(arg,NULL,0);
1435
Thiago Farina8b505ca2009-12-09 12:31:30 -08001436 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (baud_table[i] == n)
1438 break;
1439 if (i < BAUD_TABLE_SIZE) {
1440 m68328_console_baud = n;
1441 m68328_console_cbaud = 0;
1442 if (i > 15) {
1443 m68328_console_cbaud |= CBAUDEX;
1444 i -= 15;
1445 }
1446 m68328_console_cbaud |= i;
1447 }
1448
1449 m68328_set_baud(); /* make sure baud rate changes */
1450 return(0);
1451}
1452
1453
1454static struct tty_driver *m68328_console_device(struct console *c, int *index)
1455{
1456 *index = c->index;
1457 return serial_driver;
1458}
1459
1460
1461void m68328_console_write (struct console *co, const char *str,
1462 unsigned int count)
1463{
1464 if (!m68328_console_initted)
1465 m68328_set_baud();
1466 while (count--) {
1467 if (*str == '\n')
1468 rs_put_char('\r');
1469 rs_put_char( *str++ );
1470 }
1471}
1472
1473
1474static struct console m68328_driver = {
1475 .name = "ttyS",
1476 .write = m68328_console_write,
1477 .device = m68328_console_device,
1478 .setup = m68328_console_setup,
1479 .flags = CON_PRINTBUFFER,
1480 .index = -1,
1481};
1482
1483
1484static int __init m68328_console_init(void)
1485{
1486 register_console(&m68328_driver);
1487 return 0;
1488}
1489
1490console_initcall(m68328_console_init);