blob: be0ebce36e54906fce49dd9c1993d184ebaddbb9 [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
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102#ifdef CONFIG_M68VZ328
103#define CONSOLE_BAUD_RATE 19200
104#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#ifndef CONSOLE_BAUD_RATE
109#define CONSOLE_BAUD_RATE 9600
110#define DEFAULT_CBAUD B9600
111#endif
112
113
114static int m68328_console_initted = 0;
115static int m68328_console_baud = CONSOLE_BAUD_RATE;
116static int m68328_console_cbaud = DEFAULT_CBAUD;
117
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline int serial_paranoia_check(struct m68k_serial *info,
120 char *name, const char *routine)
121{
122#ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct %s in %s\n";
125 static const char *badinfo =
126 "Warning: null m68k_serial for %s in %s\n";
127
128 if (!info) {
129 printk(badinfo, name, routine);
130 return 1;
131 }
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
134 return 1;
135 }
136#endif
137 return 0;
138}
139
140/*
141 * This is used to figure out the divisor speeds and the timeouts
142 */
143static int baud_table[] = {
144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145 9600, 19200, 38400, 57600, 115200, 0 };
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* Sets or clears DTR/RTS on the requested line */
148static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149{
150 if (set) {
151 /* set the RTS/CTS line */
152 } else {
153 /* clear it */
154 }
155 return;
156}
157
158/* Utility routines */
159static inline int get_baud(struct m68k_serial *ss)
160{
161 unsigned long result = 115200;
162 unsigned short int baud = uart_addr[ss->line].ubaud;
163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165
166 return result;
167}
168
169/*
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
172 *
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
176 */
177static void rs_stop(struct tty_struct *tty)
178{
179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180 m68328_uart *uart = &uart_addr[info->line];
181 unsigned long flags;
182
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
184 return;
185
Greg Ungerer727dda82006-06-28 15:54:22 +1000186 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000188 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Alan Cox09a6ffa2008-04-30 00:54:01 -0700191static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int flags, loops = 0;
194
Greg Ungerer727dda82006-06-28 15:54:22 +1000195 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
198 loops++;
199 udelay(5);
200 }
201
202 UTX_TXDATA = ch;
203 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000204 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700205 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void rs_start(struct tty_struct *tty)
209{
210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211 m68328_uart *uart = &uart_addr[info->line];
212 unsigned long flags;
213
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 return;
216
Greg Ungerer727dda82006-06-28 15:54:22 +1000217 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219#ifdef USE_INTS
220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221#else
222 uart->ustcnt |= USTCNT_TXEN;
223#endif
224 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000225 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/* Drop into either the boot monitor or kadb upon receiving a break
229 * from keyboard/console input.
230 */
231static void batten_down_hatches(void)
232{
233 /* Drop into the debugger */
234}
235
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800236static void status_handle(struct m68k_serial *info, unsigned short status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238#if 0
239 if(status & DCD) {
Takashi Iwaia88487c2008-07-16 21:54:42 +0100240 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ((info->curregs[3] & AUTO_ENAB)==0)) {
242 info->curregs[3] |= AUTO_ENAB;
243 info->pendregs[3] |= AUTO_ENAB;
244 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
245 }
246 } else {
247 if((info->curregs[3] & AUTO_ENAB)) {
248 info->curregs[3] &= ~AUTO_ENAB;
249 info->pendregs[3] &= ~AUTO_ENAB;
250 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
251 }
252 }
253#endif
254 /* If this is console input and this is a
255 * 'break asserted' status change interrupt
256 * see if we can drop into the debugger
257 */
258 if((status & URX_BREAK) && info->break_abort)
259 batten_down_hatches();
260 return;
261}
262
David Howells7d12e782006-10-05 14:55:46 +0100263static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Takashi Iwaia88487c2008-07-16 21:54:42 +0100265 struct tty_struct *tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800267 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * This do { } while() loop will get ALL chars out of Rx FIFO
271 */
272#ifndef CONFIG_XCOPILOT_BUGS
273 do {
274#endif
275 ch = GET_FIELD(rx, URX_RXDATA);
276
277 if(info->is_cons) {
278 if(URX_BREAK & rx) { /* whee, break received */
279 status_handle(info, rx);
280 return;
281#ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch == 0x10) { /* ^P */
283 show_state();
284 show_free_areas();
285 show_buffers();
286/* show_net_buffers(); */
287 return;
288 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600289 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return;
291#endif /* CONFIG_MAGIC_SYSRQ */
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 if(!tty)
296 goto clear_and_exit;
297
Alan Cox33f0f882006-01-09 20:54:13 -0800298 flag = TTY_NORMAL;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if(rx & URX_PARITY_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800301 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 status_handle(info, rx);
303 } else if(rx & URX_OVRUN) {
Alan Cox33f0f882006-01-09 20:54:13 -0800304 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 status_handle(info, rx);
306 } else if(rx & URX_FRAME_ERROR) {
Alan Cox33f0f882006-01-09 20:54:13 -0800307 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 status_handle(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Alan Cox33f0f882006-01-09 20:54:13 -0800310 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#ifndef CONFIG_XCOPILOT_BUGS
312 } while((rx = uart->urx.w) & URX_DATA_READY);
313#endif
314
Greg Ungerer8e63e662006-02-08 09:19:17 +1000315 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317clear_and_exit:
318 return;
319}
320
Adrian Bunk41c28ff2006-03-23 03:00:56 -0800321static void transmit_chars(struct m68k_serial *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 m68328_uart *uart = &uart_addr[info->line];
324
325 if (info->x_char) {
326 /* Send next char */
327 uart->utx.b.txdata = info->x_char;
328 info->x_char = 0;
329 goto clear_and_return;
330 }
331
Takashi Iwaia88487c2008-07-16 21:54:42 +0100332 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* That's peculiar... TX ints off */
334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335 goto clear_and_return;
336 }
337
338 /* Send char */
339 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
340 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
341 info->xmit_cnt--;
342
343 if (info->xmit_cnt < WAKEUP_CHARS)
344 schedule_work(&info->tqueue);
345
346 if(info->xmit_cnt <= 0) {
347 /* All done for now... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
350 }
351
352clear_and_return:
353 /* Clear interrupt (should be auto)*/
354 return;
355}
356
357/*
358 * This is the serial driver's generic interrupt routine
359 */
David Howells7d12e782006-10-05 14:55:46 +0100360irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Alan Cox25db8ad2008-08-19 20:49:40 -0700362 struct m68k_serial *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 m68328_uart *uart;
364 unsigned short rx;
365 unsigned short tx;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 uart = &uart_addr[info->line];
368 rx = uart->urx.w;
369
370#ifdef USE_INTS
371 tx = uart->utx.w;
372
David Howells7d12e782006-10-05 14:55:46 +0100373 if (rx & URX_DATA_READY) receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (tx & UTX_TX_AVAIL) transmit_chars(info);
375#else
David Howells7d12e782006-10-05 14:55:46 +0100376 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif
378 return IRQ_HANDLED;
379}
380
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000381static void do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct tty_struct *tty;
385
Takashi Iwaia88487c2008-07-16 21:54:42 +0100386 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!tty)
388 return;
389#if 0
390 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
391 tty_wakeup(tty);
392 }
393#endif
394}
395
396/*
397 * This routine is called from the scheduler tqueue when the interrupt
398 * routine has signalled that a hangup has occurred. The path of
399 * hangup processing is:
400 *
401 * serial interrupt routine -> (scheduler tqueue) ->
402 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
403 *
404 */
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000405static void do_serial_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Greg Ungerer931f9cd2007-07-25 22:07:20 +1000407 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct tty_struct *tty;
409
Takashi Iwaia88487c2008-07-16 21:54:42 +0100410 tty = info->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!tty)
412 return;
413
414 tty_hangup(tty);
415}
416
417
418static int startup(struct m68k_serial * info)
419{
420 m68328_uart *uart = &uart_addr[info->line];
421 unsigned long flags;
422
423 if (info->flags & S_INITIALIZED)
424 return 0;
425
426 if (!info->xmit_buf) {
427 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
428 if (!info->xmit_buf)
429 return -ENOMEM;
430 }
431
Greg Ungerer727dda82006-06-28 15:54:22 +1000432 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /*
435 * Clear the FIFO buffers and disable them
436 * (they will be reenabled in change_speed())
437 */
438
439 uart->ustcnt = USTCNT_UEN;
440 info->xmit_fifo_size = 1;
441 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
442 (void)uart->urx.w;
443
444 /*
445 * Finally, enable sequencing and interrupts
446 */
447#ifdef USE_INTS
448 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
449 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
450#else
451 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
452#endif
453
Takashi Iwaia88487c2008-07-16 21:54:42 +0100454 if (info->port.tty)
455 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
457
458 /*
459 * and set the speed of the serial port
460 */
461
462 change_speed(info);
463
464 info->flags |= S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000465 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
468
469/*
470 * This routine will shutdown a serial port; interrupts are disabled, and
471 * DTR is dropped if the hangup on close termio flag is on.
472 */
473static void shutdown(struct m68k_serial * info)
474{
475 m68328_uart *uart = &uart_addr[info->line];
476 unsigned long flags;
477
478 uart->ustcnt = 0; /* All off! */
479 if (!(info->flags & S_INITIALIZED))
480 return;
481
Greg Ungerer727dda82006-06-28 15:54:22 +1000482 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (info->xmit_buf) {
485 free_page((unsigned long) info->xmit_buf);
486 info->xmit_buf = 0;
487 }
488
Takashi Iwaia88487c2008-07-16 21:54:42 +0100489 if (info->port.tty)
490 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 info->flags &= ~S_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000493 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496struct {
497 int divisor, prescale;
498}
499#ifndef CONFIG_M68VZ328
500 hw_baud_table[18] = {
501 {0,0}, /* 0 */
502 {0,0}, /* 50 */
503 {0,0}, /* 75 */
504 {0,0}, /* 110 */
505 {0,0}, /* 134 */
506 {0,0}, /* 150 */
507 {0,0}, /* 200 */
508 {7,0x26}, /* 300 */
509 {6,0x26}, /* 600 */
510 {5,0x26}, /* 1200 */
511 {0,0}, /* 1800 */
512 {4,0x26}, /* 2400 */
513 {3,0x26}, /* 4800 */
514 {2,0x26}, /* 9600 */
515 {1,0x26}, /* 19200 */
516 {0,0x26}, /* 38400 */
517 {1,0x38}, /* 57600 */
518 {0,0x38}, /* 115200 */
519};
520#else
521 hw_baud_table[18] = {
522 {0,0}, /* 0 */
523 {0,0}, /* 50 */
524 {0,0}, /* 75 */
525 {0,0}, /* 110 */
526 {0,0}, /* 134 */
527 {0,0}, /* 150 */
528 {0,0}, /* 200 */
529 {0,0}, /* 300 */
530 {7,0x26}, /* 600 */
531 {6,0x26}, /* 1200 */
532 {0,0}, /* 1800 */
533 {5,0x26}, /* 2400 */
534 {4,0x26}, /* 4800 */
535 {3,0x26}, /* 9600 */
536 {2,0x26}, /* 19200 */
537 {1,0x26}, /* 38400 */
538 {0,0x26}, /* 57600 */
539 {1,0x38}, /* 115200 */
540};
541#endif
542/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
543
544/*
545 * This routine is called to set the UART divisor registers to match
546 * the specified baud rate for a serial port.
547 */
548static void change_speed(struct m68k_serial *info)
549{
550 m68328_uart *uart = &uart_addr[info->line];
551 unsigned short port;
552 unsigned short ustcnt;
553 unsigned cflag;
554 int i;
555
Takashi Iwaia88487c2008-07-16 21:54:42 +0100556 if (!info->port.tty || !info->port.tty->termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100558 cflag = info->port.tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!(port = info->port))
560 return;
561
562 ustcnt = uart->ustcnt;
563 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
564
565 i = cflag & CBAUD;
566 if (i & CBAUDEX) {
567 i = (i & ~CBAUDEX) + B38400;
568 }
569
570 info->baud = baud_table[i];
571 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
572 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
573
574 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
575
576 if ((cflag & CSIZE) == CS8)
577 ustcnt |= USTCNT_8_7;
578
579 if (cflag & CSTOPB)
580 ustcnt |= USTCNT_STOP;
581
582 if (cflag & PARENB)
583 ustcnt |= USTCNT_PARITYEN;
584 if (cflag & PARODD)
585 ustcnt |= USTCNT_ODD_EVEN;
586
587#ifdef CONFIG_SERIAL_68328_RTS_CTS
588 if (cflag & CRTSCTS) {
589 uart->utx.w &= ~ UTX_NOCTS;
590 } else {
591 uart->utx.w |= UTX_NOCTS;
592 }
593#endif
594
595 ustcnt |= USTCNT_TXEN;
596
597 uart->ustcnt = ustcnt;
598 return;
599}
600
601/*
602 * Fair output driver allows a process to speak.
603 */
604static void rs_fair_output(void)
605{
606 int left; /* Output no more than that */
607 unsigned long flags;
608 struct m68k_serial *info = &m68k_soft[0];
609 char c;
610
611 if (info == 0) return;
612 if (info->xmit_buf == 0) return;
613
Greg Ungerer727dda82006-06-28 15:54:22 +1000614 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 left = info->xmit_cnt;
616 while (left != 0) {
617 c = info->xmit_buf[info->xmit_tail];
618 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
619 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000620 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 rs_put_char(c);
623
Greg Ungerer727dda82006-06-28 15:54:22 +1000624 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 left = min(info->xmit_cnt, left-1);
626 }
627
628 /* Last character is being transmitted now (hopefully). */
629 udelay(5);
630
Greg Ungerer727dda82006-06-28 15:54:22 +1000631 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return;
633}
634
635/*
636 * m68k_console_print is registered for printk.
637 */
638void console_print_68328(const char *p)
639{
640 char c;
641
642 while((c=*(p++)) != 0) {
643 if(c == '\n')
644 rs_put_char('\r');
645 rs_put_char(c);
646 }
647
648 /* Comment this if you want to have a strict interrupt-driven output */
649 rs_fair_output();
650
651 return;
652}
653
654static void rs_set_ldisc(struct tty_struct *tty)
655{
656 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
657
658 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
659 return;
660
661 info->is_cons = (tty->termios->c_line == N_TTY);
662
663 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
664}
665
666static void rs_flush_chars(struct tty_struct *tty)
667{
668 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
669 m68328_uart *uart = &uart_addr[info->line];
670 unsigned long flags;
671
672 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
673 return;
674#ifndef USE_INTS
675 for(;;) {
676#endif
677
678 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000679 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
682 !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000683 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return;
685 }
686
687#ifdef USE_INTS
688 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
689#else
690 uart->ustcnt |= USTCNT_TXEN;
691#endif
692
693#ifdef USE_INTS
694 if (uart->utx.w & UTX_TX_AVAIL) {
695#else
696 if (1) {
697#endif
698 /* Send char */
699 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
700 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
701 info->xmit_cnt--;
702 }
703
704#ifndef USE_INTS
705 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
706 }
707#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000708 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711extern void console_printn(const char * b, int count);
712
713static int rs_write(struct tty_struct * tty,
714 const unsigned char *buf, int count)
715{
716 int c, total = 0;
717 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
718 m68328_uart *uart = &uart_addr[info->line];
719 unsigned long flags;
720
721 if (serial_paranoia_check(info, tty->name, "rs_write"))
722 return 0;
723
724 if (!tty || !info->xmit_buf)
725 return 0;
726
Greg Ungerer727dda82006-06-28 15:54:22 +1000727 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000729 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
731 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000732 local_irq_restore(flags);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (c <= 0)
735 break;
736
737 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000738
739 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
741 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000742 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 buf += c;
744 count -= c;
745 total += c;
746 }
747
748 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
749 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000750 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#ifndef USE_INTS
752 while(info->xmit_cnt) {
753#endif
754
755 uart->ustcnt |= USTCNT_TXEN;
756#ifdef USE_INTS
757 uart->ustcnt |= USTCNT_TX_INTR_MASK;
758#else
759 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
760#endif
761 if (uart->utx.w & UTX_TX_AVAIL) {
762 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
763 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
764 info->xmit_cnt--;
765 }
766
767#ifndef USE_INTS
768 }
769#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000770 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return total;
774}
775
776static int rs_write_room(struct tty_struct *tty)
777{
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 int ret;
780
781 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
782 return 0;
783 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
784 if (ret < 0)
785 ret = 0;
786 return ret;
787}
788
789static int rs_chars_in_buffer(struct tty_struct *tty)
790{
791 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
792
793 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
794 return 0;
795 return info->xmit_cnt;
796}
797
798static void rs_flush_buffer(struct tty_struct *tty)
799{
800 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000801 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
804 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000805 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000807 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 tty_wakeup(tty);
809}
810
811/*
812 * ------------------------------------------------------------
813 * rs_throttle()
814 *
815 * This routine is called by the upper-layer tty layer to signal that
816 * incoming characters should be throttled.
817 * ------------------------------------------------------------
818 */
819static void rs_throttle(struct tty_struct * tty)
820{
821 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
822
823 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
824 return;
825
826 if (I_IXOFF(tty))
827 info->x_char = STOP_CHAR(tty);
828
829 /* Turn off RTS line (do this atomic) */
830}
831
832static void rs_unthrottle(struct tty_struct * tty)
833{
834 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
835
836 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
837 return;
838
839 if (I_IXOFF(tty)) {
840 if (info->x_char)
841 info->x_char = 0;
842 else
843 info->x_char = START_CHAR(tty);
844 }
845
846 /* Assert RTS line (do this atomic) */
847}
848
849/*
850 * ------------------------------------------------------------
851 * rs_ioctl() and friends
852 * ------------------------------------------------------------
853 */
854
855static int get_serial_info(struct m68k_serial * info,
856 struct serial_struct * retinfo)
857{
858 struct serial_struct tmp;
859
860 if (!retinfo)
861 return -EFAULT;
862 memset(&tmp, 0, sizeof(tmp));
863 tmp.type = info->type;
864 tmp.line = info->line;
865 tmp.port = info->port;
866 tmp.irq = info->irq;
867 tmp.flags = info->flags;
868 tmp.baud_base = info->baud_base;
869 tmp.close_delay = info->close_delay;
870 tmp.closing_wait = info->closing_wait;
871 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400872 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
873 return -EFAULT;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876}
877
878static int set_serial_info(struct m68k_serial * info,
879 struct serial_struct * new_info)
880{
881 struct serial_struct new_serial;
882 struct m68k_serial old_info;
883 int retval = 0;
884
885 if (!new_info)
886 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400887 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
888 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 old_info = *info;
890
891 if (!capable(CAP_SYS_ADMIN)) {
892 if ((new_serial.baud_base != info->baud_base) ||
893 (new_serial.type != info->type) ||
894 (new_serial.close_delay != info->close_delay) ||
895 ((new_serial.flags & ~S_USR_MASK) !=
896 (info->flags & ~S_USR_MASK)))
897 return -EPERM;
898 info->flags = ((info->flags & ~S_USR_MASK) |
899 (new_serial.flags & S_USR_MASK));
900 info->custom_divisor = new_serial.custom_divisor;
901 goto check_and_exit;
902 }
903
904 if (info->count > 1)
905 return -EBUSY;
906
907 /*
908 * OK, past this point, all the error checking has been done.
909 * At this point, we start making changes.....
910 */
911
912 info->baud_base = new_serial.baud_base;
913 info->flags = ((info->flags & ~S_FLAGS) |
914 (new_serial.flags & S_FLAGS));
915 info->type = new_serial.type;
916 info->close_delay = new_serial.close_delay;
917 info->closing_wait = new_serial.closing_wait;
918
919check_and_exit:
920 retval = startup(info);
921 return retval;
922}
923
924/*
925 * get_lsr_info - get line status register info
926 *
927 * Purpose: Let user call ioctl() to get info when the UART physically
928 * is emptied. On bus types like RS485, the transmitter must
929 * release the bus after transmitting. This must be done when
930 * the transmit shift register is empty, not be done when the
931 * transmit holding register is empty. This functionality
932 * allows an RS485 driver to be written in user space.
933 */
934static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
935{
936#ifdef CONFIG_SERIAL_68328_RTS_CTS
937 m68328_uart *uart = &uart_addr[info->line];
938#endif
939 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Greg Ungerer727dda82006-06-28 15:54:22 +1000942 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943#ifdef CONFIG_SERIAL_68328_RTS_CTS
944 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
945#else
946 status = 0;
947#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000948 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400949 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*
953 * This routine sends a break character out the serial port.
954 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700955static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 m68328_uart *uart = &uart_addr[info->line];
958 unsigned long flags;
959 if (!info->port)
960 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000961 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962#ifdef USE_INTS
963 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700964 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 uart->utx.w &= ~UTX_SEND_BREAK;
966#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000967 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970static int rs_ioctl(struct tty_struct *tty, struct file * file,
971 unsigned int cmd, unsigned long arg)
972{
973 int error;
974 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
975 int retval;
976
977 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
978 return -ENODEV;
979
980 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
981 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
982 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
983 if (tty->flags & (1 << TTY_IO_ERROR))
984 return -EIO;
985 }
986
987 switch (cmd) {
988 case TCSBRK: /* SVID version: non-zero arg --> no break */
989 retval = tty_check_change(tty);
990 if (retval)
991 return retval;
992 tty_wait_until_sent(tty, 0);
993 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700994 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0;
996 case TCSBRKP: /* support for POSIX tcsendbreak() */
997 retval = tty_check_change(tty);
998 if (retval)
999 return retval;
1000 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -07001001 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +04001004 return get_serial_info(info,
1005 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 case TIOCSSERIAL:
1007 return set_serial_info(info,
1008 (struct serial_struct *) arg);
1009 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +04001010 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +04001012 if (copy_to_user((struct m68k_serial *) arg,
1013 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 default:
1017 return -ENOIOCTLCMD;
1018 }
1019 return 0;
1020}
1021
Alan Cox606d0992006-12-08 02:38:45 -08001022static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 change_speed(info);
1027
1028 if ((old_termios->c_cflag & CRTSCTS) &&
1029 !(tty->termios->c_cflag & CRTSCTS)) {
1030 tty->hw_stopped = 0;
1031 rs_start(tty);
1032 }
1033
1034}
1035
1036/*
1037 * ------------------------------------------------------------
1038 * rs_close()
1039 *
1040 * This routine is called when the serial port gets closed. First, we
1041 * wait for the last remaining data to be sent. Then, we unlink its
1042 * S structure from the interrupt chain if necessary, and we free
1043 * that IRQ if nothing is left in the chain.
1044 * ------------------------------------------------------------
1045 */
1046static void rs_close(struct tty_struct *tty, struct file * filp)
1047{
1048 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1049 m68328_uart *uart = &uart_addr[info->line];
1050 unsigned long flags;
1051
1052 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1053 return;
1054
Greg Ungerer727dda82006-06-28 15:54:22 +10001055 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001058 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return;
1060 }
1061
1062 if ((tty->count == 1) && (info->count != 1)) {
1063 /*
1064 * Uh, oh. tty->count is 1, which means that the tty
1065 * structure will be freed. Info->count should always
1066 * be one in these conditions. If it's greater than
1067 * one, we've got real problems, since it means the
1068 * serial port won't be shutdown.
1069 */
1070 printk("rs_close: bad serial port count; tty->count is 1, "
1071 "info->count is %d\n", info->count);
1072 info->count = 1;
1073 }
1074 if (--info->count < 0) {
1075 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1076 info->line, info->count);
1077 info->count = 0;
1078 }
1079 if (info->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001080 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return;
1082 }
1083 info->flags |= S_CLOSING;
1084 /*
1085 * Now we wait for the transmit buffer to clear; and we notify
1086 * the line discipline to only process XON/XOFF characters.
1087 */
1088 tty->closing = 1;
1089 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1090 tty_wait_until_sent(tty, info->closing_wait);
1091 /*
1092 * At this point we stop accepting input. To do this, we
1093 * disable the receive line status interrupts, and tell the
1094 * interrupt driver to stop checking the data ready bit in the
1095 * line status register.
1096 */
1097
1098 uart->ustcnt &= ~USTCNT_RXEN;
1099 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1100
1101 shutdown(info);
Alan Cox978e5952008-04-30 00:53:59 -07001102 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 tty_ldisc_flush(tty);
1105 tty->closing = 0;
1106 info->event = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001107 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108#warning "This is not and has never been valid so fix it"
1109#if 0
1110 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1111 if (tty->ldisc.close)
1112 (tty->ldisc.close)(tty);
1113 tty->ldisc = ldiscs[N_TTY];
1114 tty->termios->c_line = N_TTY;
1115 if (tty->ldisc.open)
1116 (tty->ldisc.open)(tty);
1117 }
1118#endif
1119 if (info->blocked_open) {
1120 if (info->close_delay) {
1121 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1122 }
1123 wake_up_interruptible(&info->open_wait);
1124 }
1125 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1126 wake_up_interruptible(&info->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001127 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*
1131 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1132 */
1133void rs_hangup(struct tty_struct *tty)
1134{
1135 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1136
1137 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1138 return;
1139
1140 rs_flush_buffer(tty);
1141 shutdown(info);
1142 info->event = 0;
1143 info->count = 0;
1144 info->flags &= ~S_NORMAL_ACTIVE;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001145 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 wake_up_interruptible(&info->open_wait);
1147}
1148
1149/*
1150 * ------------------------------------------------------------
1151 * rs_open() and friends
1152 * ------------------------------------------------------------
1153 */
1154static int block_til_ready(struct tty_struct *tty, struct file * filp,
1155 struct m68k_serial *info)
1156{
1157 DECLARE_WAITQUEUE(wait, current);
1158 int retval;
1159 int do_clocal = 0;
1160
1161 /*
1162 * If the device is in the middle of being closed, then block
1163 * until it's done, and then try again.
1164 */
1165 if (info->flags & S_CLOSING) {
1166 interruptible_sleep_on(&info->close_wait);
1167#ifdef SERIAL_DO_RESTART
1168 if (info->flags & S_HUP_NOTIFY)
1169 return -EAGAIN;
1170 else
1171 return -ERESTARTSYS;
1172#else
1173 return -EAGAIN;
1174#endif
1175 }
1176
1177 /*
1178 * If non-blocking mode is set, or the port is not enabled,
1179 * then make the check up front and then exit.
1180 */
1181 if ((filp->f_flags & O_NONBLOCK) ||
1182 (tty->flags & (1 << TTY_IO_ERROR))) {
1183 info->flags |= S_NORMAL_ACTIVE;
1184 return 0;
1185 }
1186
1187 if (tty->termios->c_cflag & CLOCAL)
1188 do_clocal = 1;
1189
1190 /*
1191 * Block waiting for the carrier detect and the line to become
1192 * free (i.e., not in use by the callout). While we are in
1193 * this loop, info->count is dropped by one, so that
1194 * rs_close() knows when to free things. We restore it upon
1195 * exit, either normal or abnormal.
1196 */
1197 retval = 0;
1198 add_wait_queue(&info->open_wait, &wait);
1199
1200 info->count--;
1201 info->blocked_open++;
1202 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001203 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 m68k_rtsdtr(info, 1);
Greg Ungerer727dda82006-06-28 15:54:22 +10001205 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 current->state = TASK_INTERRUPTIBLE;
1207 if (tty_hung_up_p(filp) ||
1208 !(info->flags & S_INITIALIZED)) {
1209#ifdef SERIAL_DO_RESTART
1210 if (info->flags & S_HUP_NOTIFY)
1211 retval = -EAGAIN;
1212 else
1213 retval = -ERESTARTSYS;
1214#else
1215 retval = -EAGAIN;
1216#endif
1217 break;
1218 }
1219 if (!(info->flags & S_CLOSING) && do_clocal)
1220 break;
1221 if (signal_pending(current)) {
1222 retval = -ERESTARTSYS;
1223 break;
1224 }
Arnd Bergmanne142a312010-06-01 22:53:10 +02001225 tty_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 schedule();
Arnd Bergmanne142a312010-06-01 22:53:10 +02001227 tty_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229 current->state = TASK_RUNNING;
1230 remove_wait_queue(&info->open_wait, &wait);
1231 if (!tty_hung_up_p(filp))
1232 info->count++;
1233 info->blocked_open--;
1234
1235 if (retval)
1236 return retval;
1237 info->flags |= S_NORMAL_ACTIVE;
1238 return 0;
1239}
1240
1241/*
1242 * This routine is called whenever a serial port is opened. It
1243 * enables interrupts for a serial port, linking in its S structure into
1244 * the IRQ chain. It also performs the serial-specific
1245 * initialization for the tty structure.
1246 */
1247int rs_open(struct tty_struct *tty, struct file * filp)
1248{
1249 struct m68k_serial *info;
1250 int retval, line;
1251
1252 line = tty->index;
1253
1254 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1255 return -ENODEV;
1256
1257 info = &m68k_soft[line];
1258
1259 if (serial_paranoia_check(info, tty->name, "rs_open"))
1260 return -ENODEV;
1261
1262 info->count++;
1263 tty->driver_data = info;
Takashi Iwaia88487c2008-07-16 21:54:42 +01001264 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
1267 * Start up serial port
1268 */
1269 retval = startup(info);
1270 if (retval)
1271 return retval;
1272
1273 return block_til_ready(tty, filp, info);
1274}
1275
1276/* Finally, routines used to initialize the serial driver. */
1277
1278static void show_serial_version(void)
1279{
1280 printk("MC68328 serial driver version 1.00\n");
1281}
1282
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001283static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 .open = rs_open,
1285 .close = rs_close,
1286 .write = rs_write,
1287 .flush_chars = rs_flush_chars,
1288 .write_room = rs_write_room,
1289 .chars_in_buffer = rs_chars_in_buffer,
1290 .flush_buffer = rs_flush_buffer,
1291 .ioctl = rs_ioctl,
1292 .throttle = rs_throttle,
1293 .unthrottle = rs_unthrottle,
1294 .set_termios = rs_set_termios,
1295 .stop = rs_stop,
1296 .start = rs_start,
1297 .hangup = rs_hangup,
1298 .set_ldisc = rs_set_ldisc,
1299};
1300
1301/* rs_init inits the driver */
1302static int __init
1303rs68328_init(void)
1304{
1305 int flags, i;
1306 struct m68k_serial *info;
1307
1308 serial_driver = alloc_tty_driver(NR_PORTS);
1309 if (!serial_driver)
1310 return -ENOMEM;
1311
1312 show_serial_version();
1313
1314 /* Initialize the tty_driver structure */
1315 /* SPARC: Not all of this is exactly right for us. */
1316
1317 serial_driver->name = "ttyS";
1318 serial_driver->major = TTY_MAJOR;
1319 serial_driver->minor_start = 64;
1320 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1321 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1322 serial_driver->init_termios = tty_std_termios;
1323 serial_driver->init_termios.c_cflag =
1324 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1325 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1326 tty_set_operations(serial_driver, &rs_ops);
1327
1328 if (tty_register_driver(serial_driver)) {
1329 put_tty_driver(serial_driver);
1330 printk(KERN_ERR "Couldn't register serial driver\n");
1331 return -ENOMEM;
1332 }
1333
Greg Ungerer727dda82006-06-28 15:54:22 +10001334 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 for(i=0;i<NR_PORTS;i++) {
1337
1338 info = &m68k_soft[i];
1339 info->magic = SERIAL_MAGIC;
1340 info->port = (int) &uart_addr[i];
Takashi Iwaia88487c2008-07-16 21:54:42 +01001341 info->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 info->irq = uart_irqs[i];
1343 info->custom_divisor = 16;
1344 info->close_delay = 50;
1345 info->closing_wait = 3000;
1346 info->x_char = 0;
1347 info->event = 0;
1348 info->count = 0;
1349 info->blocked_open = 0;
Greg Ungerer931f9cd2007-07-25 22:07:20 +10001350 INIT_WORK(&info->tqueue, do_softint);
1351 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 init_waitqueue_head(&info->open_wait);
1353 init_waitqueue_head(&info->close_wait);
1354 info->line = i;
1355 info->is_cons = 1; /* Means shortcuts work */
1356
1357 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1358 info->port, info->irq);
1359 printk(" is a builtin MC68328 UART\n");
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361#ifdef CONFIG_M68VZ328
1362 if (i > 0 )
1363 PJSEL &= 0xCF; /* PSW enable second port output */
1364#endif
1365
1366 if (request_irq(uart_irqs[i],
1367 rs_interrupt,
Greg Ungerer7c748062008-02-01 17:38:32 +10001368 IRQF_DISABLED,
Alan Cox25db8ad2008-08-19 20:49:40 -07001369 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 panic("Unable to attach 68328 serial interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001372 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376module_init(rs68328_init);
1377
1378
1379
1380static void m68328_set_baud(void)
1381{
1382 unsigned short ustcnt;
1383 int i;
1384
1385 ustcnt = USTCNT;
1386 USTCNT = ustcnt & ~USTCNT_TXEN;
1387
1388again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001389 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (baud_table[i] == m68328_console_baud)
1391 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001392 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 m68328_console_baud = 9600;
1394 goto again;
1395 }
1396
1397 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1398 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1399 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1400 ustcnt |= USTCNT_8_7;
1401 ustcnt |= USTCNT_TXEN;
1402 USTCNT = ustcnt;
1403 m68328_console_initted = 1;
1404 return;
1405}
1406
1407
1408int m68328_console_setup(struct console *cp, char *arg)
1409{
1410 int i, n = CONSOLE_BAUD_RATE;
1411
1412 if (!cp)
1413 return(-1);
1414
1415 if (arg)
1416 n = simple_strtoul(arg,NULL,0);
1417
Thiago Farina8b505ca2009-12-09 12:31:30 -08001418 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (baud_table[i] == n)
1420 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001421 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 m68328_console_baud = n;
1423 m68328_console_cbaud = 0;
1424 if (i > 15) {
1425 m68328_console_cbaud |= CBAUDEX;
1426 i -= 15;
1427 }
1428 m68328_console_cbaud |= i;
1429 }
1430
1431 m68328_set_baud(); /* make sure baud rate changes */
1432 return(0);
1433}
1434
1435
1436static struct tty_driver *m68328_console_device(struct console *c, int *index)
1437{
1438 *index = c->index;
1439 return serial_driver;
1440}
1441
1442
1443void m68328_console_write (struct console *co, const char *str,
1444 unsigned int count)
1445{
1446 if (!m68328_console_initted)
1447 m68328_set_baud();
1448 while (count--) {
1449 if (*str == '\n')
1450 rs_put_char('\r');
1451 rs_put_char( *str++ );
1452 }
1453}
1454
1455
1456static struct console m68328_driver = {
1457 .name = "ttyS",
1458 .write = m68328_console_write,
1459 .device = m68328_console_device,
1460 .setup = m68328_console_setup,
1461 .flags = CON_PRINTBUFFER,
1462 .index = -1,
1463};
1464
1465
1466static int __init m68328_console_init(void)
1467{
1468 register_console(&m68328_driver);
1469 return 0;
1470}
1471
1472console_initcall(m68328_console_init);