blob: 5dc9c4bfa66e4686d5360ab33da61eff1109715a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/errno.h>
Jiri Slabycea4b2c2012-04-02 13:54:35 +020019#include <linux/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/delay.h>
43#include <asm/uaccess.h>
44
45/* (es) */
46/* note: perhaps we can murge these files, so that you can just
47 * define 1 of them, and they can sort that out for themselves
48 */
49#if defined(CONFIG_M68EZ328)
50#include <asm/MC68EZ328.h>
51#else
52#if defined(CONFIG_M68VZ328)
53#include <asm/MC68VZ328.h>
54#else
55#include <asm/MC68328.h>
56#endif /* CONFIG_M68VZ328 */
57#endif /* CONFIG_M68EZ328 */
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* Turn off usage of real serial interrupt code, to "support" Copilot */
60#ifdef CONFIG_XCOPILOT_BUGS
61#undef USE_INTS
62#else
63#define USE_INTS
64#endif
65
Jiri Slaby1bb26872012-04-02 13:54:39 +020066/*
67 * I believe this is the optimal setting that reduces the number of interrupts.
68 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
69 * if that bothers you), but in most cases it will not, since we try to
70 * transmit characters every time rs_interrupt is called. Thus, quite often
71 * you'll see that a receive interrupt occures before the transmit one.
72 * -- Vladimir Gurevich
73 */
74#define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
75
76/*
77 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
78 * "Old data interrupt" which occures whenever the data stay in the FIFO
79 * longer than 30 bits time. This allows us to use FIFO without compromising
80 * latency. '328 does not have this feature and without the real 328-based
81 * board I would assume that RXRE is the safest setting.
82 *
83 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
84 * interrupts. RXFE (receive queue full) causes the system to lose data
85 * at least at 115200 baud
86 *
87 * If your board is busy doing other stuff, you might consider to use
88 * RXRE (data ready intrrupt) instead.
89 *
90 * The other option is to make these INTR masks run-time configurable, so
91 * that people can dynamically adapt them according to the current usage.
92 * -- Vladimir Gurevich
93 */
94
95/* (es) */
96#if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
97#define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
98#elif defined(CONFIG_M68328)
99#define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
100#else
101#error Please, define the Rx interrupt events for your CPU
102#endif
103/* (/es) */
104
105/*
106 * This is our internal structure for each serial port's state.
Jiri Slaby1bb26872012-04-02 13:54:39 +0200107 */
108struct m68k_serial {
Jiri Slaby86264342012-04-02 13:54:40 +0200109 struct tty_port tport;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200110 char is_cons; /* Is this our console. */
111 int magic;
112 int baud_base;
113 int port;
114 int irq;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200115 int type; /* UART type */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200116 int custom_divisor;
117 int x_char; /* xon/xoff character */
Jiri Slaby1bb26872012-04-02 13:54:39 +0200118 int line;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200119 unsigned char *xmit_buf;
120 int xmit_head;
121 int xmit_tail;
122 int xmit_cnt;
Jiri Slaby1bb26872012-04-02 13:54:39 +0200123};
124
125#define SERIAL_MAGIC 0x5301
126
127/*
128 * Define the number of ports supported and their irqs.
129 */
130#define NR_PORTS 1
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct m68k_serial m68k_soft[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jiri Slaby1bb26872012-04-02 13:54:39 +0200134static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/* multiple ports are contiguous in memory */
137m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139struct tty_driver *serial_driver;
140
Jiri Slaby467712c2012-04-02 13:54:44 +0200141static void change_speed(struct m68k_serial *info, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * Setup for console. Argument comes from the boot command line.
145 */
146
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700147/* note: this is messy, but it works, again, perhaps defined somewhere else?*/
148#ifdef CONFIG_M68VZ328
149#define CONSOLE_BAUD_RATE 19200
150#define DEFAULT_CBAUD B19200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#endif
152
Christoph Eggerf5e92c32010-07-20 15:26:54 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifndef CONSOLE_BAUD_RATE
155#define CONSOLE_BAUD_RATE 9600
156#define DEFAULT_CBAUD B9600
157#endif
158
159
160static int m68328_console_initted = 0;
161static int m68328_console_baud = CONSOLE_BAUD_RATE;
162static int m68328_console_cbaud = DEFAULT_CBAUD;
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static inline int serial_paranoia_check(struct m68k_serial *info,
166 char *name, const char *routine)
167{
168#ifdef SERIAL_PARANOIA_CHECK
169 static const char *badmagic =
170 "Warning: bad magic number for serial struct %s in %s\n";
171 static const char *badinfo =
172 "Warning: null m68k_serial for %s in %s\n";
173
174 if (!info) {
175 printk(badinfo, name, routine);
176 return 1;
177 }
178 if (info->magic != SERIAL_MAGIC) {
179 printk(badmagic, name, routine);
180 return 1;
181 }
182#endif
183 return 0;
184}
185
186/*
187 * This is used to figure out the divisor speeds and the timeouts
188 */
189static int baud_table[] = {
190 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
191 9600, 19200, 38400, 57600, 115200, 0 };
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* Utility routines */
194static inline int get_baud(struct m68k_serial *ss)
195{
196 unsigned long result = 115200;
197 unsigned short int baud = uart_addr[ss->line].ubaud;
198 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
199 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
200
201 return result;
202}
203
204/*
205 * ------------------------------------------------------------
206 * rs_stop() and rs_start()
207 *
208 * This routines are called before setting or resetting tty->stopped.
209 * They enable or disable transmitter interrupts, as necessary.
210 * ------------------------------------------------------------
211 */
212static void rs_stop(struct tty_struct *tty)
213{
214 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
215 m68328_uart *uart = &uart_addr[info->line];
216 unsigned long flags;
217
218 if (serial_paranoia_check(info, tty->name, "rs_stop"))
219 return;
220
Greg Ungerer727dda82006-06-28 15:54:22 +1000221 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 uart->ustcnt &= ~USTCNT_TXEN;
Greg Ungerer727dda82006-06-28 15:54:22 +1000223 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Alan Cox09a6ffa2008-04-30 00:54:01 -0700226static int rs_put_char(char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Jiri Slaby107afb72012-04-02 13:54:38 +0200228 unsigned long flags;
229 int loops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Greg Ungerer727dda82006-06-28 15:54:22 +1000231 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
234 loops++;
235 udelay(5);
236 }
237
238 UTX_TXDATA = ch;
239 udelay(5);
Greg Ungerer727dda82006-06-28 15:54:22 +1000240 local_irq_restore(flags);
Alan Cox09a6ffa2008-04-30 00:54:01 -0700241 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static void rs_start(struct tty_struct *tty)
245{
246 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
247 m68328_uart *uart = &uart_addr[info->line];
248 unsigned long flags;
249
250 if (serial_paranoia_check(info, tty->name, "rs_start"))
251 return;
252
Greg Ungerer727dda82006-06-28 15:54:22 +1000253 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
255#ifdef USE_INTS
256 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
257#else
258 uart->ustcnt |= USTCNT_TXEN;
259#endif
260 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000261 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100264static void receive_chars(struct m68k_serial *info, unsigned short rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return;
280#ifdef CONFIG_MAGIC_SYSRQ
281 } else if (ch == 0x10) { /* ^P */
282 show_state();
David Rientjes7bf02ea2011-05-24 17:11:16 -0700283 show_free_areas(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 show_buffers();
285/* show_net_buffers(); */
286 return;
287 } else if (ch == 0x12) { /* ^R */
Eric W. Biederman804ebf42005-07-26 11:59:54 -0600288 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return;
290#endif /* CONFIG_MAGIC_SYSRQ */
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Alan Cox33f0f882006-01-09 20:54:13 -0800294 flag = TTY_NORMAL;
295
Jiri Slabyc21e2652012-04-02 13:54:36 +0200296 if (rx & URX_PARITY_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800297 flag = TTY_PARITY;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200298 else if (rx & URX_OVRUN)
Alan Cox33f0f882006-01-09 20:54:13 -0800299 flag = TTY_OVERRUN;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200300 else if (rx & URX_FRAME_ERROR)
Alan Cox33f0f882006-01-09 20:54:13 -0800301 flag = TTY_FRAME;
Jiri Slabyc21e2652012-04-02 13:54:36 +0200302
Jiri Slaby92a19f92013-01-03 15:53:03 +0100303 tty_insert_flip_char(&info->tport, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifndef CONFIG_XCOPILOT_BUGS
305 } while((rx = uart->urx.w) & URX_DATA_READY);
306#endif
307
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100308 tty_schedule_flip(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Jiri Slaby467712c2012-04-02 13:54:44 +0200311static void transmit_chars(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 m68328_uart *uart = &uart_addr[info->line];
314
315 if (info->x_char) {
316 /* Send next char */
317 uart->utx.b.txdata = info->x_char;
318 info->x_char = 0;
319 goto clear_and_return;
320 }
321
Jiri Slaby467712c2012-04-02 13:54:44 +0200322 if ((info->xmit_cnt <= 0) || !tty || tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* That's peculiar... TX ints off */
324 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
325 goto clear_and_return;
326 }
327
328 /* Send char */
329 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
330 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
331 info->xmit_cnt--;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if(info->xmit_cnt <= 0) {
334 /* All done for now... TX ints off */
335 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
336 goto clear_and_return;
337 }
338
339clear_and_return:
340 /* Clear interrupt (should be auto)*/
341 return;
342}
343
344/*
345 * This is the serial driver's generic interrupt routine
346 */
David Howells7d12e782006-10-05 14:55:46 +0100347irqreturn_t rs_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Alan Cox25db8ad2008-08-19 20:49:40 -0700349 struct m68k_serial *info = dev_id;
Jiri Slaby665569d2012-04-02 13:54:45 +0200350 struct tty_struct *tty = tty_port_tty_get(&info->tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 m68328_uart *uart;
352 unsigned short rx;
353 unsigned short tx;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 uart = &uart_addr[info->line];
356 rx = uart->urx.w;
357
358#ifdef USE_INTS
359 tx = uart->utx.w;
360
Jiri Slaby467712c2012-04-02 13:54:44 +0200361 if (rx & URX_DATA_READY)
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100362 receive_chars(info, rx);
Jiri Slaby467712c2012-04-02 13:54:44 +0200363 if (tx & UTX_TX_AVAIL)
364 transmit_chars(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#else
Jiri Slaby6732c8b2013-01-03 15:53:07 +0100366 receive_chars(info, rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#endif
Jiri Slaby665569d2012-04-02 13:54:45 +0200368 tty_kref_put(tty);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return IRQ_HANDLED;
371}
372
Jiri Slaby467712c2012-04-02 13:54:44 +0200373static int startup(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 m68328_uart *uart = &uart_addr[info->line];
376 unsigned long flags;
377
Jiri Slabya85dd822012-04-02 13:54:43 +0200378 if (info->tport.flags & ASYNC_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380
381 if (!info->xmit_buf) {
382 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
383 if (!info->xmit_buf)
384 return -ENOMEM;
385 }
386
Greg Ungerer727dda82006-06-28 15:54:22 +1000387 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Clear the FIFO buffers and disable them
391 * (they will be reenabled in change_speed())
392 */
393
394 uart->ustcnt = USTCNT_UEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
396 (void)uart->urx.w;
397
398 /*
399 * Finally, enable sequencing and interrupts
400 */
401#ifdef USE_INTS
402 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
403 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
404#else
405 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
406#endif
407
Jiri Slaby467712c2012-04-02 13:54:44 +0200408 if (tty)
409 clear_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
411
412 /*
413 * and set the speed of the serial port
414 */
415
Jiri Slaby467712c2012-04-02 13:54:44 +0200416 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Jiri Slabya85dd822012-04-02 13:54:43 +0200418 info->tport.flags |= ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000419 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
423/*
424 * This routine will shutdown a serial port; interrupts are disabled, and
425 * DTR is dropped if the hangup on close termio flag is on.
426 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200427static void shutdown(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 m68328_uart *uart = &uart_addr[info->line];
430 unsigned long flags;
431
432 uart->ustcnt = 0; /* All off! */
Jiri Slabya85dd822012-04-02 13:54:43 +0200433 if (!(info->tport.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435
Greg Ungerer727dda82006-06-28 15:54:22 +1000436 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (info->xmit_buf) {
439 free_page((unsigned long) info->xmit_buf);
440 info->xmit_buf = 0;
441 }
442
Jiri Slaby467712c2012-04-02 13:54:44 +0200443 if (tty)
444 set_bit(TTY_IO_ERROR, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Jiri Slabya85dd822012-04-02 13:54:43 +0200446 info->tport.flags &= ~ASYNC_INITIALIZED;
Greg Ungerer727dda82006-06-28 15:54:22 +1000447 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450struct {
451 int divisor, prescale;
452}
453#ifndef CONFIG_M68VZ328
454 hw_baud_table[18] = {
455 {0,0}, /* 0 */
456 {0,0}, /* 50 */
457 {0,0}, /* 75 */
458 {0,0}, /* 110 */
459 {0,0}, /* 134 */
460 {0,0}, /* 150 */
461 {0,0}, /* 200 */
462 {7,0x26}, /* 300 */
463 {6,0x26}, /* 600 */
464 {5,0x26}, /* 1200 */
465 {0,0}, /* 1800 */
466 {4,0x26}, /* 2400 */
467 {3,0x26}, /* 4800 */
468 {2,0x26}, /* 9600 */
469 {1,0x26}, /* 19200 */
470 {0,0x26}, /* 38400 */
471 {1,0x38}, /* 57600 */
472 {0,0x38}, /* 115200 */
473};
474#else
475 hw_baud_table[18] = {
476 {0,0}, /* 0 */
477 {0,0}, /* 50 */
478 {0,0}, /* 75 */
479 {0,0}, /* 110 */
480 {0,0}, /* 134 */
481 {0,0}, /* 150 */
482 {0,0}, /* 200 */
483 {0,0}, /* 300 */
484 {7,0x26}, /* 600 */
485 {6,0x26}, /* 1200 */
486 {0,0}, /* 1800 */
487 {5,0x26}, /* 2400 */
488 {4,0x26}, /* 4800 */
489 {3,0x26}, /* 9600 */
490 {2,0x26}, /* 19200 */
491 {1,0x26}, /* 38400 */
492 {0,0x26}, /* 57600 */
493 {1,0x38}, /* 115200 */
494};
495#endif
496/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
497
498/*
499 * This routine is called to set the UART divisor registers to match
500 * the specified baud rate for a serial port.
501 */
Jiri Slaby467712c2012-04-02 13:54:44 +0200502static void change_speed(struct m68k_serial *info, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 m68328_uart *uart = &uart_addr[info->line];
505 unsigned short port;
506 unsigned short ustcnt;
507 unsigned cflag;
508 int i;
509
Jiri Slaby3dd332c2012-08-07 21:47:27 +0200510 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!(port = info->port))
512 return;
513
514 ustcnt = uart->ustcnt;
515 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
516
517 i = cflag & CBAUD;
518 if (i & CBAUDEX) {
519 i = (i & ~CBAUDEX) + B38400;
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
523 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
524
525 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
526
527 if ((cflag & CSIZE) == CS8)
528 ustcnt |= USTCNT_8_7;
529
530 if (cflag & CSTOPB)
531 ustcnt |= USTCNT_STOP;
532
533 if (cflag & PARENB)
534 ustcnt |= USTCNT_PARITYEN;
535 if (cflag & PARODD)
536 ustcnt |= USTCNT_ODD_EVEN;
537
538#ifdef CONFIG_SERIAL_68328_RTS_CTS
539 if (cflag & CRTSCTS) {
540 uart->utx.w &= ~ UTX_NOCTS;
541 } else {
542 uart->utx.w |= UTX_NOCTS;
543 }
544#endif
545
546 ustcnt |= USTCNT_TXEN;
547
548 uart->ustcnt = ustcnt;
549 return;
550}
551
552/*
553 * Fair output driver allows a process to speak.
554 */
555static void rs_fair_output(void)
556{
557 int left; /* Output no more than that */
558 unsigned long flags;
559 struct m68k_serial *info = &m68k_soft[0];
560 char c;
561
562 if (info == 0) return;
563 if (info->xmit_buf == 0) return;
564
Greg Ungerer727dda82006-06-28 15:54:22 +1000565 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 left = info->xmit_cnt;
567 while (left != 0) {
568 c = info->xmit_buf[info->xmit_tail];
569 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
570 info->xmit_cnt--;
Greg Ungerer727dda82006-06-28 15:54:22 +1000571 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 rs_put_char(c);
574
Greg Ungerer727dda82006-06-28 15:54:22 +1000575 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 left = min(info->xmit_cnt, left-1);
577 }
578
579 /* Last character is being transmitted now (hopefully). */
580 udelay(5);
581
Greg Ungerer727dda82006-06-28 15:54:22 +1000582 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return;
584}
585
586/*
587 * m68k_console_print is registered for printk.
588 */
589void console_print_68328(const char *p)
590{
591 char c;
592
593 while((c=*(p++)) != 0) {
594 if(c == '\n')
595 rs_put_char('\r');
596 rs_put_char(c);
597 }
598
599 /* Comment this if you want to have a strict interrupt-driven output */
600 rs_fair_output();
601
602 return;
603}
604
605static void rs_set_ldisc(struct tty_struct *tty)
606{
607 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
608
609 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
610 return;
611
Jiri Slaby3dd332c2012-08-07 21:47:27 +0200612 info->is_cons = (tty->termios.c_line == N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
615}
616
617static void rs_flush_chars(struct tty_struct *tty)
618{
619 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
620 m68328_uart *uart = &uart_addr[info->line];
621 unsigned long flags;
622
623 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
624 return;
625#ifndef USE_INTS
626 for(;;) {
627#endif
628
629 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000630 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jiri Slabyee797062013-03-07 13:12:34 +0100632 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000633 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return;
635 }
636
637#ifdef USE_INTS
638 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
639#else
640 uart->ustcnt |= USTCNT_TXEN;
641#endif
642
643#ifdef USE_INTS
644 if (uart->utx.w & UTX_TX_AVAIL) {
645#else
646 if (1) {
647#endif
648 /* Send char */
649 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
650 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
651 info->xmit_cnt--;
652 }
653
654#ifndef USE_INTS
655 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
656 }
657#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000658 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661extern void console_printn(const char * b, int count);
662
663static int rs_write(struct tty_struct * tty,
664 const unsigned char *buf, int count)
665{
666 int c, total = 0;
667 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
668 m68328_uart *uart = &uart_addr[info->line];
669 unsigned long flags;
670
671 if (serial_paranoia_check(info, tty->name, "rs_write"))
672 return 0;
673
674 if (!tty || !info->xmit_buf)
675 return 0;
676
Greg Ungerer727dda82006-06-28 15:54:22 +1000677 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 while (1) {
Greg Ungerer727dda82006-06-28 15:54:22 +1000679 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
681 SERIAL_XMIT_SIZE - info->xmit_head));
Greg Ungerer727dda82006-06-28 15:54:22 +1000682 local_irq_restore(flags);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (c <= 0)
685 break;
686
687 memcpy(info->xmit_buf + info->xmit_head, buf, c);
Greg Ungerer727dda82006-06-28 15:54:22 +1000688
689 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
691 info->xmit_cnt += c;
Greg Ungerer727dda82006-06-28 15:54:22 +1000692 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 buf += c;
694 count -= c;
695 total += c;
696 }
697
Jiri Slabyee797062013-03-07 13:12:34 +0100698 if (info->xmit_cnt && !tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* Enable transmitter */
Greg Ungerer727dda82006-06-28 15:54:22 +1000700 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#ifndef USE_INTS
702 while(info->xmit_cnt) {
703#endif
704
705 uart->ustcnt |= USTCNT_TXEN;
706#ifdef USE_INTS
707 uart->ustcnt |= USTCNT_TX_INTR_MASK;
708#else
709 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
710#endif
711 if (uart->utx.w & UTX_TX_AVAIL) {
712 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
713 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
714 info->xmit_cnt--;
715 }
716
717#ifndef USE_INTS
718 }
719#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000720 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Greg Ungerer727dda82006-06-28 15:54:22 +1000722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return total;
724}
725
726static int rs_write_room(struct tty_struct *tty)
727{
728 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
729 int ret;
730
731 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
732 return 0;
733 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
734 if (ret < 0)
735 ret = 0;
736 return ret;
737}
738
739static int rs_chars_in_buffer(struct tty_struct *tty)
740{
741 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
742
743 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
744 return 0;
745 return info->xmit_cnt;
746}
747
748static void rs_flush_buffer(struct tty_struct *tty)
749{
750 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
Greg Ungerer727dda82006-06-28 15:54:22 +1000751 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
754 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000755 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
Greg Ungerer727dda82006-06-28 15:54:22 +1000757 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 tty_wakeup(tty);
759}
760
761/*
762 * ------------------------------------------------------------
763 * rs_throttle()
764 *
765 * This routine is called by the upper-layer tty layer to signal that
766 * incoming characters should be throttled.
767 * ------------------------------------------------------------
768 */
769static void rs_throttle(struct tty_struct * tty)
770{
771 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
772
773 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
774 return;
775
776 if (I_IXOFF(tty))
777 info->x_char = STOP_CHAR(tty);
778
779 /* Turn off RTS line (do this atomic) */
780}
781
782static void rs_unthrottle(struct tty_struct * tty)
783{
784 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
785
786 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
787 return;
788
789 if (I_IXOFF(tty)) {
790 if (info->x_char)
791 info->x_char = 0;
792 else
793 info->x_char = START_CHAR(tty);
794 }
795
796 /* Assert RTS line (do this atomic) */
797}
798
799/*
800 * ------------------------------------------------------------
801 * rs_ioctl() and friends
802 * ------------------------------------------------------------
803 */
804
805static int get_serial_info(struct m68k_serial * info,
806 struct serial_struct * retinfo)
807{
808 struct serial_struct tmp;
809
810 if (!retinfo)
811 return -EFAULT;
812 memset(&tmp, 0, sizeof(tmp));
813 tmp.type = info->type;
814 tmp.line = info->line;
815 tmp.port = info->port;
816 tmp.irq = info->irq;
Jiri Slabya85dd822012-04-02 13:54:43 +0200817 tmp.flags = info->tport.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 tmp.baud_base = info->baud_base;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200819 tmp.close_delay = info->tport.close_delay;
820 tmp.closing_wait = info->tport.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 tmp.custom_divisor = info->custom_divisor;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400822 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
823 return -EFAULT;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
Jiri Slaby467712c2012-04-02 13:54:44 +0200828static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct serial_struct * new_info)
830{
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200831 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct serial_struct new_serial;
833 struct m68k_serial old_info;
834 int retval = 0;
835
836 if (!new_info)
837 return -EFAULT;
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400838 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
839 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 old_info = *info;
841
842 if (!capable(CAP_SYS_ADMIN)) {
843 if ((new_serial.baud_base != info->baud_base) ||
844 (new_serial.type != info->type) ||
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200845 (new_serial.close_delay != port->close_delay) ||
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200846 ((new_serial.flags & ~ASYNC_USR_MASK) !=
Jiri Slabya85dd822012-04-02 13:54:43 +0200847 (port->flags & ~ASYNC_USR_MASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EPERM;
Jiri Slabya85dd822012-04-02 13:54:43 +0200849 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200850 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 info->custom_divisor = new_serial.custom_divisor;
852 goto check_and_exit;
853 }
854
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200855 if (port->count > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return -EBUSY;
857
858 /*
859 * OK, past this point, all the error checking has been done.
860 * At this point, we start making changes.....
861 */
862
863 info->baud_base = new_serial.baud_base;
Jiri Slabya85dd822012-04-02 13:54:43 +0200864 port->flags = ((port->flags & ~ASYNC_FLAGS) |
Jiri Slabycea4b2c2012-04-02 13:54:35 +0200865 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 info->type = new_serial.type;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +0200867 port->close_delay = new_serial.close_delay;
868 port->closing_wait = new_serial.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870check_and_exit:
Jiri Slaby467712c2012-04-02 13:54:44 +0200871 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return retval;
873}
874
875/*
876 * get_lsr_info - get line status register info
877 *
878 * Purpose: Let user call ioctl() to get info when the UART physically
879 * is emptied. On bus types like RS485, the transmitter must
880 * release the bus after transmitting. This must be done when
881 * the transmit shift register is empty, not be done when the
882 * transmit holding register is empty. This functionality
883 * allows an RS485 driver to be written in user space.
884 */
885static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
886{
887#ifdef CONFIG_SERIAL_68328_RTS_CTS
888 m68328_uart *uart = &uart_addr[info->line];
889#endif
890 unsigned char status;
Greg Ungerer727dda82006-06-28 15:54:22 +1000891 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Greg Ungerer727dda82006-06-28 15:54:22 +1000893 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#ifdef CONFIG_SERIAL_68328_RTS_CTS
895 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
896#else
897 status = 0;
898#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000899 local_irq_restore(flags);
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400900 return put_user(status, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903/*
904 * This routine sends a break character out the serial port.
905 */
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700906static void send_break(struct m68k_serial * info, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 m68328_uart *uart = &uart_addr[info->line];
909 unsigned long flags;
910 if (!info->port)
911 return;
Greg Ungerer727dda82006-06-28 15:54:22 +1000912 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#ifdef USE_INTS
914 uart->utx.w |= UTX_SEND_BREAK;
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700915 msleep_interruptible(duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 uart->utx.w &= ~UTX_SEND_BREAK;
917#endif
Greg Ungerer727dda82006-06-28 15:54:22 +1000918 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Alan Cox6caa76b2011-02-14 16:27:22 +0000921static int rs_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 unsigned int cmd, unsigned long arg)
923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
925 int retval;
926
927 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
928 return -ENODEV;
929
930 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
931 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
932 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
933 if (tty->flags & (1 << TTY_IO_ERROR))
934 return -EIO;
935 }
936
937 switch (cmd) {
938 case TCSBRK: /* SVID version: non-zero arg --> no break */
939 retval = tty_check_change(tty);
940 if (retval)
941 return retval;
942 tty_wait_until_sent(tty, 0);
943 if (!arg)
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700944 send_break(info, 250); /* 1/4 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946 case TCSBRKP: /* support for POSIX tcsendbreak() */
947 retval = tty_check_change(tty);
948 if (retval)
949 return retval;
950 tty_wait_until_sent(tty, 0);
Nishanth Aravamudan6a72c7b2005-06-25 14:59:33 -0700951 send_break(info, arg ? arg*(100) : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case TIOCGSERIAL:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400954 return get_serial_info(info,
955 (struct serial_struct *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 case TIOCSSERIAL:
Jiri Slaby467712c2012-04-02 13:54:44 +0200957 return set_serial_info(info, tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 (struct serial_struct *) arg);
959 case TIOCSERGETLSR: /* Get line status register */
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400960 return get_lsr_info(info, (unsigned int *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 case TIOCSERGSTRUCT:
Kulikov Vasiliy5d563562010-08-01 10:29:06 +0400962 if (copy_to_user((struct m68k_serial *) arg,
963 info, sizeof(struct m68k_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 default:
967 return -ENOIOCTLCMD;
968 }
969 return 0;
970}
971
Alan Cox606d0992006-12-08 02:38:45 -0800972static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
975
Jiri Slaby467712c2012-04-02 13:54:44 +0200976 change_speed(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if ((old_termios->c_cflag & CRTSCTS) &&
Jiri Slabyee797062013-03-07 13:12:34 +0100979 !(tty->termios.c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 rs_start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982}
983
984/*
985 * ------------------------------------------------------------
986 * rs_close()
987 *
988 * This routine is called when the serial port gets closed. First, we
989 * wait for the last remaining data to be sent. Then, we unlink its
990 * S structure from the interrupt chain if necessary, and we free
991 * that IRQ if nothing is left in the chain.
992 * ------------------------------------------------------------
993 */
994static void rs_close(struct tty_struct *tty, struct file * filp)
995{
996 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
Jiri Slaby86264342012-04-02 13:54:40 +0200997 struct tty_port *port = &info->tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 m68328_uart *uart = &uart_addr[info->line];
999 unsigned long flags;
1000
Cong Ding9ef20d52013-01-16 23:30:43 +01001001 if (serial_paranoia_check(info, tty->name, "rs_close"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return;
1003
Greg Ungerer727dda82006-06-28 15:54:22 +10001004 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (tty_hung_up_p(filp)) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001007 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010
Jiri Slaby86264342012-04-02 13:54:40 +02001011 if ((tty->count == 1) && (port->count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Uh, oh. tty->count is 1, which means that the tty
1014 * structure will be freed. Info->count should always
1015 * be one in these conditions. If it's greater than
1016 * one, we've got real problems, since it means the
1017 * serial port won't be shutdown.
1018 */
1019 printk("rs_close: bad serial port count; tty->count is 1, "
Jiri Slaby86264342012-04-02 13:54:40 +02001020 "port->count is %d\n", port->count);
1021 port->count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Jiri Slaby86264342012-04-02 13:54:40 +02001023 if (--port->count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk("rs_close: bad serial port count for ttyS%d: %d\n",
Jiri Slaby86264342012-04-02 13:54:40 +02001025 info->line, port->count);
1026 port->count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Jiri Slaby86264342012-04-02 13:54:40 +02001028 if (port->count) {
Greg Ungerer727dda82006-06-28 15:54:22 +10001029 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return;
1031 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001032 port->flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
1034 * Now we wait for the transmit buffer to clear; and we notify
1035 * the line discipline to only process XON/XOFF characters.
1036 */
1037 tty->closing = 1;
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001038 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1039 tty_wait_until_sent(tty, port->closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * At this point we stop accepting input. To do this, we
1042 * disable the receive line status interrupts, and tell the
1043 * interrupt driver to stop checking the data ready bit in the
1044 * line status register.
1045 */
1046
1047 uart->ustcnt &= ~USTCNT_RXEN;
1048 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1049
Jiri Slaby467712c2012-04-02 13:54:44 +02001050 shutdown(info, tty);
Alan Cox978e5952008-04-30 00:53:59 -07001051 rs_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 tty_ldisc_flush(tty);
1054 tty->closing = 0;
Jiri Slaby665569d2012-04-02 13:54:45 +02001055 tty_port_tty_set(&info->tport, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#warning "This is not and has never been valid so fix it"
1057#if 0
1058 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1059 if (tty->ldisc.close)
1060 (tty->ldisc.close)(tty);
1061 tty->ldisc = ldiscs[N_TTY];
Jiri Slaby3dd332c2012-08-07 21:47:27 +02001062 tty->termios.c_line = N_TTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (tty->ldisc.open)
1064 (tty->ldisc.open)(tty);
1065 }
1066#endif
Jiri Slaby86264342012-04-02 13:54:40 +02001067 if (port->blocked_open) {
Jiri Slaby4a85b1f2012-04-02 13:54:42 +02001068 if (port->close_delay)
1069 msleep_interruptible(jiffies_to_msecs(port->close_delay));
Jiri Slabyc26f0112012-04-02 13:54:41 +02001070 wake_up_interruptible(&port->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Jiri Slabya85dd822012-04-02 13:54:43 +02001072 port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001073 wake_up_interruptible(&port->close_wait);
Greg Ungerer727dda82006-06-28 15:54:22 +10001074 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/*
1078 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1079 */
1080void rs_hangup(struct tty_struct *tty)
1081{
1082 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1083
1084 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1085 return;
1086
1087 rs_flush_buffer(tty);
Jiri Slaby467712c2012-04-02 13:54:44 +02001088 shutdown(info, tty);
Jiri Slaby86264342012-04-02 13:54:40 +02001089 info->tport.count = 0;
Jiri Slabya85dd822012-04-02 13:54:43 +02001090 info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
Jiri Slaby665569d2012-04-02 13:54:45 +02001091 tty_port_tty_set(&info->tport, NULL);
Jiri Slabyc26f0112012-04-02 13:54:41 +02001092 wake_up_interruptible(&info->tport.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * This routine is called whenever a serial port is opened. It
1097 * enables interrupts for a serial port, linking in its S structure into
1098 * the IRQ chain. It also performs the serial-specific
1099 * initialization for the tty structure.
1100 */
1101int rs_open(struct tty_struct *tty, struct file * filp)
1102{
1103 struct m68k_serial *info;
Jiri Slaby410235f2012-03-05 14:52:01 +01001104 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Jiri Slaby410235f2012-03-05 14:52:01 +01001106 info = &m68k_soft[tty->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (serial_paranoia_check(info, tty->name, "rs_open"))
1109 return -ENODEV;
1110
Jiri Slaby86264342012-04-02 13:54:40 +02001111 info->tport.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 tty->driver_data = info;
Jiri Slaby665569d2012-04-02 13:54:45 +02001113 tty_port_tty_set(&info->tport, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * Start up serial port
1117 */
Jiri Slaby467712c2012-04-02 13:54:44 +02001118 retval = startup(info, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (retval)
1120 return retval;
1121
Jiri Slaby8e328412012-04-02 13:54:46 +02001122 return tty_port_block_til_ready(&info->tport, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/* Finally, routines used to initialize the serial driver. */
1126
1127static void show_serial_version(void)
1128{
1129 printk("MC68328 serial driver version 1.00\n");
1130}
1131
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001132static const struct tty_operations rs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 .open = rs_open,
1134 .close = rs_close,
1135 .write = rs_write,
1136 .flush_chars = rs_flush_chars,
1137 .write_room = rs_write_room,
1138 .chars_in_buffer = rs_chars_in_buffer,
1139 .flush_buffer = rs_flush_buffer,
1140 .ioctl = rs_ioctl,
1141 .throttle = rs_throttle,
1142 .unthrottle = rs_unthrottle,
1143 .set_termios = rs_set_termios,
1144 .stop = rs_stop,
1145 .start = rs_start,
1146 .hangup = rs_hangup,
1147 .set_ldisc = rs_set_ldisc,
1148};
1149
Jiri Slaby8e328412012-04-02 13:54:46 +02001150static const struct tty_port_operations rs_port_ops = {
1151};
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/* rs_init inits the driver */
1154static int __init
1155rs68328_init(void)
1156{
Jiri Slaby107afb72012-04-02 13:54:38 +02001157 unsigned long flags;
1158 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct m68k_serial *info;
1160
1161 serial_driver = alloc_tty_driver(NR_PORTS);
1162 if (!serial_driver)
1163 return -ENOMEM;
1164
1165 show_serial_version();
1166
1167 /* Initialize the tty_driver structure */
1168 /* SPARC: Not all of this is exactly right for us. */
1169
1170 serial_driver->name = "ttyS";
1171 serial_driver->major = TTY_MAJOR;
1172 serial_driver->minor_start = 64;
1173 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1174 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1175 serial_driver->init_termios = tty_std_termios;
1176 serial_driver->init_termios.c_cflag =
1177 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1178 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1179 tty_set_operations(serial_driver, &rs_ops);
1180
Greg Ungerer727dda82006-06-28 15:54:22 +10001181 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 for(i=0;i<NR_PORTS;i++) {
1184
1185 info = &m68k_soft[i];
Jiri Slaby86264342012-04-02 13:54:40 +02001186 tty_port_init(&info->tport);
Jiri Slaby8e328412012-04-02 13:54:46 +02001187 info->tport.ops = &rs_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 info->magic = SERIAL_MAGIC;
1189 info->port = (int) &uart_addr[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 info->irq = uart_irqs[i];
1191 info->custom_divisor = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 info->x_char = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 info->line = i;
1194 info->is_cons = 1; /* Means shortcuts work */
1195
1196 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1197 info->port, info->irq);
1198 printk(" is a builtin MC68328 UART\n");
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#ifdef CONFIG_M68VZ328
1201 if (i > 0 )
1202 PJSEL &= 0xCF; /* PSW enable second port output */
1203#endif
1204
1205 if (request_irq(uart_irqs[i],
1206 rs_interrupt,
Yong Zhang9cfb5c02011-09-22 16:59:15 +08001207 0,
Alan Cox25db8ad2008-08-19 20:49:40 -07001208 "M68328_UART", info))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 panic("Unable to attach 68328 serial interrupt\n");
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001210
1211 tty_port_link_device(&info->tport, serial_driver, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Greg Ungerer727dda82006-06-28 15:54:22 +10001213 local_irq_restore(flags);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001214
1215 if (tty_register_driver(serial_driver)) {
1216 put_tty_driver(serial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +01001217 for (i = 0; i < NR_PORTS; i++)
1218 tty_port_destroy(&m68k_soft[i].tport);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +02001219 printk(KERN_ERR "Couldn't register serial driver\n");
1220 return -ENOMEM;
1221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
1224}
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226module_init(rs68328_init);
1227
1228
1229
1230static void m68328_set_baud(void)
1231{
1232 unsigned short ustcnt;
1233 int i;
1234
1235 ustcnt = USTCNT;
1236 USTCNT = ustcnt & ~USTCNT_TXEN;
1237
1238again:
Thiago Farina8b505ca2009-12-09 12:31:30 -08001239 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (baud_table[i] == m68328_console_baud)
1241 break;
Thiago Farina8b505ca2009-12-09 12:31:30 -08001242 if (i >= ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 m68328_console_baud = 9600;
1244 goto again;
1245 }
1246
1247 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1248 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1249 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1250 ustcnt |= USTCNT_8_7;
1251 ustcnt |= USTCNT_TXEN;
1252 USTCNT = ustcnt;
1253 m68328_console_initted = 1;
1254 return;
1255}
1256
1257
1258int m68328_console_setup(struct console *cp, char *arg)
1259{
1260 int i, n = CONSOLE_BAUD_RATE;
1261
1262 if (!cp)
1263 return(-1);
1264
1265 if (arg)
1266 n = simple_strtoul(arg,NULL,0);
1267
Thiago Farina8b505ca2009-12-09 12:31:30 -08001268 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (baud_table[i] == n)
1270 break;
Greg Ungerere9a137c2010-05-24 14:32:55 -07001271 if (i < ARRAY_SIZE(baud_table)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 m68328_console_baud = n;
1273 m68328_console_cbaud = 0;
1274 if (i > 15) {
1275 m68328_console_cbaud |= CBAUDEX;
1276 i -= 15;
1277 }
1278 m68328_console_cbaud |= i;
1279 }
1280
1281 m68328_set_baud(); /* make sure baud rate changes */
1282 return(0);
1283}
1284
1285
1286static struct tty_driver *m68328_console_device(struct console *c, int *index)
1287{
1288 *index = c->index;
1289 return serial_driver;
1290}
1291
1292
1293void m68328_console_write (struct console *co, const char *str,
1294 unsigned int count)
1295{
1296 if (!m68328_console_initted)
1297 m68328_set_baud();
1298 while (count--) {
1299 if (*str == '\n')
1300 rs_put_char('\r');
1301 rs_put_char( *str++ );
1302 }
1303}
1304
1305
1306static struct console m68328_driver = {
1307 .name = "ttyS",
1308 .write = m68328_console_write,
1309 .device = m68328_console_device,
1310 .setup = m68328_console_setup,
1311 .flags = CON_PRINTBUFFER,
1312 .index = -1,
1313};
1314
1315
1316static int __init m68328_console_init(void)
1317{
1318 register_console(&m68328_driver);
1319 return 0;
1320}
1321
1322console_initcall(m68328_console_init);