blob: d8204f4ebbb40d05bd2ab7a921206a70856af6c3 [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>
20#include <linux/signal.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/interrupt.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/major.h>
27#include <linux/string.h>
28#include <linux/fcntl.h>
29#include <linux/mm.h>
30#include <linux/kernel.h>
31#include <linux/console.h>
32#include <linux/reboot.h>
33#include <linux/keyboard.h>
34#include <linux/init.h>
35#include <linux/pm.h>
36#include <linux/bitops.h>
37#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.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 -070083/* number of characters left in xmit buffer before we ask for more */
84#define WAKEUP_CHARS 256
85
86/* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
88 */
89#undef SERIAL_DEBUG_INTR
90#undef SERIAL_DEBUG_OPEN
91#undef SERIAL_DEBUG_FLOW
92
93#define RS_ISR_PASS_LIMIT 256
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static void change_speed(struct m68k_serial *info);
96
97/*
98 * Setup for console. Argument comes from the boot command line.
99 */
100
101#if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
102#define CONSOLE_BAUD_RATE 115200
103#define DEFAULT_CBAUD B115200
104#else
105 /* (es) */
106 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
107 #ifdef CONFIG_M68VZ328
108 #define CONSOLE_BAUD_RATE 19200
109 #define DEFAULT_CBAUD B19200
110 #endif
111 /* (/es) */
112#endif
113
114#ifndef CONSOLE_BAUD_RATE
115#define CONSOLE_BAUD_RATE 9600
116#define DEFAULT_CBAUD B9600
117#endif
118
119
120static int m68328_console_initted = 0;
121static int m68328_console_baud = CONSOLE_BAUD_RATE;
122static int m68328_console_cbaud = DEFAULT_CBAUD;
123
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline int serial_paranoia_check(struct m68k_serial *info,
126 char *name, const char *routine)
127{
128#ifdef SERIAL_PARANOIA_CHECK
129 static const char *badmagic =
130 "Warning: bad magic number for serial struct %s in %s\n";
131 static const char *badinfo =
132 "Warning: null m68k_serial for %s in %s\n";
133
134 if (!info) {
135 printk(badinfo, name, routine);
136 return 1;
137 }
138 if (info->magic != SERIAL_MAGIC) {
139 printk(badmagic, name, routine);
140 return 1;
141 }
142#endif
143 return 0;
144}
145
146/*
147 * This is used to figure out the divisor speeds and the timeouts
148 */
149static int baud_table[] = {
150 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
151 9600, 19200, 38400, 57600, 115200, 0 };
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Sets or clears DTR/RTS on the requested line */
154static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
155{
156 if (set) {
157 /* set the RTS/CTS line */
158 } else {
159 /* clear it */
160 }
161 return;
162}
163
164/* Utility routines */
165static inline int get_baud(struct m68k_serial *ss)
166{
167 unsigned long result = 115200;
168 unsigned short int baud = uart_addr[ss->line].ubaud;
169 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
170 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
171
172 return result;
173}
174
175/*
176 * ------------------------------------------------------------
177 * rs_stop() and rs_start()
178 *
179 * This routines are called before setting or resetting tty->stopped.
180 * They enable or disable transmitter interrupts, as necessary.
181 * ------------------------------------------------------------
182 */
183static void rs_stop(struct tty_struct *tty)
184{
185 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
186 m68328_uart *uart = &uart_addr[info->line];
187 unsigned long flags;
188
189 if (serial_paranoia_check(info, tty->name, "rs_stop"))
190 return;
191
Greg Ungerer727dda82006-06-28 15:54:22 +1000192 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000194 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Alan Cox09a6ffa2008-04-30 00:54:01 -0700197static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int flags, loops = 0;
200
Greg Ungerer727dda82006-06-28 15:54:22 +1000201 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
204 loops++;
205 udelay(5);
206 }
207
208 UTX_TXDATA = ch;
209 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000210 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700211 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void rs_start(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_start"))
221 return;
222
Greg Ungerer727dda82006-06-28 15:54:22 +1000223 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
225#ifdef USE_INTS
226 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
227#else
228 uart->ustcnt |= USTCNT_TXEN;
229#endif
230 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000231 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/* Drop into either the boot monitor or kadb upon receiving a break
235 * from keyboard/console input.
236 */
237static void batten_down_hatches(void)
238{
239 /* Drop into the debugger */
240}
241
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800242static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244#if 0
245 if(status & DCD) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100246 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ((info->curregs[3] & AUTO_ENAB)==0)) {
248 info->curregs[3] |= AUTO_ENAB;
249 info->pendregs[3] |= AUTO_ENAB;
250 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
251 }
252 } else {
253 if((info->curregs[3] & AUTO_ENAB)) {
254 info->curregs[3] &= ~AUTO_ENAB;
255 info->pendregs[3] &= ~AUTO_ENAB;
256 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
257 }
258 }
259#endif
260 /* If this is console input and this is a
261 * 'break asserted' status change interrupt
262 * see if we can drop into the debugger
263 */
264 if((status & URX_BREAK) && info->break_abort)
265 batten_down_hatches();
266 return;
267}
268
David Howells7d12e782006-10-05 14:55:46 +0100269static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100271 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800273 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * This do { } while() loop will get ALL chars out of Rx FIFO
277 */
278#ifndef CONFIG_XCOPILOT_BUGS
279 do {
280#endif
281 ch = GET_FIELD(rx, URX_RXDATA);
282
283 if(info->is_cons) {
284 if(URX_BREAK & rx) { /* whee, break received */
285 status_handle(info, rx);
286 return;
287#ifdef CONFIG_MAGIC_SYSRQ
288 } else if (ch == 0x10) { /* ^P */
289 show_state();
290 show_free_areas();
291 show_buffers();
292/* show_net_buffers(); */
293 return;
294 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600295 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return;
297#endif /* CONFIG_MAGIC_SYSRQ */
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 if(!tty)
302 goto clear_and_exit;
303
Alan Cox33f0f882006-01-09 20:54:13 -0800304 flag = TTY_NORMAL;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800307 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 status_handle(info, rx);
309 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800310 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 status_handle(info, rx);
312 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800313 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Alan Cox33f0f882006-01-09 20:54:13 -0800316 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#ifndef CONFIG_XCOPILOT_BUGS
318 } while((rx = uart->urx.w) & URX_DATA_READY);
319#endif
320
Greg Ungerer8e63e662006-02-08 09:19:17 +1000321 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323clear_and_exit:
324 return;
325}
326
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800327static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 m68328_uart *uart = &uart_addr[info->line];
330
331 if (info->x_char) {
332 /* Send next char */
333 uart->utx.b.txdata = info->x_char;
334 info->x_char = 0;
335 goto clear_and_return;
336 }
337
Takashi Iwaia88487c2008-07-16 21:54:42 +0100338 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* That's peculiar... TX ints off */
340 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
341 goto clear_and_return;
342 }
343
344 /* Send char */
345 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
346 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
347 info->xmit_cnt--;
348
349 if (info->xmit_cnt < WAKEUP_CHARS)
350 schedule_work(&info->tqueue);
351
352 if(info->xmit_cnt <= 0) {
353 /* All done for now... TX ints off */
354 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
355 goto clear_and_return;
356 }
357
358clear_and_return:
359 /* Clear interrupt (should be auto)*/
360 return;
361}
362
363/*
364 * This is the serial driver's generic interrupt routine
365 */
David Howells7d12e782006-10-05 14:55:46 +0100366irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Alan Cox25db8ad2008-08-19 20:49:40 -0700368 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 m68328_uart *uart;
370 unsigned short rx;
371 unsigned short tx;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 uart = &uart_addr[info->line];
374 rx = uart->urx.w;
375
376#ifdef USE_INTS
377 tx = uart->utx.w;
378
David Howells7d12e782006-10-05 14:55:46 +0100379 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (tx & UTX_TX_AVAIL) transmit_chars(info);
381#else
David Howells7d12e782006-10-05 14:55:46 +0100382 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif
384 return IRQ_HANDLED;
385}
386
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000387static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000389 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct tty_struct *tty;
391
Takashi Iwaia88487c2008-07-16 21:54:42 +0100392 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!tty)
394 return;
395#if 0
396 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
397 tty_wakeup(tty);
398 }
399#endif
400}
401
402/*
403 * This routine is called from the scheduler tqueue when the interrupt
404 * routine has signalled that a hangup has occurred. The path of
405 * hangup processing is:
406 *
407 * serial interrupt routine -> (scheduler tqueue) ->
408 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
409 *
410 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000411static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000413 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct tty_struct *tty;
415
Takashi Iwaia88487c2008-07-16 21:54:42 +0100416 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!tty)
418 return;
419
420 tty_hangup(tty);
421}
422
423
424static int startup(struct m68k_serial * info)
425{
426 m68328_uart *uart = &uart_addr[info->line];
427 unsigned long flags;
428
429 if (info->flags & S_INITIALIZED)
430 return 0;
431
432 if (!info->xmit_buf) {
433 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
434 if (!info->xmit_buf)
435 return -ENOMEM;
436 }
437
Greg Ungerer727dda82006-06-28 15:54:22 +1000438 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * Clear the FIFO buffers and disable them
442 * (they will be reenabled in change_speed())
443 */
444
445 uart->ustcnt = USTCNT_UEN;
446 info->xmit_fifo_size = 1;
447 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
448 (void)uart->urx.w;
449
450 /*
451 * Finally, enable sequencing and interrupts
452 */
453#ifdef USE_INTS
454 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
455 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
456#else
457 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
458#endif
459
Takashi Iwaia88487c2008-07-16 21:54:42 +0100460 if (info->port.tty)
461 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
463
464 /*
465 * and set the speed of the serial port
466 */
467
468 change_speed(info);
469
470 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000471 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473}
474
475/*
476 * This routine will shutdown a serial port; interrupts are disabled, and
477 * DTR is dropped if the hangup on close termio flag is on.
478 */
479static void shutdown(struct m68k_serial * info)
480{
481 m68328_uart *uart = &uart_addr[info->line];
482 unsigned long flags;
483
484 uart->ustcnt = 0; /* All off! */
485 if (!(info->flags & S_INITIALIZED))
486 return;
487
Greg Ungerer727dda82006-06-28 15:54:22 +1000488 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (info->xmit_buf) {
491 free_page((unsigned long) info->xmit_buf);
492 info->xmit_buf = 0;
493 }
494
Takashi Iwaia88487c2008-07-16 21:54:42 +0100495 if (info->port.tty)
496 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000499 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502struct {
503 int divisor, prescale;
504}
505#ifndef CONFIG_M68VZ328
506 hw_baud_table[18] = {
507 {0,0}, /* 0 */
508 {0,0}, /* 50 */
509 {0,0}, /* 75 */
510 {0,0}, /* 110 */
511 {0,0}, /* 134 */
512 {0,0}, /* 150 */
513 {0,0}, /* 200 */
514 {7,0x26}, /* 300 */
515 {6,0x26}, /* 600 */
516 {5,0x26}, /* 1200 */
517 {0,0}, /* 1800 */
518 {4,0x26}, /* 2400 */
519 {3,0x26}, /* 4800 */
520 {2,0x26}, /* 9600 */
521 {1,0x26}, /* 19200 */
522 {0,0x26}, /* 38400 */
523 {1,0x38}, /* 57600 */
524 {0,0x38}, /* 115200 */
525};
526#else
527 hw_baud_table[18] = {
528 {0,0}, /* 0 */
529 {0,0}, /* 50 */
530 {0,0}, /* 75 */
531 {0,0}, /* 110 */
532 {0,0}, /* 134 */
533 {0,0}, /* 150 */
534 {0,0}, /* 200 */
535 {0,0}, /* 300 */
536 {7,0x26}, /* 600 */
537 {6,0x26}, /* 1200 */
538 {0,0}, /* 1800 */
539 {5,0x26}, /* 2400 */
540 {4,0x26}, /* 4800 */
541 {3,0x26}, /* 9600 */
542 {2,0x26}, /* 19200 */
543 {1,0x26}, /* 38400 */
544 {0,0x26}, /* 57600 */
545 {1,0x38}, /* 115200 */
546};
547#endif
548/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
549
550/*
551 * This routine is called to set the UART divisor registers to match
552 * the specified baud rate for a serial port.
553 */
554static void change_speed(struct m68k_serial *info)
555{
556 m68328_uart *uart = &uart_addr[info->line];
557 unsigned short port;
558 unsigned short ustcnt;
559 unsigned cflag;
560 int i;
561
Takashi Iwaia88487c2008-07-16 21:54:42 +0100562 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100564 cflag = info->port.tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!(port = info->port))
566 return;
567
568 ustcnt = uart->ustcnt;
569 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
570
571 i = cflag & CBAUD;
572 if (i & CBAUDEX) {
573 i = (i & ~CBAUDEX) + B38400;
574 }
575
576 info->baud = baud_table[i];
577 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
578 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
579
580 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
581
582 if ((cflag & CSIZE) == CS8)
583 ustcnt |= USTCNT_8_7;
584
585 if (cflag & CSTOPB)
586 ustcnt |= USTCNT_STOP;
587
588 if (cflag & PARENB)
589 ustcnt |= USTCNT_PARITYEN;
590 if (cflag & PARODD)
591 ustcnt |= USTCNT_ODD_EVEN;
592
593#ifdef CONFIG_SERIAL_68328_RTS_CTS
594 if (cflag & CRTSCTS) {
595 uart->utx.w &= ~ UTX_NOCTS;
596 } else {
597 uart->utx.w |= UTX_NOCTS;
598 }
599#endif
600
601 ustcnt |= USTCNT_TXEN;
602
603 uart->ustcnt = ustcnt;
604 return;
605}
606
607/*
608 * Fair output driver allows a process to speak.
609 */
610static void rs_fair_output(void)
611{
612 int left; /* Output no more than that */
613 unsigned long flags;
614 struct m68k_serial *info = &m68k_soft[0];
615 char c;
616
617 if (info == 0) return;
618 if (info->xmit_buf == 0) return;
619
Greg Ungerer727dda82006-06-28 15:54:22 +1000620 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 left = info->xmit_cnt;
622 while (left != 0) {
623 c = info->xmit_buf[info->xmit_tail];
624 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
625 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000626 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 rs_put_char(c);
629
Greg Ungerer727dda82006-06-28 15:54:22 +1000630 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 left = min(info->xmit_cnt, left-1);
632 }
633
634 /* Last character is being transmitted now (hopefully). */
635 udelay(5);
636
Greg Ungerer727dda82006-06-28 15:54:22 +1000637 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
639}
640
641/*
642 * m68k_console_print is registered for printk.
643 */
644void console_print_68328(const char *p)
645{
646 char c;
647
648 while((c=*(p++)) != 0) {
649 if(c == '\n')
650 rs_put_char('\r');
651 rs_put_char(c);
652 }
653
654 /* Comment this if you want to have a strict interrupt-driven output */
655 rs_fair_output();
656
657 return;
658}
659
660static void rs_set_ldisc(struct tty_struct *tty)
661{
662 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
663
664 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
665 return;
666
667 info->is_cons = (tty->termios->c_line == N_TTY);
668
669 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
670}
671
672static void rs_flush_chars(struct tty_struct *tty)
673{
674 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
675 m68328_uart *uart = &uart_addr[info->line];
676 unsigned long flags;
677
678 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
679 return;
680#ifndef USE_INTS
681 for(;;) {
682#endif
683
684 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000685 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
688 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000689 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691 }
692
693#ifdef USE_INTS
694 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
695#else
696 uart->ustcnt |= USTCNT_TXEN;
697#endif
698
699#ifdef USE_INTS
700 if (uart->utx.w & UTX_TX_AVAIL) {
701#else
702 if (1) {
703#endif
704 /* Send char */
705 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
706 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
707 info->xmit_cnt--;
708 }
709
710#ifndef USE_INTS
711 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
712 }
713#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000714 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717extern void console_printn(const char * b, int count);
718
719static int rs_write(struct tty_struct * tty,
720 const unsigned char *buf, int count)
721{
722 int c, total = 0;
723 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
724 m68328_uart *uart = &uart_addr[info->line];
725 unsigned long flags;
726
727 if (serial_paranoia_check(info, tty->name, "rs_write"))
728 return 0;
729
730 if (!tty || !info->xmit_buf)
731 return 0;
732
Greg Ungerer727dda82006-06-28 15:54:22 +1000733 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000735 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
737 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000738 local_irq_restore(flags);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (c <= 0)
741 break;
742
743 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000744
745 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
747 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000748 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 buf += c;
750 count -= c;
751 total += c;
752 }
753
754 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
755 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000756 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#ifndef USE_INTS
758 while(info->xmit_cnt) {
759#endif
760
761 uart->ustcnt |= USTCNT_TXEN;
762#ifdef USE_INTS
763 uart->ustcnt |= USTCNT_TX_INTR_MASK;
764#else
765 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
766#endif
767 if (uart->utx.w & UTX_TX_AVAIL) {
768 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
769 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
770 info->xmit_cnt--;
771 }
772
773#ifndef USE_INTS
774 }
775#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000776 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return total;
780}
781
782static int rs_write_room(struct tty_struct *tty)
783{
784 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
785 int ret;
786
787 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
788 return 0;
789 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
790 if (ret < 0)
791 ret = 0;
792 return ret;
793}
794
795static int rs_chars_in_buffer(struct tty_struct *tty)
796{
797 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
798
799 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
800 return 0;
801 return info->xmit_cnt;
802}
803
804static void rs_flush_buffer(struct tty_struct *tty)
805{
806 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000807 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
810 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000811 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000813 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tty_wakeup(tty);
815}
816
817/*
818 * ------------------------------------------------------------
819 * rs_throttle()
820 *
821 * This routine is called by the upper-layer tty layer to signal that
822 * incoming characters should be throttled.
823 * ------------------------------------------------------------
824 */
825static void rs_throttle(struct tty_struct * tty)
826{
827 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
828
829 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
830 return;
831
832 if (I_IXOFF(tty))
833 info->x_char = STOP_CHAR(tty);
834
835 /* Turn off RTS line (do this atomic) */
836}
837
838static void rs_unthrottle(struct tty_struct * tty)
839{
840 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
841
842 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
843 return;
844
845 if (I_IXOFF(tty)) {
846 if (info->x_char)
847 info->x_char = 0;
848 else
849 info->x_char = START_CHAR(tty);
850 }
851
852 /* Assert RTS line (do this atomic) */
853}
854
855/*
856 * ------------------------------------------------------------
857 * rs_ioctl() and friends
858 * ------------------------------------------------------------
859 */
860
861static int get_serial_info(struct m68k_serial * info,
862 struct serial_struct * retinfo)
863{
864 struct serial_struct tmp;
865
866 if (!retinfo)
867 return -EFAULT;
868 memset(&tmp, 0, sizeof(tmp));
869 tmp.type = info->type;
870 tmp.line = info->line;
871 tmp.port = info->port;
872 tmp.irq = info->irq;
873 tmp.flags = info->flags;
874 tmp.baud_base = info->baud_base;
875 tmp.close_delay = info->close_delay;
876 tmp.closing_wait = info->closing_wait;
877 tmp.custom_divisor = info->custom_divisor;
878 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
879 return 0;
880}
881
882static int set_serial_info(struct m68k_serial * info,
883 struct serial_struct * new_info)
884{
885 struct serial_struct new_serial;
886 struct m68k_serial old_info;
887 int retval = 0;
888
889 if (!new_info)
890 return -EFAULT;
891 copy_from_user(&new_serial,new_info,sizeof(new_serial));
892 old_info = *info;
893
894 if (!capable(CAP_SYS_ADMIN)) {
895 if ((new_serial.baud_base != info->baud_base) ||
896 (new_serial.type != info->type) ||
897 (new_serial.close_delay != info->close_delay) ||
898 ((new_serial.flags & ~S_USR_MASK) !=
899 (info->flags & ~S_USR_MASK)))
900 return -EPERM;
901 info->flags = ((info->flags & ~S_USR_MASK) |
902 (new_serial.flags & S_USR_MASK));
903 info->custom_divisor = new_serial.custom_divisor;
904 goto check_and_exit;
905 }
906
907 if (info->count > 1)
908 return -EBUSY;
909
910 /*
911 * OK, past this point, all the error checking has been done.
912 * At this point, we start making changes.....
913 */
914
915 info->baud_base = new_serial.baud_base;
916 info->flags = ((info->flags & ~S_FLAGS) |
917 (new_serial.flags & S_FLAGS));
918 info->type = new_serial.type;
919 info->close_delay = new_serial.close_delay;
920 info->closing_wait = new_serial.closing_wait;
921
922check_and_exit:
923 retval = startup(info);
924 return retval;
925}
926
927/*
928 * get_lsr_info - get line status register info
929 *
930 * Purpose: Let user call ioctl() to get info when the UART physically
931 * is emptied. On bus types like RS485, the transmitter must
932 * release the bus after transmitting. This must be done when
933 * the transmit shift register is empty, not be done when the
934 * transmit holding register is empty. This functionality
935 * allows an RS485 driver to be written in user space.
936 */
937static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
938{
939#ifdef CONFIG_SERIAL_68328_RTS_CTS
940 m68328_uart *uart = &uart_addr[info->line];
941#endif
942 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000943 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Greg Ungerer727dda82006-06-28 15:54:22 +1000945 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#ifdef CONFIG_SERIAL_68328_RTS_CTS
947 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
948#else
949 status = 0;
950#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000951 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 put_user(status,value);
953 return 0;
954}
955
956/*
957 * This routine sends a break character out the serial port.
958 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700959static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 m68328_uart *uart = &uart_addr[info->line];
962 unsigned long flags;
963 if (!info->port)
964 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000965 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#ifdef USE_INTS
967 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700968 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 uart->utx.w &= ~UTX_SEND_BREAK;
970#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000971 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static int rs_ioctl(struct tty_struct *tty, struct file * file,
975 unsigned int cmd, unsigned long arg)
976{
977 int error;
978 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
979 int retval;
980
981 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
982 return -ENODEV;
983
984 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
985 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
986 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
987 if (tty->flags & (1 << TTY_IO_ERROR))
988 return -EIO;
989 }
990
991 switch (cmd) {
992 case TCSBRK: /* SVID version: non-zero arg --> no break */
993 retval = tty_check_change(tty);
994 if (retval)
995 return retval;
996 tty_wait_until_sent(tty, 0);
997 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700998 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return 0;
1000 case TCSBRKP: /* support for POSIX tcsendbreak() */
1001 retval = tty_check_change(tty);
1002 if (retval)
1003 return retval;
1004 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001005 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case TIOCGSERIAL:
1008 if (access_ok(VERIFY_WRITE, (void *) arg,
1009 sizeof(struct serial_struct)))
1010 return get_serial_info(info,
1011 (struct serial_struct *) arg);
1012 return -EFAULT;
1013 case TIOCSSERIAL:
1014 return set_serial_info(info,
1015 (struct serial_struct *) arg);
1016 case TIOCSERGETLSR: /* Get line status register */
1017 if (access_ok(VERIFY_WRITE, (void *) arg,
Greg Ungerer727dda82006-06-28 15:54:22 +10001018 sizeof(unsigned int)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return get_lsr_info(info, (unsigned int *) arg);
1020 return -EFAULT;
1021 case TIOCSERGSTRUCT:
1022 if (!access_ok(VERIFY_WRITE, (void *) arg,
1023 sizeof(struct m68k_serial)))
1024 return -EFAULT;
1025 copy_to_user((struct m68k_serial *) arg,
1026 info, sizeof(struct m68k_serial));
1027 return 0;
1028
1029 default:
1030 return -ENOIOCTLCMD;
1031 }
1032 return 0;
1033}
1034
Alan Cox606d0992006-12-08 02:38:45 -08001035static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 change_speed(info);
1040
1041 if ((old_termios->c_cflag & CRTSCTS) &&
1042 !(tty->termios->c_cflag & CRTSCTS)) {
1043 tty->hw_stopped = 0;
1044 rs_start(tty);
1045 }
1046
1047}
1048
1049/*
1050 * ------------------------------------------------------------
1051 * rs_close()
1052 *
1053 * This routine is called when the serial port gets closed. First, we
1054 * wait for the last remaining data to be sent. Then, we unlink its
1055 * S structure from the interrupt chain if necessary, and we free
1056 * that IRQ if nothing is left in the chain.
1057 * ------------------------------------------------------------
1058 */
1059static void rs_close(struct tty_struct *tty, struct file * filp)
1060{
1061 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1062 m68328_uart *uart = &uart_addr[info->line];
1063 unsigned long flags;
1064
1065 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1066 return;
1067
Greg Ungerer727dda82006-06-28 15:54:22 +10001068 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001071 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return;
1073 }
1074
1075 if ((tty->count == 1) && (info->count != 1)) {
1076 /*
1077 * Uh, oh. tty->count is 1, which means that the tty
1078 * structure will be freed. Info->count should always
1079 * be one in these conditions. If it's greater than
1080 * one, we've got real problems, since it means the
1081 * serial port won't be shutdown.
1082 */
1083 printk("rs_close: bad serial port count; tty->count is 1, "
1084 "info->count is %d\n", info->count);
1085 info->count = 1;
1086 }
1087 if (--info->count < 0) {
1088 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1089 info->line, info->count);
1090 info->count = 0;
1091 }
1092 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001093 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return;
1095 }
1096 info->flags |= S_CLOSING;
1097 /*
1098 * Now we wait for the transmit buffer to clear; and we notify
1099 * the line discipline to only process XON/XOFF characters.
1100 */
1101 tty->closing = 1;
1102 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1103 tty_wait_until_sent(tty, info->closing_wait);
1104 /*
1105 * At this point we stop accepting input. To do this, we
1106 * disable the receive line status interrupts, and tell the
1107 * interrupt driver to stop checking the data ready bit in the
1108 * line status register.
1109 */
1110
1111 uart->ustcnt &= ~USTCNT_RXEN;
1112 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1113
1114 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001115 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 tty_ldisc_flush(tty);
1118 tty->closing = 0;
1119 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001120 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121#warning "This is not and has never been valid so fix it"
1122#if 0
1123 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1124 if (tty->ldisc.close)
1125 (tty->ldisc.close)(tty);
1126 tty->ldisc = ldiscs[N_TTY];
1127 tty->termios->c_line = N_TTY;
1128 if (tty->ldisc.open)
1129 (tty->ldisc.open)(tty);
1130 }
1131#endif
1132 if (info->blocked_open) {
1133 if (info->close_delay) {
1134 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1135 }
1136 wake_up_interruptible(&info->open_wait);
1137 }
1138 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1139 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001140 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143/*
1144 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1145 */
1146void rs_hangup(struct tty_struct *tty)
1147{
1148 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1149
1150 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1151 return;
1152
1153 rs_flush_buffer(tty);
1154 shutdown(info);
1155 info->event = 0;
1156 info->count = 0;
1157 info->flags &= ~S_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001158 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 wake_up_interruptible(&info->open_wait);
1160}
1161
1162/*
1163 * ------------------------------------------------------------
1164 * rs_open() and friends
1165 * ------------------------------------------------------------
1166 */
1167static int block_til_ready(struct tty_struct *tty, struct file * filp,
1168 struct m68k_serial *info)
1169{
1170 DECLARE_WAITQUEUE(wait, current);
1171 int retval;
1172 int do_clocal = 0;
1173
1174 /*
1175 * If the device is in the middle of being closed, then block
1176 * until it's done, and then try again.
1177 */
1178 if (info->flags & S_CLOSING) {
1179 interruptible_sleep_on(&info->close_wait);
1180#ifdef SERIAL_DO_RESTART
1181 if (info->flags & S_HUP_NOTIFY)
1182 return -EAGAIN;
1183 else
1184 return -ERESTARTSYS;
1185#else
1186 return -EAGAIN;
1187#endif
1188 }
1189
1190 /*
1191 * If non-blocking mode is set, or the port is not enabled,
1192 * then make the check up front and then exit.
1193 */
1194 if ((filp->f_flags & O_NONBLOCK) ||
1195 (tty->flags & (1 << TTY_IO_ERROR))) {
1196 info->flags |= S_NORMAL_ACTIVE;
1197 return 0;
1198 }
1199
1200 if (tty->termios->c_cflag & CLOCAL)
1201 do_clocal = 1;
1202
1203 /*
1204 * Block waiting for the carrier detect and the line to become
1205 * free (i.e., not in use by the callout). While we are in
1206 * this loop, info->count is dropped by one, so that
1207 * rs_close() knows when to free things. We restore it upon
1208 * exit, either normal or abnormal.
1209 */
1210 retval = 0;
1211 add_wait_queue(&info->open_wait, &wait);
1212
1213 info->count--;
1214 info->blocked_open++;
1215 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001216 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001218 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 current->state = TASK_INTERRUPTIBLE;
1220 if (tty_hung_up_p(filp) ||
1221 !(info->flags & S_INITIALIZED)) {
1222#ifdef SERIAL_DO_RESTART
1223 if (info->flags & S_HUP_NOTIFY)
1224 retval = -EAGAIN;
1225 else
1226 retval = -ERESTARTSYS;
1227#else
1228 retval = -EAGAIN;
1229#endif
1230 break;
1231 }
1232 if (!(info->flags & S_CLOSING) && do_clocal)
1233 break;
1234 if (signal_pending(current)) {
1235 retval = -ERESTARTSYS;
1236 break;
1237 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001238 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001240 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 current->state = TASK_RUNNING;
1243 remove_wait_queue(&info->open_wait, &wait);
1244 if (!tty_hung_up_p(filp))
1245 info->count++;
1246 info->blocked_open--;
1247
1248 if (retval)
1249 return retval;
1250 info->flags |= S_NORMAL_ACTIVE;
1251 return 0;
1252}
1253
1254/*
1255 * This routine is called whenever a serial port is opened. It
1256 * enables interrupts for a serial port, linking in its S structure into
1257 * the IRQ chain. It also performs the serial-specific
1258 * initialization for the tty structure.
1259 */
1260int rs_open(struct tty_struct *tty, struct file * filp)
1261{
1262 struct m68k_serial *info;
1263 int retval, line;
1264
1265 line = tty->index;
1266
1267 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1268 return -ENODEV;
1269
1270 info = &m68k_soft[line];
1271
1272 if (serial_paranoia_check(info, tty->name, "rs_open"))
1273 return -ENODEV;
1274
1275 info->count++;
1276 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001277 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /*
1280 * Start up serial port
1281 */
1282 retval = startup(info);
1283 if (retval)
1284 return retval;
1285
1286 return block_til_ready(tty, filp, info);
1287}
1288
1289/* Finally, routines used to initialize the serial driver. */
1290
1291static void show_serial_version(void)
1292{
1293 printk("MC68328 serial driver version 1.00\n");
1294}
1295
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001296static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .open = rs_open,
1298 .close = rs_close,
1299 .write = rs_write,
1300 .flush_chars = rs_flush_chars,
1301 .write_room = rs_write_room,
1302 .chars_in_buffer = rs_chars_in_buffer,
1303 .flush_buffer = rs_flush_buffer,
1304 .ioctl = rs_ioctl,
1305 .throttle = rs_throttle,
1306 .unthrottle = rs_unthrottle,
1307 .set_termios = rs_set_termios,
1308 .stop = rs_stop,
1309 .start = rs_start,
1310 .hangup = rs_hangup,
1311 .set_ldisc = rs_set_ldisc,
1312};
1313
1314/* rs_init inits the driver */
1315static int __init
1316rs68328_init(void)
1317{
1318 int flags, i;
1319 struct m68k_serial *info;
1320
1321 serial_driver = alloc_tty_driver(NR_PORTS);
1322 if (!serial_driver)
1323 return -ENOMEM;
1324
1325 show_serial_version();
1326
1327 /* Initialize the tty_driver structure */
1328 /* SPARC: Not all of this is exactly right for us. */
1329
1330 serial_driver->name = "ttyS";
1331 serial_driver->major = TTY_MAJOR;
1332 serial_driver->minor_start = 64;
1333 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1334 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1335 serial_driver->init_termios = tty_std_termios;
1336 serial_driver->init_termios.c_cflag =
1337 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1338 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1339 tty_set_operations(serial_driver, &rs_ops);
1340
1341 if (tty_register_driver(serial_driver)) {
1342 put_tty_driver(serial_driver);
1343 printk(KERN_ERR "Couldn't register serial driver\n");
1344 return -ENOMEM;
1345 }
1346
Greg Ungerer727dda82006-06-28 15:54:22 +10001347 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 for(i=0;i<NR_PORTS;i++) {
1350
1351 info = &m68k_soft[i];
1352 info->magic = SERIAL_MAGIC;
1353 info->port = (int) &uart_addr[i];
Takashi Iwaia88487c2008-07-16 21:54:42 +01001354 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 info->irq = uart_irqs[i];
1356 info->custom_divisor = 16;
1357 info->close_delay = 50;
1358 info->closing_wait = 3000;
1359 info->x_char = 0;
1360 info->event = 0;
1361 info->count = 0;
1362 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001363 INIT_WORK(&info->tqueue, do_softint);
1364 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 init_waitqueue_head(&info->open_wait);
1366 init_waitqueue_head(&info->close_wait);
1367 info->line = i;
1368 info->is_cons = 1; /* Means shortcuts work */
1369
1370 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1371 info->port, info->irq);
1372 printk(" is a builtin MC68328 UART\n");
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374#ifdef CONFIG_M68VZ328
1375 if (i > 0 )
1376 PJSEL &= 0xCF; /* PSW enable second port output */
1377#endif
1378
1379 if (request_irq(uart_irqs[i],
1380 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001381 IRQF_DISABLED,
Alan Cox25db8ad2008-08-19 20:49:40 -07001382 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001385 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return 0;
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389module_init(rs68328_init);
1390
1391
1392
1393static void m68328_set_baud(void)
1394{
1395 unsigned short ustcnt;
1396 int i;
1397
1398 ustcnt = USTCNT;
1399 USTCNT = ustcnt & ~USTCNT_TXEN;
1400
1401again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001402 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (baud_table[i] == m68328_console_baud)
1404 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001405 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 m68328_console_baud = 9600;
1407 goto again;
1408 }
1409
1410 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1411 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1412 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1413 ustcnt |= USTCNT_8_7;
1414 ustcnt |= USTCNT_TXEN;
1415 USTCNT = ustcnt;
1416 m68328_console_initted = 1;
1417 return;
1418}
1419
1420
1421int m68328_console_setup(struct console *cp, char *arg)
1422{
1423 int i, n = CONSOLE_BAUD_RATE;
1424
1425 if (!cp)
1426 return(-1);
1427
1428 if (arg)
1429 n = simple_strtoul(arg,NULL,0);
1430
Thiago Farina8b505ca2009-12-09 12:31:30 -08001431 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (baud_table[i] == n)
1433 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001434 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 m68328_console_baud = n;
1436 m68328_console_cbaud = 0;
1437 if (i > 15) {
1438 m68328_console_cbaud |= CBAUDEX;
1439 i -= 15;
1440 }
1441 m68328_console_cbaud |= i;
1442 }
1443
1444 m68328_set_baud(); /* make sure baud rate changes */
1445 return(0);
1446}
1447
1448
1449static struct tty_driver *m68328_console_device(struct console *c, int *index)
1450{
1451 *index = c->index;
1452 return serial_driver;
1453}
1454
1455
1456void m68328_console_write (struct console *co, const char *str,
1457 unsigned int count)
1458{
1459 if (!m68328_console_initted)
1460 m68328_set_baud();
1461 while (count--) {
1462 if (*str == '\n')
1463 rs_put_char('\r');
1464 rs_put_char( *str++ );
1465 }
1466}
1467
1468
1469static struct console m68328_driver = {
1470 .name = "ttyS",
1471 .write = m68328_console_write,
1472 .device = m68328_console_device,
1473 .setup = m68328_console_setup,
1474 .flags = CON_PRINTBUFFER,
1475 .index = -1,
1476};
1477
1478
1479static int __init m68328_console_init(void)
1480{
1481 register_console(&m68328_driver);
1482 return 0;
1483}
1484
1485console_initcall(m68328_console_init);