blob: 848662c3b16e1cb2459253802e6527ab15788d2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 68328serial.c: Serial port driver for 68328 microcontroller
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
9 *
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
15 */
16
17#include <asm/dbg.h>
18#include <linux/module.h>
19#include <linux/errno.h>
Jiri Slabycea4b2c2012-04-02 13:54:35 +020020#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/timer.h>
24#include <linux/interrupt.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/major.h>
28#include <linux/string.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/kernel.h>
32#include <linux/console.h>
33#include <linux/reboot.h>
34#include <linux/keyboard.h>
35#include <linux/init.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/io.h>
42#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Turn off usage of real serial interrupt code, to "support" Copilot */
61#ifdef CONFIG_XCOPILOT_BUGS
62#undef USE_INTS
63#else
64#define USE_INTS
65#endif
66
Jiri Slaby1bb26872012-04-02 13:54:39 +020067/*
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
74 */
75#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
76
77/*
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
83 *
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
87 *
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
90 *
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
94 */
95
96/* (es) */
97#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99#elif defined(CONFIG_M68328)
100#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
101#else
102#error Please, define the Rx interrupt events for your CPU
103#endif
104/* (/es) */
105
106/*
107 * This is our internal structure for each serial port's state.
108 *
109 * For definitions of the flags field, see serial.h
110 */
111struct m68k_serial {
Jiri Slaby86264342012-04-02 13:54:40 +0200112 struct tty_port tport;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200113 char is_cons; /* Is this our console. */
114 int magic;
115 int baud_base;
116 int port;
117 int irq;
118 int flags; /* defined in tty.h */
119 int type; /* UART type */
120 struct tty_struct *tty;
121 int custom_divisor;
122 int x_char; /* xon/xoff character */
123 int close_delay;
124 unsigned short closing_wait;
125 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200126 unsigned char *xmit_buf;
127 int xmit_head;
128 int xmit_tail;
129 int xmit_cnt;
130 wait_queue_head_t open_wait;
131 wait_queue_head_t close_wait;
132};
133
134#define SERIAL_MAGIC 0x5301
135
136/*
137 * Define the number of ports supported and their irqs.
138 */
139#define NR_PORTS 1
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jiri Slaby1bb26872012-04-02 13:54:39 +0200143static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* multiple ports are contiguous in memory */
146m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148struct tty_driver *serial_driver;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static void change_speed(struct m68k_serial *info);
151
152/*
153 * Setup for console. Argument comes from the boot command line.
154 */
155
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700156/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
157#ifdef CONFIG_M68VZ328
158#define CONSOLE_BAUD_RATE 19200
159#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
161
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#ifndef CONSOLE_BAUD_RATE
164#define CONSOLE_BAUD_RATE 9600
165#define DEFAULT_CBAUD B9600
166#endif
167
168
169static int m68328_console_initted = 0;
170static int m68328_console_baud = CONSOLE_BAUD_RATE;
171static int m68328_console_cbaud = DEFAULT_CBAUD;
172
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static inline int serial_paranoia_check(struct m68k_serial *info,
175 char *name, const char *routine)
176{
177#ifdef SERIAL_PARANOIA_CHECK
178 static const char *badmagic =
179 "Warning: bad magic number for serial struct %s in %s\n";
180 static const char *badinfo =
181 "Warning: null m68k_serial for %s in %s\n";
182
183 if (!info) {
184 printk(badinfo, name, routine);
185 return 1;
186 }
187 if (info->magic != SERIAL_MAGIC) {
188 printk(badmagic, name, routine);
189 return 1;
190 }
191#endif
192 return 0;
193}
194
195/*
196 * This is used to figure out the divisor speeds and the timeouts
197 */
198static int baud_table[] = {
199 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
200 9600, 19200, 38400, 57600, 115200, 0 };
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* Utility routines */
203static inline int get_baud(struct m68k_serial *ss)
204{
205 unsigned long result = 115200;
206 unsigned short int baud = uart_addr[ss->line].ubaud;
207 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
208 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
209
210 return result;
211}
212
213/*
214 * ------------------------------------------------------------
215 * rs_stop() and rs_start()
216 *
217 * This routines are called before setting or resetting tty->stopped.
218 * They enable or disable transmitter interrupts, as necessary.
219 * ------------------------------------------------------------
220 */
221static void rs_stop(struct tty_struct *tty)
222{
223 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
224 m68328_uart *uart = &uart_addr[info->line];
225 unsigned long flags;
226
227 if (serial_paranoia_check(info, tty->name, "rs_stop"))
228 return;
229
Greg Ungerer727dda82006-06-28 15:54:22 +1000230 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000232 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Alan Cox09a6ffa2008-04-30 00:54:01 -0700235static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jiri Slaby107afb72012-04-02 13:54:38 +0200237 unsigned long flags;
238 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Greg Ungerer727dda82006-06-28 15:54:22 +1000240 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
243 loops++;
244 udelay(5);
245 }
246
247 UTX_TXDATA = ch;
248 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000249 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700250 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static void rs_start(struct tty_struct *tty)
254{
255 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
256 m68328_uart *uart = &uart_addr[info->line];
257 unsigned long flags;
258
259 if (serial_paranoia_check(info, tty->name, "rs_start"))
260 return;
261
Greg Ungerer727dda82006-06-28 15:54:22 +1000262 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
264#ifdef USE_INTS
265 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
266#else
267 uart->ustcnt |= USTCNT_TXEN;
268#endif
269 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000270 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
David Howells7d12e782006-10-05 14:55:46 +0100273static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000275 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800277 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
280 * This do { } while() loop will get ALL chars out of Rx FIFO
281 */
282#ifndef CONFIG_XCOPILOT_BUGS
283 do {
284#endif
285 ch = GET_FIELD(rx, URX_RXDATA);
286
287 if(info->is_cons) {
288 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return;
290#ifdef CONFIG_MAGIC_SYSRQ
291 } else if (ch == 0x10) { /* ^P */
292 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700293 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 show_buffers();
295/* show_net_buffers(); */
296 return;
297 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600298 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return;
300#endif /* CONFIG_MAGIC_SYSRQ */
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 if(!tty)
305 goto clear_and_exit;
306
Alan Cox33f0f882006-01-09 20:54:13 -0800307 flag = TTY_NORMAL;
308
Jiri Slabyc21e2652012-04-02 13:54:36 +0200309 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800310 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200311 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800312 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200313 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800314 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200315
Alan Cox33f0f882006-01-09 20:54:13 -0800316 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#ifndef CONFIG_XCOPILOT_BUGS
318 } while((rx = uart->urx.w) & URX_DATA_READY);
319#endif
320
Greg Ungerer8e63e662006-02-08 09:19:17 +1000321 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323clear_and_exit:
324 return;
325}
326
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800327static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 m68328_uart *uart = &uart_addr[info->line];
330
331 if (info->x_char) {
332 /* Send next char */
333 uart->utx.b.txdata = info->x_char;
334 info->x_char = 0;
335 goto clear_and_return;
336 }
337
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000338 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* That's peculiar... TX ints off */
340 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
341 goto clear_and_return;
342 }
343
344 /* Send char */
345 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
346 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
347 info->xmit_cnt--;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if(info->xmit_cnt <= 0) {
350 /* All done for now... TX ints off */
351 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
352 goto clear_and_return;
353 }
354
355clear_and_return:
356 /* Clear interrupt (should be auto)*/
357 return;
358}
359
360/*
361 * This is the serial driver's generic interrupt routine
362 */
David Howells7d12e782006-10-05 14:55:46 +0100363irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Alan Cox25db8ad2008-08-19 20:49:40 -0700365 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 m68328_uart *uart;
367 unsigned short rx;
368 unsigned short tx;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 uart = &uart_addr[info->line];
371 rx = uart->urx.w;
372
373#ifdef USE_INTS
374 tx = uart->utx.w;
375
David Howells7d12e782006-10-05 14:55:46 +0100376 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (tx & UTX_TX_AVAIL) transmit_chars(info);
378#else
David Howells7d12e782006-10-05 14:55:46 +0100379 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#endif
381 return IRQ_HANDLED;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static int startup(struct m68k_serial * info)
385{
386 m68328_uart *uart = &uart_addr[info->line];
387 unsigned long flags;
388
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200389 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391
392 if (!info->xmit_buf) {
393 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
394 if (!info->xmit_buf)
395 return -ENOMEM;
396 }
397
Greg Ungerer727dda82006-06-28 15:54:22 +1000398 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Clear the FIFO buffers and disable them
402 * (they will be reenabled in change_speed())
403 */
404
405 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
407 (void)uart->urx.w;
408
409 /*
410 * Finally, enable sequencing and interrupts
411 */
412#ifdef USE_INTS
413 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
414 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
415#else
416 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
417#endif
418
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000419 if (info->tty)
420 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
422
423 /*
424 * and set the speed of the serial port
425 */
426
427 change_speed(info);
428
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200429 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000430 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434/*
435 * This routine will shutdown a serial port; interrupts are disabled, and
436 * DTR is dropped if the hangup on close termio flag is on.
437 */
438static void shutdown(struct m68k_serial * info)
439{
440 m68328_uart *uart = &uart_addr[info->line];
441 unsigned long flags;
442
443 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200444 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return;
446
Greg Ungerer727dda82006-06-28 15:54:22 +1000447 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (info->xmit_buf) {
450 free_page((unsigned long) info->xmit_buf);
451 info->xmit_buf = 0;
452 }
453
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000454 if (info->tty)
455 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200457 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000458 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461struct {
462 int divisor, prescale;
463}
464#ifndef CONFIG_M68VZ328
465 hw_baud_table[18] = {
466 {0,0}, /* 0 */
467 {0,0}, /* 50 */
468 {0,0}, /* 75 */
469 {0,0}, /* 110 */
470 {0,0}, /* 134 */
471 {0,0}, /* 150 */
472 {0,0}, /* 200 */
473 {7,0x26}, /* 300 */
474 {6,0x26}, /* 600 */
475 {5,0x26}, /* 1200 */
476 {0,0}, /* 1800 */
477 {4,0x26}, /* 2400 */
478 {3,0x26}, /* 4800 */
479 {2,0x26}, /* 9600 */
480 {1,0x26}, /* 19200 */
481 {0,0x26}, /* 38400 */
482 {1,0x38}, /* 57600 */
483 {0,0x38}, /* 115200 */
484};
485#else
486 hw_baud_table[18] = {
487 {0,0}, /* 0 */
488 {0,0}, /* 50 */
489 {0,0}, /* 75 */
490 {0,0}, /* 110 */
491 {0,0}, /* 134 */
492 {0,0}, /* 150 */
493 {0,0}, /* 200 */
494 {0,0}, /* 300 */
495 {7,0x26}, /* 600 */
496 {6,0x26}, /* 1200 */
497 {0,0}, /* 1800 */
498 {5,0x26}, /* 2400 */
499 {4,0x26}, /* 4800 */
500 {3,0x26}, /* 9600 */
501 {2,0x26}, /* 19200 */
502 {1,0x26}, /* 38400 */
503 {0,0x26}, /* 57600 */
504 {1,0x38}, /* 115200 */
505};
506#endif
507/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
508
509/*
510 * This routine is called to set the UART divisor registers to match
511 * the specified baud rate for a serial port.
512 */
513static void change_speed(struct m68k_serial *info)
514{
515 m68328_uart *uart = &uart_addr[info->line];
516 unsigned short port;
517 unsigned short ustcnt;
518 unsigned cflag;
519 int i;
520
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000521 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000523 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!(port = info->port))
525 return;
526
527 ustcnt = uart->ustcnt;
528 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
529
530 i = cflag & CBAUD;
531 if (i & CBAUDEX) {
532 i = (i & ~CBAUDEX) + B38400;
533 }
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
536 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
537
538 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
539
540 if ((cflag & CSIZE) == CS8)
541 ustcnt |= USTCNT_8_7;
542
543 if (cflag & CSTOPB)
544 ustcnt |= USTCNT_STOP;
545
546 if (cflag & PARENB)
547 ustcnt |= USTCNT_PARITYEN;
548 if (cflag & PARODD)
549 ustcnt |= USTCNT_ODD_EVEN;
550
551#ifdef CONFIG_SERIAL_68328_RTS_CTS
552 if (cflag & CRTSCTS) {
553 uart->utx.w &= ~ UTX_NOCTS;
554 } else {
555 uart->utx.w |= UTX_NOCTS;
556 }
557#endif
558
559 ustcnt |= USTCNT_TXEN;
560
561 uart->ustcnt = ustcnt;
562 return;
563}
564
565/*
566 * Fair output driver allows a process to speak.
567 */
568static void rs_fair_output(void)
569{
570 int left; /* Output no more than that */
571 unsigned long flags;
572 struct m68k_serial *info = &m68k_soft[0];
573 char c;
574
575 if (info == 0) return;
576 if (info->xmit_buf == 0) return;
577
Greg Ungerer727dda82006-06-28 15:54:22 +1000578 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 left = info->xmit_cnt;
580 while (left != 0) {
581 c = info->xmit_buf[info->xmit_tail];
582 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
583 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000584 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 rs_put_char(c);
587
Greg Ungerer727dda82006-06-28 15:54:22 +1000588 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 left = min(info->xmit_cnt, left-1);
590 }
591
592 /* Last character is being transmitted now (hopefully). */
593 udelay(5);
594
Greg Ungerer727dda82006-06-28 15:54:22 +1000595 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597}
598
599/*
600 * m68k_console_print is registered for printk.
601 */
602void console_print_68328(const char *p)
603{
604 char c;
605
606 while((c=*(p++)) != 0) {
607 if(c == '\n')
608 rs_put_char('\r');
609 rs_put_char(c);
610 }
611
612 /* Comment this if you want to have a strict interrupt-driven output */
613 rs_fair_output();
614
615 return;
616}
617
618static void rs_set_ldisc(struct tty_struct *tty)
619{
620 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
621
622 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
623 return;
624
625 info->is_cons = (tty->termios->c_line == N_TTY);
626
627 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
628}
629
630static void rs_flush_chars(struct tty_struct *tty)
631{
632 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
633 m68328_uart *uart = &uart_addr[info->line];
634 unsigned long flags;
635
636 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
637 return;
638#ifndef USE_INTS
639 for(;;) {
640#endif
641
642 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000643 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
646 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000647 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
649 }
650
651#ifdef USE_INTS
652 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
653#else
654 uart->ustcnt |= USTCNT_TXEN;
655#endif
656
657#ifdef USE_INTS
658 if (uart->utx.w & UTX_TX_AVAIL) {
659#else
660 if (1) {
661#endif
662 /* Send char */
663 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
664 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
665 info->xmit_cnt--;
666 }
667
668#ifndef USE_INTS
669 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
670 }
671#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000672 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675extern void console_printn(const char * b, int count);
676
677static int rs_write(struct tty_struct * tty,
678 const unsigned char *buf, int count)
679{
680 int c, total = 0;
681 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
682 m68328_uart *uart = &uart_addr[info->line];
683 unsigned long flags;
684
685 if (serial_paranoia_check(info, tty->name, "rs_write"))
686 return 0;
687
688 if (!tty || !info->xmit_buf)
689 return 0;
690
Greg Ungerer727dda82006-06-28 15:54:22 +1000691 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000693 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
695 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000696 local_irq_restore(flags);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (c <= 0)
699 break;
700
701 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000702
703 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
705 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000706 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 buf += c;
708 count -= c;
709 total += c;
710 }
711
712 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
713 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000714 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715#ifndef USE_INTS
716 while(info->xmit_cnt) {
717#endif
718
719 uart->ustcnt |= USTCNT_TXEN;
720#ifdef USE_INTS
721 uart->ustcnt |= USTCNT_TX_INTR_MASK;
722#else
723 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
724#endif
725 if (uart->utx.w & UTX_TX_AVAIL) {
726 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
727 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
728 info->xmit_cnt--;
729 }
730
731#ifndef USE_INTS
732 }
733#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000734 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return total;
738}
739
740static int rs_write_room(struct tty_struct *tty)
741{
742 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
743 int ret;
744
745 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
746 return 0;
747 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
748 if (ret < 0)
749 ret = 0;
750 return ret;
751}
752
753static int rs_chars_in_buffer(struct tty_struct *tty)
754{
755 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
756
757 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
758 return 0;
759 return info->xmit_cnt;
760}
761
762static void rs_flush_buffer(struct tty_struct *tty)
763{
764 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000765 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
768 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000769 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000771 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 tty_wakeup(tty);
773}
774
775/*
776 * ------------------------------------------------------------
777 * rs_throttle()
778 *
779 * This routine is called by the upper-layer tty layer to signal that
780 * incoming characters should be throttled.
781 * ------------------------------------------------------------
782 */
783static void rs_throttle(struct tty_struct * tty)
784{
785 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
786
787 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
788 return;
789
790 if (I_IXOFF(tty))
791 info->x_char = STOP_CHAR(tty);
792
793 /* Turn off RTS line (do this atomic) */
794}
795
796static void rs_unthrottle(struct tty_struct * tty)
797{
798 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
799
800 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
801 return;
802
803 if (I_IXOFF(tty)) {
804 if (info->x_char)
805 info->x_char = 0;
806 else
807 info->x_char = START_CHAR(tty);
808 }
809
810 /* Assert RTS line (do this atomic) */
811}
812
813/*
814 * ------------------------------------------------------------
815 * rs_ioctl() and friends
816 * ------------------------------------------------------------
817 */
818
819static int get_serial_info(struct m68k_serial * info,
820 struct serial_struct * retinfo)
821{
822 struct serial_struct tmp;
823
824 if (!retinfo)
825 return -EFAULT;
826 memset(&tmp, 0, sizeof(tmp));
827 tmp.type = info->type;
828 tmp.line = info->line;
829 tmp.port = info->port;
830 tmp.irq = info->irq;
831 tmp.flags = info->flags;
832 tmp.baud_base = info->baud_base;
833 tmp.close_delay = info->close_delay;
834 tmp.closing_wait = info->closing_wait;
835 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400836 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
837 return -EFAULT;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840}
841
842static int set_serial_info(struct m68k_serial * info,
843 struct serial_struct * new_info)
844{
845 struct serial_struct new_serial;
846 struct m68k_serial old_info;
847 int retval = 0;
848
849 if (!new_info)
850 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400851 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
852 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 old_info = *info;
854
855 if (!capable(CAP_SYS_ADMIN)) {
856 if ((new_serial.baud_base != info->baud_base) ||
857 (new_serial.type != info->type) ||
858 (new_serial.close_delay != info->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200859 ((new_serial.flags & ~ASYNC_USR_MASK) !=
860 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200862 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
863 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 info->custom_divisor = new_serial.custom_divisor;
865 goto check_and_exit;
866 }
867
Jiri Slaby86264342012-04-02 13:54:40 +0200868 if (info->tport.count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EBUSY;
870
871 /*
872 * OK, past this point, all the error checking has been done.
873 * At this point, we start making changes.....
874 */
875
876 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200877 info->flags = ((info->flags & ~ASYNC_FLAGS) |
878 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 info->type = new_serial.type;
880 info->close_delay = new_serial.close_delay;
881 info->closing_wait = new_serial.closing_wait;
882
883check_and_exit:
884 retval = startup(info);
885 return retval;
886}
887
888/*
889 * get_lsr_info - get line status register info
890 *
891 * Purpose: Let user call ioctl() to get info when the UART physically
892 * is emptied. On bus types like RS485, the transmitter must
893 * release the bus after transmitting. This must be done when
894 * the transmit shift register is empty, not be done when the
895 * transmit holding register is empty. This functionality
896 * allows an RS485 driver to be written in user space.
897 */
898static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
899{
900#ifdef CONFIG_SERIAL_68328_RTS_CTS
901 m68328_uart *uart = &uart_addr[info->line];
902#endif
903 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000904 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Greg Ungerer727dda82006-06-28 15:54:22 +1000906 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#ifdef CONFIG_SERIAL_68328_RTS_CTS
908 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
909#else
910 status = 0;
911#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000912 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400913 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916/*
917 * This routine sends a break character out the serial port.
918 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700919static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 m68328_uart *uart = &uart_addr[info->line];
922 unsigned long flags;
923 if (!info->port)
924 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000925 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926#ifdef USE_INTS
927 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700928 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 uart->utx.w &= ~UTX_SEND_BREAK;
930#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000931 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Alan Cox6caa76b2011-02-14 16:27:22 +0000934static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 unsigned int cmd, unsigned long arg)
936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
938 int retval;
939
940 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
941 return -ENODEV;
942
943 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
944 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
945 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
946 if (tty->flags & (1 << TTY_IO_ERROR))
947 return -EIO;
948 }
949
950 switch (cmd) {
951 case TCSBRK: /* SVID version: non-zero arg --> no break */
952 retval = tty_check_change(tty);
953 if (retval)
954 return retval;
955 tty_wait_until_sent(tty, 0);
956 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700957 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959 case TCSBRKP: /* support for POSIX tcsendbreak() */
960 retval = tty_check_change(tty);
961 if (retval)
962 return retval;
963 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700964 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400967 return get_serial_info(info,
968 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 case TIOCSSERIAL:
970 return set_serial_info(info,
971 (struct serial_struct *) arg);
972 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400973 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400975 if (copy_to_user((struct m68k_serial *) arg,
976 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 default:
980 return -ENOIOCTLCMD;
981 }
982 return 0;
983}
984
Alan Cox606d0992006-12-08 02:38:45 -0800985static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 change_speed(info);
990
991 if ((old_termios->c_cflag & CRTSCTS) &&
992 !(tty->termios->c_cflag & CRTSCTS)) {
993 tty->hw_stopped = 0;
994 rs_start(tty);
995 }
996
997}
998
999/*
1000 * ------------------------------------------------------------
1001 * rs_close()
1002 *
1003 * This routine is called when the serial port gets closed. First, we
1004 * wait for the last remaining data to be sent. Then, we unlink its
1005 * S structure from the interrupt chain if necessary, and we free
1006 * that IRQ if nothing is left in the chain.
1007 * ------------------------------------------------------------
1008 */
1009static void rs_close(struct tty_struct *tty, struct file * filp)
1010{
1011 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +02001012 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 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
Jiri Slaby86264342012-04-02 13:54:40 +02001026 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
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, "
Jiri Slaby86264342012-04-02 13:54:40 +02001035 "port->count is %d\n", port->count);
1036 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Jiri Slaby86264342012-04-02 13:54:40 +02001038 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001040 info->line, port->count);
1041 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Jiri Slaby86264342012-04-02 13:54:40 +02001043 if (port->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
Jiri Slaby86264342012-04-02 13:54:40 +02001082 if (port->blocked_open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 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);
Jiri Slaby86264342012-04-02 13:54:40 +02001105 info->tport.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{
Jiri Slaby86264342012-04-02 13:54:40 +02001119 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 DECLARE_WAITQUEUE(wait, current);
1121 int retval;
1122 int do_clocal = 0;
1123
1124 /*
1125 * If the device is in the middle of being closed, then block
1126 * until it's done, and then try again.
1127 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001128 if (info->flags & ASYNC_CLOSING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 interruptible_sleep_on(&info->close_wait);
1130#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001131 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return -EAGAIN;
1133 else
1134 return -ERESTARTSYS;
1135#else
1136 return -EAGAIN;
1137#endif
1138 }
1139
1140 /*
1141 * If non-blocking mode is set, or the port is not enabled,
1142 * then make the check up front and then exit.
1143 */
1144 if ((filp->f_flags & O_NONBLOCK) ||
1145 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001146 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return 0;
1148 }
1149
1150 if (tty->termios->c_cflag & CLOCAL)
1151 do_clocal = 1;
1152
1153 /*
1154 * Block waiting for the carrier detect and the line to become
1155 * free (i.e., not in use by the callout). While we are in
Jiri Slaby86264342012-04-02 13:54:40 +02001156 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * rs_close() knows when to free things. We restore it upon
1158 * exit, either normal or abnormal.
1159 */
1160 retval = 0;
1161 add_wait_queue(&info->open_wait, &wait);
1162
Jiri Slaby86264342012-04-02 13:54:40 +02001163 port->count--;
1164 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 current->state = TASK_INTERRUPTIBLE;
1167 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001168 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001170 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 retval = -EAGAIN;
1172 else
1173 retval = -ERESTARTSYS;
1174#else
1175 retval = -EAGAIN;
1176#endif
1177 break;
1178 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001179 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 if (signal_pending(current)) {
1182 retval = -ERESTARTSYS;
1183 break;
1184 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001185 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001187 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 current->state = TASK_RUNNING;
1190 remove_wait_queue(&info->open_wait, &wait);
1191 if (!tty_hung_up_p(filp))
Jiri Slaby86264342012-04-02 13:54:40 +02001192 port->count++;
1193 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (retval)
1196 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001197 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199}
1200
1201/*
1202 * This routine is called whenever a serial port is opened. It
1203 * enables interrupts for a serial port, linking in its S structure into
1204 * the IRQ chain. It also performs the serial-specific
1205 * initialization for the tty structure.
1206 */
1207int rs_open(struct tty_struct *tty, struct file * filp)
1208{
1209 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001210 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jiri Slaby410235f2012-03-05 14:52:01 +01001212 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (serial_paranoia_check(info, tty->name, "rs_open"))
1215 return -ENODEV;
1216
Jiri Slaby86264342012-04-02 13:54:40 +02001217 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001219 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /*
1222 * Start up serial port
1223 */
1224 retval = startup(info);
1225 if (retval)
1226 return retval;
1227
1228 return block_til_ready(tty, filp, info);
1229}
1230
1231/* Finally, routines used to initialize the serial driver. */
1232
1233static void show_serial_version(void)
1234{
1235 printk("MC68328 serial driver version 1.00\n");
1236}
1237
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001238static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 .open = rs_open,
1240 .close = rs_close,
1241 .write = rs_write,
1242 .flush_chars = rs_flush_chars,
1243 .write_room = rs_write_room,
1244 .chars_in_buffer = rs_chars_in_buffer,
1245 .flush_buffer = rs_flush_buffer,
1246 .ioctl = rs_ioctl,
1247 .throttle = rs_throttle,
1248 .unthrottle = rs_unthrottle,
1249 .set_termios = rs_set_termios,
1250 .stop = rs_stop,
1251 .start = rs_start,
1252 .hangup = rs_hangup,
1253 .set_ldisc = rs_set_ldisc,
1254};
1255
1256/* rs_init inits the driver */
1257static int __init
1258rs68328_init(void)
1259{
Jiri Slaby107afb72012-04-02 13:54:38 +02001260 unsigned long flags;
1261 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct m68k_serial *info;
1263
1264 serial_driver = alloc_tty_driver(NR_PORTS);
1265 if (!serial_driver)
1266 return -ENOMEM;
1267
1268 show_serial_version();
1269
1270 /* Initialize the tty_driver structure */
1271 /* SPARC: Not all of this is exactly right for us. */
1272
1273 serial_driver->name = "ttyS";
1274 serial_driver->major = TTY_MAJOR;
1275 serial_driver->minor_start = 64;
1276 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1277 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1278 serial_driver->init_termios = tty_std_termios;
1279 serial_driver->init_termios.c_cflag =
1280 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1281 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1282 tty_set_operations(serial_driver, &rs_ops);
1283
1284 if (tty_register_driver(serial_driver)) {
1285 put_tty_driver(serial_driver);
1286 printk(KERN_ERR "Couldn't register serial driver\n");
1287 return -ENOMEM;
1288 }
1289
Greg Ungerer727dda82006-06-28 15:54:22 +10001290 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 for(i=0;i<NR_PORTS;i++) {
1293
1294 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001295 tty_port_init(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 info->magic = SERIAL_MAGIC;
1297 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001298 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 info->irq = uart_irqs[i];
1300 info->custom_divisor = 16;
1301 info->close_delay = 50;
1302 info->closing_wait = 3000;
1303 info->x_char = 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);