blob: ef2e08e9b5901fdd9267a66896da10f829f931ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 68328serial.c: Serial port driver for 68328 microcontroller
2 *
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
9 *
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
15 */
16
17#include <asm/dbg.h>
18#include <linux/module.h>
19#include <linux/errno.h>
Jiri Slabycea4b2c2012-04-02 13:54:35 +020020#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/timer.h>
24#include <linux/interrupt.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/major.h>
28#include <linux/string.h>
29#include <linux/fcntl.h>
30#include <linux/mm.h>
31#include <linux/kernel.h>
32#include <linux/console.h>
33#include <linux/reboot.h>
34#include <linux/keyboard.h>
35#include <linux/init.h>
36#include <linux/pm.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/io.h>
42#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/delay.h>
44#include <asm/uaccess.h>
45
46/* (es) */
47/* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
49 */
50#if defined(CONFIG_M68EZ328)
51#include <asm/MC68EZ328.h>
52#else
53#if defined(CONFIG_M68VZ328)
54#include <asm/MC68VZ328.h>
55#else
56#include <asm/MC68328.h>
57#endif /* CONFIG_M68VZ328 */
58#endif /* CONFIG_M68EZ328 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Turn off usage of real serial interrupt code, to "support" Copilot */
61#ifdef CONFIG_XCOPILOT_BUGS
62#undef USE_INTS
63#else
64#define USE_INTS
65#endif
66
Jiri Slaby1bb26872012-04-02 13:54:39 +020067/*
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
74 */
75#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
76
77/*
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
83 *
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
87 *
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
90 *
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
94 */
95
96/* (es) */
97#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99#elif defined(CONFIG_M68328)
100#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
101#else
102#error Please, define the Rx interrupt events for your CPU
103#endif
104/* (/es) */
105
106/*
107 * This is our internal structure for each serial port's state.
Jiri Slaby1bb26872012-04-02 13:54:39 +0200108 */
109struct m68k_serial {
Jiri Slaby86264342012-04-02 13:54:40 +0200110 struct tty_port tport;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200111 char is_cons; /* Is this our console. */
112 int magic;
113 int baud_base;
114 int port;
115 int irq;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200116 int type; /* UART type */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200117 int custom_divisor;
118 int x_char; /* xon/xoff character */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200119 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200120 unsigned char *xmit_buf;
121 int xmit_head;
122 int xmit_tail;
123 int xmit_cnt;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200124};
125
126#define SERIAL_MAGIC 0x5301
127
128/*
129 * Define the number of ports supported and their irqs.
130 */
131#define NR_PORTS 1
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jiri Slaby1bb26872012-04-02 13:54:39 +0200135static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* multiple ports are contiguous in memory */
138m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct tty_driver *serial_driver;
141
Jiri Slaby467712c2012-04-02 13:54:44 +0200142static void change_speed(struct m68k_serial *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/*
145 * Setup for console. Argument comes from the boot command line.
146 */
147
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700148/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
149#ifdef CONFIG_M68VZ328
150#define CONSOLE_BAUD_RATE 19200
151#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
153
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifndef CONSOLE_BAUD_RATE
156#define CONSOLE_BAUD_RATE 9600
157#define DEFAULT_CBAUD B9600
158#endif
159
160
161static int m68328_console_initted = 0;
162static int m68328_console_baud = CONSOLE_BAUD_RATE;
163static int m68328_console_cbaud = DEFAULT_CBAUD;
164
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline int serial_paranoia_check(struct m68k_serial *info,
167 char *name, const char *routine)
168{
169#ifdef SERIAL_PARANOIA_CHECK
170 static const char *badmagic =
171 "Warning: bad magic number for serial struct %s in %s\n";
172 static const char *badinfo =
173 "Warning: null m68k_serial for %s in %s\n";
174
175 if (!info) {
176 printk(badinfo, name, routine);
177 return 1;
178 }
179 if (info->magic != SERIAL_MAGIC) {
180 printk(badmagic, name, routine);
181 return 1;
182 }
183#endif
184 return 0;
185}
186
187/*
188 * This is used to figure out the divisor speeds and the timeouts
189 */
190static int baud_table[] = {
191 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
192 9600, 19200, 38400, 57600, 115200, 0 };
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* Utility routines */
195static inline int get_baud(struct m68k_serial *ss)
196{
197 unsigned long result = 115200;
198 unsigned short int baud = uart_addr[ss->line].ubaud;
199 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
200 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
201
202 return result;
203}
204
205/*
206 * ------------------------------------------------------------
207 * rs_stop() and rs_start()
208 *
209 * This routines are called before setting or resetting tty->stopped.
210 * They enable or disable transmitter interrupts, as necessary.
211 * ------------------------------------------------------------
212 */
213static void rs_stop(struct tty_struct *tty)
214{
215 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
216 m68328_uart *uart = &uart_addr[info->line];
217 unsigned long flags;
218
219 if (serial_paranoia_check(info, tty->name, "rs_stop"))
220 return;
221
Greg Ungerer727dda82006-06-28 15:54:22 +1000222 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000224 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Alan Cox09a6ffa2008-04-30 00:54:01 -0700227static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Jiri Slaby107afb72012-04-02 13:54:38 +0200229 unsigned long flags;
230 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Greg Ungerer727dda82006-06-28 15:54:22 +1000232 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
235 loops++;
236 udelay(5);
237 }
238
239 UTX_TXDATA = ch;
240 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000241 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static void rs_start(struct tty_struct *tty)
246{
247 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
248 m68328_uart *uart = &uart_addr[info->line];
249 unsigned long flags;
250
251 if (serial_paranoia_check(info, tty->name, "rs_start"))
252 return;
253
Greg Ungerer727dda82006-06-28 15:54:22 +1000254 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
256#ifdef USE_INTS
257 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
258#else
259 uart->ustcnt |= USTCNT_TXEN;
260#endif
261 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000262 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100265static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 m68328_uart *uart = &uart_addr[info->line];
Alan Cox33f0f882006-01-09 20:54:13 -0800268 unsigned char ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /*
271 * This do { } while() loop will get ALL chars out of Rx FIFO
272 */
273#ifndef CONFIG_XCOPILOT_BUGS
274 do {
275#endif
276 ch = GET_FIELD(rx, URX_RXDATA);
277
278 if(info->is_cons) {
279 if(URX_BREAK & rx) { /* whee, break received */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return;
281#ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch == 0x10) { /* ^P */
283 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700284 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 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
Alan Cox33f0f882006-01-09 20:54:13 -0800295 flag = TTY_NORMAL;
296
Jiri Slabyc21e2652012-04-02 13:54:36 +0200297 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800298 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200299 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800300 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200301 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800302 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200303
Jiri Slaby92a19f92013-01-03 15:53:03 +0100304 tty_insert_flip_char(&info->tport, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifndef CONFIG_XCOPILOT_BUGS
306 } while((rx = uart->urx.w) & URX_DATA_READY);
307#endif
308
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100309 tty_schedule_flip(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Jiri Slaby467712c2012-04-02 13:54:44 +0200312static void transmit_chars(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 m68328_uart *uart = &uart_addr[info->line];
315
316 if (info->x_char) {
317 /* Send next char */
318 uart->utx.b.txdata = info->x_char;
319 info->x_char = 0;
320 goto clear_and_return;
321 }
322
Jiri Slaby467712c2012-04-02 13:54:44 +0200323 if ((info->xmit_cnt <= 0) || !tty || tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* That's peculiar... TX ints off */
325 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
326 goto clear_and_return;
327 }
328
329 /* Send char */
330 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
331 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
332 info->xmit_cnt--;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if(info->xmit_cnt <= 0) {
335 /* All done for now... TX ints off */
336 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
337 goto clear_and_return;
338 }
339
340clear_and_return:
341 /* Clear interrupt (should be auto)*/
342 return;
343}
344
345/*
346 * This is the serial driver's generic interrupt routine
347 */
David Howells7d12e782006-10-05 14:55:46 +0100348irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Alan Cox25db8ad2008-08-19 20:49:40 -0700350 struct m68k_serial *info = dev_id;
Jiri Slaby665569d2012-04-02 13:54:45 +0200351 struct tty_struct *tty = tty_port_tty_get(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 m68328_uart *uart;
353 unsigned short rx;
354 unsigned short tx;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 uart = &uart_addr[info->line];
357 rx = uart->urx.w;
358
359#ifdef USE_INTS
360 tx = uart->utx.w;
361
Jiri Slaby467712c2012-04-02 13:54:44 +0200362 if (rx & URX_DATA_READY)
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100363 receive_chars(info, rx);
Jiri Slaby467712c2012-04-02 13:54:44 +0200364 if (tx & UTX_TX_AVAIL)
365 transmit_chars(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#else
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100367 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#endif
Jiri Slaby665569d2012-04-02 13:54:45 +0200369 tty_kref_put(tty);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return IRQ_HANDLED;
372}
373
Jiri Slaby467712c2012-04-02 13:54:44 +0200374static int startup(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 m68328_uart *uart = &uart_addr[info->line];
377 unsigned long flags;
378
Jiri Slabya85dd822012-04-02 13:54:43 +0200379 if (info->tport.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381
382 if (!info->xmit_buf) {
383 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
384 if (!info->xmit_buf)
385 return -ENOMEM;
386 }
387
Greg Ungerer727dda82006-06-28 15:54:22 +1000388 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * Clear the FIFO buffers and disable them
392 * (they will be reenabled in change_speed())
393 */
394
395 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
397 (void)uart->urx.w;
398
399 /*
400 * Finally, enable sequencing and interrupts
401 */
402#ifdef USE_INTS
403 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
404 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
405#else
406 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
407#endif
408
Jiri Slaby467712c2012-04-02 13:54:44 +0200409 if (tty)
410 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
412
413 /*
414 * and set the speed of the serial port
415 */
416
Jiri Slaby467712c2012-04-02 13:54:44 +0200417 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jiri Slabya85dd822012-04-02 13:54:43 +0200419 info->tport.flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000420 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
424/*
425 * This routine will shutdown a serial port; interrupts are disabled, and
426 * DTR is dropped if the hangup on close termio flag is on.
427 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200428static void shutdown(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 m68328_uart *uart = &uart_addr[info->line];
431 unsigned long flags;
432
433 uart->ustcnt = 0; /* All off! */
Jiri Slabya85dd822012-04-02 13:54:43 +0200434 if (!(info->tport.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return;
436
Greg Ungerer727dda82006-06-28 15:54:22 +1000437 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (info->xmit_buf) {
440 free_page((unsigned long) info->xmit_buf);
441 info->xmit_buf = 0;
442 }
443
Jiri Slaby467712c2012-04-02 13:54:44 +0200444 if (tty)
445 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jiri Slabya85dd822012-04-02 13:54:43 +0200447 info->tport.flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000448 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451struct {
452 int divisor, prescale;
453}
454#ifndef CONFIG_M68VZ328
455 hw_baud_table[18] = {
456 {0,0}, /* 0 */
457 {0,0}, /* 50 */
458 {0,0}, /* 75 */
459 {0,0}, /* 110 */
460 {0,0}, /* 134 */
461 {0,0}, /* 150 */
462 {0,0}, /* 200 */
463 {7,0x26}, /* 300 */
464 {6,0x26}, /* 600 */
465 {5,0x26}, /* 1200 */
466 {0,0}, /* 1800 */
467 {4,0x26}, /* 2400 */
468 {3,0x26}, /* 4800 */
469 {2,0x26}, /* 9600 */
470 {1,0x26}, /* 19200 */
471 {0,0x26}, /* 38400 */
472 {1,0x38}, /* 57600 */
473 {0,0x38}, /* 115200 */
474};
475#else
476 hw_baud_table[18] = {
477 {0,0}, /* 0 */
478 {0,0}, /* 50 */
479 {0,0}, /* 75 */
480 {0,0}, /* 110 */
481 {0,0}, /* 134 */
482 {0,0}, /* 150 */
483 {0,0}, /* 200 */
484 {0,0}, /* 300 */
485 {7,0x26}, /* 600 */
486 {6,0x26}, /* 1200 */
487 {0,0}, /* 1800 */
488 {5,0x26}, /* 2400 */
489 {4,0x26}, /* 4800 */
490 {3,0x26}, /* 9600 */
491 {2,0x26}, /* 19200 */
492 {1,0x26}, /* 38400 */
493 {0,0x26}, /* 57600 */
494 {1,0x38}, /* 115200 */
495};
496#endif
497/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
498
499/*
500 * This routine is called to set the UART divisor registers to match
501 * the specified baud rate for a serial port.
502 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200503static void change_speed(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 m68328_uart *uart = &uart_addr[info->line];
506 unsigned short port;
507 unsigned short ustcnt;
508 unsigned cflag;
509 int i;
510
Jiri Slaby3dd332c2012-08-07 21:47:27 +0200511 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!(port = info->port))
513 return;
514
515 ustcnt = uart->ustcnt;
516 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
517
518 i = cflag & CBAUD;
519 if (i & CBAUDEX) {
520 i = (i & ~CBAUDEX) + B38400;
521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
524 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
525
526 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
527
528 if ((cflag & CSIZE) == CS8)
529 ustcnt |= USTCNT_8_7;
530
531 if (cflag & CSTOPB)
532 ustcnt |= USTCNT_STOP;
533
534 if (cflag & PARENB)
535 ustcnt |= USTCNT_PARITYEN;
536 if (cflag & PARODD)
537 ustcnt |= USTCNT_ODD_EVEN;
538
539#ifdef CONFIG_SERIAL_68328_RTS_CTS
540 if (cflag & CRTSCTS) {
541 uart->utx.w &= ~ UTX_NOCTS;
542 } else {
543 uart->utx.w |= UTX_NOCTS;
544 }
545#endif
546
547 ustcnt |= USTCNT_TXEN;
548
549 uart->ustcnt = ustcnt;
550 return;
551}
552
553/*
554 * Fair output driver allows a process to speak.
555 */
556static void rs_fair_output(void)
557{
558 int left; /* Output no more than that */
559 unsigned long flags;
560 struct m68k_serial *info = &m68k_soft[0];
561 char c;
562
563 if (info == 0) return;
564 if (info->xmit_buf == 0) return;
565
Greg Ungerer727dda82006-06-28 15:54:22 +1000566 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 left = info->xmit_cnt;
568 while (left != 0) {
569 c = info->xmit_buf[info->xmit_tail];
570 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
571 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000572 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 rs_put_char(c);
575
Greg Ungerer727dda82006-06-28 15:54:22 +1000576 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 left = min(info->xmit_cnt, left-1);
578 }
579
580 /* Last character is being transmitted now (hopefully). */
581 udelay(5);
582
Greg Ungerer727dda82006-06-28 15:54:22 +1000583 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return;
585}
586
587/*
588 * m68k_console_print is registered for printk.
589 */
590void console_print_68328(const char *p)
591{
592 char c;
593
594 while((c=*(p++)) != 0) {
595 if(c == '\n')
596 rs_put_char('\r');
597 rs_put_char(c);
598 }
599
600 /* Comment this if you want to have a strict interrupt-driven output */
601 rs_fair_output();
602
603 return;
604}
605
606static void rs_set_ldisc(struct tty_struct *tty)
607{
608 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
609
610 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
611 return;
612
Jiri Slaby3dd332c2012-08-07 21:47:27 +0200613 info->is_cons = (tty->termios.c_line == N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
616}
617
618static void rs_flush_chars(struct tty_struct *tty)
619{
620 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
621 m68328_uart *uart = &uart_addr[info->line];
622 unsigned long flags;
623
624 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
625 return;
626#ifndef USE_INTS
627 for(;;) {
628#endif
629
630 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000631 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jiri Slabyee797062013-03-07 13:12:34 +0100633 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000634 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return;
636 }
637
638#ifdef USE_INTS
639 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
640#else
641 uart->ustcnt |= USTCNT_TXEN;
642#endif
643
644#ifdef USE_INTS
645 if (uart->utx.w & UTX_TX_AVAIL) {
646#else
647 if (1) {
648#endif
649 /* Send char */
650 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
651 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
652 info->xmit_cnt--;
653 }
654
655#ifndef USE_INTS
656 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
657 }
658#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000659 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662extern void console_printn(const char * b, int count);
663
664static int rs_write(struct tty_struct * tty,
665 const unsigned char *buf, int count)
666{
667 int c, total = 0;
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_write"))
673 return 0;
674
675 if (!tty || !info->xmit_buf)
676 return 0;
677
Greg Ungerer727dda82006-06-28 15:54:22 +1000678 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000680 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
682 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000683 local_irq_restore(flags);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (c <= 0)
686 break;
687
688 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000689
690 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
692 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000693 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 buf += c;
695 count -= c;
696 total += c;
697 }
698
Jiri Slabyee797062013-03-07 13:12:34 +0100699 if (info->xmit_cnt && !tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000701 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#ifndef USE_INTS
703 while(info->xmit_cnt) {
704#endif
705
706 uart->ustcnt |= USTCNT_TXEN;
707#ifdef USE_INTS
708 uart->ustcnt |= USTCNT_TX_INTR_MASK;
709#else
710 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
711#endif
712 if (uart->utx.w & UTX_TX_AVAIL) {
713 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
714 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
715 info->xmit_cnt--;
716 }
717
718#ifndef USE_INTS
719 }
720#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000721 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return total;
725}
726
727static int rs_write_room(struct tty_struct *tty)
728{
729 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
730 int ret;
731
732 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
733 return 0;
734 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
735 if (ret < 0)
736 ret = 0;
737 return ret;
738}
739
740static int rs_chars_in_buffer(struct tty_struct *tty)
741{
742 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
743
744 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
745 return 0;
746 return info->xmit_cnt;
747}
748
749static void rs_flush_buffer(struct tty_struct *tty)
750{
751 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000752 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
755 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000756 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000758 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 tty_wakeup(tty);
760}
761
762/*
763 * ------------------------------------------------------------
764 * rs_throttle()
765 *
766 * This routine is called by the upper-layer tty layer to signal that
767 * incoming characters should be throttled.
768 * ------------------------------------------------------------
769 */
770static void rs_throttle(struct tty_struct * tty)
771{
772 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
773
774 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
775 return;
776
777 if (I_IXOFF(tty))
778 info->x_char = STOP_CHAR(tty);
779
780 /* Turn off RTS line (do this atomic) */
781}
782
783static void rs_unthrottle(struct tty_struct * tty)
784{
785 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
786
787 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
788 return;
789
790 if (I_IXOFF(tty)) {
791 if (info->x_char)
792 info->x_char = 0;
793 else
794 info->x_char = START_CHAR(tty);
795 }
796
797 /* Assert RTS line (do this atomic) */
798}
799
800/*
801 * ------------------------------------------------------------
802 * rs_ioctl() and friends
803 * ------------------------------------------------------------
804 */
805
806static int get_serial_info(struct m68k_serial * info,
807 struct serial_struct * retinfo)
808{
809 struct serial_struct tmp;
810
811 if (!retinfo)
812 return -EFAULT;
813 memset(&tmp, 0, sizeof(tmp));
814 tmp.type = info->type;
815 tmp.line = info->line;
816 tmp.port = info->port;
817 tmp.irq = info->irq;
Jiri Slabya85dd822012-04-02 13:54:43 +0200818 tmp.flags = info->tport.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 tmp.baud_base = info->baud_base;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200820 tmp.close_delay = info->tport.close_delay;
821 tmp.closing_wait = info->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400823 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
824 return -EFAULT;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827}
828
Jiri Slaby467712c2012-04-02 13:54:44 +0200829static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct serial_struct * new_info)
831{
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200832 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct serial_struct new_serial;
834 struct m68k_serial old_info;
835 int retval = 0;
836
837 if (!new_info)
838 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400839 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
840 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 old_info = *info;
842
843 if (!capable(CAP_SYS_ADMIN)) {
844 if ((new_serial.baud_base != info->baud_base) ||
845 (new_serial.type != info->type) ||
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200846 (new_serial.close_delay != port->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200847 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabya85dd822012-04-02 13:54:43 +0200848 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -EPERM;
Jiri Slabya85dd822012-04-02 13:54:43 +0200850 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200851 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 info->custom_divisor = new_serial.custom_divisor;
853 goto check_and_exit;
854 }
855
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200856 if (port->count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EBUSY;
858
859 /*
860 * OK, past this point, all the error checking has been done.
861 * At this point, we start making changes.....
862 */
863
864 info->baud_base = new_serial.baud_base;
Jiri Slabya85dd822012-04-02 13:54:43 +0200865 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200866 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 info->type = new_serial.type;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200868 port->close_delay = new_serial.close_delay;
869 port->closing_wait = new_serial.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871check_and_exit:
Jiri Slaby467712c2012-04-02 13:54:44 +0200872 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return retval;
874}
875
876/*
877 * get_lsr_info - get line status register info
878 *
879 * Purpose: Let user call ioctl() to get info when the UART physically
880 * is emptied. On bus types like RS485, the transmitter must
881 * release the bus after transmitting. This must be done when
882 * the transmit shift register is empty, not be done when the
883 * transmit holding register is empty. This functionality
884 * allows an RS485 driver to be written in user space.
885 */
886static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
887{
888#ifdef CONFIG_SERIAL_68328_RTS_CTS
889 m68328_uart *uart = &uart_addr[info->line];
890#endif
891 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000892 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Greg Ungerer727dda82006-06-28 15:54:22 +1000894 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#ifdef CONFIG_SERIAL_68328_RTS_CTS
896 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
897#else
898 status = 0;
899#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000900 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400901 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
905 * This routine sends a break character out the serial port.
906 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700907static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 m68328_uart *uart = &uart_addr[info->line];
910 unsigned long flags;
911 if (!info->port)
912 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000913 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#ifdef USE_INTS
915 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700916 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 uart->utx.w &= ~UTX_SEND_BREAK;
918#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000919 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Alan Cox6caa76b2011-02-14 16:27:22 +0000922static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned int cmd, unsigned long arg)
924{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
926 int retval;
927
928 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
929 return -ENODEV;
930
931 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
932 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
933 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
934 if (tty->flags & (1 << TTY_IO_ERROR))
935 return -EIO;
936 }
937
938 switch (cmd) {
939 case TCSBRK: /* SVID version: non-zero arg --> no break */
940 retval = tty_check_change(tty);
941 if (retval)
942 return retval;
943 tty_wait_until_sent(tty, 0);
944 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700945 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947 case TCSBRKP: /* support for POSIX tcsendbreak() */
948 retval = tty_check_change(tty);
949 if (retval)
950 return retval;
951 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700952 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400955 return get_serial_info(info,
956 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 case TIOCSSERIAL:
Jiri Slaby467712c2012-04-02 13:54:44 +0200958 return set_serial_info(info, tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 (struct serial_struct *) arg);
960 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400961 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400963 if (copy_to_user((struct m68k_serial *) arg,
964 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 default:
968 return -ENOIOCTLCMD;
969 }
970 return 0;
971}
972
Alan Cox606d0992006-12-08 02:38:45 -0800973static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
976
Jiri Slaby467712c2012-04-02 13:54:44 +0200977 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyee797062013-03-07 13:12:34 +0100980 !(tty->termios.c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 rs_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983}
984
985/*
986 * ------------------------------------------------------------
987 * rs_close()
988 *
989 * This routine is called when the serial port gets closed. First, we
990 * wait for the last remaining data to be sent. Then, we unlink its
991 * S structure from the interrupt chain if necessary, and we free
992 * that IRQ if nothing is left in the chain.
993 * ------------------------------------------------------------
994 */
995static void rs_close(struct tty_struct *tty, struct file * filp)
996{
997 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +0200998 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 m68328_uart *uart = &uart_addr[info->line];
1000 unsigned long flags;
1001
Cong Ding9ef20d52013-01-16 23:30:43 +01001002 if (serial_paranoia_check(info, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return;
1004
Greg Ungerer727dda82006-06-28 15:54:22 +10001005 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001008 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
1010 }
1011
Jiri Slaby86264342012-04-02 13:54:40 +02001012 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * Uh, oh. tty->count is 1, which means that the tty
1015 * structure will be freed. Info->count should always
1016 * be one in these conditions. If it's greater than
1017 * one, we've got real problems, since it means the
1018 * serial port won't be shutdown.
1019 */
1020 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001021 "port->count is %d\n", port->count);
1022 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
Jiri Slaby86264342012-04-02 13:54:40 +02001024 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001026 info->line, port->count);
1027 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Jiri Slaby86264342012-04-02 13:54:40 +02001029 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001030 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001033 port->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * Now we wait for the transmit buffer to clear; and we notify
1036 * the line discipline to only process XON/XOFF characters.
1037 */
1038 tty->closing = 1;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001039 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1040 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * At this point we stop accepting input. To do this, we
1043 * disable the receive line status interrupts, and tell the
1044 * interrupt driver to stop checking the data ready bit in the
1045 * line status register.
1046 */
1047
1048 uart->ustcnt &= ~USTCNT_RXEN;
1049 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1050
Jiri Slaby467712c2012-04-02 13:54:44 +02001051 shutdown(info, tty);
Alan Cox978e5952008-04-30 00:53:59 -07001052 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 tty_ldisc_flush(tty);
1055 tty->closing = 0;
Jiri Slaby665569d2012-04-02 13:54:45 +02001056 tty_port_tty_set(&info->tport, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#warning "This is not and has never been valid so fix it"
1058#if 0
1059 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1060 if (tty->ldisc.close)
1061 (tty->ldisc.close)(tty);
1062 tty->ldisc = ldiscs[N_TTY];
Jiri Slaby3dd332c2012-08-07 21:47:27 +02001063 tty->termios.c_line = N_TTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (tty->ldisc.open)
1065 (tty->ldisc.open)(tty);
1066 }
1067#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001068 if (port->blocked_open) {
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001069 if (port->close_delay)
1070 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Jiri Slabyc26f0112012-04-02 13:54:41 +02001071 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001073 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001074 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001075 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078/*
1079 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1080 */
1081void rs_hangup(struct tty_struct *tty)
1082{
1083 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1084
1085 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1086 return;
1087
1088 rs_flush_buffer(tty);
Jiri Slaby467712c2012-04-02 13:54:44 +02001089 shutdown(info, tty);
Jiri Slaby86264342012-04-02 13:54:40 +02001090 info->tport.count = 0;
Jiri Slabya85dd822012-04-02 13:54:43 +02001091 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby665569d2012-04-02 13:54:45 +02001092 tty_port_tty_set(&info->tport, NULL);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001093 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * This routine is called whenever a serial port is opened. It
1098 * enables interrupts for a serial port, linking in its S structure into
1099 * the IRQ chain. It also performs the serial-specific
1100 * initialization for the tty structure.
1101 */
1102int rs_open(struct tty_struct *tty, struct file * filp)
1103{
1104 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001105 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jiri Slaby410235f2012-03-05 14:52:01 +01001107 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (serial_paranoia_check(info, tty->name, "rs_open"))
1110 return -ENODEV;
1111
Jiri Slaby86264342012-04-02 13:54:40 +02001112 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 tty->driver_data = info;
Jiri Slaby665569d2012-04-02 13:54:45 +02001114 tty_port_tty_set(&info->tport, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /*
1117 * Start up serial port
1118 */
Jiri Slaby467712c2012-04-02 13:54:44 +02001119 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (retval)
1121 return retval;
1122
Jiri Slaby8e328412012-04-02 13:54:46 +02001123 return tty_port_block_til_ready(&info->tport, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126/* Finally, routines used to initialize the serial driver. */
1127
1128static void show_serial_version(void)
1129{
1130 printk("MC68328 serial driver version 1.00\n");
1131}
1132
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001133static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 .open = rs_open,
1135 .close = rs_close,
1136 .write = rs_write,
1137 .flush_chars = rs_flush_chars,
1138 .write_room = rs_write_room,
1139 .chars_in_buffer = rs_chars_in_buffer,
1140 .flush_buffer = rs_flush_buffer,
1141 .ioctl = rs_ioctl,
1142 .throttle = rs_throttle,
1143 .unthrottle = rs_unthrottle,
1144 .set_termios = rs_set_termios,
1145 .stop = rs_stop,
1146 .start = rs_start,
1147 .hangup = rs_hangup,
1148 .set_ldisc = rs_set_ldisc,
1149};
1150
Jiri Slaby8e328412012-04-02 13:54:46 +02001151static const struct tty_port_operations rs_port_ops = {
1152};
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* rs_init inits the driver */
1155static int __init
1156rs68328_init(void)
1157{
Jiri Slaby107afb72012-04-02 13:54:38 +02001158 unsigned long flags;
1159 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 struct m68k_serial *info;
1161
1162 serial_driver = alloc_tty_driver(NR_PORTS);
1163 if (!serial_driver)
1164 return -ENOMEM;
1165
1166 show_serial_version();
1167
1168 /* Initialize the tty_driver structure */
1169 /* SPARC: Not all of this is exactly right for us. */
1170
1171 serial_driver->name = "ttyS";
1172 serial_driver->major = TTY_MAJOR;
1173 serial_driver->minor_start = 64;
1174 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1175 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1176 serial_driver->init_termios = tty_std_termios;
1177 serial_driver->init_termios.c_cflag =
1178 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1179 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1180 tty_set_operations(serial_driver, &rs_ops);
1181
Greg Ungerer727dda82006-06-28 15:54:22 +10001182 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 for(i=0;i<NR_PORTS;i++) {
1185
1186 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001187 tty_port_init(&info->tport);
Jiri Slaby8e328412012-04-02 13:54:46 +02001188 info->tport.ops = &rs_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 info->magic = SERIAL_MAGIC;
1190 info->port = (int) &uart_addr[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 info->irq = uart_irqs[i];
1192 info->custom_divisor = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 info->line = i;
1195 info->is_cons = 1; /* Means shortcuts work */
1196
1197 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1198 info->port, info->irq);
1199 printk(" is a builtin MC68328 UART\n");
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201#ifdef CONFIG_M68VZ328
1202 if (i > 0 )
1203 PJSEL &= 0xCF; /* PSW enable second port output */
1204#endif
1205
1206 if (request_irq(uart_irqs[i],
1207 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001208 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001209 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 panic("Unable to attach 68328 serial interrupt\n");
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001211
1212 tty_port_link_device(&info->tport, serial_driver, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001214 local_irq_restore(flags);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001215
1216 if (tty_register_driver(serial_driver)) {
1217 put_tty_driver(serial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001218 for (i = 0; i < NR_PORTS; i++)
1219 tty_port_destroy(&m68k_soft[i].tport);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001220 printk(KERN_ERR "Couldn't register serial driver\n");
1221 return -ENOMEM;
1222 }
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 0;
1225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227module_init(rs68328_init);
1228
1229
1230
1231static void m68328_set_baud(void)
1232{
1233 unsigned short ustcnt;
1234 int i;
1235
1236 ustcnt = USTCNT;
1237 USTCNT = ustcnt & ~USTCNT_TXEN;
1238
1239again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001240 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (baud_table[i] == m68328_console_baud)
1242 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001243 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 m68328_console_baud = 9600;
1245 goto again;
1246 }
1247
1248 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1249 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1250 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1251 ustcnt |= USTCNT_8_7;
1252 ustcnt |= USTCNT_TXEN;
1253 USTCNT = ustcnt;
1254 m68328_console_initted = 1;
1255 return;
1256}
1257
1258
1259int m68328_console_setup(struct console *cp, char *arg)
1260{
1261 int i, n = CONSOLE_BAUD_RATE;
1262
1263 if (!cp)
1264 return(-1);
1265
1266 if (arg)
1267 n = simple_strtoul(arg,NULL,0);
1268
Thiago Farina8b505ca2009-12-09 12:31:30 -08001269 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (baud_table[i] == n)
1271 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001272 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 m68328_console_baud = n;
1274 m68328_console_cbaud = 0;
1275 if (i > 15) {
1276 m68328_console_cbaud |= CBAUDEX;
1277 i -= 15;
1278 }
1279 m68328_console_cbaud |= i;
1280 }
1281
1282 m68328_set_baud(); /* make sure baud rate changes */
1283 return(0);
1284}
1285
1286
1287static struct tty_driver *m68328_console_device(struct console *c, int *index)
1288{
1289 *index = c->index;
1290 return serial_driver;
1291}
1292
1293
1294void m68328_console_write (struct console *co, const char *str,
1295 unsigned int count)
1296{
1297 if (!m68328_console_initted)
1298 m68328_set_baud();
1299 while (count--) {
1300 if (*str == '\n')
1301 rs_put_char('\r');
1302 rs_put_char( *str++ );
1303 }
1304}
1305
1306
1307static struct console m68328_driver = {
1308 .name = "ttyS",
1309 .write = m68328_console_write,
1310 .device = m68328_console_device,
1311 .setup = m68328_console_setup,
1312 .flags = CON_PRINTBUFFER,
1313 .index = -1,
1314};
1315
1316
1317static int __init m68328_console_init(void)
1318{
1319 register_console(&m68328_driver);
1320 return 0;
1321}
1322
1323console_initcall(m68328_console_init);