blob: 78ed24bb6a35d4eb09f9a4f90939f5b4f7151d20 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
60#include "68328serial.h"
61
62/* Turn off usage of real serial interrupt code, to "support" Copilot */
63#ifdef CONFIG_XCOPILOT_BUGS
64#undef USE_INTS
65#else
66#define USE_INTS
67#endif
68
69static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72
73/* multiple ports are contiguous in memory */
74m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75
76struct tty_struct m68k_ttys;
77struct m68k_serial *m68k_consinfo = 0;
78
79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80
81#ifdef CONFIG_CONSOLE
82extern wait_queue_head_t keypress_wait;
83#endif
84
85struct tty_driver *serial_driver;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* number of characters left in xmit buffer before we ask for more */
88#define WAKEUP_CHARS 256
89
90/* Debugging... DEBUG_INTR is bad to use when one of the zs
91 * lines is your console ;(
92 */
93#undef SERIAL_DEBUG_INTR
94#undef SERIAL_DEBUG_OPEN
95#undef SERIAL_DEBUG_FLOW
96
97#define RS_ISR_PASS_LIMIT 256
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void change_speed(struct m68k_serial *info);
100
101/*
102 * Setup for console. Argument comes from the boot command line.
103 */
104
105#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
106#define CONSOLE_BAUD_RATE 115200
107#define DEFAULT_CBAUD B115200
108#else
109 /* (es) */
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
114 #endif
115 /* (/es) */
116#endif
117
118#ifndef CONSOLE_BAUD_RATE
119#define CONSOLE_BAUD_RATE 9600
120#define DEFAULT_CBAUD B9600
121#endif
122
123
124static int m68328_console_initted = 0;
125static int m68328_console_baud = CONSOLE_BAUD_RATE;
126static int m68328_console_cbaud = DEFAULT_CBAUD;
127
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static inline int serial_paranoia_check(struct m68k_serial *info,
130 char *name, const char *routine)
131{
132#ifdef SERIAL_PARANOIA_CHECK
133 static const char *badmagic =
134 "Warning: bad magic number for serial struct %s in %s\n";
135 static const char *badinfo =
136 "Warning: null m68k_serial for %s in %s\n";
137
138 if (!info) {
139 printk(badinfo, name, routine);
140 return 1;
141 }
142 if (info->magic != SERIAL_MAGIC) {
143 printk(badmagic, name, routine);
144 return 1;
145 }
146#endif
147 return 0;
148}
149
150/*
151 * This is used to figure out the divisor speeds and the timeouts
152 */
153static int baud_table[] = {
154 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
155 9600, 19200, 38400, 57600, 115200, 0 };
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Sets or clears DTR/RTS on the requested line */
158static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
159{
160 if (set) {
161 /* set the RTS/CTS line */
162 } else {
163 /* clear it */
164 }
165 return;
166}
167
168/* Utility routines */
169static inline int get_baud(struct m68k_serial *ss)
170{
171 unsigned long result = 115200;
172 unsigned short int baud = uart_addr[ss->line].ubaud;
173 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
174 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
175
176 return result;
177}
178
179/*
180 * ------------------------------------------------------------
181 * rs_stop() and rs_start()
182 *
183 * This routines are called before setting or resetting tty->stopped.
184 * They enable or disable transmitter interrupts, as necessary.
185 * ------------------------------------------------------------
186 */
187static void rs_stop(struct tty_struct *tty)
188{
189 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
190 m68328_uart *uart = &uart_addr[info->line];
191 unsigned long flags;
192
193 if (serial_paranoia_check(info, tty->name, "rs_stop"))
194 return;
195
Greg Ungerer727dda82006-06-28 15:54:22 +1000196 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000198 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Alan Cox09a6ffa2008-04-30 00:54:01 -0700201static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int flags, loops = 0;
204
Greg Ungerer727dda82006-06-28 15:54:22 +1000205 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
208 loops++;
209 udelay(5);
210 }
211
212 UTX_TXDATA = ch;
213 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000214 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700215 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void rs_start(struct tty_struct *tty)
219{
220 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
221 m68328_uart *uart = &uart_addr[info->line];
222 unsigned long flags;
223
224 if (serial_paranoia_check(info, tty->name, "rs_start"))
225 return;
226
Greg Ungerer727dda82006-06-28 15:54:22 +1000227 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
229#ifdef USE_INTS
230 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
231#else
232 uart->ustcnt |= USTCNT_TXEN;
233#endif
234 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000235 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/* Drop into either the boot monitor or kadb upon receiving a break
239 * from keyboard/console input.
240 */
241static void batten_down_hatches(void)
242{
243 /* Drop into the debugger */
244}
245
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800246static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248#if 0
249 if(status & DCD) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100250 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ((info->curregs[3] & AUTO_ENAB)==0)) {
252 info->curregs[3] |= AUTO_ENAB;
253 info->pendregs[3] |= AUTO_ENAB;
254 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
255 }
256 } else {
257 if((info->curregs[3] & AUTO_ENAB)) {
258 info->curregs[3] &= ~AUTO_ENAB;
259 info->pendregs[3] &= ~AUTO_ENAB;
260 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
261 }
262 }
263#endif
264 /* If this is console input and this is a
265 * 'break asserted' status change interrupt
266 * see if we can drop into the debugger
267 */
268 if((status & URX_BREAK) && info->break_abort)
269 batten_down_hatches();
270 return;
271}
272
David Howells7d12e782006-10-05 14:55:46 +0100273static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100275 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800277 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
280 * This do { } while() loop will get ALL chars out of Rx FIFO
281 */
282#ifndef CONFIG_XCOPILOT_BUGS
283 do {
284#endif
285 ch = GET_FIELD(rx, URX_RXDATA);
286
287 if(info->is_cons) {
288 if(URX_BREAK & rx) { /* whee, break received */
289 status_handle(info, rx);
290 return;
291#ifdef CONFIG_MAGIC_SYSRQ
292 } else if (ch == 0x10) { /* ^P */
293 show_state();
294 show_free_areas();
295 show_buffers();
296/* show_net_buffers(); */
297 return;
298 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600299 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return;
301#endif /* CONFIG_MAGIC_SYSRQ */
302 }
303 /* It is a 'keyboard interrupt' ;-) */
304#ifdef CONFIG_CONSOLE
305 wake_up(&keypress_wait);
306#endif
307 }
308
309 if(!tty)
310 goto clear_and_exit;
311
Alan Cox33f0f882006-01-09 20:54:13 -0800312 flag = TTY_NORMAL;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800315 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 status_handle(info, rx);
317 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800318 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 status_handle(info, rx);
320 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800321 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Alan Cox33f0f882006-01-09 20:54:13 -0800324 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#ifndef CONFIG_XCOPILOT_BUGS
326 } while((rx = uart->urx.w) & URX_DATA_READY);
327#endif
328
Greg Ungerer8e63e662006-02-08 09:19:17 +1000329 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331clear_and_exit:
332 return;
333}
334
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800335static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 m68328_uart *uart = &uart_addr[info->line];
338
339 if (info->x_char) {
340 /* Send next char */
341 uart->utx.b.txdata = info->x_char;
342 info->x_char = 0;
343 goto clear_and_return;
344 }
345
Takashi Iwaia88487c2008-07-16 21:54:42 +0100346 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* That's peculiar... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
350 }
351
352 /* Send char */
353 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
354 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
355 info->xmit_cnt--;
356
357 if (info->xmit_cnt < WAKEUP_CHARS)
358 schedule_work(&info->tqueue);
359
360 if(info->xmit_cnt <= 0) {
361 /* All done for now... TX ints off */
362 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
363 goto clear_and_return;
364 }
365
366clear_and_return:
367 /* Clear interrupt (should be auto)*/
368 return;
369}
370
371/*
372 * This is the serial driver's generic interrupt routine
373 */
David Howells7d12e782006-10-05 14:55:46 +0100374irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Alan Cox25db8ad2008-08-19 20:49:40 -0700376 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 m68328_uart *uart;
378 unsigned short rx;
379 unsigned short tx;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 uart = &uart_addr[info->line];
382 rx = uart->urx.w;
383
384#ifdef USE_INTS
385 tx = uart->utx.w;
386
David Howells7d12e782006-10-05 14:55:46 +0100387 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (tx & UTX_TX_AVAIL) transmit_chars(info);
389#else
David Howells7d12e782006-10-05 14:55:46 +0100390 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#endif
392 return IRQ_HANDLED;
393}
394
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000395static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000397 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct tty_struct *tty;
399
Takashi Iwaia88487c2008-07-16 21:54:42 +0100400 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (!tty)
402 return;
403#if 0
404 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
405 tty_wakeup(tty);
406 }
407#endif
408}
409
410/*
411 * This routine is called from the scheduler tqueue when the interrupt
412 * routine has signalled that a hangup has occurred. The path of
413 * hangup processing is:
414 *
415 * serial interrupt routine -> (scheduler tqueue) ->
416 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
417 *
418 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000419static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000421 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct tty_struct *tty;
423
Takashi Iwaia88487c2008-07-16 21:54:42 +0100424 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!tty)
426 return;
427
428 tty_hangup(tty);
429}
430
431
432static int startup(struct m68k_serial * info)
433{
434 m68328_uart *uart = &uart_addr[info->line];
435 unsigned long flags;
436
437 if (info->flags & S_INITIALIZED)
438 return 0;
439
440 if (!info->xmit_buf) {
441 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
442 if (!info->xmit_buf)
443 return -ENOMEM;
444 }
445
Greg Ungerer727dda82006-06-28 15:54:22 +1000446 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * Clear the FIFO buffers and disable them
450 * (they will be reenabled in change_speed())
451 */
452
453 uart->ustcnt = USTCNT_UEN;
454 info->xmit_fifo_size = 1;
455 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
456 (void)uart->urx.w;
457
458 /*
459 * Finally, enable sequencing and interrupts
460 */
461#ifdef USE_INTS
462 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
463 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
464#else
465 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
466#endif
467
Takashi Iwaia88487c2008-07-16 21:54:42 +0100468 if (info->port.tty)
469 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
471
472 /*
473 * and set the speed of the serial port
474 */
475
476 change_speed(info);
477
478 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000479 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
482
483/*
484 * This routine will shutdown a serial port; interrupts are disabled, and
485 * DTR is dropped if the hangup on close termio flag is on.
486 */
487static void shutdown(struct m68k_serial * info)
488{
489 m68328_uart *uart = &uart_addr[info->line];
490 unsigned long flags;
491
492 uart->ustcnt = 0; /* All off! */
493 if (!(info->flags & S_INITIALIZED))
494 return;
495
Greg Ungerer727dda82006-06-28 15:54:22 +1000496 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (info->xmit_buf) {
499 free_page((unsigned long) info->xmit_buf);
500 info->xmit_buf = 0;
501 }
502
Takashi Iwaia88487c2008-07-16 21:54:42 +0100503 if (info->port.tty)
504 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000507 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510struct {
511 int divisor, prescale;
512}
513#ifndef CONFIG_M68VZ328
514 hw_baud_table[18] = {
515 {0,0}, /* 0 */
516 {0,0}, /* 50 */
517 {0,0}, /* 75 */
518 {0,0}, /* 110 */
519 {0,0}, /* 134 */
520 {0,0}, /* 150 */
521 {0,0}, /* 200 */
522 {7,0x26}, /* 300 */
523 {6,0x26}, /* 600 */
524 {5,0x26}, /* 1200 */
525 {0,0}, /* 1800 */
526 {4,0x26}, /* 2400 */
527 {3,0x26}, /* 4800 */
528 {2,0x26}, /* 9600 */
529 {1,0x26}, /* 19200 */
530 {0,0x26}, /* 38400 */
531 {1,0x38}, /* 57600 */
532 {0,0x38}, /* 115200 */
533};
534#else
535 hw_baud_table[18] = {
536 {0,0}, /* 0 */
537 {0,0}, /* 50 */
538 {0,0}, /* 75 */
539 {0,0}, /* 110 */
540 {0,0}, /* 134 */
541 {0,0}, /* 150 */
542 {0,0}, /* 200 */
543 {0,0}, /* 300 */
544 {7,0x26}, /* 600 */
545 {6,0x26}, /* 1200 */
546 {0,0}, /* 1800 */
547 {5,0x26}, /* 2400 */
548 {4,0x26}, /* 4800 */
549 {3,0x26}, /* 9600 */
550 {2,0x26}, /* 19200 */
551 {1,0x26}, /* 38400 */
552 {0,0x26}, /* 57600 */
553 {1,0x38}, /* 115200 */
554};
555#endif
556/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
557
558/*
559 * This routine is called to set the UART divisor registers to match
560 * the specified baud rate for a serial port.
561 */
562static void change_speed(struct m68k_serial *info)
563{
564 m68328_uart *uart = &uart_addr[info->line];
565 unsigned short port;
566 unsigned short ustcnt;
567 unsigned cflag;
568 int i;
569
Takashi Iwaia88487c2008-07-16 21:54:42 +0100570 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100572 cflag = info->port.tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!(port = info->port))
574 return;
575
576 ustcnt = uart->ustcnt;
577 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
578
579 i = cflag & CBAUD;
580 if (i & CBAUDEX) {
581 i = (i & ~CBAUDEX) + B38400;
582 }
583
584 info->baud = baud_table[i];
585 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
586 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
587
588 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
589
590 if ((cflag & CSIZE) == CS8)
591 ustcnt |= USTCNT_8_7;
592
593 if (cflag & CSTOPB)
594 ustcnt |= USTCNT_STOP;
595
596 if (cflag & PARENB)
597 ustcnt |= USTCNT_PARITYEN;
598 if (cflag & PARODD)
599 ustcnt |= USTCNT_ODD_EVEN;
600
601#ifdef CONFIG_SERIAL_68328_RTS_CTS
602 if (cflag & CRTSCTS) {
603 uart->utx.w &= ~ UTX_NOCTS;
604 } else {
605 uart->utx.w |= UTX_NOCTS;
606 }
607#endif
608
609 ustcnt |= USTCNT_TXEN;
610
611 uart->ustcnt = ustcnt;
612 return;
613}
614
615/*
616 * Fair output driver allows a process to speak.
617 */
618static void rs_fair_output(void)
619{
620 int left; /* Output no more than that */
621 unsigned long flags;
622 struct m68k_serial *info = &m68k_soft[0];
623 char c;
624
625 if (info == 0) return;
626 if (info->xmit_buf == 0) return;
627
Greg Ungerer727dda82006-06-28 15:54:22 +1000628 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 left = info->xmit_cnt;
630 while (left != 0) {
631 c = info->xmit_buf[info->xmit_tail];
632 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
633 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000634 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 rs_put_char(c);
637
Greg Ungerer727dda82006-06-28 15:54:22 +1000638 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 left = min(info->xmit_cnt, left-1);
640 }
641
642 /* Last character is being transmitted now (hopefully). */
643 udelay(5);
644
Greg Ungerer727dda82006-06-28 15:54:22 +1000645 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647}
648
649/*
650 * m68k_console_print is registered for printk.
651 */
652void console_print_68328(const char *p)
653{
654 char c;
655
656 while((c=*(p++)) != 0) {
657 if(c == '\n')
658 rs_put_char('\r');
659 rs_put_char(c);
660 }
661
662 /* Comment this if you want to have a strict interrupt-driven output */
663 rs_fair_output();
664
665 return;
666}
667
668static void rs_set_ldisc(struct tty_struct *tty)
669{
670 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
671
672 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
673 return;
674
675 info->is_cons = (tty->termios->c_line == N_TTY);
676
677 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
678}
679
680static void rs_flush_chars(struct tty_struct *tty)
681{
682 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
683 m68328_uart *uart = &uart_addr[info->line];
684 unsigned long flags;
685
686 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
687 return;
688#ifndef USE_INTS
689 for(;;) {
690#endif
691
692 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000693 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
696 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000697 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699 }
700
701#ifdef USE_INTS
702 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
703#else
704 uart->ustcnt |= USTCNT_TXEN;
705#endif
706
707#ifdef USE_INTS
708 if (uart->utx.w & UTX_TX_AVAIL) {
709#else
710 if (1) {
711#endif
712 /* Send char */
713 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
714 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
715 info->xmit_cnt--;
716 }
717
718#ifndef USE_INTS
719 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
720 }
721#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000722 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725extern void console_printn(const char * b, int count);
726
727static int rs_write(struct tty_struct * tty,
728 const unsigned char *buf, int count)
729{
730 int c, total = 0;
731 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
732 m68328_uart *uart = &uart_addr[info->line];
733 unsigned long flags;
734
735 if (serial_paranoia_check(info, tty->name, "rs_write"))
736 return 0;
737
738 if (!tty || !info->xmit_buf)
739 return 0;
740
Greg Ungerer727dda82006-06-28 15:54:22 +1000741 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000743 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
745 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000746 local_irq_restore(flags);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (c <= 0)
749 break;
750
751 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000752
753 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
755 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000756 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 buf += c;
758 count -= c;
759 total += c;
760 }
761
762 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
763 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000764 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#ifndef USE_INTS
766 while(info->xmit_cnt) {
767#endif
768
769 uart->ustcnt |= USTCNT_TXEN;
770#ifdef USE_INTS
771 uart->ustcnt |= USTCNT_TX_INTR_MASK;
772#else
773 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
774#endif
775 if (uart->utx.w & UTX_TX_AVAIL) {
776 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
777 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
778 info->xmit_cnt--;
779 }
780
781#ifndef USE_INTS
782 }
783#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000784 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return total;
788}
789
790static int rs_write_room(struct tty_struct *tty)
791{
792 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
793 int ret;
794
795 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
796 return 0;
797 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
798 if (ret < 0)
799 ret = 0;
800 return ret;
801}
802
803static int rs_chars_in_buffer(struct tty_struct *tty)
804{
805 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
806
807 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
808 return 0;
809 return info->xmit_cnt;
810}
811
812static void rs_flush_buffer(struct tty_struct *tty)
813{
814 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000815 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
818 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000819 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000821 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 tty_wakeup(tty);
823}
824
825/*
826 * ------------------------------------------------------------
827 * rs_throttle()
828 *
829 * This routine is called by the upper-layer tty layer to signal that
830 * incoming characters should be throttled.
831 * ------------------------------------------------------------
832 */
833static void rs_throttle(struct tty_struct * tty)
834{
835 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
836
837 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
838 return;
839
840 if (I_IXOFF(tty))
841 info->x_char = STOP_CHAR(tty);
842
843 /* Turn off RTS line (do this atomic) */
844}
845
846static void rs_unthrottle(struct tty_struct * tty)
847{
848 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
849
850 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
851 return;
852
853 if (I_IXOFF(tty)) {
854 if (info->x_char)
855 info->x_char = 0;
856 else
857 info->x_char = START_CHAR(tty);
858 }
859
860 /* Assert RTS line (do this atomic) */
861}
862
863/*
864 * ------------------------------------------------------------
865 * rs_ioctl() and friends
866 * ------------------------------------------------------------
867 */
868
869static int get_serial_info(struct m68k_serial * info,
870 struct serial_struct * retinfo)
871{
872 struct serial_struct tmp;
873
874 if (!retinfo)
875 return -EFAULT;
876 memset(&tmp, 0, sizeof(tmp));
877 tmp.type = info->type;
878 tmp.line = info->line;
879 tmp.port = info->port;
880 tmp.irq = info->irq;
881 tmp.flags = info->flags;
882 tmp.baud_base = info->baud_base;
883 tmp.close_delay = info->close_delay;
884 tmp.closing_wait = info->closing_wait;
885 tmp.custom_divisor = info->custom_divisor;
886 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
887 return 0;
888}
889
890static int set_serial_info(struct m68k_serial * info,
891 struct serial_struct * new_info)
892{
893 struct serial_struct new_serial;
894 struct m68k_serial old_info;
895 int retval = 0;
896
897 if (!new_info)
898 return -EFAULT;
899 copy_from_user(&new_serial,new_info,sizeof(new_serial));
900 old_info = *info;
901
902 if (!capable(CAP_SYS_ADMIN)) {
903 if ((new_serial.baud_base != info->baud_base) ||
904 (new_serial.type != info->type) ||
905 (new_serial.close_delay != info->close_delay) ||
906 ((new_serial.flags & ~S_USR_MASK) !=
907 (info->flags & ~S_USR_MASK)))
908 return -EPERM;
909 info->flags = ((info->flags & ~S_USR_MASK) |
910 (new_serial.flags & S_USR_MASK));
911 info->custom_divisor = new_serial.custom_divisor;
912 goto check_and_exit;
913 }
914
915 if (info->count > 1)
916 return -EBUSY;
917
918 /*
919 * OK, past this point, all the error checking has been done.
920 * At this point, we start making changes.....
921 */
922
923 info->baud_base = new_serial.baud_base;
924 info->flags = ((info->flags & ~S_FLAGS) |
925 (new_serial.flags & S_FLAGS));
926 info->type = new_serial.type;
927 info->close_delay = new_serial.close_delay;
928 info->closing_wait = new_serial.closing_wait;
929
930check_and_exit:
931 retval = startup(info);
932 return retval;
933}
934
935/*
936 * get_lsr_info - get line status register info
937 *
938 * Purpose: Let user call ioctl() to get info when the UART physically
939 * is emptied. On bus types like RS485, the transmitter must
940 * release the bus after transmitting. This must be done when
941 * the transmit shift register is empty, not be done when the
942 * transmit holding register is empty. This functionality
943 * allows an RS485 driver to be written in user space.
944 */
945static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
946{
947#ifdef CONFIG_SERIAL_68328_RTS_CTS
948 m68328_uart *uart = &uart_addr[info->line];
949#endif
950 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000951 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Greg Ungerer727dda82006-06-28 15:54:22 +1000953 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#ifdef CONFIG_SERIAL_68328_RTS_CTS
955 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
956#else
957 status = 0;
958#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000959 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 put_user(status,value);
961 return 0;
962}
963
964/*
965 * This routine sends a break character out the serial port.
966 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700967static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 m68328_uart *uart = &uart_addr[info->line];
970 unsigned long flags;
971 if (!info->port)
972 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000973 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#ifdef USE_INTS
975 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700976 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 uart->utx.w &= ~UTX_SEND_BREAK;
978#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000979 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982static int rs_ioctl(struct tty_struct *tty, struct file * file,
983 unsigned int cmd, unsigned long arg)
984{
985 int error;
986 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
987 int retval;
988
989 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
990 return -ENODEV;
991
992 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
993 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
994 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
995 if (tty->flags & (1 << TTY_IO_ERROR))
996 return -EIO;
997 }
998
999 switch (cmd) {
1000 case TCSBRK: /* SVID version: non-zero arg --> no break */
1001 retval = tty_check_change(tty);
1002 if (retval)
1003 return retval;
1004 tty_wait_until_sent(tty, 0);
1005 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001006 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0;
1008 case TCSBRKP: /* support for POSIX tcsendbreak() */
1009 retval = tty_check_change(tty);
1010 if (retval)
1011 return retval;
1012 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001013 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 case TIOCGSERIAL:
1016 if (access_ok(VERIFY_WRITE, (void *) arg,
1017 sizeof(struct serial_struct)))
1018 return get_serial_info(info,
1019 (struct serial_struct *) arg);
1020 return -EFAULT;
1021 case TIOCSSERIAL:
1022 return set_serial_info(info,
1023 (struct serial_struct *) arg);
1024 case TIOCSERGETLSR: /* Get line status register */
1025 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001026 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return get_lsr_info(info, (unsigned int *) arg);
1028 return -EFAULT;
1029 case TIOCSERGSTRUCT:
1030 if (!access_ok(VERIFY_WRITE, (void *) arg,
1031 sizeof(struct m68k_serial)))
1032 return -EFAULT;
1033 copy_to_user((struct m68k_serial *) arg,
1034 info, sizeof(struct m68k_serial));
1035 return 0;
1036
1037 default:
1038 return -ENOIOCTLCMD;
1039 }
1040 return 0;
1041}
1042
Alan Cox606d0992006-12-08 02:38:45 -08001043static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 change_speed(info);
1048
1049 if ((old_termios->c_cflag & CRTSCTS) &&
1050 !(tty->termios->c_cflag & CRTSCTS)) {
1051 tty->hw_stopped = 0;
1052 rs_start(tty);
1053 }
1054
1055}
1056
1057/*
1058 * ------------------------------------------------------------
1059 * rs_close()
1060 *
1061 * This routine is called when the serial port gets closed. First, we
1062 * wait for the last remaining data to be sent. Then, we unlink its
1063 * S structure from the interrupt chain if necessary, and we free
1064 * that IRQ if nothing is left in the chain.
1065 * ------------------------------------------------------------
1066 */
1067static void rs_close(struct tty_struct *tty, struct file * filp)
1068{
1069 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1070 m68328_uart *uart = &uart_addr[info->line];
1071 unsigned long flags;
1072
1073 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1074 return;
1075
Greg Ungerer727dda82006-06-28 15:54:22 +10001076 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001079 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return;
1081 }
1082
1083 if ((tty->count == 1) && (info->count != 1)) {
1084 /*
1085 * Uh, oh. tty->count is 1, which means that the tty
1086 * structure will be freed. Info->count should always
1087 * be one in these conditions. If it's greater than
1088 * one, we've got real problems, since it means the
1089 * serial port won't be shutdown.
1090 */
1091 printk("rs_close: bad serial port count; tty->count is 1, "
1092 "info->count is %d\n", info->count);
1093 info->count = 1;
1094 }
1095 if (--info->count < 0) {
1096 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1097 info->line, info->count);
1098 info->count = 0;
1099 }
1100 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001101 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return;
1103 }
1104 info->flags |= S_CLOSING;
1105 /*
1106 * Now we wait for the transmit buffer to clear; and we notify
1107 * the line discipline to only process XON/XOFF characters.
1108 */
1109 tty->closing = 1;
1110 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1111 tty_wait_until_sent(tty, info->closing_wait);
1112 /*
1113 * At this point we stop accepting input. To do this, we
1114 * disable the receive line status interrupts, and tell the
1115 * interrupt driver to stop checking the data ready bit in the
1116 * line status register.
1117 */
1118
1119 uart->ustcnt &= ~USTCNT_RXEN;
1120 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1121
1122 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001123 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 tty_ldisc_flush(tty);
1126 tty->closing = 0;
1127 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001128 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#warning "This is not and has never been valid so fix it"
1130#if 0
1131 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1132 if (tty->ldisc.close)
1133 (tty->ldisc.close)(tty);
1134 tty->ldisc = ldiscs[N_TTY];
1135 tty->termios->c_line = N_TTY;
1136 if (tty->ldisc.open)
1137 (tty->ldisc.open)(tty);
1138 }
1139#endif
1140 if (info->blocked_open) {
1141 if (info->close_delay) {
1142 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1143 }
1144 wake_up_interruptible(&info->open_wait);
1145 }
1146 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1147 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001148 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151/*
1152 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1153 */
1154void rs_hangup(struct tty_struct *tty)
1155{
1156 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1157
1158 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1159 return;
1160
1161 rs_flush_buffer(tty);
1162 shutdown(info);
1163 info->event = 0;
1164 info->count = 0;
1165 info->flags &= ~S_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001166 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 wake_up_interruptible(&info->open_wait);
1168}
1169
1170/*
1171 * ------------------------------------------------------------
1172 * rs_open() and friends
1173 * ------------------------------------------------------------
1174 */
1175static int block_til_ready(struct tty_struct *tty, struct file * filp,
1176 struct m68k_serial *info)
1177{
1178 DECLARE_WAITQUEUE(wait, current);
1179 int retval;
1180 int do_clocal = 0;
1181
1182 /*
1183 * If the device is in the middle of being closed, then block
1184 * until it's done, and then try again.
1185 */
1186 if (info->flags & S_CLOSING) {
1187 interruptible_sleep_on(&info->close_wait);
1188#ifdef SERIAL_DO_RESTART
1189 if (info->flags & S_HUP_NOTIFY)
1190 return -EAGAIN;
1191 else
1192 return -ERESTARTSYS;
1193#else
1194 return -EAGAIN;
1195#endif
1196 }
1197
1198 /*
1199 * If non-blocking mode is set, or the port is not enabled,
1200 * then make the check up front and then exit.
1201 */
1202 if ((filp->f_flags & O_NONBLOCK) ||
1203 (tty->flags & (1 << TTY_IO_ERROR))) {
1204 info->flags |= S_NORMAL_ACTIVE;
1205 return 0;
1206 }
1207
1208 if (tty->termios->c_cflag & CLOCAL)
1209 do_clocal = 1;
1210
1211 /*
1212 * Block waiting for the carrier detect and the line to become
1213 * free (i.e., not in use by the callout). While we are in
1214 * this loop, info->count is dropped by one, so that
1215 * rs_close() knows when to free things. We restore it upon
1216 * exit, either normal or abnormal.
1217 */
1218 retval = 0;
1219 add_wait_queue(&info->open_wait, &wait);
1220
1221 info->count--;
1222 info->blocked_open++;
1223 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001224 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001226 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 current->state = TASK_INTERRUPTIBLE;
1228 if (tty_hung_up_p(filp) ||
1229 !(info->flags & S_INITIALIZED)) {
1230#ifdef SERIAL_DO_RESTART
1231 if (info->flags & S_HUP_NOTIFY)
1232 retval = -EAGAIN;
1233 else
1234 retval = -ERESTARTSYS;
1235#else
1236 retval = -EAGAIN;
1237#endif
1238 break;
1239 }
1240 if (!(info->flags & S_CLOSING) && do_clocal)
1241 break;
1242 if (signal_pending(current)) {
1243 retval = -ERESTARTSYS;
1244 break;
1245 }
1246 schedule();
1247 }
1248 current->state = TASK_RUNNING;
1249 remove_wait_queue(&info->open_wait, &wait);
1250 if (!tty_hung_up_p(filp))
1251 info->count++;
1252 info->blocked_open--;
1253
1254 if (retval)
1255 return retval;
1256 info->flags |= S_NORMAL_ACTIVE;
1257 return 0;
1258}
1259
1260/*
1261 * This routine is called whenever a serial port is opened. It
1262 * enables interrupts for a serial port, linking in its S structure into
1263 * the IRQ chain. It also performs the serial-specific
1264 * initialization for the tty structure.
1265 */
1266int rs_open(struct tty_struct *tty, struct file * filp)
1267{
1268 struct m68k_serial *info;
1269 int retval, line;
1270
1271 line = tty->index;
1272
1273 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1274 return -ENODEV;
1275
1276 info = &m68k_soft[line];
1277
1278 if (serial_paranoia_check(info, tty->name, "rs_open"))
1279 return -ENODEV;
1280
1281 info->count++;
1282 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001283 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * Start up serial port
1287 */
1288 retval = startup(info);
1289 if (retval)
1290 return retval;
1291
1292 return block_til_ready(tty, filp, info);
1293}
1294
1295/* Finally, routines used to initialize the serial driver. */
1296
1297static void show_serial_version(void)
1298{
1299 printk("MC68328 serial driver version 1.00\n");
1300}
1301
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001302static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .open = rs_open,
1304 .close = rs_close,
1305 .write = rs_write,
1306 .flush_chars = rs_flush_chars,
1307 .write_room = rs_write_room,
1308 .chars_in_buffer = rs_chars_in_buffer,
1309 .flush_buffer = rs_flush_buffer,
1310 .ioctl = rs_ioctl,
1311 .throttle = rs_throttle,
1312 .unthrottle = rs_unthrottle,
1313 .set_termios = rs_set_termios,
1314 .stop = rs_stop,
1315 .start = rs_start,
1316 .hangup = rs_hangup,
1317 .set_ldisc = rs_set_ldisc,
1318};
1319
1320/* rs_init inits the driver */
1321static int __init
1322rs68328_init(void)
1323{
1324 int flags, i;
1325 struct m68k_serial *info;
1326
1327 serial_driver = alloc_tty_driver(NR_PORTS);
1328 if (!serial_driver)
1329 return -ENOMEM;
1330
1331 show_serial_version();
1332
1333 /* Initialize the tty_driver structure */
1334 /* SPARC: Not all of this is exactly right for us. */
1335
1336 serial_driver->name = "ttyS";
1337 serial_driver->major = TTY_MAJOR;
1338 serial_driver->minor_start = 64;
1339 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1340 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1341 serial_driver->init_termios = tty_std_termios;
1342 serial_driver->init_termios.c_cflag =
1343 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1344 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1345 tty_set_operations(serial_driver, &rs_ops);
1346
1347 if (tty_register_driver(serial_driver)) {
1348 put_tty_driver(serial_driver);
1349 printk(KERN_ERR "Couldn't register serial driver\n");
1350 return -ENOMEM;
1351 }
1352
Greg Ungerer727dda82006-06-28 15:54:22 +10001353 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 for(i=0;i<NR_PORTS;i++) {
1356
1357 info = &m68k_soft[i];
1358 info->magic = SERIAL_MAGIC;
1359 info->port = (int) &uart_addr[i];
Takashi Iwaia88487c2008-07-16 21:54:42 +01001360 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 info->irq = uart_irqs[i];
1362 info->custom_divisor = 16;
1363 info->close_delay = 50;
1364 info->closing_wait = 3000;
1365 info->x_char = 0;
1366 info->event = 0;
1367 info->count = 0;
1368 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001369 INIT_WORK(&info->tqueue, do_softint);
1370 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 init_waitqueue_head(&info->open_wait);
1372 init_waitqueue_head(&info->close_wait);
1373 info->line = i;
1374 info->is_cons = 1; /* Means shortcuts work */
1375
1376 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1377 info->port, info->irq);
1378 printk(" is a builtin MC68328 UART\n");
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_M68VZ328
1381 if (i > 0 )
1382 PJSEL &= 0xCF; /* PSW enable second port output */
1383#endif
1384
1385 if (request_irq(uart_irqs[i],
1386 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001387 IRQF_DISABLED,
Alan Cox25db8ad2008-08-19 20:49:40 -07001388 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001391 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 0;
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395module_init(rs68328_init);
1396
1397
1398
1399static void m68328_set_baud(void)
1400{
1401 unsigned short ustcnt;
1402 int i;
1403
1404 ustcnt = USTCNT;
1405 USTCNT = ustcnt & ~USTCNT_TXEN;
1406
1407again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001408 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (baud_table[i] == m68328_console_baud)
1410 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001411 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 m68328_console_baud = 9600;
1413 goto again;
1414 }
1415
1416 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1417 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1418 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1419 ustcnt |= USTCNT_8_7;
1420 ustcnt |= USTCNT_TXEN;
1421 USTCNT = ustcnt;
1422 m68328_console_initted = 1;
1423 return;
1424}
1425
1426
1427int m68328_console_setup(struct console *cp, char *arg)
1428{
1429 int i, n = CONSOLE_BAUD_RATE;
1430
1431 if (!cp)
1432 return(-1);
1433
1434 if (arg)
1435 n = simple_strtoul(arg,NULL,0);
1436
Thiago Farina8b505ca2009-12-09 12:31:30 -08001437 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (baud_table[i] == n)
1439 break;
1440 if (i < BAUD_TABLE_SIZE) {
1441 m68328_console_baud = n;
1442 m68328_console_cbaud = 0;
1443 if (i > 15) {
1444 m68328_console_cbaud |= CBAUDEX;
1445 i -= 15;
1446 }
1447 m68328_console_cbaud |= i;
1448 }
1449
1450 m68328_set_baud(); /* make sure baud rate changes */
1451 return(0);
1452}
1453
1454
1455static struct tty_driver *m68328_console_device(struct console *c, int *index)
1456{
1457 *index = c->index;
1458 return serial_driver;
1459}
1460
1461
1462void m68328_console_write (struct console *co, const char *str,
1463 unsigned int count)
1464{
1465 if (!m68328_console_initted)
1466 m68328_set_baud();
1467 while (count--) {
1468 if (*str == '\n')
1469 rs_put_char('\r');
1470 rs_put_char( *str++ );
1471 }
1472}
1473
1474
1475static struct console m68328_driver = {
1476 .name = "ttyS",
1477 .write = m68328_console_write,
1478 .device = m68328_console_device,
1479 .setup = m68328_console_setup,
1480 .flags = CON_PRINTBUFFER,
1481 .index = -1,
1482};
1483
1484
1485static int __init m68328_console_init(void)
1486{
1487 register_console(&m68328_driver);
1488 return 0;
1489}
1490
1491console_initcall(m68328_console_init);