blob: 7398390e7e65111b0658a52e1559045651e42363 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct tty_driver *serial_driver;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* number of characters left in xmit buffer before we ask for more */
84#define WAKEUP_CHARS 256
85
86/* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
88 */
89#undef SERIAL_DEBUG_INTR
90#undef SERIAL_DEBUG_OPEN
91#undef SERIAL_DEBUG_FLOW
92
93#define RS_ISR_PASS_LIMIT 256
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static void change_speed(struct m68k_serial *info);
96
97/*
98 * Setup for console. Argument comes from the boot command line.
99 */
100
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102#ifdef CONFIG_M68VZ328
103#define CONSOLE_BAUD_RATE 19200
104#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#ifndef CONSOLE_BAUD_RATE
109#define CONSOLE_BAUD_RATE 9600
110#define DEFAULT_CBAUD B9600
111#endif
112
113
114static int m68328_console_initted = 0;
115static int m68328_console_baud = CONSOLE_BAUD_RATE;
116static int m68328_console_cbaud = DEFAULT_CBAUD;
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline int serial_paranoia_check(struct m68k_serial *info,
120 char *name, const char *routine)
121{
122#ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct %s in %s\n";
125 static const char *badinfo =
126 "Warning: null m68k_serial for %s in %s\n";
127
128 if (!info) {
129 printk(badinfo, name, routine);
130 return 1;
131 }
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
134 return 1;
135 }
136#endif
137 return 0;
138}
139
140/*
141 * This is used to figure out the divisor speeds and the timeouts
142 */
143static int baud_table[] = {
144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145 9600, 19200, 38400, 57600, 115200, 0 };
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* Sets or clears DTR/RTS on the requested line */
148static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149{
150 if (set) {
151 /* set the RTS/CTS line */
152 } else {
153 /* clear it */
154 }
155 return;
156}
157
158/* Utility routines */
159static inline int get_baud(struct m68k_serial *ss)
160{
161 unsigned long result = 115200;
162 unsigned short int baud = uart_addr[ss->line].ubaud;
163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165
166 return result;
167}
168
169/*
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
172 *
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
176 */
177static void rs_stop(struct tty_struct *tty)
178{
179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180 m68328_uart *uart = &uart_addr[info->line];
181 unsigned long flags;
182
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
184 return;
185
Greg Ungerer727dda82006-06-28 15:54:22 +1000186 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000188 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Alan Cox09a6ffa2008-04-30 00:54:01 -0700191static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int flags, loops = 0;
194
Greg Ungerer727dda82006-06-28 15:54:22 +1000195 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
198 loops++;
199 udelay(5);
200 }
201
202 UTX_TXDATA = ch;
203 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000204 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700205 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void rs_start(struct tty_struct *tty)
209{
210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211 m68328_uart *uart = &uart_addr[info->line];
212 unsigned long flags;
213
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
216
Greg Ungerer727dda82006-06-28 15:54:22 +1000217 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219#ifdef USE_INTS
220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221#else
222 uart->ustcnt |= USTCNT_TXEN;
223#endif
224 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000225 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/* Drop into either the boot monitor or kadb upon receiving a break
229 * from keyboard/console input.
230 */
231static void batten_down_hatches(void)
232{
233 /* Drop into the debugger */
234}
235
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800236static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* If this is console input and this is a
239 * 'break asserted' status change interrupt
240 * see if we can drop into the debugger
241 */
242 if((status & URX_BREAK) && info->break_abort)
243 batten_down_hatches();
244 return;
245}
246
David Howells7d12e782006-10-05 14:55:46 +0100247static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000249 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800251 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
254 * This do { } while() loop will get ALL chars out of Rx FIFO
255 */
256#ifndef CONFIG_XCOPILOT_BUGS
257 do {
258#endif
259 ch = GET_FIELD(rx, URX_RXDATA);
260
261 if(info->is_cons) {
262 if(URX_BREAK & rx) { /* whee, break received */
263 status_handle(info, rx);
264 return;
265#ifdef CONFIG_MAGIC_SYSRQ
266 } else if (ch == 0x10) { /* ^P */
267 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700268 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 show_buffers();
270/* show_net_buffers(); */
271 return;
272 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600273 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return;
275#endif /* CONFIG_MAGIC_SYSRQ */
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
279 if(!tty)
280 goto clear_and_exit;
281
Alan Cox33f0f882006-01-09 20:54:13 -0800282 flag = TTY_NORMAL;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800285 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 status_handle(info, rx);
287 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800288 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 status_handle(info, rx);
290 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800291 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Alan Cox33f0f882006-01-09 20:54:13 -0800294 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#ifndef CONFIG_XCOPILOT_BUGS
296 } while((rx = uart->urx.w) & URX_DATA_READY);
297#endif
298
Greg Ungerer8e63e662006-02-08 09:19:17 +1000299 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301clear_and_exit:
302 return;
303}
304
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800305static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 m68328_uart *uart = &uart_addr[info->line];
308
309 if (info->x_char) {
310 /* Send next char */
311 uart->utx.b.txdata = info->x_char;
312 info->x_char = 0;
313 goto clear_and_return;
314 }
315
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000316 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* That's peculiar... TX ints off */
318 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
319 goto clear_and_return;
320 }
321
322 /* Send char */
323 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
324 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
325 info->xmit_cnt--;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if(info->xmit_cnt <= 0) {
328 /* All done for now... TX ints off */
329 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
330 goto clear_and_return;
331 }
332
333clear_and_return:
334 /* Clear interrupt (should be auto)*/
335 return;
336}
337
338/*
339 * This is the serial driver's generic interrupt routine
340 */
David Howells7d12e782006-10-05 14:55:46 +0100341irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Alan Cox25db8ad2008-08-19 20:49:40 -0700343 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 m68328_uart *uart;
345 unsigned short rx;
346 unsigned short tx;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 uart = &uart_addr[info->line];
349 rx = uart->urx.w;
350
351#ifdef USE_INTS
352 tx = uart->utx.w;
353
David Howells7d12e782006-10-05 14:55:46 +0100354 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (tx & UTX_TX_AVAIL) transmit_chars(info);
356#else
David Howells7d12e782006-10-05 14:55:46 +0100357 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#endif
359 return IRQ_HANDLED;
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static int startup(struct m68k_serial * info)
363{
364 m68328_uart *uart = &uart_addr[info->line];
365 unsigned long flags;
366
367 if (info->flags & S_INITIALIZED)
368 return 0;
369
370 if (!info->xmit_buf) {
371 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
372 if (!info->xmit_buf)
373 return -ENOMEM;
374 }
375
Greg Ungerer727dda82006-06-28 15:54:22 +1000376 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /*
379 * Clear the FIFO buffers and disable them
380 * (they will be reenabled in change_speed())
381 */
382
383 uart->ustcnt = USTCNT_UEN;
384 info->xmit_fifo_size = 1;
385 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
386 (void)uart->urx.w;
387
388 /*
389 * Finally, enable sequencing and interrupts
390 */
391#ifdef USE_INTS
392 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
393 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
394#else
395 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
396#endif
397
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000398 if (info->tty)
399 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
401
402 /*
403 * and set the speed of the serial port
404 */
405
406 change_speed(info);
407
408 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000409 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413/*
414 * This routine will shutdown a serial port; interrupts are disabled, and
415 * DTR is dropped if the hangup on close termio flag is on.
416 */
417static void shutdown(struct m68k_serial * info)
418{
419 m68328_uart *uart = &uart_addr[info->line];
420 unsigned long flags;
421
422 uart->ustcnt = 0; /* All off! */
423 if (!(info->flags & S_INITIALIZED))
424 return;
425
Greg Ungerer727dda82006-06-28 15:54:22 +1000426 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (info->xmit_buf) {
429 free_page((unsigned long) info->xmit_buf);
430 info->xmit_buf = 0;
431 }
432
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000433 if (info->tty)
434 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000437 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440struct {
441 int divisor, prescale;
442}
443#ifndef CONFIG_M68VZ328
444 hw_baud_table[18] = {
445 {0,0}, /* 0 */
446 {0,0}, /* 50 */
447 {0,0}, /* 75 */
448 {0,0}, /* 110 */
449 {0,0}, /* 134 */
450 {0,0}, /* 150 */
451 {0,0}, /* 200 */
452 {7,0x26}, /* 300 */
453 {6,0x26}, /* 600 */
454 {5,0x26}, /* 1200 */
455 {0,0}, /* 1800 */
456 {4,0x26}, /* 2400 */
457 {3,0x26}, /* 4800 */
458 {2,0x26}, /* 9600 */
459 {1,0x26}, /* 19200 */
460 {0,0x26}, /* 38400 */
461 {1,0x38}, /* 57600 */
462 {0,0x38}, /* 115200 */
463};
464#else
465 hw_baud_table[18] = {
466 {0,0}, /* 0 */
467 {0,0}, /* 50 */
468 {0,0}, /* 75 */
469 {0,0}, /* 110 */
470 {0,0}, /* 134 */
471 {0,0}, /* 150 */
472 {0,0}, /* 200 */
473 {0,0}, /* 300 */
474 {7,0x26}, /* 600 */
475 {6,0x26}, /* 1200 */
476 {0,0}, /* 1800 */
477 {5,0x26}, /* 2400 */
478 {4,0x26}, /* 4800 */
479 {3,0x26}, /* 9600 */
480 {2,0x26}, /* 19200 */
481 {1,0x26}, /* 38400 */
482 {0,0x26}, /* 57600 */
483 {1,0x38}, /* 115200 */
484};
485#endif
486/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
487
488/*
489 * This routine is called to set the UART divisor registers to match
490 * the specified baud rate for a serial port.
491 */
492static void change_speed(struct m68k_serial *info)
493{
494 m68328_uart *uart = &uart_addr[info->line];
495 unsigned short port;
496 unsigned short ustcnt;
497 unsigned cflag;
498 int i;
499
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000500 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000502 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (!(port = info->port))
504 return;
505
506 ustcnt = uart->ustcnt;
507 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
508
509 i = cflag & CBAUD;
510 if (i & CBAUDEX) {
511 i = (i & ~CBAUDEX) + B38400;
512 }
513
514 info->baud = baud_table[i];
515 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
516 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
517
518 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
519
520 if ((cflag & CSIZE) == CS8)
521 ustcnt |= USTCNT_8_7;
522
523 if (cflag & CSTOPB)
524 ustcnt |= USTCNT_STOP;
525
526 if (cflag & PARENB)
527 ustcnt |= USTCNT_PARITYEN;
528 if (cflag & PARODD)
529 ustcnt |= USTCNT_ODD_EVEN;
530
531#ifdef CONFIG_SERIAL_68328_RTS_CTS
532 if (cflag & CRTSCTS) {
533 uart->utx.w &= ~ UTX_NOCTS;
534 } else {
535 uart->utx.w |= UTX_NOCTS;
536 }
537#endif
538
539 ustcnt |= USTCNT_TXEN;
540
541 uart->ustcnt = ustcnt;
542 return;
543}
544
545/*
546 * Fair output driver allows a process to speak.
547 */
548static void rs_fair_output(void)
549{
550 int left; /* Output no more than that */
551 unsigned long flags;
552 struct m68k_serial *info = &m68k_soft[0];
553 char c;
554
555 if (info == 0) return;
556 if (info->xmit_buf == 0) return;
557
Greg Ungerer727dda82006-06-28 15:54:22 +1000558 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 left = info->xmit_cnt;
560 while (left != 0) {
561 c = info->xmit_buf[info->xmit_tail];
562 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
563 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000564 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 rs_put_char(c);
567
Greg Ungerer727dda82006-06-28 15:54:22 +1000568 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 left = min(info->xmit_cnt, left-1);
570 }
571
572 /* Last character is being transmitted now (hopefully). */
573 udelay(5);
574
Greg Ungerer727dda82006-06-28 15:54:22 +1000575 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return;
577}
578
579/*
580 * m68k_console_print is registered for printk.
581 */
582void console_print_68328(const char *p)
583{
584 char c;
585
586 while((c=*(p++)) != 0) {
587 if(c == '\n')
588 rs_put_char('\r');
589 rs_put_char(c);
590 }
591
592 /* Comment this if you want to have a strict interrupt-driven output */
593 rs_fair_output();
594
595 return;
596}
597
598static void rs_set_ldisc(struct tty_struct *tty)
599{
600 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
601
602 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
603 return;
604
605 info->is_cons = (tty->termios->c_line == N_TTY);
606
607 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
608}
609
610static void rs_flush_chars(struct tty_struct *tty)
611{
612 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
613 m68328_uart *uart = &uart_addr[info->line];
614 unsigned long flags;
615
616 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
617 return;
618#ifndef USE_INTS
619 for(;;) {
620#endif
621
622 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000623 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
626 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000627 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return;
629 }
630
631#ifdef USE_INTS
632 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
633#else
634 uart->ustcnt |= USTCNT_TXEN;
635#endif
636
637#ifdef USE_INTS
638 if (uart->utx.w & UTX_TX_AVAIL) {
639#else
640 if (1) {
641#endif
642 /* Send char */
643 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
644 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
645 info->xmit_cnt--;
646 }
647
648#ifndef USE_INTS
649 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
650 }
651#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000652 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655extern void console_printn(const char * b, int count);
656
657static int rs_write(struct tty_struct * tty,
658 const unsigned char *buf, int count)
659{
660 int c, total = 0;
661 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
662 m68328_uart *uart = &uart_addr[info->line];
663 unsigned long flags;
664
665 if (serial_paranoia_check(info, tty->name, "rs_write"))
666 return 0;
667
668 if (!tty || !info->xmit_buf)
669 return 0;
670
Greg Ungerer727dda82006-06-28 15:54:22 +1000671 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000673 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
675 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000676 local_irq_restore(flags);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (c <= 0)
679 break;
680
681 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000682
683 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
685 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000686 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 buf += c;
688 count -= c;
689 total += c;
690 }
691
692 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
693 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000694 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695#ifndef USE_INTS
696 while(info->xmit_cnt) {
697#endif
698
699 uart->ustcnt |= USTCNT_TXEN;
700#ifdef USE_INTS
701 uart->ustcnt |= USTCNT_TX_INTR_MASK;
702#else
703 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
704#endif
705 if (uart->utx.w & UTX_TX_AVAIL) {
706 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
707 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
708 info->xmit_cnt--;
709 }
710
711#ifndef USE_INTS
712 }
713#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000714 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return total;
718}
719
720static int rs_write_room(struct tty_struct *tty)
721{
722 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
723 int ret;
724
725 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
726 return 0;
727 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
728 if (ret < 0)
729 ret = 0;
730 return ret;
731}
732
733static int rs_chars_in_buffer(struct tty_struct *tty)
734{
735 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
736
737 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
738 return 0;
739 return info->xmit_cnt;
740}
741
742static void rs_flush_buffer(struct tty_struct *tty)
743{
744 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000745 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
748 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000749 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000751 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 tty_wakeup(tty);
753}
754
755/*
756 * ------------------------------------------------------------
757 * rs_throttle()
758 *
759 * This routine is called by the upper-layer tty layer to signal that
760 * incoming characters should be throttled.
761 * ------------------------------------------------------------
762 */
763static void rs_throttle(struct tty_struct * tty)
764{
765 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
766
767 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
768 return;
769
770 if (I_IXOFF(tty))
771 info->x_char = STOP_CHAR(tty);
772
773 /* Turn off RTS line (do this atomic) */
774}
775
776static void rs_unthrottle(struct tty_struct * tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779
780 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
781 return;
782
783 if (I_IXOFF(tty)) {
784 if (info->x_char)
785 info->x_char = 0;
786 else
787 info->x_char = START_CHAR(tty);
788 }
789
790 /* Assert RTS line (do this atomic) */
791}
792
793/*
794 * ------------------------------------------------------------
795 * rs_ioctl() and friends
796 * ------------------------------------------------------------
797 */
798
799static int get_serial_info(struct m68k_serial * info,
800 struct serial_struct * retinfo)
801{
802 struct serial_struct tmp;
803
804 if (!retinfo)
805 return -EFAULT;
806 memset(&tmp, 0, sizeof(tmp));
807 tmp.type = info->type;
808 tmp.line = info->line;
809 tmp.port = info->port;
810 tmp.irq = info->irq;
811 tmp.flags = info->flags;
812 tmp.baud_base = info->baud_base;
813 tmp.close_delay = info->close_delay;
814 tmp.closing_wait = info->closing_wait;
815 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400816 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
817 return -EFAULT;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820}
821
822static int set_serial_info(struct m68k_serial * info,
823 struct serial_struct * new_info)
824{
825 struct serial_struct new_serial;
826 struct m68k_serial old_info;
827 int retval = 0;
828
829 if (!new_info)
830 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400831 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
832 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 old_info = *info;
834
835 if (!capable(CAP_SYS_ADMIN)) {
836 if ((new_serial.baud_base != info->baud_base) ||
837 (new_serial.type != info->type) ||
838 (new_serial.close_delay != info->close_delay) ||
839 ((new_serial.flags & ~S_USR_MASK) !=
840 (info->flags & ~S_USR_MASK)))
841 return -EPERM;
842 info->flags = ((info->flags & ~S_USR_MASK) |
843 (new_serial.flags & S_USR_MASK));
844 info->custom_divisor = new_serial.custom_divisor;
845 goto check_and_exit;
846 }
847
848 if (info->count > 1)
849 return -EBUSY;
850
851 /*
852 * OK, past this point, all the error checking has been done.
853 * At this point, we start making changes.....
854 */
855
856 info->baud_base = new_serial.baud_base;
857 info->flags = ((info->flags & ~S_FLAGS) |
858 (new_serial.flags & S_FLAGS));
859 info->type = new_serial.type;
860 info->close_delay = new_serial.close_delay;
861 info->closing_wait = new_serial.closing_wait;
862
863check_and_exit:
864 retval = startup(info);
865 return retval;
866}
867
868/*
869 * get_lsr_info - get line status register info
870 *
871 * Purpose: Let user call ioctl() to get info when the UART physically
872 * is emptied. On bus types like RS485, the transmitter must
873 * release the bus after transmitting. This must be done when
874 * the transmit shift register is empty, not be done when the
875 * transmit holding register is empty. This functionality
876 * allows an RS485 driver to be written in user space.
877 */
878static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
879{
880#ifdef CONFIG_SERIAL_68328_RTS_CTS
881 m68328_uart *uart = &uart_addr[info->line];
882#endif
883 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000884 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Greg Ungerer727dda82006-06-28 15:54:22 +1000886 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#ifdef CONFIG_SERIAL_68328_RTS_CTS
888 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
889#else
890 status = 0;
891#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000892 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400893 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/*
897 * This routine sends a break character out the serial port.
898 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700899static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 m68328_uart *uart = &uart_addr[info->line];
902 unsigned long flags;
903 if (!info->port)
904 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000905 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#ifdef USE_INTS
907 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700908 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 uart->utx.w &= ~UTX_SEND_BREAK;
910#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000911 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Alan Cox6caa76b2011-02-14 16:27:22 +0000914static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 unsigned int cmd, unsigned long arg)
916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
918 int retval;
919
920 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
921 return -ENODEV;
922
923 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
924 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
925 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
926 if (tty->flags & (1 << TTY_IO_ERROR))
927 return -EIO;
928 }
929
930 switch (cmd) {
931 case TCSBRK: /* SVID version: non-zero arg --> no break */
932 retval = tty_check_change(tty);
933 if (retval)
934 return retval;
935 tty_wait_until_sent(tty, 0);
936 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700937 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939 case TCSBRKP: /* support for POSIX tcsendbreak() */
940 retval = tty_check_change(tty);
941 if (retval)
942 return retval;
943 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700944 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400947 return get_serial_info(info,
948 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case TIOCSSERIAL:
950 return set_serial_info(info,
951 (struct serial_struct *) arg);
952 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400953 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400955 if (copy_to_user((struct m68k_serial *) arg,
956 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 default:
960 return -ENOIOCTLCMD;
961 }
962 return 0;
963}
964
Alan Cox606d0992006-12-08 02:38:45 -0800965static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 change_speed(info);
970
971 if ((old_termios->c_cflag & CRTSCTS) &&
972 !(tty->termios->c_cflag & CRTSCTS)) {
973 tty->hw_stopped = 0;
974 rs_start(tty);
975 }
976
977}
978
979/*
980 * ------------------------------------------------------------
981 * rs_close()
982 *
983 * This routine is called when the serial port gets closed. First, we
984 * wait for the last remaining data to be sent. Then, we unlink its
985 * S structure from the interrupt chain if necessary, and we free
986 * that IRQ if nothing is left in the chain.
987 * ------------------------------------------------------------
988 */
989static void rs_close(struct tty_struct *tty, struct file * filp)
990{
991 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
992 m68328_uart *uart = &uart_addr[info->line];
993 unsigned long flags;
994
995 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
996 return;
997
Greg Ungerer727dda82006-06-28 15:54:22 +1000998 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001001 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return;
1003 }
1004
1005 if ((tty->count == 1) && (info->count != 1)) {
1006 /*
1007 * Uh, oh. tty->count is 1, which means that the tty
1008 * structure will be freed. Info->count should always
1009 * be one in these conditions. If it's greater than
1010 * one, we've got real problems, since it means the
1011 * serial port won't be shutdown.
1012 */
1013 printk("rs_close: bad serial port count; tty->count is 1, "
1014 "info->count is %d\n", info->count);
1015 info->count = 1;
1016 }
1017 if (--info->count < 0) {
1018 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1019 info->line, info->count);
1020 info->count = 0;
1021 }
1022 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001023 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 }
1026 info->flags |= S_CLOSING;
1027 /*
1028 * Now we wait for the transmit buffer to clear; and we notify
1029 * the line discipline to only process XON/XOFF characters.
1030 */
1031 tty->closing = 1;
1032 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1033 tty_wait_until_sent(tty, info->closing_wait);
1034 /*
1035 * At this point we stop accepting input. To do this, we
1036 * disable the receive line status interrupts, and tell the
1037 * interrupt driver to stop checking the data ready bit in the
1038 * line status register.
1039 */
1040
1041 uart->ustcnt &= ~USTCNT_RXEN;
1042 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1043
1044 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001045 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 tty_ldisc_flush(tty);
1048 tty->closing = 0;
1049 info->event = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001050 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051#warning "This is not and has never been valid so fix it"
1052#if 0
1053 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1054 if (tty->ldisc.close)
1055 (tty->ldisc.close)(tty);
1056 tty->ldisc = ldiscs[N_TTY];
1057 tty->termios->c_line = N_TTY;
1058 if (tty->ldisc.open)
1059 (tty->ldisc.open)(tty);
1060 }
1061#endif
1062 if (info->blocked_open) {
1063 if (info->close_delay) {
1064 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1065 }
1066 wake_up_interruptible(&info->open_wait);
1067 }
1068 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1069 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001070 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073/*
1074 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1075 */
1076void rs_hangup(struct tty_struct *tty)
1077{
1078 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1079
1080 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1081 return;
1082
1083 rs_flush_buffer(tty);
1084 shutdown(info);
1085 info->event = 0;
1086 info->count = 0;
1087 info->flags &= ~S_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001088 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 wake_up_interruptible(&info->open_wait);
1090}
1091
1092/*
1093 * ------------------------------------------------------------
1094 * rs_open() and friends
1095 * ------------------------------------------------------------
1096 */
1097static int block_til_ready(struct tty_struct *tty, struct file * filp,
1098 struct m68k_serial *info)
1099{
1100 DECLARE_WAITQUEUE(wait, current);
1101 int retval;
1102 int do_clocal = 0;
1103
1104 /*
1105 * If the device is in the middle of being closed, then block
1106 * until it's done, and then try again.
1107 */
1108 if (info->flags & S_CLOSING) {
1109 interruptible_sleep_on(&info->close_wait);
1110#ifdef SERIAL_DO_RESTART
1111 if (info->flags & S_HUP_NOTIFY)
1112 return -EAGAIN;
1113 else
1114 return -ERESTARTSYS;
1115#else
1116 return -EAGAIN;
1117#endif
1118 }
1119
1120 /*
1121 * If non-blocking mode is set, or the port is not enabled,
1122 * then make the check up front and then exit.
1123 */
1124 if ((filp->f_flags & O_NONBLOCK) ||
1125 (tty->flags & (1 << TTY_IO_ERROR))) {
1126 info->flags |= S_NORMAL_ACTIVE;
1127 return 0;
1128 }
1129
1130 if (tty->termios->c_cflag & CLOCAL)
1131 do_clocal = 1;
1132
1133 /*
1134 * Block waiting for the carrier detect and the line to become
1135 * free (i.e., not in use by the callout). While we are in
1136 * this loop, info->count is dropped by one, so that
1137 * rs_close() knows when to free things. We restore it upon
1138 * exit, either normal or abnormal.
1139 */
1140 retval = 0;
1141 add_wait_queue(&info->open_wait, &wait);
1142
1143 info->count--;
1144 info->blocked_open++;
1145 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001146 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001148 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 current->state = TASK_INTERRUPTIBLE;
1150 if (tty_hung_up_p(filp) ||
1151 !(info->flags & S_INITIALIZED)) {
1152#ifdef SERIAL_DO_RESTART
1153 if (info->flags & S_HUP_NOTIFY)
1154 retval = -EAGAIN;
1155 else
1156 retval = -ERESTARTSYS;
1157#else
1158 retval = -EAGAIN;
1159#endif
1160 break;
1161 }
1162 if (!(info->flags & S_CLOSING) && do_clocal)
1163 break;
1164 if (signal_pending(current)) {
1165 retval = -ERESTARTSYS;
1166 break;
1167 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001168 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001170 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 current->state = TASK_RUNNING;
1173 remove_wait_queue(&info->open_wait, &wait);
1174 if (!tty_hung_up_p(filp))
1175 info->count++;
1176 info->blocked_open--;
1177
1178 if (retval)
1179 return retval;
1180 info->flags |= S_NORMAL_ACTIVE;
1181 return 0;
1182}
1183
1184/*
1185 * This routine is called whenever a serial port is opened. It
1186 * enables interrupts for a serial port, linking in its S structure into
1187 * the IRQ chain. It also performs the serial-specific
1188 * initialization for the tty structure.
1189 */
1190int rs_open(struct tty_struct *tty, struct file * filp)
1191{
1192 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001193 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jiri Slaby410235f2012-03-05 14:52:01 +01001195 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (serial_paranoia_check(info, tty->name, "rs_open"))
1198 return -ENODEV;
1199
1200 info->count++;
1201 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001202 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /*
1205 * Start up serial port
1206 */
1207 retval = startup(info);
1208 if (retval)
1209 return retval;
1210
1211 return block_til_ready(tty, filp, info);
1212}
1213
1214/* Finally, routines used to initialize the serial driver. */
1215
1216static void show_serial_version(void)
1217{
1218 printk("MC68328 serial driver version 1.00\n");
1219}
1220
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001221static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 .open = rs_open,
1223 .close = rs_close,
1224 .write = rs_write,
1225 .flush_chars = rs_flush_chars,
1226 .write_room = rs_write_room,
1227 .chars_in_buffer = rs_chars_in_buffer,
1228 .flush_buffer = rs_flush_buffer,
1229 .ioctl = rs_ioctl,
1230 .throttle = rs_throttle,
1231 .unthrottle = rs_unthrottle,
1232 .set_termios = rs_set_termios,
1233 .stop = rs_stop,
1234 .start = rs_start,
1235 .hangup = rs_hangup,
1236 .set_ldisc = rs_set_ldisc,
1237};
1238
1239/* rs_init inits the driver */
1240static int __init
1241rs68328_init(void)
1242{
1243 int flags, i;
1244 struct m68k_serial *info;
1245
1246 serial_driver = alloc_tty_driver(NR_PORTS);
1247 if (!serial_driver)
1248 return -ENOMEM;
1249
1250 show_serial_version();
1251
1252 /* Initialize the tty_driver structure */
1253 /* SPARC: Not all of this is exactly right for us. */
1254
1255 serial_driver->name = "ttyS";
1256 serial_driver->major = TTY_MAJOR;
1257 serial_driver->minor_start = 64;
1258 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1259 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1260 serial_driver->init_termios = tty_std_termios;
1261 serial_driver->init_termios.c_cflag =
1262 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1263 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1264 tty_set_operations(serial_driver, &rs_ops);
1265
1266 if (tty_register_driver(serial_driver)) {
1267 put_tty_driver(serial_driver);
1268 printk(KERN_ERR "Couldn't register serial driver\n");
1269 return -ENOMEM;
1270 }
1271
Greg Ungerer727dda82006-06-28 15:54:22 +10001272 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 for(i=0;i<NR_PORTS;i++) {
1275
1276 info = &m68k_soft[i];
1277 info->magic = SERIAL_MAGIC;
1278 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001279 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 info->irq = uart_irqs[i];
1281 info->custom_divisor = 16;
1282 info->close_delay = 50;
1283 info->closing_wait = 3000;
1284 info->x_char = 0;
1285 info->event = 0;
1286 info->count = 0;
1287 info->blocked_open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 init_waitqueue_head(&info->open_wait);
1289 init_waitqueue_head(&info->close_wait);
1290 info->line = i;
1291 info->is_cons = 1; /* Means shortcuts work */
1292
1293 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1294 info->port, info->irq);
1295 printk(" is a builtin MC68328 UART\n");
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297#ifdef CONFIG_M68VZ328
1298 if (i > 0 )
1299 PJSEL &= 0xCF; /* PSW enable second port output */
1300#endif
1301
1302 if (request_irq(uart_irqs[i],
1303 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001304 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001305 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001308 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312module_init(rs68328_init);
1313
1314
1315
1316static void m68328_set_baud(void)
1317{
1318 unsigned short ustcnt;
1319 int i;
1320
1321 ustcnt = USTCNT;
1322 USTCNT = ustcnt & ~USTCNT_TXEN;
1323
1324again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001325 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (baud_table[i] == m68328_console_baud)
1327 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001328 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 m68328_console_baud = 9600;
1330 goto again;
1331 }
1332
1333 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1334 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1335 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1336 ustcnt |= USTCNT_8_7;
1337 ustcnt |= USTCNT_TXEN;
1338 USTCNT = ustcnt;
1339 m68328_console_initted = 1;
1340 return;
1341}
1342
1343
1344int m68328_console_setup(struct console *cp, char *arg)
1345{
1346 int i, n = CONSOLE_BAUD_RATE;
1347
1348 if (!cp)
1349 return(-1);
1350
1351 if (arg)
1352 n = simple_strtoul(arg,NULL,0);
1353
Thiago Farina8b505ca2009-12-09 12:31:30 -08001354 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (baud_table[i] == n)
1356 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001357 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 m68328_console_baud = n;
1359 m68328_console_cbaud = 0;
1360 if (i > 15) {
1361 m68328_console_cbaud |= CBAUDEX;
1362 i -= 15;
1363 }
1364 m68328_console_cbaud |= i;
1365 }
1366
1367 m68328_set_baud(); /* make sure baud rate changes */
1368 return(0);
1369}
1370
1371
1372static struct tty_driver *m68328_console_device(struct console *c, int *index)
1373{
1374 *index = c->index;
1375 return serial_driver;
1376}
1377
1378
1379void m68328_console_write (struct console *co, const char *str,
1380 unsigned int count)
1381{
1382 if (!m68328_console_initted)
1383 m68328_set_baud();
1384 while (count--) {
1385 if (*str == '\n')
1386 rs_put_char('\r');
1387 rs_put_char( *str++ );
1388 }
1389}
1390
1391
1392static struct console m68328_driver = {
1393 .name = "ttyS",
1394 .write = m68328_console_write,
1395 .device = m68328_console_device,
1396 .setup = m68328_console_setup,
1397 .flags = CON_PRINTBUFFER,
1398 .index = -1,
1399};
1400
1401
1402static int __init m68328_console_init(void)
1403{
1404 register_console(&m68328_driver);
1405 return 0;
1406}
1407
1408console_initcall(m68328_console_init);