blob: c1cd2147f9dd098b2e6a4cb693882114470efb42 [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
60#include "68328serial.h"
61
62/* Turn off usage of real serial interrupt code, to "support" Copilot */
63#ifdef CONFIG_XCOPILOT_BUGS
64#undef USE_INTS
65#else
66#define USE_INTS
67#endif
68
69static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72
73/* multiple ports are contiguous in memory */
74m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75
76struct tty_struct m68k_ttys;
77struct m68k_serial *m68k_consinfo = 0;
78
79#define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct tty_driver *serial_driver;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void change_speed(struct m68k_serial *info);
84
85/*
86 * Setup for console. Argument comes from the boot command line.
87 */
88
Christoph Eggerf5e92c32010-07-20 15:26:54 -070089/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
90#ifdef CONFIG_M68VZ328
91#define CONSOLE_BAUD_RATE 19200
92#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
94
Christoph Eggerf5e92c32010-07-20 15:26:54 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifndef CONSOLE_BAUD_RATE
97#define CONSOLE_BAUD_RATE 9600
98#define DEFAULT_CBAUD B9600
99#endif
100
101
102static int m68328_console_initted = 0;
103static int m68328_console_baud = CONSOLE_BAUD_RATE;
104static int m68328_console_cbaud = DEFAULT_CBAUD;
105
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static inline int serial_paranoia_check(struct m68k_serial *info,
108 char *name, const char *routine)
109{
110#ifdef SERIAL_PARANOIA_CHECK
111 static const char *badmagic =
112 "Warning: bad magic number for serial struct %s in %s\n";
113 static const char *badinfo =
114 "Warning: null m68k_serial for %s in %s\n";
115
116 if (!info) {
117 printk(badinfo, name, routine);
118 return 1;
119 }
120 if (info->magic != SERIAL_MAGIC) {
121 printk(badmagic, name, routine);
122 return 1;
123 }
124#endif
125 return 0;
126}
127
128/*
129 * This is used to figure out the divisor speeds and the timeouts
130 */
131static int baud_table[] = {
132 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
133 9600, 19200, 38400, 57600, 115200, 0 };
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Sets or clears DTR/RTS on the requested line */
136static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
137{
138 if (set) {
139 /* set the RTS/CTS line */
140 } else {
141 /* clear it */
142 }
143 return;
144}
145
146/* Utility routines */
147static inline int get_baud(struct m68k_serial *ss)
148{
149 unsigned long result = 115200;
150 unsigned short int baud = uart_addr[ss->line].ubaud;
151 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
152 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
153
154 return result;
155}
156
157/*
158 * ------------------------------------------------------------
159 * rs_stop() and rs_start()
160 *
161 * This routines are called before setting or resetting tty->stopped.
162 * They enable or disable transmitter interrupts, as necessary.
163 * ------------------------------------------------------------
164 */
165static void rs_stop(struct tty_struct *tty)
166{
167 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
168 m68328_uart *uart = &uart_addr[info->line];
169 unsigned long flags;
170
171 if (serial_paranoia_check(info, tty->name, "rs_stop"))
172 return;
173
Greg Ungerer727dda82006-06-28 15:54:22 +1000174 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000176 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Alan Cox09a6ffa2008-04-30 00:54:01 -0700179static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int flags, loops = 0;
182
Greg Ungerer727dda82006-06-28 15:54:22 +1000183 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
186 loops++;
187 udelay(5);
188 }
189
190 UTX_TXDATA = ch;
191 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000192 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700193 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static void rs_start(struct tty_struct *tty)
197{
198 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
199 m68328_uart *uart = &uart_addr[info->line];
200 unsigned long flags;
201
202 if (serial_paranoia_check(info, tty->name, "rs_start"))
203 return;
204
Greg Ungerer727dda82006-06-28 15:54:22 +1000205 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
207#ifdef USE_INTS
208 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
209#else
210 uart->ustcnt |= USTCNT_TXEN;
211#endif
212 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000213 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
David Howells7d12e782006-10-05 14:55:46 +0100216static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000218 struct tty_struct *tty = info->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800220 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /*
223 * This do { } while() loop will get ALL chars out of Rx FIFO
224 */
225#ifndef CONFIG_XCOPILOT_BUGS
226 do {
227#endif
228 ch = GET_FIELD(rx, URX_RXDATA);
229
230 if(info->is_cons) {
231 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233#ifdef CONFIG_MAGIC_SYSRQ
234 } else if (ch == 0x10) { /* ^P */
235 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700236 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 show_buffers();
238/* show_net_buffers(); */
239 return;
240 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600241 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
243#endif /* CONFIG_MAGIC_SYSRQ */
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 if(!tty)
248 goto clear_and_exit;
249
Alan Cox33f0f882006-01-09 20:54:13 -0800250 flag = TTY_NORMAL;
251
Jiri Slabyc21e2652012-04-02 13:54:36 +0200252 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800253 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200254 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800255 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200256 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800257 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200258
Alan Cox33f0f882006-01-09 20:54:13 -0800259 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260#ifndef CONFIG_XCOPILOT_BUGS
261 } while((rx = uart->urx.w) & URX_DATA_READY);
262#endif
263
Greg Ungerer8e63e662006-02-08 09:19:17 +1000264 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266clear_and_exit:
267 return;
268}
269
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800270static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 m68328_uart *uart = &uart_addr[info->line];
273
274 if (info->x_char) {
275 /* Send next char */
276 uart->utx.b.txdata = info->x_char;
277 info->x_char = 0;
278 goto clear_and_return;
279 }
280
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000281 if((info->xmit_cnt <= 0) || info->tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* That's peculiar... TX ints off */
283 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
284 goto clear_and_return;
285 }
286
287 /* Send char */
288 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
289 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
290 info->xmit_cnt--;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if(info->xmit_cnt <= 0) {
293 /* All done for now... TX ints off */
294 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
295 goto clear_and_return;
296 }
297
298clear_and_return:
299 /* Clear interrupt (should be auto)*/
300 return;
301}
302
303/*
304 * This is the serial driver's generic interrupt routine
305 */
David Howells7d12e782006-10-05 14:55:46 +0100306irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Alan Cox25db8ad2008-08-19 20:49:40 -0700308 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 m68328_uart *uart;
310 unsigned short rx;
311 unsigned short tx;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 uart = &uart_addr[info->line];
314 rx = uart->urx.w;
315
316#ifdef USE_INTS
317 tx = uart->utx.w;
318
David Howells7d12e782006-10-05 14:55:46 +0100319 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (tx & UTX_TX_AVAIL) transmit_chars(info);
321#else
David Howells7d12e782006-10-05 14:55:46 +0100322 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif
324 return IRQ_HANDLED;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static int startup(struct m68k_serial * info)
328{
329 m68328_uart *uart = &uart_addr[info->line];
330 unsigned long flags;
331
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200332 if (info->flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334
335 if (!info->xmit_buf) {
336 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
337 if (!info->xmit_buf)
338 return -ENOMEM;
339 }
340
Greg Ungerer727dda82006-06-28 15:54:22 +1000341 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
344 * Clear the FIFO buffers and disable them
345 * (they will be reenabled in change_speed())
346 */
347
348 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
350 (void)uart->urx.w;
351
352 /*
353 * Finally, enable sequencing and interrupts
354 */
355#ifdef USE_INTS
356 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
357 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
358#else
359 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
360#endif
361
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000362 if (info->tty)
363 clear_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
365
366 /*
367 * and set the speed of the serial port
368 */
369
370 change_speed(info);
371
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200372 info->flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000373 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
377/*
378 * This routine will shutdown a serial port; interrupts are disabled, and
379 * DTR is dropped if the hangup on close termio flag is on.
380 */
381static void shutdown(struct m68k_serial * info)
382{
383 m68328_uart *uart = &uart_addr[info->line];
384 unsigned long flags;
385
386 uart->ustcnt = 0; /* All off! */
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200387 if (!(info->flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return;
389
Greg Ungerer727dda82006-06-28 15:54:22 +1000390 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (info->xmit_buf) {
393 free_page((unsigned long) info->xmit_buf);
394 info->xmit_buf = 0;
395 }
396
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000397 if (info->tty)
398 set_bit(TTY_IO_ERROR, &info->tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200400 info->flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000401 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404struct {
405 int divisor, prescale;
406}
407#ifndef CONFIG_M68VZ328
408 hw_baud_table[18] = {
409 {0,0}, /* 0 */
410 {0,0}, /* 50 */
411 {0,0}, /* 75 */
412 {0,0}, /* 110 */
413 {0,0}, /* 134 */
414 {0,0}, /* 150 */
415 {0,0}, /* 200 */
416 {7,0x26}, /* 300 */
417 {6,0x26}, /* 600 */
418 {5,0x26}, /* 1200 */
419 {0,0}, /* 1800 */
420 {4,0x26}, /* 2400 */
421 {3,0x26}, /* 4800 */
422 {2,0x26}, /* 9600 */
423 {1,0x26}, /* 19200 */
424 {0,0x26}, /* 38400 */
425 {1,0x38}, /* 57600 */
426 {0,0x38}, /* 115200 */
427};
428#else
429 hw_baud_table[18] = {
430 {0,0}, /* 0 */
431 {0,0}, /* 50 */
432 {0,0}, /* 75 */
433 {0,0}, /* 110 */
434 {0,0}, /* 134 */
435 {0,0}, /* 150 */
436 {0,0}, /* 200 */
437 {0,0}, /* 300 */
438 {7,0x26}, /* 600 */
439 {6,0x26}, /* 1200 */
440 {0,0}, /* 1800 */
441 {5,0x26}, /* 2400 */
442 {4,0x26}, /* 4800 */
443 {3,0x26}, /* 9600 */
444 {2,0x26}, /* 19200 */
445 {1,0x26}, /* 38400 */
446 {0,0x26}, /* 57600 */
447 {1,0x38}, /* 115200 */
448};
449#endif
450/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
451
452/*
453 * This routine is called to set the UART divisor registers to match
454 * the specified baud rate for a serial port.
455 */
456static void change_speed(struct m68k_serial *info)
457{
458 m68328_uart *uart = &uart_addr[info->line];
459 unsigned short port;
460 unsigned short ustcnt;
461 unsigned cflag;
462 int i;
463
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000464 if (!info->tty || !info->tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return;
Greg Ungererbc0c36d2011-02-08 21:32:36 +1000466 cflag = info->tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!(port = info->port))
468 return;
469
470 ustcnt = uart->ustcnt;
471 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
472
473 i = cflag & CBAUD;
474 if (i & CBAUDEX) {
475 i = (i & ~CBAUDEX) + B38400;
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
479 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
480
481 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
482
483 if ((cflag & CSIZE) == CS8)
484 ustcnt |= USTCNT_8_7;
485
486 if (cflag & CSTOPB)
487 ustcnt |= USTCNT_STOP;
488
489 if (cflag & PARENB)
490 ustcnt |= USTCNT_PARITYEN;
491 if (cflag & PARODD)
492 ustcnt |= USTCNT_ODD_EVEN;
493
494#ifdef CONFIG_SERIAL_68328_RTS_CTS
495 if (cflag & CRTSCTS) {
496 uart->utx.w &= ~ UTX_NOCTS;
497 } else {
498 uart->utx.w |= UTX_NOCTS;
499 }
500#endif
501
502 ustcnt |= USTCNT_TXEN;
503
504 uart->ustcnt = ustcnt;
505 return;
506}
507
508/*
509 * Fair output driver allows a process to speak.
510 */
511static void rs_fair_output(void)
512{
513 int left; /* Output no more than that */
514 unsigned long flags;
515 struct m68k_serial *info = &m68k_soft[0];
516 char c;
517
518 if (info == 0) return;
519 if (info->xmit_buf == 0) return;
520
Greg Ungerer727dda82006-06-28 15:54:22 +1000521 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 left = info->xmit_cnt;
523 while (left != 0) {
524 c = info->xmit_buf[info->xmit_tail];
525 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
526 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000527 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 rs_put_char(c);
530
Greg Ungerer727dda82006-06-28 15:54:22 +1000531 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 left = min(info->xmit_cnt, left-1);
533 }
534
535 /* Last character is being transmitted now (hopefully). */
536 udelay(5);
537
Greg Ungerer727dda82006-06-28 15:54:22 +1000538 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return;
540}
541
542/*
543 * m68k_console_print is registered for printk.
544 */
545void console_print_68328(const char *p)
546{
547 char c;
548
549 while((c=*(p++)) != 0) {
550 if(c == '\n')
551 rs_put_char('\r');
552 rs_put_char(c);
553 }
554
555 /* Comment this if you want to have a strict interrupt-driven output */
556 rs_fair_output();
557
558 return;
559}
560
561static void rs_set_ldisc(struct tty_struct *tty)
562{
563 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
564
565 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
566 return;
567
568 info->is_cons = (tty->termios->c_line == N_TTY);
569
570 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
571}
572
573static void rs_flush_chars(struct tty_struct *tty)
574{
575 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
576 m68328_uart *uart = &uart_addr[info->line];
577 unsigned long flags;
578
579 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
580 return;
581#ifndef USE_INTS
582 for(;;) {
583#endif
584
585 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000586 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
589 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000590 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return;
592 }
593
594#ifdef USE_INTS
595 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
596#else
597 uart->ustcnt |= USTCNT_TXEN;
598#endif
599
600#ifdef USE_INTS
601 if (uart->utx.w & UTX_TX_AVAIL) {
602#else
603 if (1) {
604#endif
605 /* Send char */
606 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
607 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
608 info->xmit_cnt--;
609 }
610
611#ifndef USE_INTS
612 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
613 }
614#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000615 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618extern void console_printn(const char * b, int count);
619
620static int rs_write(struct tty_struct * tty,
621 const unsigned char *buf, int count)
622{
623 int c, total = 0;
624 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
625 m68328_uart *uart = &uart_addr[info->line];
626 unsigned long flags;
627
628 if (serial_paranoia_check(info, tty->name, "rs_write"))
629 return 0;
630
631 if (!tty || !info->xmit_buf)
632 return 0;
633
Greg Ungerer727dda82006-06-28 15:54:22 +1000634 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000636 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
638 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000639 local_irq_restore(flags);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (c <= 0)
642 break;
643
644 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000645
646 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
648 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000649 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 buf += c;
651 count -= c;
652 total += c;
653 }
654
655 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
656 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000657 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#ifndef USE_INTS
659 while(info->xmit_cnt) {
660#endif
661
662 uart->ustcnt |= USTCNT_TXEN;
663#ifdef USE_INTS
664 uart->ustcnt |= USTCNT_TX_INTR_MASK;
665#else
666 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
667#endif
668 if (uart->utx.w & UTX_TX_AVAIL) {
669 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
670 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
671 info->xmit_cnt--;
672 }
673
674#ifndef USE_INTS
675 }
676#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000677 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return total;
681}
682
683static int rs_write_room(struct tty_struct *tty)
684{
685 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
686 int ret;
687
688 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
689 return 0;
690 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
691 if (ret < 0)
692 ret = 0;
693 return ret;
694}
695
696static int rs_chars_in_buffer(struct tty_struct *tty)
697{
698 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
699
700 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
701 return 0;
702 return info->xmit_cnt;
703}
704
705static void rs_flush_buffer(struct tty_struct *tty)
706{
707 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000708 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
711 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000712 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000714 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 tty_wakeup(tty);
716}
717
718/*
719 * ------------------------------------------------------------
720 * rs_throttle()
721 *
722 * This routine is called by the upper-layer tty layer to signal that
723 * incoming characters should be throttled.
724 * ------------------------------------------------------------
725 */
726static void rs_throttle(struct tty_struct * tty)
727{
728 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
729
730 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
731 return;
732
733 if (I_IXOFF(tty))
734 info->x_char = STOP_CHAR(tty);
735
736 /* Turn off RTS line (do this atomic) */
737}
738
739static void rs_unthrottle(struct tty_struct * tty)
740{
741 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
742
743 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
744 return;
745
746 if (I_IXOFF(tty)) {
747 if (info->x_char)
748 info->x_char = 0;
749 else
750 info->x_char = START_CHAR(tty);
751 }
752
753 /* Assert RTS line (do this atomic) */
754}
755
756/*
757 * ------------------------------------------------------------
758 * rs_ioctl() and friends
759 * ------------------------------------------------------------
760 */
761
762static int get_serial_info(struct m68k_serial * info,
763 struct serial_struct * retinfo)
764{
765 struct serial_struct tmp;
766
767 if (!retinfo)
768 return -EFAULT;
769 memset(&tmp, 0, sizeof(tmp));
770 tmp.type = info->type;
771 tmp.line = info->line;
772 tmp.port = info->port;
773 tmp.irq = info->irq;
774 tmp.flags = info->flags;
775 tmp.baud_base = info->baud_base;
776 tmp.close_delay = info->close_delay;
777 tmp.closing_wait = info->closing_wait;
778 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400779 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
780 return -EFAULT;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
783}
784
785static int set_serial_info(struct m68k_serial * info,
786 struct serial_struct * new_info)
787{
788 struct serial_struct new_serial;
789 struct m68k_serial old_info;
790 int retval = 0;
791
792 if (!new_info)
793 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400794 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
795 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 old_info = *info;
797
798 if (!capable(CAP_SYS_ADMIN)) {
799 if ((new_serial.baud_base != info->baud_base) ||
800 (new_serial.type != info->type) ||
801 (new_serial.close_delay != info->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200802 ((new_serial.flags & ~ASYNC_USR_MASK) !=
803 (info->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EPERM;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200805 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
806 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 info->custom_divisor = new_serial.custom_divisor;
808 goto check_and_exit;
809 }
810
811 if (info->count > 1)
812 return -EBUSY;
813
814 /*
815 * OK, past this point, all the error checking has been done.
816 * At this point, we start making changes.....
817 */
818
819 info->baud_base = new_serial.baud_base;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200820 info->flags = ((info->flags & ~ASYNC_FLAGS) |
821 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 info->type = new_serial.type;
823 info->close_delay = new_serial.close_delay;
824 info->closing_wait = new_serial.closing_wait;
825
826check_and_exit:
827 retval = startup(info);
828 return retval;
829}
830
831/*
832 * get_lsr_info - get line status register info
833 *
834 * Purpose: Let user call ioctl() to get info when the UART physically
835 * is emptied. On bus types like RS485, the transmitter must
836 * release the bus after transmitting. This must be done when
837 * the transmit shift register is empty, not be done when the
838 * transmit holding register is empty. This functionality
839 * allows an RS485 driver to be written in user space.
840 */
841static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
842{
843#ifdef CONFIG_SERIAL_68328_RTS_CTS
844 m68328_uart *uart = &uart_addr[info->line];
845#endif
846 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000847 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Greg Ungerer727dda82006-06-28 15:54:22 +1000849 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850#ifdef CONFIG_SERIAL_68328_RTS_CTS
851 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
852#else
853 status = 0;
854#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000855 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400856 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
860 * This routine sends a break character out the serial port.
861 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700862static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 m68328_uart *uart = &uart_addr[info->line];
865 unsigned long flags;
866 if (!info->port)
867 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000868 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#ifdef USE_INTS
870 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700871 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 uart->utx.w &= ~UTX_SEND_BREAK;
873#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000874 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Alan Cox6caa76b2011-02-14 16:27:22 +0000877static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int cmd, unsigned long arg)
879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
881 int retval;
882
883 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
884 return -ENODEV;
885
886 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
887 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
888 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
889 if (tty->flags & (1 << TTY_IO_ERROR))
890 return -EIO;
891 }
892
893 switch (cmd) {
894 case TCSBRK: /* SVID version: non-zero arg --> no break */
895 retval = tty_check_change(tty);
896 if (retval)
897 return retval;
898 tty_wait_until_sent(tty, 0);
899 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700900 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902 case TCSBRKP: /* support for POSIX tcsendbreak() */
903 retval = tty_check_change(tty);
904 if (retval)
905 return retval;
906 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700907 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400910 return get_serial_info(info,
911 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 case TIOCSSERIAL:
913 return set_serial_info(info,
914 (struct serial_struct *) arg);
915 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400916 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400918 if (copy_to_user((struct m68k_serial *) arg,
919 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 default:
923 return -ENOIOCTLCMD;
924 }
925 return 0;
926}
927
Alan Cox606d0992006-12-08 02:38:45 -0800928static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 change_speed(info);
933
934 if ((old_termios->c_cflag & CRTSCTS) &&
935 !(tty->termios->c_cflag & CRTSCTS)) {
936 tty->hw_stopped = 0;
937 rs_start(tty);
938 }
939
940}
941
942/*
943 * ------------------------------------------------------------
944 * rs_close()
945 *
946 * This routine is called when the serial port gets closed. First, we
947 * wait for the last remaining data to be sent. Then, we unlink its
948 * S structure from the interrupt chain if necessary, and we free
949 * that IRQ if nothing is left in the chain.
950 * ------------------------------------------------------------
951 */
952static void rs_close(struct tty_struct *tty, struct file * filp)
953{
954 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
955 m68328_uart *uart = &uart_addr[info->line];
956 unsigned long flags;
957
958 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
959 return;
960
Greg Ungerer727dda82006-06-28 15:54:22 +1000961 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000964 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return;
966 }
967
968 if ((tty->count == 1) && (info->count != 1)) {
969 /*
970 * Uh, oh. tty->count is 1, which means that the tty
971 * structure will be freed. Info->count should always
972 * be one in these conditions. If it's greater than
973 * one, we've got real problems, since it means the
974 * serial port won't be shutdown.
975 */
976 printk("rs_close: bad serial port count; tty->count is 1, "
977 "info->count is %d\n", info->count);
978 info->count = 1;
979 }
980 if (--info->count < 0) {
981 printk("rs_close: bad serial port count for ttyS%d: %d\n",
982 info->line, info->count);
983 info->count = 0;
984 }
985 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000986 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return;
988 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200989 info->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * Now we wait for the transmit buffer to clear; and we notify
992 * the line discipline to only process XON/XOFF characters.
993 */
994 tty->closing = 1;
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200995 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 tty_wait_until_sent(tty, info->closing_wait);
997 /*
998 * At this point we stop accepting input. To do this, we
999 * disable the receive line status interrupts, and tell the
1000 * interrupt driver to stop checking the data ready bit in the
1001 * line status register.
1002 */
1003
1004 uart->ustcnt &= ~USTCNT_RXEN;
1005 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1006
1007 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001008 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 tty_ldisc_flush(tty);
1011 tty->closing = 0;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001012 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013#warning "This is not and has never been valid so fix it"
1014#if 0
1015 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1016 if (tty->ldisc.close)
1017 (tty->ldisc.close)(tty);
1018 tty->ldisc = ldiscs[N_TTY];
1019 tty->termios->c_line = N_TTY;
1020 if (tty->ldisc.open)
1021 (tty->ldisc.open)(tty);
1022 }
1023#endif
1024 if (info->blocked_open) {
1025 if (info->close_delay) {
1026 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1027 }
1028 wake_up_interruptible(&info->open_wait);
1029 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001030 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001032 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
1036 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1037 */
1038void rs_hangup(struct tty_struct *tty)
1039{
1040 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1041
1042 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1043 return;
1044
1045 rs_flush_buffer(tty);
1046 shutdown(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 info->count = 0;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001048 info->flags &= ~ASYNC_NORMAL_ACTIVE;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001049 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 wake_up_interruptible(&info->open_wait);
1051}
1052
1053/*
1054 * ------------------------------------------------------------
1055 * rs_open() and friends
1056 * ------------------------------------------------------------
1057 */
1058static int block_til_ready(struct tty_struct *tty, struct file * filp,
1059 struct m68k_serial *info)
1060{
1061 DECLARE_WAITQUEUE(wait, current);
1062 int retval;
1063 int do_clocal = 0;
1064
1065 /*
1066 * If the device is in the middle of being closed, then block
1067 * until it's done, and then try again.
1068 */
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001069 if (info->flags & ASYNC_CLOSING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 interruptible_sleep_on(&info->close_wait);
1071#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001072 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -EAGAIN;
1074 else
1075 return -ERESTARTSYS;
1076#else
1077 return -EAGAIN;
1078#endif
1079 }
1080
1081 /*
1082 * If non-blocking mode is set, or the port is not enabled,
1083 * then make the check up front and then exit.
1084 */
1085 if ((filp->f_flags & O_NONBLOCK) ||
1086 (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001087 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return 0;
1089 }
1090
1091 if (tty->termios->c_cflag & CLOCAL)
1092 do_clocal = 1;
1093
1094 /*
1095 * Block waiting for the carrier detect and the line to become
1096 * free (i.e., not in use by the callout). While we are in
1097 * this loop, info->count is dropped by one, so that
1098 * rs_close() knows when to free things. We restore it upon
1099 * exit, either normal or abnormal.
1100 */
1101 retval = 0;
1102 add_wait_queue(&info->open_wait, &wait);
1103
1104 info->count--;
1105 info->blocked_open++;
1106 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001107 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001109 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 current->state = TASK_INTERRUPTIBLE;
1111 if (tty_hung_up_p(filp) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001112 !(info->flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#ifdef SERIAL_DO_RESTART
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001114 if (info->flags & ASYNC_HUP_NOTIFY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 retval = -EAGAIN;
1116 else
1117 retval = -ERESTARTSYS;
1118#else
1119 retval = -EAGAIN;
1120#endif
1121 break;
1122 }
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001123 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 if (signal_pending(current)) {
1126 retval = -ERESTARTSYS;
1127 break;
1128 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001129 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001131 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 current->state = TASK_RUNNING;
1134 remove_wait_queue(&info->open_wait, &wait);
1135 if (!tty_hung_up_p(filp))
1136 info->count++;
1137 info->blocked_open--;
1138
1139 if (retval)
1140 return retval;
Jiri Slabycea4b2c2012-04-02 13:54:35 +02001141 info->flags |= ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143}
1144
1145/*
1146 * This routine is called whenever a serial port is opened. It
1147 * enables interrupts for a serial port, linking in its S structure into
1148 * the IRQ chain. It also performs the serial-specific
1149 * initialization for the tty structure.
1150 */
1151int rs_open(struct tty_struct *tty, struct file * filp)
1152{
1153 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001154 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jiri Slaby410235f2012-03-05 14:52:01 +01001156 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (serial_paranoia_check(info, tty->name, "rs_open"))
1159 return -ENODEV;
1160
1161 info->count++;
1162 tty->driver_data = info;
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001163 info->tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /*
1166 * Start up serial port
1167 */
1168 retval = startup(info);
1169 if (retval)
1170 return retval;
1171
1172 return block_til_ready(tty, filp, info);
1173}
1174
1175/* Finally, routines used to initialize the serial driver. */
1176
1177static void show_serial_version(void)
1178{
1179 printk("MC68328 serial driver version 1.00\n");
1180}
1181
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001182static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 .open = rs_open,
1184 .close = rs_close,
1185 .write = rs_write,
1186 .flush_chars = rs_flush_chars,
1187 .write_room = rs_write_room,
1188 .chars_in_buffer = rs_chars_in_buffer,
1189 .flush_buffer = rs_flush_buffer,
1190 .ioctl = rs_ioctl,
1191 .throttle = rs_throttle,
1192 .unthrottle = rs_unthrottle,
1193 .set_termios = rs_set_termios,
1194 .stop = rs_stop,
1195 .start = rs_start,
1196 .hangup = rs_hangup,
1197 .set_ldisc = rs_set_ldisc,
1198};
1199
1200/* rs_init inits the driver */
1201static int __init
1202rs68328_init(void)
1203{
1204 int flags, i;
1205 struct m68k_serial *info;
1206
1207 serial_driver = alloc_tty_driver(NR_PORTS);
1208 if (!serial_driver)
1209 return -ENOMEM;
1210
1211 show_serial_version();
1212
1213 /* Initialize the tty_driver structure */
1214 /* SPARC: Not all of this is exactly right for us. */
1215
1216 serial_driver->name = "ttyS";
1217 serial_driver->major = TTY_MAJOR;
1218 serial_driver->minor_start = 64;
1219 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1220 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1221 serial_driver->init_termios = tty_std_termios;
1222 serial_driver->init_termios.c_cflag =
1223 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1224 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1225 tty_set_operations(serial_driver, &rs_ops);
1226
1227 if (tty_register_driver(serial_driver)) {
1228 put_tty_driver(serial_driver);
1229 printk(KERN_ERR "Couldn't register serial driver\n");
1230 return -ENOMEM;
1231 }
1232
Greg Ungerer727dda82006-06-28 15:54:22 +10001233 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 for(i=0;i<NR_PORTS;i++) {
1236
1237 info = &m68k_soft[i];
1238 info->magic = SERIAL_MAGIC;
1239 info->port = (int) &uart_addr[i];
Greg Ungererbc0c36d2011-02-08 21:32:36 +10001240 info->tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 info->irq = uart_irqs[i];
1242 info->custom_divisor = 16;
1243 info->close_delay = 50;
1244 info->closing_wait = 3000;
1245 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 info->count = 0;
1247 info->blocked_open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 init_waitqueue_head(&info->open_wait);
1249 init_waitqueue_head(&info->close_wait);
1250 info->line = i;
1251 info->is_cons = 1; /* Means shortcuts work */
1252
1253 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1254 info->port, info->irq);
1255 printk(" is a builtin MC68328 UART\n");
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257#ifdef CONFIG_M68VZ328
1258 if (i > 0 )
1259 PJSEL &= 0xCF; /* PSW enable second port output */
1260#endif
1261
1262 if (request_irq(uart_irqs[i],
1263 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001264 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001265 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001268 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272module_init(rs68328_init);
1273
1274
1275
1276static void m68328_set_baud(void)
1277{
1278 unsigned short ustcnt;
1279 int i;
1280
1281 ustcnt = USTCNT;
1282 USTCNT = ustcnt & ~USTCNT_TXEN;
1283
1284again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001285 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (baud_table[i] == m68328_console_baud)
1287 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001288 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 m68328_console_baud = 9600;
1290 goto again;
1291 }
1292
1293 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1294 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1295 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1296 ustcnt |= USTCNT_8_7;
1297 ustcnt |= USTCNT_TXEN;
1298 USTCNT = ustcnt;
1299 m68328_console_initted = 1;
1300 return;
1301}
1302
1303
1304int m68328_console_setup(struct console *cp, char *arg)
1305{
1306 int i, n = CONSOLE_BAUD_RATE;
1307
1308 if (!cp)
1309 return(-1);
1310
1311 if (arg)
1312 n = simple_strtoul(arg,NULL,0);
1313
Thiago Farina8b505ca2009-12-09 12:31:30 -08001314 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (baud_table[i] == n)
1316 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001317 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 m68328_console_baud = n;
1319 m68328_console_cbaud = 0;
1320 if (i > 15) {
1321 m68328_console_cbaud |= CBAUDEX;
1322 i -= 15;
1323 }
1324 m68328_console_cbaud |= i;
1325 }
1326
1327 m68328_set_baud(); /* make sure baud rate changes */
1328 return(0);
1329}
1330
1331
1332static struct tty_driver *m68328_console_device(struct console *c, int *index)
1333{
1334 *index = c->index;
1335 return serial_driver;
1336}
1337
1338
1339void m68328_console_write (struct console *co, const char *str,
1340 unsigned int count)
1341{
1342 if (!m68328_console_initted)
1343 m68328_set_baud();
1344 while (count--) {
1345 if (*str == '\n')
1346 rs_put_char('\r');
1347 rs_put_char( *str++ );
1348 }
1349}
1350
1351
1352static struct console m68328_driver = {
1353 .name = "ttyS",
1354 .write = m68328_console_write,
1355 .device = m68328_console_device,
1356 .setup = m68328_console_setup,
1357 .flags = CON_PRINTBUFFER,
1358 .index = -1,
1359};
1360
1361
1362static int __init m68328_console_init(void)
1363{
1364 register_console(&m68328_driver);
1365 return 0;
1366}
1367
1368console_initcall(m68328_console_init);