blob: bf5f81113a0e30e66b8dea8e29f35aacb4cedd1c [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>
Jiri Slabycea4b2c2012-04-02 13:54:35 +020020#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/timer.h>
24#include <linux/interrupt.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/major.h>
28#include <linux/string.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/kernel.h>
32#include <linux/console.h>
33#include <linux/reboot.h>
34#include <linux/keyboard.h>
35#include <linux/init.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/io.h>
42#include <asm/irq.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 -070083static void change_speed(struct m68k_serial *info);
84
85/*
86 * Setup for console. Argument comes from the boot command line.
87 */
88
Christoph Eggerf5e92c32010-07-20 15:26:54 -070089/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
90#ifdef CONFIG_M68VZ328
91#define CONSOLE_BAUD_RATE 19200
92#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
94
Christoph Eggerf5e92c32010-07-20 15:26:54 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifndef CONSOLE_BAUD_RATE
97#define CONSOLE_BAUD_RATE 9600
98#define DEFAULT_CBAUD B9600
99#endif
100
101
102static int m68328_console_initted = 0;
103static int m68328_console_baud = CONSOLE_BAUD_RATE;
104static int m68328_console_cbaud = DEFAULT_CBAUD;
105
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static inline int serial_paranoia_check(struct m68k_serial *info,
108 char *name, const char *routine)
109{
110#ifdef SERIAL_PARANOIA_CHECK
111 static const char *badmagic =
112 "Warning: bad magic number for serial struct %s in %s\n";
113 static const char *badinfo =
114 "Warning: null m68k_serial for %s in %s\n";
115
116 if (!info) {
117 printk(badinfo, name, routine);
118 return 1;
119 }
120 if (info->magic != SERIAL_MAGIC) {
121 printk(badmagic, name, routine);
122 return 1;
123 }
124#endif
125 return 0;
126}
127
128/*
129 * This is used to figure out the divisor speeds and the timeouts
130 */
131static int baud_table[] = {
132 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
133 9600, 19200, 38400, 57600, 115200, 0 };
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Sets or clears DTR/RTS on the requested line */
136static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
137{
138 if (set) {
139 /* set the RTS/CTS line */
140 } else {
141 /* clear it */
142 }
143 return;
144}
145
146/* Utility routines */
147static inline int get_baud(struct m68k_serial *ss)
148{
149 unsigned long result = 115200;
150 unsigned short int baud = uart_addr[ss->line].ubaud;
151 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
152 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
153
154 return result;
155}
156
157/*
158 * ------------------------------------------------------------
159 * rs_stop() and rs_start()
160 *
161 * This routines are called before setting or resetting tty->stopped.
162 * They enable or disable transmitter interrupts, as necessary.
163 * ------------------------------------------------------------
164 */
165static void rs_stop(struct tty_struct *tty)
166{
167 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
168 m68328_uart *uart = &uart_addr[info->line];
169 unsigned long flags;
170
171 if (serial_paranoia_check(info, tty->name, "rs_stop"))
172 return;
173
Greg Ungerer727dda82006-06-28 15:54:22 +1000174 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000176 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Alan Cox09a6ffa2008-04-30 00:54:01 -0700179static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int flags, loops = 0;
182
Greg Ungerer727dda82006-06-28 15:54:22 +1000183 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
186 loops++;
187 udelay(5);
188 }
189
190 UTX_TXDATA = ch;
191 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000192 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700193 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static void rs_start(struct tty_struct *tty)
197{
198 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
199 m68328_uart *uart = &uart_addr[info->line];
200 unsigned long flags;
201
202 if (serial_paranoia_check(info, tty->name, "rs_start"))
203 return;
204
Greg Ungerer727dda82006-06-28 15:54:22 +1000205 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
207#ifdef USE_INTS
208 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
209#else
210 uart->ustcnt |= USTCNT_TXEN;
211#endif
212 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000213 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/* Drop into either the boot monitor or kadb upon receiving a break
217 * from keyboard/console input.
218 */
219static void batten_down_hatches(void)
220{
221 /* Drop into the debugger */
222}
223
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800224static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* If this is console input and this is a
227 * 'break asserted' status change interrupt
228 * see if we can drop into the debugger
229 */
230 if((status & URX_BREAK) && info->break_abort)
231 batten_down_hatches();
232 return;
233}
234
David Howells7d12e782006-10-05 14:55:46 +0100235static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000237 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800239 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * This do { } while() loop will get ALL chars out of Rx FIFO
243 */
244#ifndef CONFIG_XCOPILOT_BUGS
245 do {
246#endif
247 ch = GET_FIELD(rx, URX_RXDATA);
248
249 if(info->is_cons) {
250 if(URX_BREAK & rx) { /* whee, break received */
251 status_handle(info, rx);
252 return;
253#ifdef CONFIG_MAGIC_SYSRQ
254 } else if (ch == 0x10) { /* ^P */
255 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700256 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 show_buffers();
258/* show_net_buffers(); */
259 return;
260 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600261 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
263#endif /* CONFIG_MAGIC_SYSRQ */
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 if(!tty)
268 goto clear_and_exit;
269
Alan Cox33f0f882006-01-09 20:54:13 -0800270 flag = TTY_NORMAL;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800273 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 status_handle(info, rx);
275 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800276 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 status_handle(info, rx);
278 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800279 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Alan Cox33f0f882006-01-09 20:54:13 -0800282 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#ifndef CONFIG_XCOPILOT_BUGS
284 } while((rx = uart->urx.w) & URX_DATA_READY);
285#endif
286
Greg Ungerer8e63e662006-02-08 09:19:17 +1000287 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289clear_and_exit:
290 return;
291}
292
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800293static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 m68328_uart *uart = &uart_addr[info->line];
296
297 if (info->x_char) {
298 /* Send next char */
299 uart->utx.b.txdata = info->x_char;
300 info->x_char = 0;
301 goto clear_and_return;
302 }
303
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000304 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /* That's peculiar... TX ints off */
306 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
307 goto clear_and_return;
308 }
309
310 /* Send char */
311 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
312 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
313 info->xmit_cnt--;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if(info->xmit_cnt <= 0) {
316 /* All done for now... TX ints off */
317 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
318 goto clear_and_return;
319 }
320
321clear_and_return:
322 /* Clear interrupt (should be auto)*/
323 return;
324}
325
326/*
327 * This is the serial driver's generic interrupt routine
328 */
David Howells7d12e782006-10-05 14:55:46 +0100329irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Alan Cox25db8ad2008-08-19 20:49:40 -0700331 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 m68328_uart *uart;
333 unsigned short rx;
334 unsigned short tx;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 uart = &uart_addr[info->line];
337 rx = uart->urx.w;
338
339#ifdef USE_INTS
340 tx = uart->utx.w;
341
David Howells7d12e782006-10-05 14:55:46 +0100342 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (tx & UTX_TX_AVAIL) transmit_chars(info);
344#else
David Howells7d12e782006-10-05 14:55:46 +0100345 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347 return IRQ_HANDLED;
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int startup(struct m68k_serial * info)
351{
352 m68328_uart *uart = &uart_addr[info->line];
353 unsigned long flags;
354
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200355 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357
358 if (!info->xmit_buf) {
359 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
360 if (!info->xmit_buf)
361 return -ENOMEM;
362 }
363
Greg Ungerer727dda82006-06-28 15:54:22 +1000364 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /*
367 * Clear the FIFO buffers and disable them
368 * (they will be reenabled in change_speed())
369 */
370
371 uart->ustcnt = USTCNT_UEN;
372 info->xmit_fifo_size = 1;
373 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
374 (void)uart->urx.w;
375
376 /*
377 * Finally, enable sequencing and interrupts
378 */
379#ifdef USE_INTS
380 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
381 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
382#else
383 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
384#endif
385
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000386 if (info->tty)
387 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
389
390 /*
391 * and set the speed of the serial port
392 */
393
394 change_speed(info);
395
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200396 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000397 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
401/*
402 * This routine will shutdown a serial port; interrupts are disabled, and
403 * DTR is dropped if the hangup on close termio flag is on.
404 */
405static void shutdown(struct m68k_serial * info)
406{
407 m68328_uart *uart = &uart_addr[info->line];
408 unsigned long flags;
409
410 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200411 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return;
413
Greg Ungerer727dda82006-06-28 15:54:22 +1000414 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (info->xmit_buf) {
417 free_page((unsigned long) info->xmit_buf);
418 info->xmit_buf = 0;
419 }
420
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000421 if (info->tty)
422 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200424 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000425 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428struct {
429 int divisor, prescale;
430}
431#ifndef CONFIG_M68VZ328
432 hw_baud_table[18] = {
433 {0,0}, /* 0 */
434 {0,0}, /* 50 */
435 {0,0}, /* 75 */
436 {0,0}, /* 110 */
437 {0,0}, /* 134 */
438 {0,0}, /* 150 */
439 {0,0}, /* 200 */
440 {7,0x26}, /* 300 */
441 {6,0x26}, /* 600 */
442 {5,0x26}, /* 1200 */
443 {0,0}, /* 1800 */
444 {4,0x26}, /* 2400 */
445 {3,0x26}, /* 4800 */
446 {2,0x26}, /* 9600 */
447 {1,0x26}, /* 19200 */
448 {0,0x26}, /* 38400 */
449 {1,0x38}, /* 57600 */
450 {0,0x38}, /* 115200 */
451};
452#else
453 hw_baud_table[18] = {
454 {0,0}, /* 0 */
455 {0,0}, /* 50 */
456 {0,0}, /* 75 */
457 {0,0}, /* 110 */
458 {0,0}, /* 134 */
459 {0,0}, /* 150 */
460 {0,0}, /* 200 */
461 {0,0}, /* 300 */
462 {7,0x26}, /* 600 */
463 {6,0x26}, /* 1200 */
464 {0,0}, /* 1800 */
465 {5,0x26}, /* 2400 */
466 {4,0x26}, /* 4800 */
467 {3,0x26}, /* 9600 */
468 {2,0x26}, /* 19200 */
469 {1,0x26}, /* 38400 */
470 {0,0x26}, /* 57600 */
471 {1,0x38}, /* 115200 */
472};
473#endif
474/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
475
476/*
477 * This routine is called to set the UART divisor registers to match
478 * the specified baud rate for a serial port.
479 */
480static void change_speed(struct m68k_serial *info)
481{
482 m68328_uart *uart = &uart_addr[info->line];
483 unsigned short port;
484 unsigned short ustcnt;
485 unsigned cflag;
486 int i;
487
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000488 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000490 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!(port = info->port))
492 return;
493
494 ustcnt = uart->ustcnt;
495 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
496
497 i = cflag & CBAUD;
498 if (i & CBAUDEX) {
499 i = (i & ~CBAUDEX) + B38400;
500 }
501
502 info->baud = baud_table[i];
503 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
504 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
505
506 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
507
508 if ((cflag & CSIZE) == CS8)
509 ustcnt |= USTCNT_8_7;
510
511 if (cflag & CSTOPB)
512 ustcnt |= USTCNT_STOP;
513
514 if (cflag & PARENB)
515 ustcnt |= USTCNT_PARITYEN;
516 if (cflag & PARODD)
517 ustcnt |= USTCNT_ODD_EVEN;
518
519#ifdef CONFIG_SERIAL_68328_RTS_CTS
520 if (cflag & CRTSCTS) {
521 uart->utx.w &= ~ UTX_NOCTS;
522 } else {
523 uart->utx.w |= UTX_NOCTS;
524 }
525#endif
526
527 ustcnt |= USTCNT_TXEN;
528
529 uart->ustcnt = ustcnt;
530 return;
531}
532
533/*
534 * Fair output driver allows a process to speak.
535 */
536static void rs_fair_output(void)
537{
538 int left; /* Output no more than that */
539 unsigned long flags;
540 struct m68k_serial *info = &m68k_soft[0];
541 char c;
542
543 if (info == 0) return;
544 if (info->xmit_buf == 0) return;
545
Greg Ungerer727dda82006-06-28 15:54:22 +1000546 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 left = info->xmit_cnt;
548 while (left != 0) {
549 c = info->xmit_buf[info->xmit_tail];
550 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
551 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000552 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 rs_put_char(c);
555
Greg Ungerer727dda82006-06-28 15:54:22 +1000556 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 left = min(info->xmit_cnt, left-1);
558 }
559
560 /* Last character is being transmitted now (hopefully). */
561 udelay(5);
562
Greg Ungerer727dda82006-06-28 15:54:22 +1000563 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565}
566
567/*
568 * m68k_console_print is registered for printk.
569 */
570void console_print_68328(const char *p)
571{
572 char c;
573
574 while((c=*(p++)) != 0) {
575 if(c == '\n')
576 rs_put_char('\r');
577 rs_put_char(c);
578 }
579
580 /* Comment this if you want to have a strict interrupt-driven output */
581 rs_fair_output();
582
583 return;
584}
585
586static void rs_set_ldisc(struct tty_struct *tty)
587{
588 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
589
590 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
591 return;
592
593 info->is_cons = (tty->termios->c_line == N_TTY);
594
595 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
596}
597
598static void rs_flush_chars(struct tty_struct *tty)
599{
600 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
601 m68328_uart *uart = &uart_addr[info->line];
602 unsigned long flags;
603
604 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
605 return;
606#ifndef USE_INTS
607 for(;;) {
608#endif
609
610 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000611 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
614 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000615 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
617 }
618
619#ifdef USE_INTS
620 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
621#else
622 uart->ustcnt |= USTCNT_TXEN;
623#endif
624
625#ifdef USE_INTS
626 if (uart->utx.w & UTX_TX_AVAIL) {
627#else
628 if (1) {
629#endif
630 /* Send char */
631 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
632 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
633 info->xmit_cnt--;
634 }
635
636#ifndef USE_INTS
637 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
638 }
639#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000640 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643extern void console_printn(const char * b, int count);
644
645static int rs_write(struct tty_struct * tty,
646 const unsigned char *buf, int count)
647{
648 int c, total = 0;
649 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
650 m68328_uart *uart = &uart_addr[info->line];
651 unsigned long flags;
652
653 if (serial_paranoia_check(info, tty->name, "rs_write"))
654 return 0;
655
656 if (!tty || !info->xmit_buf)
657 return 0;
658
Greg Ungerer727dda82006-06-28 15:54:22 +1000659 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000661 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
663 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000664 local_irq_restore(flags);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (c <= 0)
667 break;
668
669 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000670
671 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
673 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000674 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 buf += c;
676 count -= c;
677 total += c;
678 }
679
680 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
681 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000682 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#ifndef USE_INTS
684 while(info->xmit_cnt) {
685#endif
686
687 uart->ustcnt |= USTCNT_TXEN;
688#ifdef USE_INTS
689 uart->ustcnt |= USTCNT_TX_INTR_MASK;
690#else
691 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
692#endif
693 if (uart->utx.w & UTX_TX_AVAIL) {
694 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
695 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
696 info->xmit_cnt--;
697 }
698
699#ifndef USE_INTS
700 }
701#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000702 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return total;
706}
707
708static int rs_write_room(struct tty_struct *tty)
709{
710 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
711 int ret;
712
713 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
714 return 0;
715 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
716 if (ret < 0)
717 ret = 0;
718 return ret;
719}
720
721static int rs_chars_in_buffer(struct tty_struct *tty)
722{
723 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
724
725 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
726 return 0;
727 return info->xmit_cnt;
728}
729
730static void rs_flush_buffer(struct tty_struct *tty)
731{
732 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000733 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
736 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000737 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000739 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tty_wakeup(tty);
741}
742
743/*
744 * ------------------------------------------------------------
745 * rs_throttle()
746 *
747 * This routine is called by the upper-layer tty layer to signal that
748 * incoming characters should be throttled.
749 * ------------------------------------------------------------
750 */
751static void rs_throttle(struct tty_struct * tty)
752{
753 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
754
755 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
756 return;
757
758 if (I_IXOFF(tty))
759 info->x_char = STOP_CHAR(tty);
760
761 /* Turn off RTS line (do this atomic) */
762}
763
764static void rs_unthrottle(struct tty_struct * tty)
765{
766 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
767
768 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
769 return;
770
771 if (I_IXOFF(tty)) {
772 if (info->x_char)
773 info->x_char = 0;
774 else
775 info->x_char = START_CHAR(tty);
776 }
777
778 /* Assert RTS line (do this atomic) */
779}
780
781/*
782 * ------------------------------------------------------------
783 * rs_ioctl() and friends
784 * ------------------------------------------------------------
785 */
786
787static int get_serial_info(struct m68k_serial * info,
788 struct serial_struct * retinfo)
789{
790 struct serial_struct tmp;
791
792 if (!retinfo)
793 return -EFAULT;
794 memset(&tmp, 0, sizeof(tmp));
795 tmp.type = info->type;
796 tmp.line = info->line;
797 tmp.port = info->port;
798 tmp.irq = info->irq;
799 tmp.flags = info->flags;
800 tmp.baud_base = info->baud_base;
801 tmp.close_delay = info->close_delay;
802 tmp.closing_wait = info->closing_wait;
803 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400804 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
805 return -EFAULT;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808}
809
810static int set_serial_info(struct m68k_serial * info,
811 struct serial_struct * new_info)
812{
813 struct serial_struct new_serial;
814 struct m68k_serial old_info;
815 int retval = 0;
816
817 if (!new_info)
818 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400819 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
820 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 old_info = *info;
822
823 if (!capable(CAP_SYS_ADMIN)) {
824 if ((new_serial.baud_base != info->baud_base) ||
825 (new_serial.type != info->type) ||
826 (new_serial.close_delay != info->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200827 ((new_serial.flags & ~ASYNC_USR_MASK) !=
828 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200830 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
831 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 info->custom_divisor = new_serial.custom_divisor;
833 goto check_and_exit;
834 }
835
836 if (info->count > 1)
837 return -EBUSY;
838
839 /*
840 * OK, past this point, all the error checking has been done.
841 * At this point, we start making changes.....
842 */
843
844 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200845 info->flags = ((info->flags & ~ASYNC_FLAGS) |
846 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 info->type = new_serial.type;
848 info->close_delay = new_serial.close_delay;
849 info->closing_wait = new_serial.closing_wait;
850
851check_and_exit:
852 retval = startup(info);
853 return retval;
854}
855
856/*
857 * get_lsr_info - get line status register info
858 *
859 * Purpose: Let user call ioctl() to get info when the UART physically
860 * is emptied. On bus types like RS485, the transmitter must
861 * release the bus after transmitting. This must be done when
862 * the transmit shift register is empty, not be done when the
863 * transmit holding register is empty. This functionality
864 * allows an RS485 driver to be written in user space.
865 */
866static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
867{
868#ifdef CONFIG_SERIAL_68328_RTS_CTS
869 m68328_uart *uart = &uart_addr[info->line];
870#endif
871 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000872 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Greg Ungerer727dda82006-06-28 15:54:22 +1000874 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875#ifdef CONFIG_SERIAL_68328_RTS_CTS
876 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
877#else
878 status = 0;
879#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000880 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400881 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884/*
885 * This routine sends a break character out the serial port.
886 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700887static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 m68328_uart *uart = &uart_addr[info->line];
890 unsigned long flags;
891 if (!info->port)
892 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000893 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#ifdef USE_INTS
895 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700896 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 uart->utx.w &= ~UTX_SEND_BREAK;
898#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000899 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Alan Cox6caa76b2011-02-14 16:27:22 +0000902static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned int cmd, unsigned long arg)
904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
906 int retval;
907
908 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
909 return -ENODEV;
910
911 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
912 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
913 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
914 if (tty->flags & (1 << TTY_IO_ERROR))
915 return -EIO;
916 }
917
918 switch (cmd) {
919 case TCSBRK: /* SVID version: non-zero arg --> no break */
920 retval = tty_check_change(tty);
921 if (retval)
922 return retval;
923 tty_wait_until_sent(tty, 0);
924 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700925 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927 case TCSBRKP: /* support for POSIX tcsendbreak() */
928 retval = tty_check_change(tty);
929 if (retval)
930 return retval;
931 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700932 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400935 return get_serial_info(info,
936 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 case TIOCSSERIAL:
938 return set_serial_info(info,
939 (struct serial_struct *) arg);
940 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400941 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400943 if (copy_to_user((struct m68k_serial *) arg,
944 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 default:
948 return -ENOIOCTLCMD;
949 }
950 return 0;
951}
952
Alan Cox606d0992006-12-08 02:38:45 -0800953static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 change_speed(info);
958
959 if ((old_termios->c_cflag & CRTSCTS) &&
960 !(tty->termios->c_cflag & CRTSCTS)) {
961 tty->hw_stopped = 0;
962 rs_start(tty);
963 }
964
965}
966
967/*
968 * ------------------------------------------------------------
969 * rs_close()
970 *
971 * This routine is called when the serial port gets closed. First, we
972 * wait for the last remaining data to be sent. Then, we unlink its
973 * S structure from the interrupt chain if necessary, and we free
974 * that IRQ if nothing is left in the chain.
975 * ------------------------------------------------------------
976 */
977static void rs_close(struct tty_struct *tty, struct file * filp)
978{
979 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
980 m68328_uart *uart = &uart_addr[info->line];
981 unsigned long flags;
982
983 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
984 return;
985
Greg Ungerer727dda82006-06-28 15:54:22 +1000986 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000989 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return;
991 }
992
993 if ((tty->count == 1) && (info->count != 1)) {
994 /*
995 * Uh, oh. tty->count is 1, which means that the tty
996 * structure will be freed. Info->count should always
997 * be one in these conditions. If it's greater than
998 * one, we've got real problems, since it means the
999 * serial port won't be shutdown.
1000 */
1001 printk("rs_close: bad serial port count; tty->count is 1, "
1002 "info->count is %d\n", info->count);
1003 info->count = 1;
1004 }
1005 if (--info->count < 0) {
1006 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1007 info->line, info->count);
1008 info->count = 0;
1009 }
1010 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001011 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return;
1013 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001014 info->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Now we wait for the transmit buffer to clear; and we notify
1017 * the line discipline to only process XON/XOFF characters.
1018 */
1019 tty->closing = 1;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001020 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 tty_wait_until_sent(tty, info->closing_wait);
1022 /*
1023 * At this point we stop accepting input. To do this, we
1024 * disable the receive line status interrupts, and tell the
1025 * interrupt driver to stop checking the data ready bit in the
1026 * line status register.
1027 */
1028
1029 uart->ustcnt &= ~USTCNT_RXEN;
1030 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1031
1032 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001033 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 tty_ldisc_flush(tty);
1036 tty->closing = 0;
1037 info->event = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001038 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#warning "This is not and has never been valid so fix it"
1040#if 0
1041 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1042 if (tty->ldisc.close)
1043 (tty->ldisc.close)(tty);
1044 tty->ldisc = ldiscs[N_TTY];
1045 tty->termios->c_line = N_TTY;
1046 if (tty->ldisc.open)
1047 (tty->ldisc.open)(tty);
1048 }
1049#endif
1050 if (info->blocked_open) {
1051 if (info->close_delay) {
1052 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1053 }
1054 wake_up_interruptible(&info->open_wait);
1055 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001056 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001058 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/*
1062 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1063 */
1064void rs_hangup(struct tty_struct *tty)
1065{
1066 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1067
1068 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1069 return;
1070
1071 rs_flush_buffer(tty);
1072 shutdown(info);
1073 info->event = 0;
1074 info->count = 0;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001075 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001076 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 wake_up_interruptible(&info->open_wait);
1078}
1079
1080/*
1081 * ------------------------------------------------------------
1082 * rs_open() and friends
1083 * ------------------------------------------------------------
1084 */
1085static int block_til_ready(struct tty_struct *tty, struct file * filp,
1086 struct m68k_serial *info)
1087{
1088 DECLARE_WAITQUEUE(wait, current);
1089 int retval;
1090 int do_clocal = 0;
1091
1092 /*
1093 * If the device is in the middle of being closed, then block
1094 * until it's done, and then try again.
1095 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001096 if (info->flags & ASYNC_CLOSING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 interruptible_sleep_on(&info->close_wait);
1098#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001099 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -EAGAIN;
1101 else
1102 return -ERESTARTSYS;
1103#else
1104 return -EAGAIN;
1105#endif
1106 }
1107
1108 /*
1109 * If non-blocking mode is set, or the port is not enabled,
1110 * then make the check up front and then exit.
1111 */
1112 if ((filp->f_flags & O_NONBLOCK) ||
1113 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001114 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116 }
1117
1118 if (tty->termios->c_cflag & CLOCAL)
1119 do_clocal = 1;
1120
1121 /*
1122 * Block waiting for the carrier detect and the line to become
1123 * free (i.e., not in use by the callout). While we are in
1124 * this loop, info->count is dropped by one, so that
1125 * rs_close() knows when to free things. We restore it upon
1126 * exit, either normal or abnormal.
1127 */
1128 retval = 0;
1129 add_wait_queue(&info->open_wait, &wait);
1130
1131 info->count--;
1132 info->blocked_open++;
1133 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001134 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001136 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 current->state = TASK_INTERRUPTIBLE;
1138 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001139 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001141 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 retval = -EAGAIN;
1143 else
1144 retval = -ERESTARTSYS;
1145#else
1146 retval = -EAGAIN;
1147#endif
1148 break;
1149 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001150 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 if (signal_pending(current)) {
1153 retval = -ERESTARTSYS;
1154 break;
1155 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001156 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001158 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 current->state = TASK_RUNNING;
1161 remove_wait_queue(&info->open_wait, &wait);
1162 if (!tty_hung_up_p(filp))
1163 info->count++;
1164 info->blocked_open--;
1165
1166 if (retval)
1167 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001168 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170}
1171
1172/*
1173 * This routine is called whenever a serial port is opened. It
1174 * enables interrupts for a serial port, linking in its S structure into
1175 * the IRQ chain. It also performs the serial-specific
1176 * initialization for the tty structure.
1177 */
1178int rs_open(struct tty_struct *tty, struct file * filp)
1179{
1180 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001181 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jiri Slaby410235f2012-03-05 14:52:01 +01001183 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (serial_paranoia_check(info, tty->name, "rs_open"))
1186 return -ENODEV;
1187
1188 info->count++;
1189 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001190 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
1193 * Start up serial port
1194 */
1195 retval = startup(info);
1196 if (retval)
1197 return retval;
1198
1199 return block_til_ready(tty, filp, info);
1200}
1201
1202/* Finally, routines used to initialize the serial driver. */
1203
1204static void show_serial_version(void)
1205{
1206 printk("MC68328 serial driver version 1.00\n");
1207}
1208
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001209static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 .open = rs_open,
1211 .close = rs_close,
1212 .write = rs_write,
1213 .flush_chars = rs_flush_chars,
1214 .write_room = rs_write_room,
1215 .chars_in_buffer = rs_chars_in_buffer,
1216 .flush_buffer = rs_flush_buffer,
1217 .ioctl = rs_ioctl,
1218 .throttle = rs_throttle,
1219 .unthrottle = rs_unthrottle,
1220 .set_termios = rs_set_termios,
1221 .stop = rs_stop,
1222 .start = rs_start,
1223 .hangup = rs_hangup,
1224 .set_ldisc = rs_set_ldisc,
1225};
1226
1227/* rs_init inits the driver */
1228static int __init
1229rs68328_init(void)
1230{
1231 int flags, i;
1232 struct m68k_serial *info;
1233
1234 serial_driver = alloc_tty_driver(NR_PORTS);
1235 if (!serial_driver)
1236 return -ENOMEM;
1237
1238 show_serial_version();
1239
1240 /* Initialize the tty_driver structure */
1241 /* SPARC: Not all of this is exactly right for us. */
1242
1243 serial_driver->name = "ttyS";
1244 serial_driver->major = TTY_MAJOR;
1245 serial_driver->minor_start = 64;
1246 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1247 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1248 serial_driver->init_termios = tty_std_termios;
1249 serial_driver->init_termios.c_cflag =
1250 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1251 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1252 tty_set_operations(serial_driver, &rs_ops);
1253
1254 if (tty_register_driver(serial_driver)) {
1255 put_tty_driver(serial_driver);
1256 printk(KERN_ERR "Couldn't register serial driver\n");
1257 return -ENOMEM;
1258 }
1259
Greg Ungerer727dda82006-06-28 15:54:22 +10001260 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 for(i=0;i<NR_PORTS;i++) {
1263
1264 info = &m68k_soft[i];
1265 info->magic = SERIAL_MAGIC;
1266 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001267 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 info->irq = uart_irqs[i];
1269 info->custom_divisor = 16;
1270 info->close_delay = 50;
1271 info->closing_wait = 3000;
1272 info->x_char = 0;
1273 info->event = 0;
1274 info->count = 0;
1275 info->blocked_open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 init_waitqueue_head(&info->open_wait);
1277 init_waitqueue_head(&info->close_wait);
1278 info->line = i;
1279 info->is_cons = 1; /* Means shortcuts work */
1280
1281 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1282 info->port, info->irq);
1283 printk(" is a builtin MC68328 UART\n");
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285#ifdef CONFIG_M68VZ328
1286 if (i > 0 )
1287 PJSEL &= 0xCF; /* PSW enable second port output */
1288#endif
1289
1290 if (request_irq(uart_irqs[i],
1291 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001292 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001293 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001296 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 0;
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300module_init(rs68328_init);
1301
1302
1303
1304static void m68328_set_baud(void)
1305{
1306 unsigned short ustcnt;
1307 int i;
1308
1309 ustcnt = USTCNT;
1310 USTCNT = ustcnt & ~USTCNT_TXEN;
1311
1312again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001313 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (baud_table[i] == m68328_console_baud)
1315 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001316 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 m68328_console_baud = 9600;
1318 goto again;
1319 }
1320
1321 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1322 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1323 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1324 ustcnt |= USTCNT_8_7;
1325 ustcnt |= USTCNT_TXEN;
1326 USTCNT = ustcnt;
1327 m68328_console_initted = 1;
1328 return;
1329}
1330
1331
1332int m68328_console_setup(struct console *cp, char *arg)
1333{
1334 int i, n = CONSOLE_BAUD_RATE;
1335
1336 if (!cp)
1337 return(-1);
1338
1339 if (arg)
1340 n = simple_strtoul(arg,NULL,0);
1341
Thiago Farina8b505ca2009-12-09 12:31:30 -08001342 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (baud_table[i] == n)
1344 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001345 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 m68328_console_baud = n;
1347 m68328_console_cbaud = 0;
1348 if (i > 15) {
1349 m68328_console_cbaud |= CBAUDEX;
1350 i -= 15;
1351 }
1352 m68328_console_cbaud |= i;
1353 }
1354
1355 m68328_set_baud(); /* make sure baud rate changes */
1356 return(0);
1357}
1358
1359
1360static struct tty_driver *m68328_console_device(struct console *c, int *index)
1361{
1362 *index = c->index;
1363 return serial_driver;
1364}
1365
1366
1367void m68328_console_write (struct console *co, const char *str,
1368 unsigned int count)
1369{
1370 if (!m68328_console_initted)
1371 m68328_set_baud();
1372 while (count--) {
1373 if (*str == '\n')
1374 rs_put_char('\r');
1375 rs_put_char( *str++ );
1376 }
1377}
1378
1379
1380static struct console m68328_driver = {
1381 .name = "ttyS",
1382 .write = m68328_console_write,
1383 .device = m68328_console_device,
1384 .setup = m68328_console_setup,
1385 .flags = CON_PRINTBUFFER,
1386 .index = -1,
1387};
1388
1389
1390static int __init m68328_console_init(void)
1391{
1392 register_console(&m68328_driver);
1393 return 0;
1394}
1395
1396console_initcall(m68328_console_init);