blob: e3a1c557fe951989c98549c4b199419e187a4fd4 [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.
Jiri Slaby1bb26872012-04-02 13:54:39 +0200108 */
109struct m68k_serial {
Jiri Slaby86264342012-04-02 13:54:40 +0200110 struct tty_port tport;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200111 char is_cons; /* Is this our console. */
112 int magic;
113 int baud_base;
114 int port;
115 int irq;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200116 int type; /* UART type */
117 struct tty_struct *tty;
118 int custom_divisor;
119 int x_char; /* xon/xoff character */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200120 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200121 unsigned char *xmit_buf;
122 int xmit_head;
123 int xmit_tail;
124 int xmit_cnt;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200125};
126
127#define SERIAL_MAGIC 0x5301
128
129/*
130 * Define the number of ports supported and their irqs.
131 */
132#define NR_PORTS 1
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jiri Slaby1bb26872012-04-02 13:54:39 +0200136static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* multiple ports are contiguous in memory */
139m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct tty_driver *serial_driver;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void change_speed(struct m68k_serial *info);
144
145/*
146 * Setup for console. Argument comes from the boot command line.
147 */
148
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700149/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
150#ifdef CONFIG_M68VZ328
151#define CONSOLE_BAUD_RATE 19200
152#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif
154
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifndef CONSOLE_BAUD_RATE
157#define CONSOLE_BAUD_RATE 9600
158#define DEFAULT_CBAUD B9600
159#endif
160
161
162static int m68328_console_initted = 0;
163static int m68328_console_baud = CONSOLE_BAUD_RATE;
164static int m68328_console_cbaud = DEFAULT_CBAUD;
165
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline int serial_paranoia_check(struct m68k_serial *info,
168 char *name, const char *routine)
169{
170#ifdef SERIAL_PARANOIA_CHECK
171 static const char *badmagic =
172 "Warning: bad magic number for serial struct %s in %s\n";
173 static const char *badinfo =
174 "Warning: null m68k_serial for %s in %s\n";
175
176 if (!info) {
177 printk(badinfo, name, routine);
178 return 1;
179 }
180 if (info->magic != SERIAL_MAGIC) {
181 printk(badmagic, name, routine);
182 return 1;
183 }
184#endif
185 return 0;
186}
187
188/*
189 * This is used to figure out the divisor speeds and the timeouts
190 */
191static int baud_table[] = {
192 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
193 9600, 19200, 38400, 57600, 115200, 0 };
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* Utility routines */
196static inline int get_baud(struct m68k_serial *ss)
197{
198 unsigned long result = 115200;
199 unsigned short int baud = uart_addr[ss->line].ubaud;
200 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
201 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
202
203 return result;
204}
205
206/*
207 * ------------------------------------------------------------
208 * rs_stop() and rs_start()
209 *
210 * This routines are called before setting or resetting tty->stopped.
211 * They enable or disable transmitter interrupts, as necessary.
212 * ------------------------------------------------------------
213 */
214static void rs_stop(struct tty_struct *tty)
215{
216 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
217 m68328_uart *uart = &uart_addr[info->line];
218 unsigned long flags;
219
220 if (serial_paranoia_check(info, tty->name, "rs_stop"))
221 return;
222
Greg Ungerer727dda82006-06-28 15:54:22 +1000223 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000225 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Alan Cox09a6ffa2008-04-30 00:54:01 -0700228static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Jiri Slaby107afb72012-04-02 13:54:38 +0200230 unsigned long flags;
231 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Greg Ungerer727dda82006-06-28 15:54:22 +1000233 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
236 loops++;
237 udelay(5);
238 }
239
240 UTX_TXDATA = ch;
241 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000242 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700243 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246static void rs_start(struct tty_struct *tty)
247{
248 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
249 m68328_uart *uart = &uart_addr[info->line];
250 unsigned long flags;
251
252 if (serial_paranoia_check(info, tty->name, "rs_start"))
253 return;
254
Greg Ungerer727dda82006-06-28 15:54:22 +1000255 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
257#ifdef USE_INTS
258 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
259#else
260 uart->ustcnt |= USTCNT_TXEN;
261#endif
262 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000263 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
David Howells7d12e782006-10-05 14:55:46 +0100266static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000268 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800270 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /*
273 * This do { } while() loop will get ALL chars out of Rx FIFO
274 */
275#ifndef CONFIG_XCOPILOT_BUGS
276 do {
277#endif
278 ch = GET_FIELD(rx, URX_RXDATA);
279
280 if(info->is_cons) {
281 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283#ifdef CONFIG_MAGIC_SYSRQ
284 } else if (ch == 0x10) { /* ^P */
285 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700286 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 show_buffers();
288/* show_net_buffers(); */
289 return;
290 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600291 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
293#endif /* CONFIG_MAGIC_SYSRQ */
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 if(!tty)
298 goto clear_and_exit;
299
Alan Cox33f0f882006-01-09 20:54:13 -0800300 flag = TTY_NORMAL;
301
Jiri Slabyc21e2652012-04-02 13:54:36 +0200302 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800303 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200304 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800305 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200306 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800307 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200308
Alan Cox33f0f882006-01-09 20:54:13 -0800309 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#ifndef CONFIG_XCOPILOT_BUGS
311 } while((rx = uart->urx.w) & URX_DATA_READY);
312#endif
313
Greg Ungerer8e63e662006-02-08 09:19:17 +1000314 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316clear_and_exit:
317 return;
318}
319
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800320static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 m68328_uart *uart = &uart_addr[info->line];
323
324 if (info->x_char) {
325 /* Send next char */
326 uart->utx.b.txdata = info->x_char;
327 info->x_char = 0;
328 goto clear_and_return;
329 }
330
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000331 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* That's peculiar... TX ints off */
333 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
334 goto clear_and_return;
335 }
336
337 /* Send char */
338 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
339 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
340 info->xmit_cnt--;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if(info->xmit_cnt <= 0) {
343 /* All done for now... TX ints off */
344 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
345 goto clear_and_return;
346 }
347
348clear_and_return:
349 /* Clear interrupt (should be auto)*/
350 return;
351}
352
353/*
354 * This is the serial driver's generic interrupt routine
355 */
David Howells7d12e782006-10-05 14:55:46 +0100356irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Alan Cox25db8ad2008-08-19 20:49:40 -0700358 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 m68328_uart *uart;
360 unsigned short rx;
361 unsigned short tx;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 uart = &uart_addr[info->line];
364 rx = uart->urx.w;
365
366#ifdef USE_INTS
367 tx = uart->utx.w;
368
David Howells7d12e782006-10-05 14:55:46 +0100369 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (tx & UTX_TX_AVAIL) transmit_chars(info);
371#else
David Howells7d12e782006-10-05 14:55:46 +0100372 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#endif
374 return IRQ_HANDLED;
375}
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static int startup(struct m68k_serial * info)
378{
379 m68328_uart *uart = &uart_addr[info->line];
380 unsigned long flags;
381
Jiri Slabya85dd822012-04-02 13:54:43 +0200382 if (info->tport.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384
385 if (!info->xmit_buf) {
386 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
387 if (!info->xmit_buf)
388 return -ENOMEM;
389 }
390
Greg Ungerer727dda82006-06-28 15:54:22 +1000391 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /*
394 * Clear the FIFO buffers and disable them
395 * (they will be reenabled in change_speed())
396 */
397
398 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
400 (void)uart->urx.w;
401
402 /*
403 * Finally, enable sequencing and interrupts
404 */
405#ifdef USE_INTS
406 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
407 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
408#else
409 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
410#endif
411
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000412 if (info->tty)
413 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
415
416 /*
417 * and set the speed of the serial port
418 */
419
420 change_speed(info);
421
Jiri Slabya85dd822012-04-02 13:54:43 +0200422 info->tport.flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000423 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425}
426
427/*
428 * This routine will shutdown a serial port; interrupts are disabled, and
429 * DTR is dropped if the hangup on close termio flag is on.
430 */
431static void shutdown(struct m68k_serial * info)
432{
433 m68328_uart *uart = &uart_addr[info->line];
434 unsigned long flags;
435
436 uart->ustcnt = 0; /* All off! */
Jiri Slabya85dd822012-04-02 13:54:43 +0200437 if (!(info->tport.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return;
439
Greg Ungerer727dda82006-06-28 15:54:22 +1000440 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (info->xmit_buf) {
443 free_page((unsigned long) info->xmit_buf);
444 info->xmit_buf = 0;
445 }
446
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000447 if (info->tty)
448 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jiri Slabya85dd822012-04-02 13:54:43 +0200450 info->tport.flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000451 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454struct {
455 int divisor, prescale;
456}
457#ifndef CONFIG_M68VZ328
458 hw_baud_table[18] = {
459 {0,0}, /* 0 */
460 {0,0}, /* 50 */
461 {0,0}, /* 75 */
462 {0,0}, /* 110 */
463 {0,0}, /* 134 */
464 {0,0}, /* 150 */
465 {0,0}, /* 200 */
466 {7,0x26}, /* 300 */
467 {6,0x26}, /* 600 */
468 {5,0x26}, /* 1200 */
469 {0,0}, /* 1800 */
470 {4,0x26}, /* 2400 */
471 {3,0x26}, /* 4800 */
472 {2,0x26}, /* 9600 */
473 {1,0x26}, /* 19200 */
474 {0,0x26}, /* 38400 */
475 {1,0x38}, /* 57600 */
476 {0,0x38}, /* 115200 */
477};
478#else
479 hw_baud_table[18] = {
480 {0,0}, /* 0 */
481 {0,0}, /* 50 */
482 {0,0}, /* 75 */
483 {0,0}, /* 110 */
484 {0,0}, /* 134 */
485 {0,0}, /* 150 */
486 {0,0}, /* 200 */
487 {0,0}, /* 300 */
488 {7,0x26}, /* 600 */
489 {6,0x26}, /* 1200 */
490 {0,0}, /* 1800 */
491 {5,0x26}, /* 2400 */
492 {4,0x26}, /* 4800 */
493 {3,0x26}, /* 9600 */
494 {2,0x26}, /* 19200 */
495 {1,0x26}, /* 38400 */
496 {0,0x26}, /* 57600 */
497 {1,0x38}, /* 115200 */
498};
499#endif
500/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
501
502/*
503 * This routine is called to set the UART divisor registers to match
504 * the specified baud rate for a serial port.
505 */
506static void change_speed(struct m68k_serial *info)
507{
508 m68328_uart *uart = &uart_addr[info->line];
509 unsigned short port;
510 unsigned short ustcnt;
511 unsigned cflag;
512 int i;
513
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000514 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000516 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!(port = info->port))
518 return;
519
520 ustcnt = uart->ustcnt;
521 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
522
523 i = cflag & CBAUD;
524 if (i & CBAUDEX) {
525 i = (i & ~CBAUDEX) + B38400;
526 }
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
529 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
530
531 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
532
533 if ((cflag & CSIZE) == CS8)
534 ustcnt |= USTCNT_8_7;
535
536 if (cflag & CSTOPB)
537 ustcnt |= USTCNT_STOP;
538
539 if (cflag & PARENB)
540 ustcnt |= USTCNT_PARITYEN;
541 if (cflag & PARODD)
542 ustcnt |= USTCNT_ODD_EVEN;
543
544#ifdef CONFIG_SERIAL_68328_RTS_CTS
545 if (cflag & CRTSCTS) {
546 uart->utx.w &= ~ UTX_NOCTS;
547 } else {
548 uart->utx.w |= UTX_NOCTS;
549 }
550#endif
551
552 ustcnt |= USTCNT_TXEN;
553
554 uart->ustcnt = ustcnt;
555 return;
556}
557
558/*
559 * Fair output driver allows a process to speak.
560 */
561static void rs_fair_output(void)
562{
563 int left; /* Output no more than that */
564 unsigned long flags;
565 struct m68k_serial *info = &m68k_soft[0];
566 char c;
567
568 if (info == 0) return;
569 if (info->xmit_buf == 0) return;
570
Greg Ungerer727dda82006-06-28 15:54:22 +1000571 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 left = info->xmit_cnt;
573 while (left != 0) {
574 c = info->xmit_buf[info->xmit_tail];
575 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
576 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000577 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 rs_put_char(c);
580
Greg Ungerer727dda82006-06-28 15:54:22 +1000581 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 left = min(info->xmit_cnt, left-1);
583 }
584
585 /* Last character is being transmitted now (hopefully). */
586 udelay(5);
587
Greg Ungerer727dda82006-06-28 15:54:22 +1000588 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590}
591
592/*
593 * m68k_console_print is registered for printk.
594 */
595void console_print_68328(const char *p)
596{
597 char c;
598
599 while((c=*(p++)) != 0) {
600 if(c == '\n')
601 rs_put_char('\r');
602 rs_put_char(c);
603 }
604
605 /* Comment this if you want to have a strict interrupt-driven output */
606 rs_fair_output();
607
608 return;
609}
610
611static void rs_set_ldisc(struct tty_struct *tty)
612{
613 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
614
615 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
616 return;
617
618 info->is_cons = (tty->termios->c_line == N_TTY);
619
620 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
621}
622
623static void rs_flush_chars(struct tty_struct *tty)
624{
625 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
626 m68328_uart *uart = &uart_addr[info->line];
627 unsigned long flags;
628
629 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
630 return;
631#ifndef USE_INTS
632 for(;;) {
633#endif
634
635 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000636 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
639 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000640 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return;
642 }
643
644#ifdef USE_INTS
645 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
646#else
647 uart->ustcnt |= USTCNT_TXEN;
648#endif
649
650#ifdef USE_INTS
651 if (uart->utx.w & UTX_TX_AVAIL) {
652#else
653 if (1) {
654#endif
655 /* Send char */
656 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
657 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
658 info->xmit_cnt--;
659 }
660
661#ifndef USE_INTS
662 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
663 }
664#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000665 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668extern void console_printn(const char * b, int count);
669
670static int rs_write(struct tty_struct * tty,
671 const unsigned char *buf, int count)
672{
673 int c, total = 0;
674 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
675 m68328_uart *uart = &uart_addr[info->line];
676 unsigned long flags;
677
678 if (serial_paranoia_check(info, tty->name, "rs_write"))
679 return 0;
680
681 if (!tty || !info->xmit_buf)
682 return 0;
683
Greg Ungerer727dda82006-06-28 15:54:22 +1000684 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000686 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
688 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000689 local_irq_restore(flags);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (c <= 0)
692 break;
693
694 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000695
696 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
698 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000699 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 buf += c;
701 count -= c;
702 total += c;
703 }
704
705 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
706 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000707 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708#ifndef USE_INTS
709 while(info->xmit_cnt) {
710#endif
711
712 uart->ustcnt |= USTCNT_TXEN;
713#ifdef USE_INTS
714 uart->ustcnt |= USTCNT_TX_INTR_MASK;
715#else
716 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
717#endif
718 if (uart->utx.w & UTX_TX_AVAIL) {
719 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
720 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
721 info->xmit_cnt--;
722 }
723
724#ifndef USE_INTS
725 }
726#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000727 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return total;
731}
732
733static int rs_write_room(struct tty_struct *tty)
734{
735 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
736 int ret;
737
738 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
739 return 0;
740 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
741 if (ret < 0)
742 ret = 0;
743 return ret;
744}
745
746static int rs_chars_in_buffer(struct tty_struct *tty)
747{
748 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
749
750 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
751 return 0;
752 return info->xmit_cnt;
753}
754
755static void rs_flush_buffer(struct tty_struct *tty)
756{
757 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000758 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
761 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000762 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000764 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 tty_wakeup(tty);
766}
767
768/*
769 * ------------------------------------------------------------
770 * rs_throttle()
771 *
772 * This routine is called by the upper-layer tty layer to signal that
773 * incoming characters should be throttled.
774 * ------------------------------------------------------------
775 */
776static void rs_throttle(struct tty_struct * tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779
780 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
781 return;
782
783 if (I_IXOFF(tty))
784 info->x_char = STOP_CHAR(tty);
785
786 /* Turn off RTS line (do this atomic) */
787}
788
789static void rs_unthrottle(struct tty_struct * tty)
790{
791 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
792
793 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
794 return;
795
796 if (I_IXOFF(tty)) {
797 if (info->x_char)
798 info->x_char = 0;
799 else
800 info->x_char = START_CHAR(tty);
801 }
802
803 /* Assert RTS line (do this atomic) */
804}
805
806/*
807 * ------------------------------------------------------------
808 * rs_ioctl() and friends
809 * ------------------------------------------------------------
810 */
811
812static int get_serial_info(struct m68k_serial * info,
813 struct serial_struct * retinfo)
814{
815 struct serial_struct tmp;
816
817 if (!retinfo)
818 return -EFAULT;
819 memset(&tmp, 0, sizeof(tmp));
820 tmp.type = info->type;
821 tmp.line = info->line;
822 tmp.port = info->port;
823 tmp.irq = info->irq;
Jiri Slabya85dd822012-04-02 13:54:43 +0200824 tmp.flags = info->tport.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 tmp.baud_base = info->baud_base;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200826 tmp.close_delay = info->tport.close_delay;
827 tmp.closing_wait = info->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400829 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
830 return -EFAULT;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
835static int set_serial_info(struct m68k_serial * info,
836 struct serial_struct * new_info)
837{
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200838 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct serial_struct new_serial;
840 struct m68k_serial old_info;
841 int retval = 0;
842
843 if (!new_info)
844 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400845 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
846 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 old_info = *info;
848
849 if (!capable(CAP_SYS_ADMIN)) {
850 if ((new_serial.baud_base != info->baud_base) ||
851 (new_serial.type != info->type) ||
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200852 (new_serial.close_delay != port->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200853 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabya85dd822012-04-02 13:54:43 +0200854 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return -EPERM;
Jiri Slabya85dd822012-04-02 13:54:43 +0200856 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200857 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 info->custom_divisor = new_serial.custom_divisor;
859 goto check_and_exit;
860 }
861
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200862 if (port->count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return -EBUSY;
864
865 /*
866 * OK, past this point, all the error checking has been done.
867 * At this point, we start making changes.....
868 */
869
870 info->baud_base = new_serial.baud_base;
Jiri Slabya85dd822012-04-02 13:54:43 +0200871 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200872 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 info->type = new_serial.type;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200874 port->close_delay = new_serial.close_delay;
875 port->closing_wait = new_serial.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877check_and_exit:
878 retval = startup(info);
879 return retval;
880}
881
882/*
883 * get_lsr_info - get line status register info
884 *
885 * Purpose: Let user call ioctl() to get info when the UART physically
886 * is emptied. On bus types like RS485, the transmitter must
887 * release the bus after transmitting. This must be done when
888 * the transmit shift register is empty, not be done when the
889 * transmit holding register is empty. This functionality
890 * allows an RS485 driver to be written in user space.
891 */
892static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
893{
894#ifdef CONFIG_SERIAL_68328_RTS_CTS
895 m68328_uart *uart = &uart_addr[info->line];
896#endif
897 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000898 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Greg Ungerer727dda82006-06-28 15:54:22 +1000900 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#ifdef CONFIG_SERIAL_68328_RTS_CTS
902 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
903#else
904 status = 0;
905#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000906 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400907 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/*
911 * This routine sends a break character out the serial port.
912 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700913static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 m68328_uart *uart = &uart_addr[info->line];
916 unsigned long flags;
917 if (!info->port)
918 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000919 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#ifdef USE_INTS
921 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700922 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 uart->utx.w &= ~UTX_SEND_BREAK;
924#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000925 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Alan Cox6caa76b2011-02-14 16:27:22 +0000928static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int cmd, unsigned long arg)
930{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
932 int retval;
933
934 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
935 return -ENODEV;
936
937 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
938 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
939 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
940 if (tty->flags & (1 << TTY_IO_ERROR))
941 return -EIO;
942 }
943
944 switch (cmd) {
945 case TCSBRK: /* SVID version: non-zero arg --> no break */
946 retval = tty_check_change(tty);
947 if (retval)
948 return retval;
949 tty_wait_until_sent(tty, 0);
950 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700951 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953 case TCSBRKP: /* support for POSIX tcsendbreak() */
954 retval = tty_check_change(tty);
955 if (retval)
956 return retval;
957 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700958 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400961 return get_serial_info(info,
962 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 case TIOCSSERIAL:
964 return set_serial_info(info,
965 (struct serial_struct *) arg);
966 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400967 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400969 if (copy_to_user((struct m68k_serial *) arg,
970 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 default:
974 return -ENOIOCTLCMD;
975 }
976 return 0;
977}
978
Alan Cox606d0992006-12-08 02:38:45 -0800979static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 change_speed(info);
984
985 if ((old_termios->c_cflag & CRTSCTS) &&
986 !(tty->termios->c_cflag & CRTSCTS)) {
987 tty->hw_stopped = 0;
988 rs_start(tty);
989 }
990
991}
992
993/*
994 * ------------------------------------------------------------
995 * rs_close()
996 *
997 * This routine is called when the serial port gets closed. First, we
998 * wait for the last remaining data to be sent. Then, we unlink its
999 * S structure from the interrupt chain if necessary, and we free
1000 * that IRQ if nothing is left in the chain.
1001 * ------------------------------------------------------------
1002 */
1003static void rs_close(struct tty_struct *tty, struct file * filp)
1004{
1005 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +02001006 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 m68328_uart *uart = &uart_addr[info->line];
1008 unsigned long flags;
1009
1010 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1011 return;
1012
Greg Ungerer727dda82006-06-28 15:54:22 +10001013 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001016 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return;
1018 }
1019
Jiri Slaby86264342012-04-02 13:54:40 +02001020 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * Uh, oh. tty->count is 1, which means that the tty
1023 * structure will be freed. Info->count should always
1024 * be one in these conditions. If it's greater than
1025 * one, we've got real problems, since it means the
1026 * serial port won't be shutdown.
1027 */
1028 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001029 "port->count is %d\n", port->count);
1030 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Jiri Slaby86264342012-04-02 13:54:40 +02001032 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001034 info->line, port->count);
1035 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Jiri Slaby86264342012-04-02 13:54:40 +02001037 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001038 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return;
1040 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001041 port->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * Now we wait for the transmit buffer to clear; and we notify
1044 * the line discipline to only process XON/XOFF characters.
1045 */
1046 tty->closing = 1;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001047 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1048 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * At this point we stop accepting input. To do this, we
1051 * disable the receive line status interrupts, and tell the
1052 * interrupt driver to stop checking the data ready bit in the
1053 * line status register.
1054 */
1055
1056 uart->ustcnt &= ~USTCNT_RXEN;
1057 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1058
1059 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001060 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 tty_ldisc_flush(tty);
1063 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001064 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065#warning "This is not and has never been valid so fix it"
1066#if 0
1067 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1068 if (tty->ldisc.close)
1069 (tty->ldisc.close)(tty);
1070 tty->ldisc = ldiscs[N_TTY];
1071 tty->termios->c_line = N_TTY;
1072 if (tty->ldisc.open)
1073 (tty->ldisc.open)(tty);
1074 }
1075#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001076 if (port->blocked_open) {
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001077 if (port->close_delay)
1078 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Jiri Slabyc26f0112012-04-02 13:54:41 +02001079 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001081 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001082 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001083 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
1086/*
1087 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1088 */
1089void rs_hangup(struct tty_struct *tty)
1090{
1091 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1092
1093 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1094 return;
1095
1096 rs_flush_buffer(tty);
1097 shutdown(info);
Jiri Slaby86264342012-04-02 13:54:40 +02001098 info->tport.count = 0;
Jiri Slabya85dd822012-04-02 13:54:43 +02001099 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001100 info->tty = NULL;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001101 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
1105 * ------------------------------------------------------------
1106 * rs_open() and friends
1107 * ------------------------------------------------------------
1108 */
1109static int block_til_ready(struct tty_struct *tty, struct file * filp,
1110 struct m68k_serial *info)
1111{
Jiri Slaby86264342012-04-02 13:54:40 +02001112 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 DECLARE_WAITQUEUE(wait, current);
1114 int retval;
1115 int do_clocal = 0;
1116
1117 /*
1118 * If the device is in the middle of being closed, then block
1119 * until it's done, and then try again.
1120 */
Jiri Slabya85dd822012-04-02 13:54:43 +02001121 if (port->flags & ASYNC_CLOSING) {
Jiri Slabyc26f0112012-04-02 13:54:41 +02001122 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123#ifdef SERIAL_DO_RESTART
Jiri Slabya85dd822012-04-02 13:54:43 +02001124 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -EAGAIN;
1126 else
1127 return -ERESTARTSYS;
1128#else
1129 return -EAGAIN;
1130#endif
1131 }
1132
1133 /*
1134 * If non-blocking mode is set, or the port is not enabled,
1135 * then make the check up front and then exit.
1136 */
1137 if ((filp->f_flags & O_NONBLOCK) ||
1138 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabya85dd822012-04-02 13:54:43 +02001139 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
1141 }
1142
1143 if (tty->termios->c_cflag & CLOCAL)
1144 do_clocal = 1;
1145
1146 /*
1147 * Block waiting for the carrier detect and the line to become
1148 * free (i.e., not in use by the callout). While we are in
Jiri Slaby86264342012-04-02 13:54:40 +02001149 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * rs_close() knows when to free things. We restore it upon
1151 * exit, either normal or abnormal.
1152 */
1153 retval = 0;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001154 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jiri Slaby86264342012-04-02 13:54:40 +02001156 port->count--;
1157 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 current->state = TASK_INTERRUPTIBLE;
1160 if (tty_hung_up_p(filp) ||
Jiri Slabya85dd822012-04-02 13:54:43 +02001161 !(port->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162#ifdef SERIAL_DO_RESTART
Jiri Slabya85dd822012-04-02 13:54:43 +02001163 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 retval = -EAGAIN;
1165 else
1166 retval = -ERESTARTSYS;
1167#else
1168 retval = -EAGAIN;
1169#endif
1170 break;
1171 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001172 if (!(port->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 if (signal_pending(current)) {
1175 retval = -ERESTARTSYS;
1176 break;
1177 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001178 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001180 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 current->state = TASK_RUNNING;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001183 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!tty_hung_up_p(filp))
Jiri Slaby86264342012-04-02 13:54:40 +02001185 port->count++;
1186 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (retval)
1189 return retval;
Jiri Slabya85dd822012-04-02 13:54:43 +02001190 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194/*
1195 * This routine is called whenever a serial port is opened. It
1196 * enables interrupts for a serial port, linking in its S structure into
1197 * the IRQ chain. It also performs the serial-specific
1198 * initialization for the tty structure.
1199 */
1200int rs_open(struct tty_struct *tty, struct file * filp)
1201{
1202 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001203 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jiri Slaby410235f2012-03-05 14:52:01 +01001205 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (serial_paranoia_check(info, tty->name, "rs_open"))
1208 return -ENODEV;
1209
Jiri Slaby86264342012-04-02 13:54:40 +02001210 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001212 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * Start up serial port
1216 */
1217 retval = startup(info);
1218 if (retval)
1219 return retval;
1220
1221 return block_til_ready(tty, filp, info);
1222}
1223
1224/* Finally, routines used to initialize the serial driver. */
1225
1226static void show_serial_version(void)
1227{
1228 printk("MC68328 serial driver version 1.00\n");
1229}
1230
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001231static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 .open = rs_open,
1233 .close = rs_close,
1234 .write = rs_write,
1235 .flush_chars = rs_flush_chars,
1236 .write_room = rs_write_room,
1237 .chars_in_buffer = rs_chars_in_buffer,
1238 .flush_buffer = rs_flush_buffer,
1239 .ioctl = rs_ioctl,
1240 .throttle = rs_throttle,
1241 .unthrottle = rs_unthrottle,
1242 .set_termios = rs_set_termios,
1243 .stop = rs_stop,
1244 .start = rs_start,
1245 .hangup = rs_hangup,
1246 .set_ldisc = rs_set_ldisc,
1247};
1248
1249/* rs_init inits the driver */
1250static int __init
1251rs68328_init(void)
1252{
Jiri Slaby107afb72012-04-02 13:54:38 +02001253 unsigned long flags;
1254 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct m68k_serial *info;
1256
1257 serial_driver = alloc_tty_driver(NR_PORTS);
1258 if (!serial_driver)
1259 return -ENOMEM;
1260
1261 show_serial_version();
1262
1263 /* Initialize the tty_driver structure */
1264 /* SPARC: Not all of this is exactly right for us. */
1265
1266 serial_driver->name = "ttyS";
1267 serial_driver->major = TTY_MAJOR;
1268 serial_driver->minor_start = 64;
1269 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1270 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1271 serial_driver->init_termios = tty_std_termios;
1272 serial_driver->init_termios.c_cflag =
1273 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1274 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1275 tty_set_operations(serial_driver, &rs_ops);
1276
1277 if (tty_register_driver(serial_driver)) {
1278 put_tty_driver(serial_driver);
1279 printk(KERN_ERR "Couldn't register serial driver\n");
1280 return -ENOMEM;
1281 }
1282
Greg Ungerer727dda82006-06-28 15:54:22 +10001283 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 for(i=0;i<NR_PORTS;i++) {
1286
1287 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001288 tty_port_init(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 info->magic = SERIAL_MAGIC;
1290 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001291 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 info->irq = uart_irqs[i];
1293 info->custom_divisor = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 info->line = i;
1296 info->is_cons = 1; /* Means shortcuts work */
1297
1298 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1299 info->port, info->irq);
1300 printk(" is a builtin MC68328 UART\n");
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302#ifdef CONFIG_M68VZ328
1303 if (i > 0 )
1304 PJSEL &= 0xCF; /* PSW enable second port output */
1305#endif
1306
1307 if (request_irq(uart_irqs[i],
1308 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001309 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001310 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001313 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317module_init(rs68328_init);
1318
1319
1320
1321static void m68328_set_baud(void)
1322{
1323 unsigned short ustcnt;
1324 int i;
1325
1326 ustcnt = USTCNT;
1327 USTCNT = ustcnt & ~USTCNT_TXEN;
1328
1329again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001330 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (baud_table[i] == m68328_console_baud)
1332 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001333 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 m68328_console_baud = 9600;
1335 goto again;
1336 }
1337
1338 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1339 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1340 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1341 ustcnt |= USTCNT_8_7;
1342 ustcnt |= USTCNT_TXEN;
1343 USTCNT = ustcnt;
1344 m68328_console_initted = 1;
1345 return;
1346}
1347
1348
1349int m68328_console_setup(struct console *cp, char *arg)
1350{
1351 int i, n = CONSOLE_BAUD_RATE;
1352
1353 if (!cp)
1354 return(-1);
1355
1356 if (arg)
1357 n = simple_strtoul(arg,NULL,0);
1358
Thiago Farina8b505ca2009-12-09 12:31:30 -08001359 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (baud_table[i] == n)
1361 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001362 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 m68328_console_baud = n;
1364 m68328_console_cbaud = 0;
1365 if (i > 15) {
1366 m68328_console_cbaud |= CBAUDEX;
1367 i -= 15;
1368 }
1369 m68328_console_cbaud |= i;
1370 }
1371
1372 m68328_set_baud(); /* make sure baud rate changes */
1373 return(0);
1374}
1375
1376
1377static struct tty_driver *m68328_console_device(struct console *c, int *index)
1378{
1379 *index = c->index;
1380 return serial_driver;
1381}
1382
1383
1384void m68328_console_write (struct console *co, const char *str,
1385 unsigned int count)
1386{
1387 if (!m68328_console_initted)
1388 m68328_set_baud();
1389 while (count--) {
1390 if (*str == '\n')
1391 rs_put_char('\r');
1392 rs_put_char( *str++ );
1393 }
1394}
1395
1396
1397static struct console m68328_driver = {
1398 .name = "ttyS",
1399 .write = m68328_console_write,
1400 .device = m68328_console_device,
1401 .setup = m68328_console_setup,
1402 .flags = CON_PRINTBUFFER,
1403 .index = -1,
1404};
1405
1406
1407static int __init m68328_console_init(void)
1408{
1409 register_console(&m68328_driver);
1410 return 0;
1411}
1412
1413console_initcall(m68328_console_init);