blob: e9fd13ec52a8325be8bc8b53412fd032c222bd7e [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 */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200123 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200124 unsigned char *xmit_buf;
125 int xmit_head;
126 int xmit_tail;
127 int xmit_cnt;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200128};
129
130#define SERIAL_MAGIC 0x5301
131
132/*
133 * Define the number of ports supported and their irqs.
134 */
135#define NR_PORTS 1
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jiri Slaby1bb26872012-04-02 13:54:39 +0200139static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/* multiple ports are contiguous in memory */
142m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct tty_driver *serial_driver;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static void change_speed(struct m68k_serial *info);
147
148/*
149 * Setup for console. Argument comes from the boot command line.
150 */
151
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700152/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
153#ifdef CONFIG_M68VZ328
154#define CONSOLE_BAUD_RATE 19200
155#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif
157
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifndef CONSOLE_BAUD_RATE
160#define CONSOLE_BAUD_RATE 9600
161#define DEFAULT_CBAUD B9600
162#endif
163
164
165static int m68328_console_initted = 0;
166static int m68328_console_baud = CONSOLE_BAUD_RATE;
167static int m68328_console_cbaud = DEFAULT_CBAUD;
168
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static inline int serial_paranoia_check(struct m68k_serial *info,
171 char *name, const char *routine)
172{
173#ifdef SERIAL_PARANOIA_CHECK
174 static const char *badmagic =
175 "Warning: bad magic number for serial struct %s in %s\n";
176 static const char *badinfo =
177 "Warning: null m68k_serial for %s in %s\n";
178
179 if (!info) {
180 printk(badinfo, name, routine);
181 return 1;
182 }
183 if (info->magic != SERIAL_MAGIC) {
184 printk(badmagic, name, routine);
185 return 1;
186 }
187#endif
188 return 0;
189}
190
191/*
192 * This is used to figure out the divisor speeds and the timeouts
193 */
194static int baud_table[] = {
195 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
196 9600, 19200, 38400, 57600, 115200, 0 };
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* Utility routines */
199static inline int get_baud(struct m68k_serial *ss)
200{
201 unsigned long result = 115200;
202 unsigned short int baud = uart_addr[ss->line].ubaud;
203 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
204 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
205
206 return result;
207}
208
209/*
210 * ------------------------------------------------------------
211 * rs_stop() and rs_start()
212 *
213 * This routines are called before setting or resetting tty->stopped.
214 * They enable or disable transmitter interrupts, as necessary.
215 * ------------------------------------------------------------
216 */
217static void rs_stop(struct tty_struct *tty)
218{
219 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
220 m68328_uart *uart = &uart_addr[info->line];
221 unsigned long flags;
222
223 if (serial_paranoia_check(info, tty->name, "rs_stop"))
224 return;
225
Greg Ungerer727dda82006-06-28 15:54:22 +1000226 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000228 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Alan Cox09a6ffa2008-04-30 00:54:01 -0700231static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Jiri Slaby107afb72012-04-02 13:54:38 +0200233 unsigned long flags;
234 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Greg Ungerer727dda82006-06-28 15:54:22 +1000236 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
239 loops++;
240 udelay(5);
241 }
242
243 UTX_TXDATA = ch;
244 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000245 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700246 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static void rs_start(struct tty_struct *tty)
250{
251 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
252 m68328_uart *uart = &uart_addr[info->line];
253 unsigned long flags;
254
255 if (serial_paranoia_check(info, tty->name, "rs_start"))
256 return;
257
Greg Ungerer727dda82006-06-28 15:54:22 +1000258 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
260#ifdef USE_INTS
261 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
262#else
263 uart->ustcnt |= USTCNT_TXEN;
264#endif
265 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000266 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
David Howells7d12e782006-10-05 14:55:46 +0100269static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000271 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800273 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * This do { } while() loop will get ALL chars out of Rx FIFO
277 */
278#ifndef CONFIG_XCOPILOT_BUGS
279 do {
280#endif
281 ch = GET_FIELD(rx, URX_RXDATA);
282
283 if(info->is_cons) {
284 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return;
286#ifdef CONFIG_MAGIC_SYSRQ
287 } else if (ch == 0x10) { /* ^P */
288 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700289 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 show_buffers();
291/* show_net_buffers(); */
292 return;
293 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600294 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return;
296#endif /* CONFIG_MAGIC_SYSRQ */
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 if(!tty)
301 goto clear_and_exit;
302
Alan Cox33f0f882006-01-09 20:54:13 -0800303 flag = TTY_NORMAL;
304
Jiri Slabyc21e2652012-04-02 13:54:36 +0200305 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800306 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200307 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800308 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200309 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800310 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200311
Alan Cox33f0f882006-01-09 20:54:13 -0800312 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#ifndef CONFIG_XCOPILOT_BUGS
314 } while((rx = uart->urx.w) & URX_DATA_READY);
315#endif
316
Greg Ungerer8e63e662006-02-08 09:19:17 +1000317 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319clear_and_exit:
320 return;
321}
322
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800323static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 m68328_uart *uart = &uart_addr[info->line];
326
327 if (info->x_char) {
328 /* Send next char */
329 uart->utx.b.txdata = info->x_char;
330 info->x_char = 0;
331 goto clear_and_return;
332 }
333
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000334 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* That's peculiar... TX ints off */
336 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
337 goto clear_and_return;
338 }
339
340 /* Send char */
341 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
342 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
343 info->xmit_cnt--;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if(info->xmit_cnt <= 0) {
346 /* All done for now... TX ints off */
347 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
348 goto clear_and_return;
349 }
350
351clear_and_return:
352 /* Clear interrupt (should be auto)*/
353 return;
354}
355
356/*
357 * This is the serial driver's generic interrupt routine
358 */
David Howells7d12e782006-10-05 14:55:46 +0100359irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Alan Cox25db8ad2008-08-19 20:49:40 -0700361 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 m68328_uart *uart;
363 unsigned short rx;
364 unsigned short tx;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 uart = &uart_addr[info->line];
367 rx = uart->urx.w;
368
369#ifdef USE_INTS
370 tx = uart->utx.w;
371
David Howells7d12e782006-10-05 14:55:46 +0100372 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (tx & UTX_TX_AVAIL) transmit_chars(info);
374#else
David Howells7d12e782006-10-05 14:55:46 +0100375 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
377 return IRQ_HANDLED;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static int startup(struct m68k_serial * info)
381{
382 m68328_uart *uart = &uart_addr[info->line];
383 unsigned long flags;
384
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200385 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387
388 if (!info->xmit_buf) {
389 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
390 if (!info->xmit_buf)
391 return -ENOMEM;
392 }
393
Greg Ungerer727dda82006-06-28 15:54:22 +1000394 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * Clear the FIFO buffers and disable them
398 * (they will be reenabled in change_speed())
399 */
400
401 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
403 (void)uart->urx.w;
404
405 /*
406 * Finally, enable sequencing and interrupts
407 */
408#ifdef USE_INTS
409 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
410 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
411#else
412 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
413#endif
414
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000415 if (info->tty)
416 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
418
419 /*
420 * and set the speed of the serial port
421 */
422
423 change_speed(info);
424
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200425 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000426 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
430/*
431 * This routine will shutdown a serial port; interrupts are disabled, and
432 * DTR is dropped if the hangup on close termio flag is on.
433 */
434static void shutdown(struct m68k_serial * info)
435{
436 m68328_uart *uart = &uart_addr[info->line];
437 unsigned long flags;
438
439 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200440 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
442
Greg Ungerer727dda82006-06-28 15:54:22 +1000443 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (info->xmit_buf) {
446 free_page((unsigned long) info->xmit_buf);
447 info->xmit_buf = 0;
448 }
449
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000450 if (info->tty)
451 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200453 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000454 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457struct {
458 int divisor, prescale;
459}
460#ifndef CONFIG_M68VZ328
461 hw_baud_table[18] = {
462 {0,0}, /* 0 */
463 {0,0}, /* 50 */
464 {0,0}, /* 75 */
465 {0,0}, /* 110 */
466 {0,0}, /* 134 */
467 {0,0}, /* 150 */
468 {0,0}, /* 200 */
469 {7,0x26}, /* 300 */
470 {6,0x26}, /* 600 */
471 {5,0x26}, /* 1200 */
472 {0,0}, /* 1800 */
473 {4,0x26}, /* 2400 */
474 {3,0x26}, /* 4800 */
475 {2,0x26}, /* 9600 */
476 {1,0x26}, /* 19200 */
477 {0,0x26}, /* 38400 */
478 {1,0x38}, /* 57600 */
479 {0,0x38}, /* 115200 */
480};
481#else
482 hw_baud_table[18] = {
483 {0,0}, /* 0 */
484 {0,0}, /* 50 */
485 {0,0}, /* 75 */
486 {0,0}, /* 110 */
487 {0,0}, /* 134 */
488 {0,0}, /* 150 */
489 {0,0}, /* 200 */
490 {0,0}, /* 300 */
491 {7,0x26}, /* 600 */
492 {6,0x26}, /* 1200 */
493 {0,0}, /* 1800 */
494 {5,0x26}, /* 2400 */
495 {4,0x26}, /* 4800 */
496 {3,0x26}, /* 9600 */
497 {2,0x26}, /* 19200 */
498 {1,0x26}, /* 38400 */
499 {0,0x26}, /* 57600 */
500 {1,0x38}, /* 115200 */
501};
502#endif
503/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
504
505/*
506 * This routine is called to set the UART divisor registers to match
507 * the specified baud rate for a serial port.
508 */
509static void change_speed(struct m68k_serial *info)
510{
511 m68328_uart *uart = &uart_addr[info->line];
512 unsigned short port;
513 unsigned short ustcnt;
514 unsigned cflag;
515 int i;
516
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000517 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000519 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!(port = info->port))
521 return;
522
523 ustcnt = uart->ustcnt;
524 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
525
526 i = cflag & CBAUD;
527 if (i & CBAUDEX) {
528 i = (i & ~CBAUDEX) + B38400;
529 }
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
532 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
533
534 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
535
536 if ((cflag & CSIZE) == CS8)
537 ustcnt |= USTCNT_8_7;
538
539 if (cflag & CSTOPB)
540 ustcnt |= USTCNT_STOP;
541
542 if (cflag & PARENB)
543 ustcnt |= USTCNT_PARITYEN;
544 if (cflag & PARODD)
545 ustcnt |= USTCNT_ODD_EVEN;
546
547#ifdef CONFIG_SERIAL_68328_RTS_CTS
548 if (cflag & CRTSCTS) {
549 uart->utx.w &= ~ UTX_NOCTS;
550 } else {
551 uart->utx.w |= UTX_NOCTS;
552 }
553#endif
554
555 ustcnt |= USTCNT_TXEN;
556
557 uart->ustcnt = ustcnt;
558 return;
559}
560
561/*
562 * Fair output driver allows a process to speak.
563 */
564static void rs_fair_output(void)
565{
566 int left; /* Output no more than that */
567 unsigned long flags;
568 struct m68k_serial *info = &m68k_soft[0];
569 char c;
570
571 if (info == 0) return;
572 if (info->xmit_buf == 0) return;
573
Greg Ungerer727dda82006-06-28 15:54:22 +1000574 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 left = info->xmit_cnt;
576 while (left != 0) {
577 c = info->xmit_buf[info->xmit_tail];
578 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
579 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000580 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 rs_put_char(c);
583
Greg Ungerer727dda82006-06-28 15:54:22 +1000584 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 left = min(info->xmit_cnt, left-1);
586 }
587
588 /* Last character is being transmitted now (hopefully). */
589 udelay(5);
590
Greg Ungerer727dda82006-06-28 15:54:22 +1000591 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593}
594
595/*
596 * m68k_console_print is registered for printk.
597 */
598void console_print_68328(const char *p)
599{
600 char c;
601
602 while((c=*(p++)) != 0) {
603 if(c == '\n')
604 rs_put_char('\r');
605 rs_put_char(c);
606 }
607
608 /* Comment this if you want to have a strict interrupt-driven output */
609 rs_fair_output();
610
611 return;
612}
613
614static void rs_set_ldisc(struct tty_struct *tty)
615{
616 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
617
618 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
619 return;
620
621 info->is_cons = (tty->termios->c_line == N_TTY);
622
623 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
624}
625
626static void rs_flush_chars(struct tty_struct *tty)
627{
628 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
629 m68328_uart *uart = &uart_addr[info->line];
630 unsigned long flags;
631
632 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
633 return;
634#ifndef USE_INTS
635 for(;;) {
636#endif
637
638 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000639 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
642 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000643 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return;
645 }
646
647#ifdef USE_INTS
648 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
649#else
650 uart->ustcnt |= USTCNT_TXEN;
651#endif
652
653#ifdef USE_INTS
654 if (uart->utx.w & UTX_TX_AVAIL) {
655#else
656 if (1) {
657#endif
658 /* Send char */
659 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
660 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
661 info->xmit_cnt--;
662 }
663
664#ifndef USE_INTS
665 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
666 }
667#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000668 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671extern void console_printn(const char * b, int count);
672
673static int rs_write(struct tty_struct * tty,
674 const unsigned char *buf, int count)
675{
676 int c, total = 0;
677 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
678 m68328_uart *uart = &uart_addr[info->line];
679 unsigned long flags;
680
681 if (serial_paranoia_check(info, tty->name, "rs_write"))
682 return 0;
683
684 if (!tty || !info->xmit_buf)
685 return 0;
686
Greg Ungerer727dda82006-06-28 15:54:22 +1000687 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000689 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
691 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000692 local_irq_restore(flags);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (c <= 0)
695 break;
696
697 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000698
699 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
701 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000702 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 buf += c;
704 count -= c;
705 total += c;
706 }
707
708 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
709 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000710 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711#ifndef USE_INTS
712 while(info->xmit_cnt) {
713#endif
714
715 uart->ustcnt |= USTCNT_TXEN;
716#ifdef USE_INTS
717 uart->ustcnt |= USTCNT_TX_INTR_MASK;
718#else
719 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
720#endif
721 if (uart->utx.w & UTX_TX_AVAIL) {
722 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
723 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
724 info->xmit_cnt--;
725 }
726
727#ifndef USE_INTS
728 }
729#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000730 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return total;
734}
735
736static int rs_write_room(struct tty_struct *tty)
737{
738 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
739 int ret;
740
741 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
742 return 0;
743 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
744 if (ret < 0)
745 ret = 0;
746 return ret;
747}
748
749static int rs_chars_in_buffer(struct tty_struct *tty)
750{
751 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
752
753 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
754 return 0;
755 return info->xmit_cnt;
756}
757
758static void rs_flush_buffer(struct tty_struct *tty)
759{
760 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000761 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
764 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000765 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000767 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 tty_wakeup(tty);
769}
770
771/*
772 * ------------------------------------------------------------
773 * rs_throttle()
774 *
775 * This routine is called by the upper-layer tty layer to signal that
776 * incoming characters should be throttled.
777 * ------------------------------------------------------------
778 */
779static void rs_throttle(struct tty_struct * tty)
780{
781 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
782
783 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
784 return;
785
786 if (I_IXOFF(tty))
787 info->x_char = STOP_CHAR(tty);
788
789 /* Turn off RTS line (do this atomic) */
790}
791
792static void rs_unthrottle(struct tty_struct * tty)
793{
794 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
795
796 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
797 return;
798
799 if (I_IXOFF(tty)) {
800 if (info->x_char)
801 info->x_char = 0;
802 else
803 info->x_char = START_CHAR(tty);
804 }
805
806 /* Assert RTS line (do this atomic) */
807}
808
809/*
810 * ------------------------------------------------------------
811 * rs_ioctl() and friends
812 * ------------------------------------------------------------
813 */
814
815static int get_serial_info(struct m68k_serial * info,
816 struct serial_struct * retinfo)
817{
818 struct serial_struct tmp;
819
820 if (!retinfo)
821 return -EFAULT;
822 memset(&tmp, 0, sizeof(tmp));
823 tmp.type = info->type;
824 tmp.line = info->line;
825 tmp.port = info->port;
826 tmp.irq = info->irq;
827 tmp.flags = info->flags;
828 tmp.baud_base = info->baud_base;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200829 tmp.close_delay = info->tport.close_delay;
830 tmp.closing_wait = info->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400832 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
833 return -EFAULT;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
836}
837
838static int set_serial_info(struct m68k_serial * info,
839 struct serial_struct * new_info)
840{
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200841 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct serial_struct new_serial;
843 struct m68k_serial old_info;
844 int retval = 0;
845
846 if (!new_info)
847 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400848 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
849 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 old_info = *info;
851
852 if (!capable(CAP_SYS_ADMIN)) {
853 if ((new_serial.baud_base != info->baud_base) ||
854 (new_serial.type != info->type) ||
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200855 (new_serial.close_delay != port->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200856 ((new_serial.flags & ~ASYNC_USR_MASK) !=
857 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200859 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
860 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 info->custom_divisor = new_serial.custom_divisor;
862 goto check_and_exit;
863 }
864
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200865 if (port->count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EBUSY;
867
868 /*
869 * OK, past this point, all the error checking has been done.
870 * At this point, we start making changes.....
871 */
872
873 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200874 info->flags = ((info->flags & ~ASYNC_FLAGS) |
875 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 info->type = new_serial.type;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200877 port->close_delay = new_serial.close_delay;
878 port->closing_wait = new_serial.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880check_and_exit:
881 retval = startup(info);
882 return retval;
883}
884
885/*
886 * get_lsr_info - get line status register info
887 *
888 * Purpose: Let user call ioctl() to get info when the UART physically
889 * is emptied. On bus types like RS485, the transmitter must
890 * release the bus after transmitting. This must be done when
891 * the transmit shift register is empty, not be done when the
892 * transmit holding register is empty. This functionality
893 * allows an RS485 driver to be written in user space.
894 */
895static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
896{
897#ifdef CONFIG_SERIAL_68328_RTS_CTS
898 m68328_uart *uart = &uart_addr[info->line];
899#endif
900 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000901 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Greg Ungerer727dda82006-06-28 15:54:22 +1000903 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#ifdef CONFIG_SERIAL_68328_RTS_CTS
905 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
906#else
907 status = 0;
908#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000909 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400910 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * This routine sends a break character out the serial port.
915 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700916static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 m68328_uart *uart = &uart_addr[info->line];
919 unsigned long flags;
920 if (!info->port)
921 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000922 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923#ifdef USE_INTS
924 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700925 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 uart->utx.w &= ~UTX_SEND_BREAK;
927#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000928 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Alan Cox6caa76b2011-02-14 16:27:22 +0000931static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned int cmd, unsigned long arg)
933{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
935 int retval;
936
937 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
938 return -ENODEV;
939
940 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
941 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
942 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
943 if (tty->flags & (1 << TTY_IO_ERROR))
944 return -EIO;
945 }
946
947 switch (cmd) {
948 case TCSBRK: /* SVID version: non-zero arg --> no break */
949 retval = tty_check_change(tty);
950 if (retval)
951 return retval;
952 tty_wait_until_sent(tty, 0);
953 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700954 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956 case TCSBRKP: /* support for POSIX tcsendbreak() */
957 retval = tty_check_change(tty);
958 if (retval)
959 return retval;
960 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700961 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400964 return get_serial_info(info,
965 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 case TIOCSSERIAL:
967 return set_serial_info(info,
968 (struct serial_struct *) arg);
969 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400970 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400972 if (copy_to_user((struct m68k_serial *) arg,
973 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 default:
977 return -ENOIOCTLCMD;
978 }
979 return 0;
980}
981
Alan Cox606d0992006-12-08 02:38:45 -0800982static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 change_speed(info);
987
988 if ((old_termios->c_cflag & CRTSCTS) &&
989 !(tty->termios->c_cflag & CRTSCTS)) {
990 tty->hw_stopped = 0;
991 rs_start(tty);
992 }
993
994}
995
996/*
997 * ------------------------------------------------------------
998 * rs_close()
999 *
1000 * This routine is called when the serial port gets closed. First, we
1001 * wait for the last remaining data to be sent. Then, we unlink its
1002 * S structure from the interrupt chain if necessary, and we free
1003 * that IRQ if nothing is left in the chain.
1004 * ------------------------------------------------------------
1005 */
1006static void rs_close(struct tty_struct *tty, struct file * filp)
1007{
1008 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +02001009 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 m68328_uart *uart = &uart_addr[info->line];
1011 unsigned long flags;
1012
1013 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1014 return;
1015
Greg Ungerer727dda82006-06-28 15:54:22 +10001016 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001019 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return;
1021 }
1022
Jiri Slaby86264342012-04-02 13:54:40 +02001023 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * Uh, oh. tty->count is 1, which means that the tty
1026 * structure will be freed. Info->count should always
1027 * be one in these conditions. If it's greater than
1028 * one, we've got real problems, since it means the
1029 * serial port won't be shutdown.
1030 */
1031 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001032 "port->count is %d\n", port->count);
1033 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Jiri Slaby86264342012-04-02 13:54:40 +02001035 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001037 info->line, port->count);
1038 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Jiri Slaby86264342012-04-02 13:54:40 +02001040 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001041 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return;
1043 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001044 info->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * Now we wait for the transmit buffer to clear; and we notify
1047 * the line discipline to only process XON/XOFF characters.
1048 */
1049 tty->closing = 1;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001050 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1051 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * At this point we stop accepting input. To do this, we
1054 * disable the receive line status interrupts, and tell the
1055 * interrupt driver to stop checking the data ready bit in the
1056 * line status register.
1057 */
1058
1059 uart->ustcnt &= ~USTCNT_RXEN;
1060 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1061
1062 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001063 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 tty_ldisc_flush(tty);
1066 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001067 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#warning "This is not and has never been valid so fix it"
1069#if 0
1070 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1071 if (tty->ldisc.close)
1072 (tty->ldisc.close)(tty);
1073 tty->ldisc = ldiscs[N_TTY];
1074 tty->termios->c_line = N_TTY;
1075 if (tty->ldisc.open)
1076 (tty->ldisc.open)(tty);
1077 }
1078#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001079 if (port->blocked_open) {
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001080 if (port->close_delay)
1081 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Jiri Slabyc26f0112012-04-02 13:54:41 +02001082 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001084 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001085 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001086 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1091 */
1092void rs_hangup(struct tty_struct *tty)
1093{
1094 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1095
1096 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1097 return;
1098
1099 rs_flush_buffer(tty);
1100 shutdown(info);
Jiri Slaby86264342012-04-02 13:54:40 +02001101 info->tport.count = 0;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001102 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001103 info->tty = NULL;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001104 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107/*
1108 * ------------------------------------------------------------
1109 * rs_open() and friends
1110 * ------------------------------------------------------------
1111 */
1112static int block_til_ready(struct tty_struct *tty, struct file * filp,
1113 struct m68k_serial *info)
1114{
Jiri Slaby86264342012-04-02 13:54:40 +02001115 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 DECLARE_WAITQUEUE(wait, current);
1117 int retval;
1118 int do_clocal = 0;
1119
1120 /*
1121 * If the device is in the middle of being closed, then block
1122 * until it's done, and then try again.
1123 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001124 if (info->flags & ASYNC_CLOSING) {
Jiri Slabyc26f0112012-04-02 13:54:41 +02001125 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001127 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -EAGAIN;
1129 else
1130 return -ERESTARTSYS;
1131#else
1132 return -EAGAIN;
1133#endif
1134 }
1135
1136 /*
1137 * If non-blocking mode is set, or the port is not enabled,
1138 * then make the check up front and then exit.
1139 */
1140 if ((filp->f_flags & O_NONBLOCK) ||
1141 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001142 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return 0;
1144 }
1145
1146 if (tty->termios->c_cflag & CLOCAL)
1147 do_clocal = 1;
1148
1149 /*
1150 * Block waiting for the carrier detect and the line to become
1151 * free (i.e., not in use by the callout). While we are in
Jiri Slaby86264342012-04-02 13:54:40 +02001152 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * rs_close() knows when to free things. We restore it upon
1154 * exit, either normal or abnormal.
1155 */
1156 retval = 0;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001157 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jiri Slaby86264342012-04-02 13:54:40 +02001159 port->count--;
1160 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 current->state = TASK_INTERRUPTIBLE;
1163 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001164 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001166 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 retval = -EAGAIN;
1168 else
1169 retval = -ERESTARTSYS;
1170#else
1171 retval = -EAGAIN;
1172#endif
1173 break;
1174 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001175 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177 if (signal_pending(current)) {
1178 retval = -ERESTARTSYS;
1179 break;
1180 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001181 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001183 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 current->state = TASK_RUNNING;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001186 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!tty_hung_up_p(filp))
Jiri Slaby86264342012-04-02 13:54:40 +02001188 port->count++;
1189 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 if (retval)
1192 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001193 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195}
1196
1197/*
1198 * This routine is called whenever a serial port is opened. It
1199 * enables interrupts for a serial port, linking in its S structure into
1200 * the IRQ chain. It also performs the serial-specific
1201 * initialization for the tty structure.
1202 */
1203int rs_open(struct tty_struct *tty, struct file * filp)
1204{
1205 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001206 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jiri Slaby410235f2012-03-05 14:52:01 +01001208 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (serial_paranoia_check(info, tty->name, "rs_open"))
1211 return -ENODEV;
1212
Jiri Slaby86264342012-04-02 13:54:40 +02001213 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001215 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /*
1218 * Start up serial port
1219 */
1220 retval = startup(info);
1221 if (retval)
1222 return retval;
1223
1224 return block_til_ready(tty, filp, info);
1225}
1226
1227/* Finally, routines used to initialize the serial driver. */
1228
1229static void show_serial_version(void)
1230{
1231 printk("MC68328 serial driver version 1.00\n");
1232}
1233
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001234static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 .open = rs_open,
1236 .close = rs_close,
1237 .write = rs_write,
1238 .flush_chars = rs_flush_chars,
1239 .write_room = rs_write_room,
1240 .chars_in_buffer = rs_chars_in_buffer,
1241 .flush_buffer = rs_flush_buffer,
1242 .ioctl = rs_ioctl,
1243 .throttle = rs_throttle,
1244 .unthrottle = rs_unthrottle,
1245 .set_termios = rs_set_termios,
1246 .stop = rs_stop,
1247 .start = rs_start,
1248 .hangup = rs_hangup,
1249 .set_ldisc = rs_set_ldisc,
1250};
1251
1252/* rs_init inits the driver */
1253static int __init
1254rs68328_init(void)
1255{
Jiri Slaby107afb72012-04-02 13:54:38 +02001256 unsigned long flags;
1257 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct m68k_serial *info;
1259
1260 serial_driver = alloc_tty_driver(NR_PORTS);
1261 if (!serial_driver)
1262 return -ENOMEM;
1263
1264 show_serial_version();
1265
1266 /* Initialize the tty_driver structure */
1267 /* SPARC: Not all of this is exactly right for us. */
1268
1269 serial_driver->name = "ttyS";
1270 serial_driver->major = TTY_MAJOR;
1271 serial_driver->minor_start = 64;
1272 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1273 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1274 serial_driver->init_termios = tty_std_termios;
1275 serial_driver->init_termios.c_cflag =
1276 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1277 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1278 tty_set_operations(serial_driver, &rs_ops);
1279
1280 if (tty_register_driver(serial_driver)) {
1281 put_tty_driver(serial_driver);
1282 printk(KERN_ERR "Couldn't register serial driver\n");
1283 return -ENOMEM;
1284 }
1285
Greg Ungerer727dda82006-06-28 15:54:22 +10001286 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 for(i=0;i<NR_PORTS;i++) {
1289
1290 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001291 tty_port_init(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 info->magic = SERIAL_MAGIC;
1293 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001294 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 info->irq = uart_irqs[i];
1296 info->custom_divisor = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 info->line = i;
1299 info->is_cons = 1; /* Means shortcuts work */
1300
1301 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1302 info->port, info->irq);
1303 printk(" is a builtin MC68328 UART\n");
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305#ifdef CONFIG_M68VZ328
1306 if (i > 0 )
1307 PJSEL &= 0xCF; /* PSW enable second port output */
1308#endif
1309
1310 if (request_irq(uart_irqs[i],
1311 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001312 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001313 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001316 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320module_init(rs68328_init);
1321
1322
1323
1324static void m68328_set_baud(void)
1325{
1326 unsigned short ustcnt;
1327 int i;
1328
1329 ustcnt = USTCNT;
1330 USTCNT = ustcnt & ~USTCNT_TXEN;
1331
1332again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001333 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (baud_table[i] == m68328_console_baud)
1335 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001336 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 m68328_console_baud = 9600;
1338 goto again;
1339 }
1340
1341 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1342 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1343 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1344 ustcnt |= USTCNT_8_7;
1345 ustcnt |= USTCNT_TXEN;
1346 USTCNT = ustcnt;
1347 m68328_console_initted = 1;
1348 return;
1349}
1350
1351
1352int m68328_console_setup(struct console *cp, char *arg)
1353{
1354 int i, n = CONSOLE_BAUD_RATE;
1355
1356 if (!cp)
1357 return(-1);
1358
1359 if (arg)
1360 n = simple_strtoul(arg,NULL,0);
1361
Thiago Farina8b505ca2009-12-09 12:31:30 -08001362 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (baud_table[i] == n)
1364 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001365 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 m68328_console_baud = n;
1367 m68328_console_cbaud = 0;
1368 if (i > 15) {
1369 m68328_console_cbaud |= CBAUDEX;
1370 i -= 15;
1371 }
1372 m68328_console_cbaud |= i;
1373 }
1374
1375 m68328_set_baud(); /* make sure baud rate changes */
1376 return(0);
1377}
1378
1379
1380static struct tty_driver *m68328_console_device(struct console *c, int *index)
1381{
1382 *index = c->index;
1383 return serial_driver;
1384}
1385
1386
1387void m68328_console_write (struct console *co, const char *str,
1388 unsigned int count)
1389{
1390 if (!m68328_console_initted)
1391 m68328_set_baud();
1392 while (count--) {
1393 if (*str == '\n')
1394 rs_put_char('\r');
1395 rs_put_char( *str++ );
1396 }
1397}
1398
1399
1400static struct console m68328_driver = {
1401 .name = "ttyS",
1402 .write = m68328_console_write,
1403 .device = m68328_console_device,
1404 .setup = m68328_console_setup,
1405 .flags = CON_PRINTBUFFER,
1406 .index = -1,
1407};
1408
1409
1410static int __init m68328_console_init(void)
1411{
1412 register_console(&m68328_driver);
1413 return 0;
1414}
1415
1416console_initcall(m68328_console_init);