blob: 0c50f2e1ae01e876caef6c53639ec2b65161cfdf [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Turn off usage of real serial interrupt code, to "support" Copilot */
61#ifdef CONFIG_XCOPILOT_BUGS
62#undef USE_INTS
63#else
64#define USE_INTS
65#endif
66
Jiri Slaby1bb26872012-04-02 13:54:39 +020067/*
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
74 */
75#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
76
77/*
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
83 *
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
87 *
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
90 *
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
94 */
95
96/* (es) */
97#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99#elif defined(CONFIG_M68328)
100#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
101#else
102#error Please, define the Rx interrupt events for your CPU
103#endif
104/* (/es) */
105
106/*
107 * This is our internal structure for each serial port's state.
108 *
109 * For definitions of the flags field, see serial.h
110 */
111struct m68k_serial {
Jiri Slaby86264342012-04-02 13:54:40 +0200112 struct tty_port tport;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200113 char is_cons; /* Is this our console. */
114 int magic;
115 int baud_base;
116 int port;
117 int irq;
118 int flags; /* defined in tty.h */
119 int type; /* UART type */
120 struct tty_struct *tty;
121 int custom_divisor;
122 int x_char; /* xon/xoff character */
123 int close_delay;
124 unsigned short closing_wait;
125 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200126 unsigned char *xmit_buf;
127 int xmit_head;
128 int xmit_tail;
129 int xmit_cnt;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200130};
131
132#define SERIAL_MAGIC 0x5301
133
134/*
135 * Define the number of ports supported and their irqs.
136 */
137#define NR_PORTS 1
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jiri Slaby1bb26872012-04-02 13:54:39 +0200141static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/* multiple ports are contiguous in memory */
144m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146struct tty_driver *serial_driver;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static void change_speed(struct m68k_serial *info);
149
150/*
151 * Setup for console. Argument comes from the boot command line.
152 */
153
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700154/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
155#ifdef CONFIG_M68VZ328
156#define CONSOLE_BAUD_RATE 19200
157#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#endif
159
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#ifndef CONSOLE_BAUD_RATE
162#define CONSOLE_BAUD_RATE 9600
163#define DEFAULT_CBAUD B9600
164#endif
165
166
167static int m68328_console_initted = 0;
168static int m68328_console_baud = CONSOLE_BAUD_RATE;
169static int m68328_console_cbaud = DEFAULT_CBAUD;
170
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static inline int serial_paranoia_check(struct m68k_serial *info,
173 char *name, const char *routine)
174{
175#ifdef SERIAL_PARANOIA_CHECK
176 static const char *badmagic =
177 "Warning: bad magic number for serial struct %s in %s\n";
178 static const char *badinfo =
179 "Warning: null m68k_serial for %s in %s\n";
180
181 if (!info) {
182 printk(badinfo, name, routine);
183 return 1;
184 }
185 if (info->magic != SERIAL_MAGIC) {
186 printk(badmagic, name, routine);
187 return 1;
188 }
189#endif
190 return 0;
191}
192
193/*
194 * This is used to figure out the divisor speeds and the timeouts
195 */
196static int baud_table[] = {
197 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
198 9600, 19200, 38400, 57600, 115200, 0 };
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* Utility routines */
201static inline int get_baud(struct m68k_serial *ss)
202{
203 unsigned long result = 115200;
204 unsigned short int baud = uart_addr[ss->line].ubaud;
205 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
206 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
207
208 return result;
209}
210
211/*
212 * ------------------------------------------------------------
213 * rs_stop() and rs_start()
214 *
215 * This routines are called before setting or resetting tty->stopped.
216 * They enable or disable transmitter interrupts, as necessary.
217 * ------------------------------------------------------------
218 */
219static void rs_stop(struct tty_struct *tty)
220{
221 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
222 m68328_uart *uart = &uart_addr[info->line];
223 unsigned long flags;
224
225 if (serial_paranoia_check(info, tty->name, "rs_stop"))
226 return;
227
Greg Ungerer727dda82006-06-28 15:54:22 +1000228 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000230 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Alan Cox09a6ffa2008-04-30 00:54:01 -0700233static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Jiri Slaby107afb72012-04-02 13:54:38 +0200235 unsigned long flags;
236 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Greg Ungerer727dda82006-06-28 15:54:22 +1000238 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
241 loops++;
242 udelay(5);
243 }
244
245 UTX_TXDATA = ch;
246 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000247 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700248 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static void rs_start(struct tty_struct *tty)
252{
253 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
254 m68328_uart *uart = &uart_addr[info->line];
255 unsigned long flags;
256
257 if (serial_paranoia_check(info, tty->name, "rs_start"))
258 return;
259
Greg Ungerer727dda82006-06-28 15:54:22 +1000260 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
262#ifdef USE_INTS
263 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
264#else
265 uart->ustcnt |= USTCNT_TXEN;
266#endif
267 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000268 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
David Howells7d12e782006-10-05 14:55:46 +0100271static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000273 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800275 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /*
278 * This do { } while() loop will get ALL chars out of Rx FIFO
279 */
280#ifndef CONFIG_XCOPILOT_BUGS
281 do {
282#endif
283 ch = GET_FIELD(rx, URX_RXDATA);
284
285 if(info->is_cons) {
286 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return;
288#ifdef CONFIG_MAGIC_SYSRQ
289 } else if (ch == 0x10) { /* ^P */
290 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700291 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 show_buffers();
293/* show_net_buffers(); */
294 return;
295 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600296 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return;
298#endif /* CONFIG_MAGIC_SYSRQ */
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 if(!tty)
303 goto clear_and_exit;
304
Alan Cox33f0f882006-01-09 20:54:13 -0800305 flag = TTY_NORMAL;
306
Jiri Slabyc21e2652012-04-02 13:54:36 +0200307 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800308 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200309 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800310 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200311 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800312 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200313
Alan Cox33f0f882006-01-09 20:54:13 -0800314 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#ifndef CONFIG_XCOPILOT_BUGS
316 } while((rx = uart->urx.w) & URX_DATA_READY);
317#endif
318
Greg Ungerer8e63e662006-02-08 09:19:17 +1000319 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321clear_and_exit:
322 return;
323}
324
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800325static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 m68328_uart *uart = &uart_addr[info->line];
328
329 if (info->x_char) {
330 /* Send next char */
331 uart->utx.b.txdata = info->x_char;
332 info->x_char = 0;
333 goto clear_and_return;
334 }
335
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000336 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* That's peculiar... TX ints off */
338 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
339 goto clear_and_return;
340 }
341
342 /* Send char */
343 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
344 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
345 info->xmit_cnt--;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if(info->xmit_cnt <= 0) {
348 /* All done for now... TX ints off */
349 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
350 goto clear_and_return;
351 }
352
353clear_and_return:
354 /* Clear interrupt (should be auto)*/
355 return;
356}
357
358/*
359 * This is the serial driver's generic interrupt routine
360 */
David Howells7d12e782006-10-05 14:55:46 +0100361irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Alan Cox25db8ad2008-08-19 20:49:40 -0700363 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 m68328_uart *uart;
365 unsigned short rx;
366 unsigned short tx;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 uart = &uart_addr[info->line];
369 rx = uart->urx.w;
370
371#ifdef USE_INTS
372 tx = uart->utx.w;
373
David Howells7d12e782006-10-05 14:55:46 +0100374 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (tx & UTX_TX_AVAIL) transmit_chars(info);
376#else
David Howells7d12e782006-10-05 14:55:46 +0100377 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#endif
379 return IRQ_HANDLED;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static int startup(struct m68k_serial * info)
383{
384 m68328_uart *uart = &uart_addr[info->line];
385 unsigned long flags;
386
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200387 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389
390 if (!info->xmit_buf) {
391 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
392 if (!info->xmit_buf)
393 return -ENOMEM;
394 }
395
Greg Ungerer727dda82006-06-28 15:54:22 +1000396 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
399 * Clear the FIFO buffers and disable them
400 * (they will be reenabled in change_speed())
401 */
402
403 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
405 (void)uart->urx.w;
406
407 /*
408 * Finally, enable sequencing and interrupts
409 */
410#ifdef USE_INTS
411 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
412 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
413#else
414 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
415#endif
416
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000417 if (info->tty)
418 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
420
421 /*
422 * and set the speed of the serial port
423 */
424
425 change_speed(info);
426
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200427 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000428 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432/*
433 * This routine will shutdown a serial port; interrupts are disabled, and
434 * DTR is dropped if the hangup on close termio flag is on.
435 */
436static void shutdown(struct m68k_serial * info)
437{
438 m68328_uart *uart = &uart_addr[info->line];
439 unsigned long flags;
440
441 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200442 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return;
444
Greg Ungerer727dda82006-06-28 15:54:22 +1000445 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (info->xmit_buf) {
448 free_page((unsigned long) info->xmit_buf);
449 info->xmit_buf = 0;
450 }
451
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000452 if (info->tty)
453 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200455 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000456 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459struct {
460 int divisor, prescale;
461}
462#ifndef CONFIG_M68VZ328
463 hw_baud_table[18] = {
464 {0,0}, /* 0 */
465 {0,0}, /* 50 */
466 {0,0}, /* 75 */
467 {0,0}, /* 110 */
468 {0,0}, /* 134 */
469 {0,0}, /* 150 */
470 {0,0}, /* 200 */
471 {7,0x26}, /* 300 */
472 {6,0x26}, /* 600 */
473 {5,0x26}, /* 1200 */
474 {0,0}, /* 1800 */
475 {4,0x26}, /* 2400 */
476 {3,0x26}, /* 4800 */
477 {2,0x26}, /* 9600 */
478 {1,0x26}, /* 19200 */
479 {0,0x26}, /* 38400 */
480 {1,0x38}, /* 57600 */
481 {0,0x38}, /* 115200 */
482};
483#else
484 hw_baud_table[18] = {
485 {0,0}, /* 0 */
486 {0,0}, /* 50 */
487 {0,0}, /* 75 */
488 {0,0}, /* 110 */
489 {0,0}, /* 134 */
490 {0,0}, /* 150 */
491 {0,0}, /* 200 */
492 {0,0}, /* 300 */
493 {7,0x26}, /* 600 */
494 {6,0x26}, /* 1200 */
495 {0,0}, /* 1800 */
496 {5,0x26}, /* 2400 */
497 {4,0x26}, /* 4800 */
498 {3,0x26}, /* 9600 */
499 {2,0x26}, /* 19200 */
500 {1,0x26}, /* 38400 */
501 {0,0x26}, /* 57600 */
502 {1,0x38}, /* 115200 */
503};
504#endif
505/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
506
507/*
508 * This routine is called to set the UART divisor registers to match
509 * the specified baud rate for a serial port.
510 */
511static void change_speed(struct m68k_serial *info)
512{
513 m68328_uart *uart = &uart_addr[info->line];
514 unsigned short port;
515 unsigned short ustcnt;
516 unsigned cflag;
517 int i;
518
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000519 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000521 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!(port = info->port))
523 return;
524
525 ustcnt = uart->ustcnt;
526 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
527
528 i = cflag & CBAUD;
529 if (i & CBAUDEX) {
530 i = (i & ~CBAUDEX) + B38400;
531 }
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
534 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
535
536 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
537
538 if ((cflag & CSIZE) == CS8)
539 ustcnt |= USTCNT_8_7;
540
541 if (cflag & CSTOPB)
542 ustcnt |= USTCNT_STOP;
543
544 if (cflag & PARENB)
545 ustcnt |= USTCNT_PARITYEN;
546 if (cflag & PARODD)
547 ustcnt |= USTCNT_ODD_EVEN;
548
549#ifdef CONFIG_SERIAL_68328_RTS_CTS
550 if (cflag & CRTSCTS) {
551 uart->utx.w &= ~ UTX_NOCTS;
552 } else {
553 uart->utx.w |= UTX_NOCTS;
554 }
555#endif
556
557 ustcnt |= USTCNT_TXEN;
558
559 uart->ustcnt = ustcnt;
560 return;
561}
562
563/*
564 * Fair output driver allows a process to speak.
565 */
566static void rs_fair_output(void)
567{
568 int left; /* Output no more than that */
569 unsigned long flags;
570 struct m68k_serial *info = &m68k_soft[0];
571 char c;
572
573 if (info == 0) return;
574 if (info->xmit_buf == 0) return;
575
Greg Ungerer727dda82006-06-28 15:54:22 +1000576 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 left = info->xmit_cnt;
578 while (left != 0) {
579 c = info->xmit_buf[info->xmit_tail];
580 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
581 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000582 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 rs_put_char(c);
585
Greg Ungerer727dda82006-06-28 15:54:22 +1000586 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 left = min(info->xmit_cnt, left-1);
588 }
589
590 /* Last character is being transmitted now (hopefully). */
591 udelay(5);
592
Greg Ungerer727dda82006-06-28 15:54:22 +1000593 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595}
596
597/*
598 * m68k_console_print is registered for printk.
599 */
600void console_print_68328(const char *p)
601{
602 char c;
603
604 while((c=*(p++)) != 0) {
605 if(c == '\n')
606 rs_put_char('\r');
607 rs_put_char(c);
608 }
609
610 /* Comment this if you want to have a strict interrupt-driven output */
611 rs_fair_output();
612
613 return;
614}
615
616static void rs_set_ldisc(struct tty_struct *tty)
617{
618 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
619
620 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
621 return;
622
623 info->is_cons = (tty->termios->c_line == N_TTY);
624
625 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
626}
627
628static void rs_flush_chars(struct tty_struct *tty)
629{
630 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
631 m68328_uart *uart = &uart_addr[info->line];
632 unsigned long flags;
633
634 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
635 return;
636#ifndef USE_INTS
637 for(;;) {
638#endif
639
640 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000641 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
644 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000645 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647 }
648
649#ifdef USE_INTS
650 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
651#else
652 uart->ustcnt |= USTCNT_TXEN;
653#endif
654
655#ifdef USE_INTS
656 if (uart->utx.w & UTX_TX_AVAIL) {
657#else
658 if (1) {
659#endif
660 /* Send char */
661 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
662 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
663 info->xmit_cnt--;
664 }
665
666#ifndef USE_INTS
667 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
668 }
669#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000670 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673extern void console_printn(const char * b, int count);
674
675static int rs_write(struct tty_struct * tty,
676 const unsigned char *buf, int count)
677{
678 int c, total = 0;
679 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
680 m68328_uart *uart = &uart_addr[info->line];
681 unsigned long flags;
682
683 if (serial_paranoia_check(info, tty->name, "rs_write"))
684 return 0;
685
686 if (!tty || !info->xmit_buf)
687 return 0;
688
Greg Ungerer727dda82006-06-28 15:54:22 +1000689 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000691 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
693 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000694 local_irq_restore(flags);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (c <= 0)
697 break;
698
699 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000700
701 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
703 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000704 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 buf += c;
706 count -= c;
707 total += c;
708 }
709
710 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
711 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000712 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713#ifndef USE_INTS
714 while(info->xmit_cnt) {
715#endif
716
717 uart->ustcnt |= USTCNT_TXEN;
718#ifdef USE_INTS
719 uart->ustcnt |= USTCNT_TX_INTR_MASK;
720#else
721 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
722#endif
723 if (uart->utx.w & UTX_TX_AVAIL) {
724 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
725 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
726 info->xmit_cnt--;
727 }
728
729#ifndef USE_INTS
730 }
731#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000732 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return total;
736}
737
738static int rs_write_room(struct tty_struct *tty)
739{
740 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
741 int ret;
742
743 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
744 return 0;
745 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
746 if (ret < 0)
747 ret = 0;
748 return ret;
749}
750
751static int rs_chars_in_buffer(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_chars_in_buffer"))
756 return 0;
757 return info->xmit_cnt;
758}
759
760static void rs_flush_buffer(struct tty_struct *tty)
761{
762 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000763 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
766 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000767 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000769 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 tty_wakeup(tty);
771}
772
773/*
774 * ------------------------------------------------------------
775 * rs_throttle()
776 *
777 * This routine is called by the upper-layer tty layer to signal that
778 * incoming characters should be throttled.
779 * ------------------------------------------------------------
780 */
781static void rs_throttle(struct tty_struct * tty)
782{
783 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
784
785 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
786 return;
787
788 if (I_IXOFF(tty))
789 info->x_char = STOP_CHAR(tty);
790
791 /* Turn off RTS line (do this atomic) */
792}
793
794static void rs_unthrottle(struct tty_struct * tty)
795{
796 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
797
798 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
799 return;
800
801 if (I_IXOFF(tty)) {
802 if (info->x_char)
803 info->x_char = 0;
804 else
805 info->x_char = START_CHAR(tty);
806 }
807
808 /* Assert RTS line (do this atomic) */
809}
810
811/*
812 * ------------------------------------------------------------
813 * rs_ioctl() and friends
814 * ------------------------------------------------------------
815 */
816
817static int get_serial_info(struct m68k_serial * info,
818 struct serial_struct * retinfo)
819{
820 struct serial_struct tmp;
821
822 if (!retinfo)
823 return -EFAULT;
824 memset(&tmp, 0, sizeof(tmp));
825 tmp.type = info->type;
826 tmp.line = info->line;
827 tmp.port = info->port;
828 tmp.irq = info->irq;
829 tmp.flags = info->flags;
830 tmp.baud_base = info->baud_base;
831 tmp.close_delay = info->close_delay;
832 tmp.closing_wait = info->closing_wait;
833 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400834 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
835 return -EFAULT;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 0;
838}
839
840static int set_serial_info(struct m68k_serial * info,
841 struct serial_struct * new_info)
842{
843 struct serial_struct new_serial;
844 struct m68k_serial old_info;
845 int retval = 0;
846
847 if (!new_info)
848 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400849 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
850 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 old_info = *info;
852
853 if (!capable(CAP_SYS_ADMIN)) {
854 if ((new_serial.baud_base != info->baud_base) ||
855 (new_serial.type != info->type) ||
856 (new_serial.close_delay != info->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200857 ((new_serial.flags & ~ASYNC_USR_MASK) !=
858 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200860 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
861 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 info->custom_divisor = new_serial.custom_divisor;
863 goto check_and_exit;
864 }
865
Jiri Slaby86264342012-04-02 13:54:40 +0200866 if (info->tport.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EBUSY;
868
869 /*
870 * OK, past this point, all the error checking has been done.
871 * At this point, we start making changes.....
872 */
873
874 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200875 info->flags = ((info->flags & ~ASYNC_FLAGS) |
876 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 info->type = new_serial.type;
878 info->close_delay = new_serial.close_delay;
879 info->closing_wait = new_serial.closing_wait;
880
881check_and_exit:
882 retval = startup(info);
883 return retval;
884}
885
886/*
887 * get_lsr_info - get line status register info
888 *
889 * Purpose: Let user call ioctl() to get info when the UART physically
890 * is emptied. On bus types like RS485, the transmitter must
891 * release the bus after transmitting. This must be done when
892 * the transmit shift register is empty, not be done when the
893 * transmit holding register is empty. This functionality
894 * allows an RS485 driver to be written in user space.
895 */
896static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
897{
898#ifdef CONFIG_SERIAL_68328_RTS_CTS
899 m68328_uart *uart = &uart_addr[info->line];
900#endif
901 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000902 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Greg Ungerer727dda82006-06-28 15:54:22 +1000904 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#ifdef CONFIG_SERIAL_68328_RTS_CTS
906 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
907#else
908 status = 0;
909#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000910 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400911 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/*
915 * This routine sends a break character out the serial port.
916 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700917static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 m68328_uart *uart = &uart_addr[info->line];
920 unsigned long flags;
921 if (!info->port)
922 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000923 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924#ifdef USE_INTS
925 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700926 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 uart->utx.w &= ~UTX_SEND_BREAK;
928#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000929 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Alan Cox6caa76b2011-02-14 16:27:22 +0000932static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned int cmd, unsigned long arg)
934{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
936 int retval;
937
938 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
939 return -ENODEV;
940
941 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
942 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
943 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
944 if (tty->flags & (1 << TTY_IO_ERROR))
945 return -EIO;
946 }
947
948 switch (cmd) {
949 case TCSBRK: /* SVID version: non-zero arg --> no break */
950 retval = tty_check_change(tty);
951 if (retval)
952 return retval;
953 tty_wait_until_sent(tty, 0);
954 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700955 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957 case TCSBRKP: /* support for POSIX tcsendbreak() */
958 retval = tty_check_change(tty);
959 if (retval)
960 return retval;
961 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700962 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400965 return get_serial_info(info,
966 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 case TIOCSSERIAL:
968 return set_serial_info(info,
969 (struct serial_struct *) arg);
970 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400971 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400973 if (copy_to_user((struct m68k_serial *) arg,
974 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 default:
978 return -ENOIOCTLCMD;
979 }
980 return 0;
981}
982
Alan Cox606d0992006-12-08 02:38:45 -0800983static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 change_speed(info);
988
989 if ((old_termios->c_cflag & CRTSCTS) &&
990 !(tty->termios->c_cflag & CRTSCTS)) {
991 tty->hw_stopped = 0;
992 rs_start(tty);
993 }
994
995}
996
997/*
998 * ------------------------------------------------------------
999 * rs_close()
1000 *
1001 * This routine is called when the serial port gets closed. First, we
1002 * wait for the last remaining data to be sent. Then, we unlink its
1003 * S structure from the interrupt chain if necessary, and we free
1004 * that IRQ if nothing is left in the chain.
1005 * ------------------------------------------------------------
1006 */
1007static void rs_close(struct tty_struct *tty, struct file * filp)
1008{
1009 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +02001010 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 m68328_uart *uart = &uart_addr[info->line];
1012 unsigned long flags;
1013
1014 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1015 return;
1016
Greg Ungerer727dda82006-06-28 15:54:22 +10001017 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001020 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return;
1022 }
1023
Jiri Slaby86264342012-04-02 13:54:40 +02001024 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * Uh, oh. tty->count is 1, which means that the tty
1027 * structure will be freed. Info->count should always
1028 * be one in these conditions. If it's greater than
1029 * one, we've got real problems, since it means the
1030 * serial port won't be shutdown.
1031 */
1032 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001033 "port->count is %d\n", port->count);
1034 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Jiri Slaby86264342012-04-02 13:54:40 +02001036 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001038 info->line, port->count);
1039 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Jiri Slaby86264342012-04-02 13:54:40 +02001041 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001042 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return;
1044 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001045 info->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * Now we wait for the transmit buffer to clear; and we notify
1048 * the line discipline to only process XON/XOFF characters.
1049 */
1050 tty->closing = 1;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001051 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 tty_wait_until_sent(tty, info->closing_wait);
1053 /*
1054 * At this point we stop accepting input. To do this, we
1055 * disable the receive line status interrupts, and tell the
1056 * interrupt driver to stop checking the data ready bit in the
1057 * line status register.
1058 */
1059
1060 uart->ustcnt &= ~USTCNT_RXEN;
1061 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1062
1063 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001064 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 tty_ldisc_flush(tty);
1067 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001068 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069#warning "This is not and has never been valid so fix it"
1070#if 0
1071 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1072 if (tty->ldisc.close)
1073 (tty->ldisc.close)(tty);
1074 tty->ldisc = ldiscs[N_TTY];
1075 tty->termios->c_line = N_TTY;
1076 if (tty->ldisc.open)
1077 (tty->ldisc.open)(tty);
1078 }
1079#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001080 if (port->blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (info->close_delay) {
1082 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1083 }
Jiri Slabyc26f0112012-04-02 13:54:41 +02001084 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001086 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001087 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001088 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091/*
1092 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1093 */
1094void rs_hangup(struct tty_struct *tty)
1095{
1096 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1097
1098 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1099 return;
1100
1101 rs_flush_buffer(tty);
1102 shutdown(info);
Jiri Slaby86264342012-04-02 13:54:40 +02001103 info->tport.count = 0;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001104 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001105 info->tty = NULL;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001106 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109/*
1110 * ------------------------------------------------------------
1111 * rs_open() and friends
1112 * ------------------------------------------------------------
1113 */
1114static int block_til_ready(struct tty_struct *tty, struct file * filp,
1115 struct m68k_serial *info)
1116{
Jiri Slaby86264342012-04-02 13:54:40 +02001117 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 DECLARE_WAITQUEUE(wait, current);
1119 int retval;
1120 int do_clocal = 0;
1121
1122 /*
1123 * If the device is in the middle of being closed, then block
1124 * until it's done, and then try again.
1125 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001126 if (info->flags & ASYNC_CLOSING) {
Jiri Slabyc26f0112012-04-02 13:54:41 +02001127 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001129 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return -EAGAIN;
1131 else
1132 return -ERESTARTSYS;
1133#else
1134 return -EAGAIN;
1135#endif
1136 }
1137
1138 /*
1139 * If non-blocking mode is set, or the port is not enabled,
1140 * then make the check up front and then exit.
1141 */
1142 if ((filp->f_flags & O_NONBLOCK) ||
1143 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001144 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146 }
1147
1148 if (tty->termios->c_cflag & CLOCAL)
1149 do_clocal = 1;
1150
1151 /*
1152 * Block waiting for the carrier detect and the line to become
1153 * free (i.e., not in use by the callout). While we are in
Jiri Slaby86264342012-04-02 13:54:40 +02001154 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * rs_close() knows when to free things. We restore it upon
1156 * exit, either normal or abnormal.
1157 */
1158 retval = 0;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001159 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jiri Slaby86264342012-04-02 13:54:40 +02001161 port->count--;
1162 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 current->state = TASK_INTERRUPTIBLE;
1165 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001166 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001168 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 retval = -EAGAIN;
1170 else
1171 retval = -ERESTARTSYS;
1172#else
1173 retval = -EAGAIN;
1174#endif
1175 break;
1176 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001177 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 if (signal_pending(current)) {
1180 retval = -ERESTARTSYS;
1181 break;
1182 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001183 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001185 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 current->state = TASK_RUNNING;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001188 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!tty_hung_up_p(filp))
Jiri Slaby86264342012-04-02 13:54:40 +02001190 port->count++;
1191 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (retval)
1194 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001195 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
1198
1199/*
1200 * This routine is called whenever a serial port is opened. It
1201 * enables interrupts for a serial port, linking in its S structure into
1202 * the IRQ chain. It also performs the serial-specific
1203 * initialization for the tty structure.
1204 */
1205int rs_open(struct tty_struct *tty, struct file * filp)
1206{
1207 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001208 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Jiri Slaby410235f2012-03-05 14:52:01 +01001210 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (serial_paranoia_check(info, tty->name, "rs_open"))
1213 return -ENODEV;
1214
Jiri Slaby86264342012-04-02 13:54:40 +02001215 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001217 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * Start up serial port
1221 */
1222 retval = startup(info);
1223 if (retval)
1224 return retval;
1225
1226 return block_til_ready(tty, filp, info);
1227}
1228
1229/* Finally, routines used to initialize the serial driver. */
1230
1231static void show_serial_version(void)
1232{
1233 printk("MC68328 serial driver version 1.00\n");
1234}
1235
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001236static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 .open = rs_open,
1238 .close = rs_close,
1239 .write = rs_write,
1240 .flush_chars = rs_flush_chars,
1241 .write_room = rs_write_room,
1242 .chars_in_buffer = rs_chars_in_buffer,
1243 .flush_buffer = rs_flush_buffer,
1244 .ioctl = rs_ioctl,
1245 .throttle = rs_throttle,
1246 .unthrottle = rs_unthrottle,
1247 .set_termios = rs_set_termios,
1248 .stop = rs_stop,
1249 .start = rs_start,
1250 .hangup = rs_hangup,
1251 .set_ldisc = rs_set_ldisc,
1252};
1253
1254/* rs_init inits the driver */
1255static int __init
1256rs68328_init(void)
1257{
Jiri Slaby107afb72012-04-02 13:54:38 +02001258 unsigned long flags;
1259 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct m68k_serial *info;
1261
1262 serial_driver = alloc_tty_driver(NR_PORTS);
1263 if (!serial_driver)
1264 return -ENOMEM;
1265
1266 show_serial_version();
1267
1268 /* Initialize the tty_driver structure */
1269 /* SPARC: Not all of this is exactly right for us. */
1270
1271 serial_driver->name = "ttyS";
1272 serial_driver->major = TTY_MAJOR;
1273 serial_driver->minor_start = 64;
1274 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1275 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1276 serial_driver->init_termios = tty_std_termios;
1277 serial_driver->init_termios.c_cflag =
1278 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1279 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1280 tty_set_operations(serial_driver, &rs_ops);
1281
1282 if (tty_register_driver(serial_driver)) {
1283 put_tty_driver(serial_driver);
1284 printk(KERN_ERR "Couldn't register serial driver\n");
1285 return -ENOMEM;
1286 }
1287
Greg Ungerer727dda82006-06-28 15:54:22 +10001288 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 for(i=0;i<NR_PORTS;i++) {
1291
1292 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001293 tty_port_init(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 info->magic = SERIAL_MAGIC;
1295 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001296 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 info->irq = uart_irqs[i];
1298 info->custom_divisor = 16;
1299 info->close_delay = 50;
1300 info->closing_wait = 3000;
1301 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 info->line = i;
1303 info->is_cons = 1; /* Means shortcuts work */
1304
1305 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1306 info->port, info->irq);
1307 printk(" is a builtin MC68328 UART\n");
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309#ifdef CONFIG_M68VZ328
1310 if (i > 0 )
1311 PJSEL &= 0xCF; /* PSW enable second port output */
1312#endif
1313
1314 if (request_irq(uart_irqs[i],
1315 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001316 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001317 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001320 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return 0;
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324module_init(rs68328_init);
1325
1326
1327
1328static void m68328_set_baud(void)
1329{
1330 unsigned short ustcnt;
1331 int i;
1332
1333 ustcnt = USTCNT;
1334 USTCNT = ustcnt & ~USTCNT_TXEN;
1335
1336again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001337 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (baud_table[i] == m68328_console_baud)
1339 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001340 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 m68328_console_baud = 9600;
1342 goto again;
1343 }
1344
1345 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1346 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1347 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1348 ustcnt |= USTCNT_8_7;
1349 ustcnt |= USTCNT_TXEN;
1350 USTCNT = ustcnt;
1351 m68328_console_initted = 1;
1352 return;
1353}
1354
1355
1356int m68328_console_setup(struct console *cp, char *arg)
1357{
1358 int i, n = CONSOLE_BAUD_RATE;
1359
1360 if (!cp)
1361 return(-1);
1362
1363 if (arg)
1364 n = simple_strtoul(arg,NULL,0);
1365
Thiago Farina8b505ca2009-12-09 12:31:30 -08001366 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (baud_table[i] == n)
1368 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001369 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 m68328_console_baud = n;
1371 m68328_console_cbaud = 0;
1372 if (i > 15) {
1373 m68328_console_cbaud |= CBAUDEX;
1374 i -= 15;
1375 }
1376 m68328_console_cbaud |= i;
1377 }
1378
1379 m68328_set_baud(); /* make sure baud rate changes */
1380 return(0);
1381}
1382
1383
1384static struct tty_driver *m68328_console_device(struct console *c, int *index)
1385{
1386 *index = c->index;
1387 return serial_driver;
1388}
1389
1390
1391void m68328_console_write (struct console *co, const char *str,
1392 unsigned int count)
1393{
1394 if (!m68328_console_initted)
1395 m68328_set_baud();
1396 while (count--) {
1397 if (*str == '\n')
1398 rs_put_char('\r');
1399 rs_put_char( *str++ );
1400 }
1401}
1402
1403
1404static struct console m68328_driver = {
1405 .name = "ttyS",
1406 .write = m68328_console_write,
1407 .device = m68328_console_device,
1408 .setup = m68328_console_setup,
1409 .flags = CON_PRINTBUFFER,
1410 .index = -1,
1411};
1412
1413
1414static int __init m68328_console_init(void)
1415{
1416 register_console(&m68328_driver);
1417 return 0;
1418}
1419
1420console_initcall(m68328_console_init);