blob: 116d932401ed3695189bf9299622e13076e91e4d [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 {
112 char is_cons; /* Is this our console. */
113 int magic;
114 int baud_base;
115 int port;
116 int irq;
117 int flags; /* defined in tty.h */
118 int type; /* UART type */
119 struct tty_struct *tty;
120 int custom_divisor;
121 int x_char; /* xon/xoff character */
122 int close_delay;
123 unsigned short closing_wait;
124 int line;
125 int count; /* # of fd on device */
126 int blocked_open; /* # of blocked opens */
127 unsigned char *xmit_buf;
128 int xmit_head;
129 int xmit_tail;
130 int xmit_cnt;
131 wait_queue_head_t open_wait;
132 wait_queue_head_t close_wait;
133};
134
135#define SERIAL_MAGIC 0x5301
136
137/*
138 * Define the number of ports supported and their irqs.
139 */
140#define NR_PORTS 1
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jiri Slaby1bb26872012-04-02 13:54:39 +0200144static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/* multiple ports are contiguous in memory */
147m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct tty_driver *serial_driver;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void change_speed(struct m68k_serial *info);
152
153/*
154 * Setup for console. Argument comes from the boot command line.
155 */
156
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700157/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
158#ifdef CONFIG_M68VZ328
159#define CONSOLE_BAUD_RATE 19200
160#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#endif
162
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#ifndef CONSOLE_BAUD_RATE
165#define CONSOLE_BAUD_RATE 9600
166#define DEFAULT_CBAUD B9600
167#endif
168
169
170static int m68328_console_initted = 0;
171static int m68328_console_baud = CONSOLE_BAUD_RATE;
172static int m68328_console_cbaud = DEFAULT_CBAUD;
173
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static inline int serial_paranoia_check(struct m68k_serial *info,
176 char *name, const char *routine)
177{
178#ifdef SERIAL_PARANOIA_CHECK
179 static const char *badmagic =
180 "Warning: bad magic number for serial struct %s in %s\n";
181 static const char *badinfo =
182 "Warning: null m68k_serial for %s in %s\n";
183
184 if (!info) {
185 printk(badinfo, name, routine);
186 return 1;
187 }
188 if (info->magic != SERIAL_MAGIC) {
189 printk(badmagic, name, routine);
190 return 1;
191 }
192#endif
193 return 0;
194}
195
196/*
197 * This is used to figure out the divisor speeds and the timeouts
198 */
199static int baud_table[] = {
200 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
201 9600, 19200, 38400, 57600, 115200, 0 };
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* Utility routines */
204static inline int get_baud(struct m68k_serial *ss)
205{
206 unsigned long result = 115200;
207 unsigned short int baud = uart_addr[ss->line].ubaud;
208 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
209 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
210
211 return result;
212}
213
214/*
215 * ------------------------------------------------------------
216 * rs_stop() and rs_start()
217 *
218 * This routines are called before setting or resetting tty->stopped.
219 * They enable or disable transmitter interrupts, as necessary.
220 * ------------------------------------------------------------
221 */
222static void rs_stop(struct tty_struct *tty)
223{
224 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
225 m68328_uart *uart = &uart_addr[info->line];
226 unsigned long flags;
227
228 if (serial_paranoia_check(info, tty->name, "rs_stop"))
229 return;
230
Greg Ungerer727dda82006-06-28 15:54:22 +1000231 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000233 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Alan Cox09a6ffa2008-04-30 00:54:01 -0700236static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Jiri Slaby107afb72012-04-02 13:54:38 +0200238 unsigned long flags;
239 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Greg Ungerer727dda82006-06-28 15:54:22 +1000241 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
244 loops++;
245 udelay(5);
246 }
247
248 UTX_TXDATA = ch;
249 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000250 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700251 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static void rs_start(struct tty_struct *tty)
255{
256 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
257 m68328_uart *uart = &uart_addr[info->line];
258 unsigned long flags;
259
260 if (serial_paranoia_check(info, tty->name, "rs_start"))
261 return;
262
Greg Ungerer727dda82006-06-28 15:54:22 +1000263 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
265#ifdef USE_INTS
266 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
267#else
268 uart->ustcnt |= USTCNT_TXEN;
269#endif
270 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000271 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
David Howells7d12e782006-10-05 14:55:46 +0100274static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000276 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800278 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /*
281 * This do { } while() loop will get ALL chars out of Rx FIFO
282 */
283#ifndef CONFIG_XCOPILOT_BUGS
284 do {
285#endif
286 ch = GET_FIELD(rx, URX_RXDATA);
287
288 if(info->is_cons) {
289 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return;
291#ifdef CONFIG_MAGIC_SYSRQ
292 } else if (ch == 0x10) { /* ^P */
293 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700294 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 show_buffers();
296/* show_net_buffers(); */
297 return;
298 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600299 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return;
301#endif /* CONFIG_MAGIC_SYSRQ */
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305 if(!tty)
306 goto clear_and_exit;
307
Alan Cox33f0f882006-01-09 20:54:13 -0800308 flag = TTY_NORMAL;
309
Jiri Slabyc21e2652012-04-02 13:54:36 +0200310 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800311 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200312 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800313 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200314 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800315 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200316
Alan Cox33f0f882006-01-09 20:54:13 -0800317 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#ifndef CONFIG_XCOPILOT_BUGS
319 } while((rx = uart->urx.w) & URX_DATA_READY);
320#endif
321
Greg Ungerer8e63e662006-02-08 09:19:17 +1000322 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324clear_and_exit:
325 return;
326}
327
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800328static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 m68328_uart *uart = &uart_addr[info->line];
331
332 if (info->x_char) {
333 /* Send next char */
334 uart->utx.b.txdata = info->x_char;
335 info->x_char = 0;
336 goto clear_and_return;
337 }
338
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000339 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* That's peculiar... TX ints off */
341 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
342 goto clear_and_return;
343 }
344
345 /* Send char */
346 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
347 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
348 info->xmit_cnt--;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if(info->xmit_cnt <= 0) {
351 /* All done for now... TX ints off */
352 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
353 goto clear_and_return;
354 }
355
356clear_and_return:
357 /* Clear interrupt (should be auto)*/
358 return;
359}
360
361/*
362 * This is the serial driver's generic interrupt routine
363 */
David Howells7d12e782006-10-05 14:55:46 +0100364irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Alan Cox25db8ad2008-08-19 20:49:40 -0700366 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 m68328_uart *uart;
368 unsigned short rx;
369 unsigned short tx;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 uart = &uart_addr[info->line];
372 rx = uart->urx.w;
373
374#ifdef USE_INTS
375 tx = uart->utx.w;
376
David Howells7d12e782006-10-05 14:55:46 +0100377 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (tx & UTX_TX_AVAIL) transmit_chars(info);
379#else
David Howells7d12e782006-10-05 14:55:46 +0100380 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
382 return IRQ_HANDLED;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static int startup(struct m68k_serial * info)
386{
387 m68328_uart *uart = &uart_addr[info->line];
388 unsigned long flags;
389
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200390 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392
393 if (!info->xmit_buf) {
394 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
395 if (!info->xmit_buf)
396 return -ENOMEM;
397 }
398
Greg Ungerer727dda82006-06-28 15:54:22 +1000399 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /*
402 * Clear the FIFO buffers and disable them
403 * (they will be reenabled in change_speed())
404 */
405
406 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
408 (void)uart->urx.w;
409
410 /*
411 * Finally, enable sequencing and interrupts
412 */
413#ifdef USE_INTS
414 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
415 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
416#else
417 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
418#endif
419
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000420 if (info->tty)
421 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
423
424 /*
425 * and set the speed of the serial port
426 */
427
428 change_speed(info);
429
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200430 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000431 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
435/*
436 * This routine will shutdown a serial port; interrupts are disabled, and
437 * DTR is dropped if the hangup on close termio flag is on.
438 */
439static void shutdown(struct m68k_serial * info)
440{
441 m68328_uart *uart = &uart_addr[info->line];
442 unsigned long flags;
443
444 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200445 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return;
447
Greg Ungerer727dda82006-06-28 15:54:22 +1000448 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (info->xmit_buf) {
451 free_page((unsigned long) info->xmit_buf);
452 info->xmit_buf = 0;
453 }
454
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000455 if (info->tty)
456 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200458 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000459 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462struct {
463 int divisor, prescale;
464}
465#ifndef CONFIG_M68VZ328
466 hw_baud_table[18] = {
467 {0,0}, /* 0 */
468 {0,0}, /* 50 */
469 {0,0}, /* 75 */
470 {0,0}, /* 110 */
471 {0,0}, /* 134 */
472 {0,0}, /* 150 */
473 {0,0}, /* 200 */
474 {7,0x26}, /* 300 */
475 {6,0x26}, /* 600 */
476 {5,0x26}, /* 1200 */
477 {0,0}, /* 1800 */
478 {4,0x26}, /* 2400 */
479 {3,0x26}, /* 4800 */
480 {2,0x26}, /* 9600 */
481 {1,0x26}, /* 19200 */
482 {0,0x26}, /* 38400 */
483 {1,0x38}, /* 57600 */
484 {0,0x38}, /* 115200 */
485};
486#else
487 hw_baud_table[18] = {
488 {0,0}, /* 0 */
489 {0,0}, /* 50 */
490 {0,0}, /* 75 */
491 {0,0}, /* 110 */
492 {0,0}, /* 134 */
493 {0,0}, /* 150 */
494 {0,0}, /* 200 */
495 {0,0}, /* 300 */
496 {7,0x26}, /* 600 */
497 {6,0x26}, /* 1200 */
498 {0,0}, /* 1800 */
499 {5,0x26}, /* 2400 */
500 {4,0x26}, /* 4800 */
501 {3,0x26}, /* 9600 */
502 {2,0x26}, /* 19200 */
503 {1,0x26}, /* 38400 */
504 {0,0x26}, /* 57600 */
505 {1,0x38}, /* 115200 */
506};
507#endif
508/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
509
510/*
511 * This routine is called to set the UART divisor registers to match
512 * the specified baud rate for a serial port.
513 */
514static void change_speed(struct m68k_serial *info)
515{
516 m68328_uart *uart = &uart_addr[info->line];
517 unsigned short port;
518 unsigned short ustcnt;
519 unsigned cflag;
520 int i;
521
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000522 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000524 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!(port = info->port))
526 return;
527
528 ustcnt = uart->ustcnt;
529 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
530
531 i = cflag & CBAUD;
532 if (i & CBAUDEX) {
533 i = (i & ~CBAUDEX) + B38400;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
537 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
538
539 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
540
541 if ((cflag & CSIZE) == CS8)
542 ustcnt |= USTCNT_8_7;
543
544 if (cflag & CSTOPB)
545 ustcnt |= USTCNT_STOP;
546
547 if (cflag & PARENB)
548 ustcnt |= USTCNT_PARITYEN;
549 if (cflag & PARODD)
550 ustcnt |= USTCNT_ODD_EVEN;
551
552#ifdef CONFIG_SERIAL_68328_RTS_CTS
553 if (cflag & CRTSCTS) {
554 uart->utx.w &= ~ UTX_NOCTS;
555 } else {
556 uart->utx.w |= UTX_NOCTS;
557 }
558#endif
559
560 ustcnt |= USTCNT_TXEN;
561
562 uart->ustcnt = ustcnt;
563 return;
564}
565
566/*
567 * Fair output driver allows a process to speak.
568 */
569static void rs_fair_output(void)
570{
571 int left; /* Output no more than that */
572 unsigned long flags;
573 struct m68k_serial *info = &m68k_soft[0];
574 char c;
575
576 if (info == 0) return;
577 if (info->xmit_buf == 0) return;
578
Greg Ungerer727dda82006-06-28 15:54:22 +1000579 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 left = info->xmit_cnt;
581 while (left != 0) {
582 c = info->xmit_buf[info->xmit_tail];
583 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
584 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000585 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 rs_put_char(c);
588
Greg Ungerer727dda82006-06-28 15:54:22 +1000589 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 left = min(info->xmit_cnt, left-1);
591 }
592
593 /* Last character is being transmitted now (hopefully). */
594 udelay(5);
595
Greg Ungerer727dda82006-06-28 15:54:22 +1000596 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return;
598}
599
600/*
601 * m68k_console_print is registered for printk.
602 */
603void console_print_68328(const char *p)
604{
605 char c;
606
607 while((c=*(p++)) != 0) {
608 if(c == '\n')
609 rs_put_char('\r');
610 rs_put_char(c);
611 }
612
613 /* Comment this if you want to have a strict interrupt-driven output */
614 rs_fair_output();
615
616 return;
617}
618
619static void rs_set_ldisc(struct tty_struct *tty)
620{
621 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
622
623 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
624 return;
625
626 info->is_cons = (tty->termios->c_line == N_TTY);
627
628 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
629}
630
631static void rs_flush_chars(struct tty_struct *tty)
632{
633 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
634 m68328_uart *uart = &uart_addr[info->line];
635 unsigned long flags;
636
637 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
638 return;
639#ifndef USE_INTS
640 for(;;) {
641#endif
642
643 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000644 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
647 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000648 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return;
650 }
651
652#ifdef USE_INTS
653 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
654#else
655 uart->ustcnt |= USTCNT_TXEN;
656#endif
657
658#ifdef USE_INTS
659 if (uart->utx.w & UTX_TX_AVAIL) {
660#else
661 if (1) {
662#endif
663 /* Send char */
664 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
665 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
666 info->xmit_cnt--;
667 }
668
669#ifndef USE_INTS
670 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
671 }
672#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000673 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676extern void console_printn(const char * b, int count);
677
678static int rs_write(struct tty_struct * tty,
679 const unsigned char *buf, int count)
680{
681 int c, total = 0;
682 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
683 m68328_uart *uart = &uart_addr[info->line];
684 unsigned long flags;
685
686 if (serial_paranoia_check(info, tty->name, "rs_write"))
687 return 0;
688
689 if (!tty || !info->xmit_buf)
690 return 0;
691
Greg Ungerer727dda82006-06-28 15:54:22 +1000692 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000694 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
696 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000697 local_irq_restore(flags);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (c <= 0)
700 break;
701
702 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000703
704 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
706 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000707 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 buf += c;
709 count -= c;
710 total += c;
711 }
712
713 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
714 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000715 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#ifndef USE_INTS
717 while(info->xmit_cnt) {
718#endif
719
720 uart->ustcnt |= USTCNT_TXEN;
721#ifdef USE_INTS
722 uart->ustcnt |= USTCNT_TX_INTR_MASK;
723#else
724 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
725#endif
726 if (uart->utx.w & UTX_TX_AVAIL) {
727 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
728 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
729 info->xmit_cnt--;
730 }
731
732#ifndef USE_INTS
733 }
734#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000735 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return total;
739}
740
741static int rs_write_room(struct tty_struct *tty)
742{
743 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
744 int ret;
745
746 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
747 return 0;
748 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
749 if (ret < 0)
750 ret = 0;
751 return ret;
752}
753
754static int rs_chars_in_buffer(struct tty_struct *tty)
755{
756 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
757
758 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
759 return 0;
760 return info->xmit_cnt;
761}
762
763static void rs_flush_buffer(struct tty_struct *tty)
764{
765 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000766 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
769 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000770 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000772 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 tty_wakeup(tty);
774}
775
776/*
777 * ------------------------------------------------------------
778 * rs_throttle()
779 *
780 * This routine is called by the upper-layer tty layer to signal that
781 * incoming characters should be throttled.
782 * ------------------------------------------------------------
783 */
784static void rs_throttle(struct tty_struct * tty)
785{
786 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
787
788 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
789 return;
790
791 if (I_IXOFF(tty))
792 info->x_char = STOP_CHAR(tty);
793
794 /* Turn off RTS line (do this atomic) */
795}
796
797static void rs_unthrottle(struct tty_struct * tty)
798{
799 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
800
801 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
802 return;
803
804 if (I_IXOFF(tty)) {
805 if (info->x_char)
806 info->x_char = 0;
807 else
808 info->x_char = START_CHAR(tty);
809 }
810
811 /* Assert RTS line (do this atomic) */
812}
813
814/*
815 * ------------------------------------------------------------
816 * rs_ioctl() and friends
817 * ------------------------------------------------------------
818 */
819
820static int get_serial_info(struct m68k_serial * info,
821 struct serial_struct * retinfo)
822{
823 struct serial_struct tmp;
824
825 if (!retinfo)
826 return -EFAULT;
827 memset(&tmp, 0, sizeof(tmp));
828 tmp.type = info->type;
829 tmp.line = info->line;
830 tmp.port = info->port;
831 tmp.irq = info->irq;
832 tmp.flags = info->flags;
833 tmp.baud_base = info->baud_base;
834 tmp.close_delay = info->close_delay;
835 tmp.closing_wait = info->closing_wait;
836 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400837 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
838 return -EFAULT;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841}
842
843static int set_serial_info(struct m68k_serial * info,
844 struct serial_struct * new_info)
845{
846 struct serial_struct new_serial;
847 struct m68k_serial old_info;
848 int retval = 0;
849
850 if (!new_info)
851 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400852 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
853 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 old_info = *info;
855
856 if (!capable(CAP_SYS_ADMIN)) {
857 if ((new_serial.baud_base != info->baud_base) ||
858 (new_serial.type != info->type) ||
859 (new_serial.close_delay != info->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200860 ((new_serial.flags & ~ASYNC_USR_MASK) !=
861 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200863 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
864 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 info->custom_divisor = new_serial.custom_divisor;
866 goto check_and_exit;
867 }
868
869 if (info->count > 1)
870 return -EBUSY;
871
872 /*
873 * OK, past this point, all the error checking has been done.
874 * At this point, we start making changes.....
875 */
876
877 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200878 info->flags = ((info->flags & ~ASYNC_FLAGS) |
879 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 info->type = new_serial.type;
881 info->close_delay = new_serial.close_delay;
882 info->closing_wait = new_serial.closing_wait;
883
884check_and_exit:
885 retval = startup(info);
886 return retval;
887}
888
889/*
890 * get_lsr_info - get line status register info
891 *
892 * Purpose: Let user call ioctl() to get info when the UART physically
893 * is emptied. On bus types like RS485, the transmitter must
894 * release the bus after transmitting. This must be done when
895 * the transmit shift register is empty, not be done when the
896 * transmit holding register is empty. This functionality
897 * allows an RS485 driver to be written in user space.
898 */
899static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
900{
901#ifdef CONFIG_SERIAL_68328_RTS_CTS
902 m68328_uart *uart = &uart_addr[info->line];
903#endif
904 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000905 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Greg Ungerer727dda82006-06-28 15:54:22 +1000907 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#ifdef CONFIG_SERIAL_68328_RTS_CTS
909 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
910#else
911 status = 0;
912#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000913 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400914 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/*
918 * This routine sends a break character out the serial port.
919 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700920static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 m68328_uart *uart = &uart_addr[info->line];
923 unsigned long flags;
924 if (!info->port)
925 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000926 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#ifdef USE_INTS
928 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700929 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 uart->utx.w &= ~UTX_SEND_BREAK;
931#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000932 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Alan Cox6caa76b2011-02-14 16:27:22 +0000935static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unsigned int cmd, unsigned long arg)
937{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
939 int retval;
940
941 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
942 return -ENODEV;
943
944 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
945 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
946 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
947 if (tty->flags & (1 << TTY_IO_ERROR))
948 return -EIO;
949 }
950
951 switch (cmd) {
952 case TCSBRK: /* SVID version: non-zero arg --> no break */
953 retval = tty_check_change(tty);
954 if (retval)
955 return retval;
956 tty_wait_until_sent(tty, 0);
957 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700958 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960 case TCSBRKP: /* support for POSIX tcsendbreak() */
961 retval = tty_check_change(tty);
962 if (retval)
963 return retval;
964 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700965 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400968 return get_serial_info(info,
969 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 case TIOCSSERIAL:
971 return set_serial_info(info,
972 (struct serial_struct *) arg);
973 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400974 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400976 if (copy_to_user((struct m68k_serial *) arg,
977 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 default:
981 return -ENOIOCTLCMD;
982 }
983 return 0;
984}
985
Alan Cox606d0992006-12-08 02:38:45 -0800986static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 change_speed(info);
991
992 if ((old_termios->c_cflag & CRTSCTS) &&
993 !(tty->termios->c_cflag & CRTSCTS)) {
994 tty->hw_stopped = 0;
995 rs_start(tty);
996 }
997
998}
999
1000/*
1001 * ------------------------------------------------------------
1002 * rs_close()
1003 *
1004 * This routine is called when the serial port gets closed. First, we
1005 * wait for the last remaining data to be sent. Then, we unlink its
1006 * S structure from the interrupt chain if necessary, and we free
1007 * that IRQ if nothing is left in the chain.
1008 * ------------------------------------------------------------
1009 */
1010static void rs_close(struct tty_struct *tty, struct file * filp)
1011{
1012 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1013 m68328_uart *uart = &uart_addr[info->line];
1014 unsigned long flags;
1015
1016 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1017 return;
1018
Greg Ungerer727dda82006-06-28 15:54:22 +10001019 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001022 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return;
1024 }
1025
1026 if ((tty->count == 1) && (info->count != 1)) {
1027 /*
1028 * Uh, oh. tty->count is 1, which means that the tty
1029 * structure will be freed. Info->count should always
1030 * be one in these conditions. If it's greater than
1031 * one, we've got real problems, since it means the
1032 * serial port won't be shutdown.
1033 */
1034 printk("rs_close: bad serial port count; tty->count is 1, "
1035 "info->count is %d\n", info->count);
1036 info->count = 1;
1037 }
1038 if (--info->count < 0) {
1039 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1040 info->line, info->count);
1041 info->count = 0;
1042 }
1043 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001044 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return;
1046 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001047 info->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Now we wait for the transmit buffer to clear; and we notify
1050 * the line discipline to only process XON/XOFF characters.
1051 */
1052 tty->closing = 1;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001053 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 tty_wait_until_sent(tty, info->closing_wait);
1055 /*
1056 * At this point we stop accepting input. To do this, we
1057 * disable the receive line status interrupts, and tell the
1058 * interrupt driver to stop checking the data ready bit in the
1059 * line status register.
1060 */
1061
1062 uart->ustcnt &= ~USTCNT_RXEN;
1063 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1064
1065 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001066 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 tty_ldisc_flush(tty);
1069 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001070 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#warning "This is not and has never been valid so fix it"
1072#if 0
1073 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1074 if (tty->ldisc.close)
1075 (tty->ldisc.close)(tty);
1076 tty->ldisc = ldiscs[N_TTY];
1077 tty->termios->c_line = N_TTY;
1078 if (tty->ldisc.open)
1079 (tty->ldisc.open)(tty);
1080 }
1081#endif
1082 if (info->blocked_open) {
1083 if (info->close_delay) {
1084 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1085 }
1086 wake_up_interruptible(&info->open_wait);
1087 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001088 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001090 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093/*
1094 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1095 */
1096void rs_hangup(struct tty_struct *tty)
1097{
1098 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1099
1100 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1101 return;
1102
1103 rs_flush_buffer(tty);
1104 shutdown(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 info->count = 0;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001106 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001107 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 wake_up_interruptible(&info->open_wait);
1109}
1110
1111/*
1112 * ------------------------------------------------------------
1113 * rs_open() and friends
1114 * ------------------------------------------------------------
1115 */
1116static int block_til_ready(struct tty_struct *tty, struct file * filp,
1117 struct m68k_serial *info)
1118{
1119 DECLARE_WAITQUEUE(wait, current);
1120 int retval;
1121 int do_clocal = 0;
1122
1123 /*
1124 * If the device is in the middle of being closed, then block
1125 * until it's done, and then try again.
1126 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001127 if (info->flags & ASYNC_CLOSING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 interruptible_sleep_on(&info->close_wait);
1129#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001130 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -EAGAIN;
1132 else
1133 return -ERESTARTSYS;
1134#else
1135 return -EAGAIN;
1136#endif
1137 }
1138
1139 /*
1140 * If non-blocking mode is set, or the port is not enabled,
1141 * then make the check up front and then exit.
1142 */
1143 if ((filp->f_flags & O_NONBLOCK) ||
1144 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001145 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147 }
1148
1149 if (tty->termios->c_cflag & CLOCAL)
1150 do_clocal = 1;
1151
1152 /*
1153 * Block waiting for the carrier detect and the line to become
1154 * free (i.e., not in use by the callout). While we are in
1155 * this loop, info->count is dropped by one, so that
1156 * rs_close() knows when to free things. We restore it upon
1157 * exit, either normal or abnormal.
1158 */
1159 retval = 0;
1160 add_wait_queue(&info->open_wait, &wait);
1161
1162 info->count--;
1163 info->blocked_open++;
1164 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 current->state = TASK_INTERRUPTIBLE;
1166 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001167 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001169 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 retval = -EAGAIN;
1171 else
1172 retval = -ERESTARTSYS;
1173#else
1174 retval = -EAGAIN;
1175#endif
1176 break;
1177 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001178 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 if (signal_pending(current)) {
1181 retval = -ERESTARTSYS;
1182 break;
1183 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001184 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001186 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 current->state = TASK_RUNNING;
1189 remove_wait_queue(&info->open_wait, &wait);
1190 if (!tty_hung_up_p(filp))
1191 info->count++;
1192 info->blocked_open--;
1193
1194 if (retval)
1195 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001196 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198}
1199
1200/*
1201 * This routine is called whenever a serial port is opened. It
1202 * enables interrupts for a serial port, linking in its S structure into
1203 * the IRQ chain. It also performs the serial-specific
1204 * initialization for the tty structure.
1205 */
1206int rs_open(struct tty_struct *tty, struct file * filp)
1207{
1208 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001209 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Jiri Slaby410235f2012-03-05 14:52:01 +01001211 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (serial_paranoia_check(info, tty->name, "rs_open"))
1214 return -ENODEV;
1215
1216 info->count++;
1217 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001218 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * Start up serial port
1222 */
1223 retval = startup(info);
1224 if (retval)
1225 return retval;
1226
1227 return block_til_ready(tty, filp, info);
1228}
1229
1230/* Finally, routines used to initialize the serial driver. */
1231
1232static void show_serial_version(void)
1233{
1234 printk("MC68328 serial driver version 1.00\n");
1235}
1236
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001237static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .open = rs_open,
1239 .close = rs_close,
1240 .write = rs_write,
1241 .flush_chars = rs_flush_chars,
1242 .write_room = rs_write_room,
1243 .chars_in_buffer = rs_chars_in_buffer,
1244 .flush_buffer = rs_flush_buffer,
1245 .ioctl = rs_ioctl,
1246 .throttle = rs_throttle,
1247 .unthrottle = rs_unthrottle,
1248 .set_termios = rs_set_termios,
1249 .stop = rs_stop,
1250 .start = rs_start,
1251 .hangup = rs_hangup,
1252 .set_ldisc = rs_set_ldisc,
1253};
1254
1255/* rs_init inits the driver */
1256static int __init
1257rs68328_init(void)
1258{
Jiri Slaby107afb72012-04-02 13:54:38 +02001259 unsigned long flags;
1260 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct m68k_serial *info;
1262
1263 serial_driver = alloc_tty_driver(NR_PORTS);
1264 if (!serial_driver)
1265 return -ENOMEM;
1266
1267 show_serial_version();
1268
1269 /* Initialize the tty_driver structure */
1270 /* SPARC: Not all of this is exactly right for us. */
1271
1272 serial_driver->name = "ttyS";
1273 serial_driver->major = TTY_MAJOR;
1274 serial_driver->minor_start = 64;
1275 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1276 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1277 serial_driver->init_termios = tty_std_termios;
1278 serial_driver->init_termios.c_cflag =
1279 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1280 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1281 tty_set_operations(serial_driver, &rs_ops);
1282
1283 if (tty_register_driver(serial_driver)) {
1284 put_tty_driver(serial_driver);
1285 printk(KERN_ERR "Couldn't register serial driver\n");
1286 return -ENOMEM;
1287 }
1288
Greg Ungerer727dda82006-06-28 15:54:22 +10001289 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 for(i=0;i<NR_PORTS;i++) {
1292
1293 info = &m68k_soft[i];
1294 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->count = 0;
1303 info->blocked_open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 init_waitqueue_head(&info->open_wait);
1305 init_waitqueue_head(&info->close_wait);
1306 info->line = i;
1307 info->is_cons = 1; /* Means shortcuts work */
1308
1309 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1310 info->port, info->irq);
1311 printk(" is a builtin MC68328 UART\n");
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#ifdef CONFIG_M68VZ328
1314 if (i > 0 )
1315 PJSEL &= 0xCF; /* PSW enable second port output */
1316#endif
1317
1318 if (request_irq(uart_irqs[i],
1319 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001320 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001321 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001324 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328module_init(rs68328_init);
1329
1330
1331
1332static void m68328_set_baud(void)
1333{
1334 unsigned short ustcnt;
1335 int i;
1336
1337 ustcnt = USTCNT;
1338 USTCNT = ustcnt & ~USTCNT_TXEN;
1339
1340again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001341 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (baud_table[i] == m68328_console_baud)
1343 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001344 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 m68328_console_baud = 9600;
1346 goto again;
1347 }
1348
1349 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1350 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1351 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1352 ustcnt |= USTCNT_8_7;
1353 ustcnt |= USTCNT_TXEN;
1354 USTCNT = ustcnt;
1355 m68328_console_initted = 1;
1356 return;
1357}
1358
1359
1360int m68328_console_setup(struct console *cp, char *arg)
1361{
1362 int i, n = CONSOLE_BAUD_RATE;
1363
1364 if (!cp)
1365 return(-1);
1366
1367 if (arg)
1368 n = simple_strtoul(arg,NULL,0);
1369
Thiago Farina8b505ca2009-12-09 12:31:30 -08001370 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (baud_table[i] == n)
1372 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001373 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 m68328_console_baud = n;
1375 m68328_console_cbaud = 0;
1376 if (i > 15) {
1377 m68328_console_cbaud |= CBAUDEX;
1378 i -= 15;
1379 }
1380 m68328_console_cbaud |= i;
1381 }
1382
1383 m68328_set_baud(); /* make sure baud rate changes */
1384 return(0);
1385}
1386
1387
1388static struct tty_driver *m68328_console_device(struct console *c, int *index)
1389{
1390 *index = c->index;
1391 return serial_driver;
1392}
1393
1394
1395void m68328_console_write (struct console *co, const char *str,
1396 unsigned int count)
1397{
1398 if (!m68328_console_initted)
1399 m68328_set_baud();
1400 while (count--) {
1401 if (*str == '\n')
1402 rs_put_char('\r');
1403 rs_put_char( *str++ );
1404 }
1405}
1406
1407
1408static struct console m68328_driver = {
1409 .name = "ttyS",
1410 .write = m68328_console_write,
1411 .device = m68328_console_device,
1412 .setup = m68328_console_setup,
1413 .flags = CON_PRINTBUFFER,
1414 .index = -1,
1415};
1416
1417
1418static int __init m68328_console_init(void)
1419{
1420 register_console(&m68328_driver);
1421 return 0;
1422}
1423
1424console_initcall(m68328_console_init);