blob: fde573b32ce0b90e3aaa1af8fc8855e7ec34c12e [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
Jiri Slaby467712c2012-04-02 13:54:44 +0200143static void change_speed(struct m68k_serial *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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
Jiri Slaby467712c2012-04-02 13:54:44 +0200266static void receive_chars(struct m68k_serial *info, struct tty_struct *tty,
267 unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
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
Jiri Slaby467712c2012-04-02 13:54:44 +0200320static void transmit_chars(struct m68k_serial *info, struct tty_struct *tty)
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
Jiri Slaby467712c2012-04-02 13:54:44 +0200331 if ((info->xmit_cnt <= 0) || !tty || 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;
Jiri Slaby467712c2012-04-02 13:54:44 +0200359 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 m68328_uart *uart;
361 unsigned short rx;
362 unsigned short tx;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 uart = &uart_addr[info->line];
365 rx = uart->urx.w;
366
367#ifdef USE_INTS
368 tx = uart->utx.w;
369
Jiri Slaby467712c2012-04-02 13:54:44 +0200370 if (rx & URX_DATA_READY)
371 receive_chars(info, tty, rx);
372 if (tx & UTX_TX_AVAIL)
373 transmit_chars(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#else
Jiri Slaby467712c2012-04-02 13:54:44 +0200375 receive_chars(info, tty, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#endif
377 return IRQ_HANDLED;
378}
379
Jiri Slaby467712c2012-04-02 13:54:44 +0200380static int startup(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 m68328_uart *uart = &uart_addr[info->line];
383 unsigned long flags;
384
Jiri Slabya85dd822012-04-02 13:54:43 +0200385 if (info->tport.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return 0;
387
388 if (!info->xmit_buf) {
389 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
390 if (!info->xmit_buf)
391 return -ENOMEM;
392 }
393
Greg Ungerer727dda82006-06-28 15:54:22 +1000394 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * Clear the FIFO buffers and disable them
398 * (they will be reenabled in change_speed())
399 */
400
401 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
403 (void)uart->urx.w;
404
405 /*
406 * Finally, enable sequencing and interrupts
407 */
408#ifdef USE_INTS
409 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
410 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
411#else
412 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
413#endif
414
Jiri Slaby467712c2012-04-02 13:54:44 +0200415 if (tty)
416 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
418
419 /*
420 * and set the speed of the serial port
421 */
422
Jiri Slaby467712c2012-04-02 13:54:44 +0200423 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jiri Slabya85dd822012-04-02 13:54:43 +0200425 info->tport.flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000426 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
430/*
431 * This routine will shutdown a serial port; interrupts are disabled, and
432 * DTR is dropped if the hangup on close termio flag is on.
433 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200434static void shutdown(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 m68328_uart *uart = &uart_addr[info->line];
437 unsigned long flags;
438
439 uart->ustcnt = 0; /* All off! */
Jiri Slabya85dd822012-04-02 13:54:43 +0200440 if (!(info->tport.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
442
Greg Ungerer727dda82006-06-28 15:54:22 +1000443 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (info->xmit_buf) {
446 free_page((unsigned long) info->xmit_buf);
447 info->xmit_buf = 0;
448 }
449
Jiri Slaby467712c2012-04-02 13:54:44 +0200450 if (tty)
451 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jiri Slabya85dd822012-04-02 13:54:43 +0200453 info->tport.flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000454 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457struct {
458 int divisor, prescale;
459}
460#ifndef CONFIG_M68VZ328
461 hw_baud_table[18] = {
462 {0,0}, /* 0 */
463 {0,0}, /* 50 */
464 {0,0}, /* 75 */
465 {0,0}, /* 110 */
466 {0,0}, /* 134 */
467 {0,0}, /* 150 */
468 {0,0}, /* 200 */
469 {7,0x26}, /* 300 */
470 {6,0x26}, /* 600 */
471 {5,0x26}, /* 1200 */
472 {0,0}, /* 1800 */
473 {4,0x26}, /* 2400 */
474 {3,0x26}, /* 4800 */
475 {2,0x26}, /* 9600 */
476 {1,0x26}, /* 19200 */
477 {0,0x26}, /* 38400 */
478 {1,0x38}, /* 57600 */
479 {0,0x38}, /* 115200 */
480};
481#else
482 hw_baud_table[18] = {
483 {0,0}, /* 0 */
484 {0,0}, /* 50 */
485 {0,0}, /* 75 */
486 {0,0}, /* 110 */
487 {0,0}, /* 134 */
488 {0,0}, /* 150 */
489 {0,0}, /* 200 */
490 {0,0}, /* 300 */
491 {7,0x26}, /* 600 */
492 {6,0x26}, /* 1200 */
493 {0,0}, /* 1800 */
494 {5,0x26}, /* 2400 */
495 {4,0x26}, /* 4800 */
496 {3,0x26}, /* 9600 */
497 {2,0x26}, /* 19200 */
498 {1,0x26}, /* 38400 */
499 {0,0x26}, /* 57600 */
500 {1,0x38}, /* 115200 */
501};
502#endif
503/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
504
505/*
506 * This routine is called to set the UART divisor registers to match
507 * the specified baud rate for a serial port.
508 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200509static void change_speed(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 m68328_uart *uart = &uart_addr[info->line];
512 unsigned short port;
513 unsigned short ustcnt;
514 unsigned cflag;
515 int i;
516
Jiri Slaby467712c2012-04-02 13:54:44 +0200517 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!(port = info->port))
519 return;
520
521 ustcnt = uart->ustcnt;
522 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
523
524 i = cflag & CBAUD;
525 if (i & CBAUDEX) {
526 i = (i & ~CBAUDEX) + B38400;
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
530 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
531
532 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
533
534 if ((cflag & CSIZE) == CS8)
535 ustcnt |= USTCNT_8_7;
536
537 if (cflag & CSTOPB)
538 ustcnt |= USTCNT_STOP;
539
540 if (cflag & PARENB)
541 ustcnt |= USTCNT_PARITYEN;
542 if (cflag & PARODD)
543 ustcnt |= USTCNT_ODD_EVEN;
544
545#ifdef CONFIG_SERIAL_68328_RTS_CTS
546 if (cflag & CRTSCTS) {
547 uart->utx.w &= ~ UTX_NOCTS;
548 } else {
549 uart->utx.w |= UTX_NOCTS;
550 }
551#endif
552
553 ustcnt |= USTCNT_TXEN;
554
555 uart->ustcnt = ustcnt;
556 return;
557}
558
559/*
560 * Fair output driver allows a process to speak.
561 */
562static void rs_fair_output(void)
563{
564 int left; /* Output no more than that */
565 unsigned long flags;
566 struct m68k_serial *info = &m68k_soft[0];
567 char c;
568
569 if (info == 0) return;
570 if (info->xmit_buf == 0) return;
571
Greg Ungerer727dda82006-06-28 15:54:22 +1000572 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 left = info->xmit_cnt;
574 while (left != 0) {
575 c = info->xmit_buf[info->xmit_tail];
576 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
577 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000578 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 rs_put_char(c);
581
Greg Ungerer727dda82006-06-28 15:54:22 +1000582 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 left = min(info->xmit_cnt, left-1);
584 }
585
586 /* Last character is being transmitted now (hopefully). */
587 udelay(5);
588
Greg Ungerer727dda82006-06-28 15:54:22 +1000589 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return;
591}
592
593/*
594 * m68k_console_print is registered for printk.
595 */
596void console_print_68328(const char *p)
597{
598 char c;
599
600 while((c=*(p++)) != 0) {
601 if(c == '\n')
602 rs_put_char('\r');
603 rs_put_char(c);
604 }
605
606 /* Comment this if you want to have a strict interrupt-driven output */
607 rs_fair_output();
608
609 return;
610}
611
612static void rs_set_ldisc(struct tty_struct *tty)
613{
614 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
615
616 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
617 return;
618
619 info->is_cons = (tty->termios->c_line == N_TTY);
620
621 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
622}
623
624static void rs_flush_chars(struct tty_struct *tty)
625{
626 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
627 m68328_uart *uart = &uart_addr[info->line];
628 unsigned long flags;
629
630 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
631 return;
632#ifndef USE_INTS
633 for(;;) {
634#endif
635
636 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000637 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
640 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000641 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return;
643 }
644
645#ifdef USE_INTS
646 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
647#else
648 uart->ustcnt |= USTCNT_TXEN;
649#endif
650
651#ifdef USE_INTS
652 if (uart->utx.w & UTX_TX_AVAIL) {
653#else
654 if (1) {
655#endif
656 /* Send char */
657 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
658 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
659 info->xmit_cnt--;
660 }
661
662#ifndef USE_INTS
663 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
664 }
665#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000666 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669extern void console_printn(const char * b, int count);
670
671static int rs_write(struct tty_struct * tty,
672 const unsigned char *buf, int count)
673{
674 int c, total = 0;
675 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
676 m68328_uart *uart = &uart_addr[info->line];
677 unsigned long flags;
678
679 if (serial_paranoia_check(info, tty->name, "rs_write"))
680 return 0;
681
682 if (!tty || !info->xmit_buf)
683 return 0;
684
Greg Ungerer727dda82006-06-28 15:54:22 +1000685 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000687 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
689 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000690 local_irq_restore(flags);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (c <= 0)
693 break;
694
695 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000696
697 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
699 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000700 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 buf += c;
702 count -= c;
703 total += c;
704 }
705
706 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
707 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000708 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#ifndef USE_INTS
710 while(info->xmit_cnt) {
711#endif
712
713 uart->ustcnt |= USTCNT_TXEN;
714#ifdef USE_INTS
715 uart->ustcnt |= USTCNT_TX_INTR_MASK;
716#else
717 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
718#endif
719 if (uart->utx.w & UTX_TX_AVAIL) {
720 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
721 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
722 info->xmit_cnt--;
723 }
724
725#ifndef USE_INTS
726 }
727#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000728 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return total;
732}
733
734static int rs_write_room(struct tty_struct *tty)
735{
736 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
737 int ret;
738
739 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
740 return 0;
741 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
742 if (ret < 0)
743 ret = 0;
744 return ret;
745}
746
747static int rs_chars_in_buffer(struct tty_struct *tty)
748{
749 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
750
751 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
752 return 0;
753 return info->xmit_cnt;
754}
755
756static void rs_flush_buffer(struct tty_struct *tty)
757{
758 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000759 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
762 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000763 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000765 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 tty_wakeup(tty);
767}
768
769/*
770 * ------------------------------------------------------------
771 * rs_throttle()
772 *
773 * This routine is called by the upper-layer tty layer to signal that
774 * incoming characters should be throttled.
775 * ------------------------------------------------------------
776 */
777static void rs_throttle(struct tty_struct * tty)
778{
779 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
780
781 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
782 return;
783
784 if (I_IXOFF(tty))
785 info->x_char = STOP_CHAR(tty);
786
787 /* Turn off RTS line (do this atomic) */
788}
789
790static void rs_unthrottle(struct tty_struct * tty)
791{
792 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
793
794 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
795 return;
796
797 if (I_IXOFF(tty)) {
798 if (info->x_char)
799 info->x_char = 0;
800 else
801 info->x_char = START_CHAR(tty);
802 }
803
804 /* Assert RTS line (do this atomic) */
805}
806
807/*
808 * ------------------------------------------------------------
809 * rs_ioctl() and friends
810 * ------------------------------------------------------------
811 */
812
813static int get_serial_info(struct m68k_serial * info,
814 struct serial_struct * retinfo)
815{
816 struct serial_struct tmp;
817
818 if (!retinfo)
819 return -EFAULT;
820 memset(&tmp, 0, sizeof(tmp));
821 tmp.type = info->type;
822 tmp.line = info->line;
823 tmp.port = info->port;
824 tmp.irq = info->irq;
Jiri Slabya85dd822012-04-02 13:54:43 +0200825 tmp.flags = info->tport.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 tmp.baud_base = info->baud_base;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200827 tmp.close_delay = info->tport.close_delay;
828 tmp.closing_wait = info->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400830 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
831 return -EFAULT;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
834}
835
Jiri Slaby467712c2012-04-02 13:54:44 +0200836static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct serial_struct * new_info)
838{
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200839 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct serial_struct new_serial;
841 struct m68k_serial old_info;
842 int retval = 0;
843
844 if (!new_info)
845 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400846 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
847 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 old_info = *info;
849
850 if (!capable(CAP_SYS_ADMIN)) {
851 if ((new_serial.baud_base != info->baud_base) ||
852 (new_serial.type != info->type) ||
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200853 (new_serial.close_delay != port->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200854 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabya85dd822012-04-02 13:54:43 +0200855 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -EPERM;
Jiri Slabya85dd822012-04-02 13:54:43 +0200857 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200858 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 info->custom_divisor = new_serial.custom_divisor;
860 goto check_and_exit;
861 }
862
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200863 if (port->count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -EBUSY;
865
866 /*
867 * OK, past this point, all the error checking has been done.
868 * At this point, we start making changes.....
869 */
870
871 info->baud_base = new_serial.baud_base;
Jiri Slabya85dd822012-04-02 13:54:43 +0200872 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200873 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 info->type = new_serial.type;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200875 port->close_delay = new_serial.close_delay;
876 port->closing_wait = new_serial.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878check_and_exit:
Jiri Slaby467712c2012-04-02 13:54:44 +0200879 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return retval;
881}
882
883/*
884 * get_lsr_info - get line status register info
885 *
886 * Purpose: Let user call ioctl() to get info when the UART physically
887 * is emptied. On bus types like RS485, the transmitter must
888 * release the bus after transmitting. This must be done when
889 * the transmit shift register is empty, not be done when the
890 * transmit holding register is empty. This functionality
891 * allows an RS485 driver to be written in user space.
892 */
893static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
894{
895#ifdef CONFIG_SERIAL_68328_RTS_CTS
896 m68328_uart *uart = &uart_addr[info->line];
897#endif
898 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000899 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Greg Ungerer727dda82006-06-28 15:54:22 +1000901 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#ifdef CONFIG_SERIAL_68328_RTS_CTS
903 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
904#else
905 status = 0;
906#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000907 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400908 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * This routine sends a break character out the serial port.
913 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700914static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 m68328_uart *uart = &uart_addr[info->line];
917 unsigned long flags;
918 if (!info->port)
919 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000920 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#ifdef USE_INTS
922 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700923 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 uart->utx.w &= ~UTX_SEND_BREAK;
925#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000926 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Alan Cox6caa76b2011-02-14 16:27:22 +0000929static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int cmd, unsigned long arg)
931{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
933 int retval;
934
935 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
936 return -ENODEV;
937
938 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
939 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
940 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
941 if (tty->flags & (1 << TTY_IO_ERROR))
942 return -EIO;
943 }
944
945 switch (cmd) {
946 case TCSBRK: /* SVID version: non-zero arg --> no break */
947 retval = tty_check_change(tty);
948 if (retval)
949 return retval;
950 tty_wait_until_sent(tty, 0);
951 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700952 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954 case TCSBRKP: /* support for POSIX tcsendbreak() */
955 retval = tty_check_change(tty);
956 if (retval)
957 return retval;
958 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700959 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400962 return get_serial_info(info,
963 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case TIOCSSERIAL:
Jiri Slaby467712c2012-04-02 13:54:44 +0200965 return set_serial_info(info, tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 (struct serial_struct *) arg);
967 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400968 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400970 if (copy_to_user((struct m68k_serial *) arg,
971 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 default:
975 return -ENOIOCTLCMD;
976 }
977 return 0;
978}
979
Alan Cox606d0992006-12-08 02:38:45 -0800980static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
983
Jiri Slaby467712c2012-04-02 13:54:44 +0200984 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if ((old_termios->c_cflag & CRTSCTS) &&
987 !(tty->termios->c_cflag & CRTSCTS)) {
988 tty->hw_stopped = 0;
989 rs_start(tty);
990 }
991
992}
993
994/*
995 * ------------------------------------------------------------
996 * rs_close()
997 *
998 * This routine is called when the serial port gets closed. First, we
999 * wait for the last remaining data to be sent. Then, we unlink its
1000 * S structure from the interrupt chain if necessary, and we free
1001 * that IRQ if nothing is left in the chain.
1002 * ------------------------------------------------------------
1003 */
1004static void rs_close(struct tty_struct *tty, struct file * filp)
1005{
1006 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +02001007 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 m68328_uart *uart = &uart_addr[info->line];
1009 unsigned long flags;
1010
1011 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1012 return;
1013
Greg Ungerer727dda82006-06-28 15:54:22 +10001014 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001017 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
1019 }
1020
Jiri Slaby86264342012-04-02 13:54:40 +02001021 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Uh, oh. tty->count is 1, which means that the tty
1024 * structure will be freed. Info->count should always
1025 * be one in these conditions. If it's greater than
1026 * one, we've got real problems, since it means the
1027 * serial port won't be shutdown.
1028 */
1029 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001030 "port->count is %d\n", port->count);
1031 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Jiri Slaby86264342012-04-02 13:54:40 +02001033 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001035 info->line, port->count);
1036 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Jiri Slaby86264342012-04-02 13:54:40 +02001038 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001039 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return;
1041 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001042 port->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Now we wait for the transmit buffer to clear; and we notify
1045 * the line discipline to only process XON/XOFF characters.
1046 */
1047 tty->closing = 1;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001048 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1049 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * At this point we stop accepting input. To do this, we
1052 * disable the receive line status interrupts, and tell the
1053 * interrupt driver to stop checking the data ready bit in the
1054 * line status register.
1055 */
1056
1057 uart->ustcnt &= ~USTCNT_RXEN;
1058 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1059
Jiri Slaby467712c2012-04-02 13:54:44 +02001060 shutdown(info, tty);
Alan Cox978e5952008-04-30 00:53:59 -07001061 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 tty_ldisc_flush(tty);
1064 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001065 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#warning "This is not and has never been valid so fix it"
1067#if 0
1068 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1069 if (tty->ldisc.close)
1070 (tty->ldisc.close)(tty);
1071 tty->ldisc = ldiscs[N_TTY];
1072 tty->termios->c_line = N_TTY;
1073 if (tty->ldisc.open)
1074 (tty->ldisc.open)(tty);
1075 }
1076#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001077 if (port->blocked_open) {
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001078 if (port->close_delay)
1079 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Jiri Slabyc26f0112012-04-02 13:54:41 +02001080 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001082 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001083 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001084 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087/*
1088 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1089 */
1090void rs_hangup(struct tty_struct *tty)
1091{
1092 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1093
1094 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1095 return;
1096
1097 rs_flush_buffer(tty);
Jiri Slaby467712c2012-04-02 13:54:44 +02001098 shutdown(info, tty);
Jiri Slaby86264342012-04-02 13:54:40 +02001099 info->tport.count = 0;
Jiri Slabya85dd822012-04-02 13:54:43 +02001100 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001101 info->tty = NULL;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001102 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105/*
1106 * ------------------------------------------------------------
1107 * rs_open() and friends
1108 * ------------------------------------------------------------
1109 */
1110static int block_til_ready(struct tty_struct *tty, struct file * filp,
1111 struct m68k_serial *info)
1112{
Jiri Slaby86264342012-04-02 13:54:40 +02001113 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 DECLARE_WAITQUEUE(wait, current);
1115 int retval;
1116 int do_clocal = 0;
1117
1118 /*
1119 * If the device is in the middle of being closed, then block
1120 * until it's done, and then try again.
1121 */
Jiri Slabya85dd822012-04-02 13:54:43 +02001122 if (port->flags & ASYNC_CLOSING) {
Jiri Slabyc26f0112012-04-02 13:54:41 +02001123 interruptible_sleep_on(&port->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124#ifdef SERIAL_DO_RESTART
Jiri Slabya85dd822012-04-02 13:54:43 +02001125 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return -EAGAIN;
1127 else
1128 return -ERESTARTSYS;
1129#else
1130 return -EAGAIN;
1131#endif
1132 }
1133
1134 /*
1135 * If non-blocking mode is set, or the port is not enabled,
1136 * then make the check up front and then exit.
1137 */
1138 if ((filp->f_flags & O_NONBLOCK) ||
1139 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabya85dd822012-04-02 13:54:43 +02001140 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142 }
1143
1144 if (tty->termios->c_cflag & CLOCAL)
1145 do_clocal = 1;
1146
1147 /*
1148 * Block waiting for the carrier detect and the line to become
1149 * free (i.e., not in use by the callout). While we are in
Jiri Slaby86264342012-04-02 13:54:40 +02001150 * this loop, port->count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 * rs_close() knows when to free things. We restore it upon
1152 * exit, either normal or abnormal.
1153 */
1154 retval = 0;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001155 add_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Jiri Slaby86264342012-04-02 13:54:40 +02001157 port->count--;
1158 port->blocked_open++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 current->state = TASK_INTERRUPTIBLE;
1161 if (tty_hung_up_p(filp) ||
Jiri Slabya85dd822012-04-02 13:54:43 +02001162 !(port->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163#ifdef SERIAL_DO_RESTART
Jiri Slabya85dd822012-04-02 13:54:43 +02001164 if (port->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 retval = -EAGAIN;
1166 else
1167 retval = -ERESTARTSYS;
1168#else
1169 retval = -EAGAIN;
1170#endif
1171 break;
1172 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001173 if (!(port->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 if (signal_pending(current)) {
1176 retval = -ERESTARTSYS;
1177 break;
1178 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001179 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001181 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 current->state = TASK_RUNNING;
Jiri Slabyc26f0112012-04-02 13:54:41 +02001184 remove_wait_queue(&port->open_wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!tty_hung_up_p(filp))
Jiri Slaby86264342012-04-02 13:54:40 +02001186 port->count++;
1187 port->blocked_open--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (retval)
1190 return retval;
Jiri Slabya85dd822012-04-02 13:54:43 +02001191 port->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return 0;
1193}
1194
1195/*
1196 * This routine is called whenever a serial port is opened. It
1197 * enables interrupts for a serial port, linking in its S structure into
1198 * the IRQ chain. It also performs the serial-specific
1199 * initialization for the tty structure.
1200 */
1201int rs_open(struct tty_struct *tty, struct file * filp)
1202{
1203 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001204 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jiri Slaby410235f2012-03-05 14:52:01 +01001206 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (serial_paranoia_check(info, tty->name, "rs_open"))
1209 return -ENODEV;
1210
Jiri Slaby86264342012-04-02 13:54:40 +02001211 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001213 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /*
1216 * Start up serial port
1217 */
Jiri Slaby467712c2012-04-02 13:54:44 +02001218 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (retval)
1220 return retval;
1221
1222 return block_til_ready(tty, filp, info);
1223}
1224
1225/* Finally, routines used to initialize the serial driver. */
1226
1227static void show_serial_version(void)
1228{
1229 printk("MC68328 serial driver version 1.00\n");
1230}
1231
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001232static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 .open = rs_open,
1234 .close = rs_close,
1235 .write = rs_write,
1236 .flush_chars = rs_flush_chars,
1237 .write_room = rs_write_room,
1238 .chars_in_buffer = rs_chars_in_buffer,
1239 .flush_buffer = rs_flush_buffer,
1240 .ioctl = rs_ioctl,
1241 .throttle = rs_throttle,
1242 .unthrottle = rs_unthrottle,
1243 .set_termios = rs_set_termios,
1244 .stop = rs_stop,
1245 .start = rs_start,
1246 .hangup = rs_hangup,
1247 .set_ldisc = rs_set_ldisc,
1248};
1249
1250/* rs_init inits the driver */
1251static int __init
1252rs68328_init(void)
1253{
Jiri Slaby107afb72012-04-02 13:54:38 +02001254 unsigned long flags;
1255 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct m68k_serial *info;
1257
1258 serial_driver = alloc_tty_driver(NR_PORTS);
1259 if (!serial_driver)
1260 return -ENOMEM;
1261
1262 show_serial_version();
1263
1264 /* Initialize the tty_driver structure */
1265 /* SPARC: Not all of this is exactly right for us. */
1266
1267 serial_driver->name = "ttyS";
1268 serial_driver->major = TTY_MAJOR;
1269 serial_driver->minor_start = 64;
1270 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1271 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1272 serial_driver->init_termios = tty_std_termios;
1273 serial_driver->init_termios.c_cflag =
1274 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1275 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1276 tty_set_operations(serial_driver, &rs_ops);
1277
1278 if (tty_register_driver(serial_driver)) {
1279 put_tty_driver(serial_driver);
1280 printk(KERN_ERR "Couldn't register serial driver\n");
1281 return -ENOMEM;
1282 }
1283
Greg Ungerer727dda82006-06-28 15:54:22 +10001284 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 for(i=0;i<NR_PORTS;i++) {
1287
1288 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001289 tty_port_init(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 info->magic = SERIAL_MAGIC;
1291 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001292 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 info->irq = uart_irqs[i];
1294 info->custom_divisor = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 info->line = i;
1297 info->is_cons = 1; /* Means shortcuts work */
1298
1299 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1300 info->port, info->irq);
1301 printk(" is a builtin MC68328 UART\n");
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303#ifdef CONFIG_M68VZ328
1304 if (i > 0 )
1305 PJSEL &= 0xCF; /* PSW enable second port output */
1306#endif
1307
1308 if (request_irq(uart_irqs[i],
1309 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001310 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001311 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001314 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318module_init(rs68328_init);
1319
1320
1321
1322static void m68328_set_baud(void)
1323{
1324 unsigned short ustcnt;
1325 int i;
1326
1327 ustcnt = USTCNT;
1328 USTCNT = ustcnt & ~USTCNT_TXEN;
1329
1330again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001331 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (baud_table[i] == m68328_console_baud)
1333 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001334 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 m68328_console_baud = 9600;
1336 goto again;
1337 }
1338
1339 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1340 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1341 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1342 ustcnt |= USTCNT_8_7;
1343 ustcnt |= USTCNT_TXEN;
1344 USTCNT = ustcnt;
1345 m68328_console_initted = 1;
1346 return;
1347}
1348
1349
1350int m68328_console_setup(struct console *cp, char *arg)
1351{
1352 int i, n = CONSOLE_BAUD_RATE;
1353
1354 if (!cp)
1355 return(-1);
1356
1357 if (arg)
1358 n = simple_strtoul(arg,NULL,0);
1359
Thiago Farina8b505ca2009-12-09 12:31:30 -08001360 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (baud_table[i] == n)
1362 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001363 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 m68328_console_baud = n;
1365 m68328_console_cbaud = 0;
1366 if (i > 15) {
1367 m68328_console_cbaud |= CBAUDEX;
1368 i -= 15;
1369 }
1370 m68328_console_cbaud |= i;
1371 }
1372
1373 m68328_set_baud(); /* make sure baud rate changes */
1374 return(0);
1375}
1376
1377
1378static struct tty_driver *m68328_console_device(struct console *c, int *index)
1379{
1380 *index = c->index;
1381 return serial_driver;
1382}
1383
1384
1385void m68328_console_write (struct console *co, const char *str,
1386 unsigned int count)
1387{
1388 if (!m68328_console_initted)
1389 m68328_set_baud();
1390 while (count--) {
1391 if (*str == '\n')
1392 rs_put_char('\r');
1393 rs_put_char( *str++ );
1394 }
1395}
1396
1397
1398static struct console m68328_driver = {
1399 .name = "ttyS",
1400 .write = m68328_console_write,
1401 .device = m68328_console_device,
1402 .setup = m68328_console_setup,
1403 .flags = CON_PRINTBUFFER,
1404 .index = -1,
1405};
1406
1407
1408static int __init m68328_console_init(void)
1409{
1410 register_console(&m68328_driver);
1411 return 0;
1412}
1413
1414console_initcall(m68328_console_init);