Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Slaby | cea4b2c | 2012-04-02 13:54:35 +0200 | [diff] [blame] | 20 | #include <linux/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <asm/io.h> |
| 42 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* 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 Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 67 | /* |
| 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 Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 108 | */ |
| 109 | struct m68k_serial { |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 110 | struct tty_port tport; |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 111 | char is_cons; /* Is this our console. */ |
| 112 | int magic; |
| 113 | int baud_base; |
| 114 | int port; |
| 115 | int irq; |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 116 | int type; /* UART type */ |
| 117 | struct tty_struct *tty; |
| 118 | int custom_divisor; |
| 119 | int x_char; /* xon/xoff character */ |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 120 | int line; |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 121 | unsigned char *xmit_buf; |
| 122 | int xmit_head; |
| 123 | int xmit_tail; |
| 124 | int xmit_cnt; |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | #define SERIAL_MAGIC 0x5301 |
| 128 | |
| 129 | /* |
| 130 | * Define the number of ports supported and their irqs. |
| 131 | */ |
| 132 | #define NR_PORTS 1 |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static struct m68k_serial m68k_soft[NR_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Jiri Slaby | 1bb2687 | 2012-04-02 13:54:39 +0200 | [diff] [blame] | 136 | static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /* multiple ports are contiguous in memory */ |
| 139 | m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR; |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct tty_driver *serial_driver; |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | static void change_speed(struct m68k_serial *info); |
| 144 | |
| 145 | /* |
| 146 | * Setup for console. Argument comes from the boot command line. |
| 147 | */ |
| 148 | |
Christoph Egger | f5e92c3 | 2010-07-20 15:26:54 -0700 | [diff] [blame] | 149 | /* note: this is messy, but it works, again, perhaps defined somewhere else?*/ |
| 150 | #ifdef CONFIG_M68VZ328 |
| 151 | #define CONSOLE_BAUD_RATE 19200 |
| 152 | #define DEFAULT_CBAUD B19200 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #endif |
| 154 | |
Christoph Egger | f5e92c3 | 2010-07-20 15:26:54 -0700 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | #ifndef CONSOLE_BAUD_RATE |
| 157 | #define CONSOLE_BAUD_RATE 9600 |
| 158 | #define DEFAULT_CBAUD B9600 |
| 159 | #endif |
| 160 | |
| 161 | |
| 162 | static int m68328_console_initted = 0; |
| 163 | static int m68328_console_baud = CONSOLE_BAUD_RATE; |
| 164 | static int m68328_console_cbaud = DEFAULT_CBAUD; |
| 165 | |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | static inline int serial_paranoia_check(struct m68k_serial *info, |
| 168 | char *name, const char *routine) |
| 169 | { |
| 170 | #ifdef SERIAL_PARANOIA_CHECK |
| 171 | static const char *badmagic = |
| 172 | "Warning: bad magic number for serial struct %s in %s\n"; |
| 173 | static const char *badinfo = |
| 174 | "Warning: null m68k_serial for %s in %s\n"; |
| 175 | |
| 176 | if (!info) { |
| 177 | printk(badinfo, name, routine); |
| 178 | return 1; |
| 179 | } |
| 180 | if (info->magic != SERIAL_MAGIC) { |
| 181 | printk(badmagic, name, routine); |
| 182 | return 1; |
| 183 | } |
| 184 | #endif |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * This is used to figure out the divisor speeds and the timeouts |
| 190 | */ |
| 191 | static int baud_table[] = { |
| 192 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 193 | 9600, 19200, 38400, 57600, 115200, 0 }; |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* Utility routines */ |
| 196 | static inline int get_baud(struct m68k_serial *ss) |
| 197 | { |
| 198 | unsigned long result = 115200; |
| 199 | unsigned short int baud = uart_addr[ss->line].ubaud; |
| 200 | if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400; |
| 201 | result >>= GET_FIELD(baud, UBAUD_DIVIDE); |
| 202 | |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * ------------------------------------------------------------ |
| 208 | * rs_stop() and rs_start() |
| 209 | * |
| 210 | * This routines are called before setting or resetting tty->stopped. |
| 211 | * They enable or disable transmitter interrupts, as necessary. |
| 212 | * ------------------------------------------------------------ |
| 213 | */ |
| 214 | static void rs_stop(struct tty_struct *tty) |
| 215 | { |
| 216 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 217 | m68328_uart *uart = &uart_addr[info->line]; |
| 218 | unsigned long flags; |
| 219 | |
| 220 | if (serial_paranoia_check(info, tty->name, "rs_stop")) |
| 221 | return; |
| 222 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 223 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | uart->ustcnt &= ~USTCNT_TXEN; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 225 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Alan Cox | 09a6ffa | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 228 | static int rs_put_char(char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Jiri Slaby | 107afb7 | 2012-04-02 13:54:38 +0200 | [diff] [blame] | 230 | unsigned long flags; |
| 231 | int loops = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 233 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
| 235 | while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) { |
| 236 | loops++; |
| 237 | udelay(5); |
| 238 | } |
| 239 | |
| 240 | UTX_TXDATA = ch; |
| 241 | udelay(5); |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 242 | local_irq_restore(flags); |
Alan Cox | 09a6ffa | 2008-04-30 00:54:01 -0700 | [diff] [blame] | 243 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static void rs_start(struct tty_struct *tty) |
| 247 | { |
| 248 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 249 | m68328_uart *uart = &uart_addr[info->line]; |
| 250 | unsigned long flags; |
| 251 | |
| 252 | if (serial_paranoia_check(info, tty->name, "rs_start")) |
| 253 | return; |
| 254 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 255 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) { |
| 257 | #ifdef USE_INTS |
| 258 | uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK; |
| 259 | #else |
| 260 | uart->ustcnt |= USTCNT_TXEN; |
| 261 | #endif |
| 262 | } |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 263 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 266 | static void receive_chars(struct m68k_serial *info, unsigned short rx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 268 | struct tty_struct *tty = info->tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | m68328_uart *uart = &uart_addr[info->line]; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 270 | unsigned char ch, flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * This do { } while() loop will get ALL chars out of Rx FIFO |
| 274 | */ |
| 275 | #ifndef CONFIG_XCOPILOT_BUGS |
| 276 | do { |
| 277 | #endif |
| 278 | ch = GET_FIELD(rx, URX_RXDATA); |
| 279 | |
| 280 | if(info->is_cons) { |
| 281 | if(URX_BREAK & rx) { /* whee, break received */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return; |
| 283 | #ifdef CONFIG_MAGIC_SYSRQ |
| 284 | } else if (ch == 0x10) { /* ^P */ |
| 285 | show_state(); |
David Rientjes | 7bf02ea | 2011-05-24 17:11:16 -0700 | [diff] [blame] | 286 | show_free_areas(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | show_buffers(); |
| 288 | /* show_net_buffers(); */ |
| 289 | return; |
| 290 | } else if (ch == 0x12) { /* ^R */ |
Eric W. Biederman | 804ebf4 | 2005-07-26 11:59:54 -0600 | [diff] [blame] | 291 | emergency_restart(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return; |
| 293 | #endif /* CONFIG_MAGIC_SYSRQ */ |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if(!tty) |
| 298 | goto clear_and_exit; |
| 299 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 300 | flag = TTY_NORMAL; |
| 301 | |
Jiri Slaby | c21e265 | 2012-04-02 13:54:36 +0200 | [diff] [blame] | 302 | if (rx & URX_PARITY_ERROR) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 303 | flag = TTY_PARITY; |
Jiri Slaby | c21e265 | 2012-04-02 13:54:36 +0200 | [diff] [blame] | 304 | else if (rx & URX_OVRUN) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 305 | flag = TTY_OVERRUN; |
Jiri Slaby | c21e265 | 2012-04-02 13:54:36 +0200 | [diff] [blame] | 306 | else if (rx & URX_FRAME_ERROR) |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 307 | flag = TTY_FRAME; |
Jiri Slaby | c21e265 | 2012-04-02 13:54:36 +0200 | [diff] [blame] | 308 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 309 | tty_insert_flip_char(tty, ch, flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | #ifndef CONFIG_XCOPILOT_BUGS |
| 311 | } while((rx = uart->urx.w) & URX_DATA_READY); |
| 312 | #endif |
| 313 | |
Greg Ungerer | 8e63e66 | 2006-02-08 09:19:17 +1000 | [diff] [blame] | 314 | tty_schedule_flip(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | clear_and_exit: |
| 317 | return; |
| 318 | } |
| 319 | |
Adrian Bunk | 41c28ff | 2006-03-23 03:00:56 -0800 | [diff] [blame] | 320 | static void transmit_chars(struct m68k_serial *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | m68328_uart *uart = &uart_addr[info->line]; |
| 323 | |
| 324 | if (info->x_char) { |
| 325 | /* Send next char */ |
| 326 | uart->utx.b.txdata = info->x_char; |
| 327 | info->x_char = 0; |
| 328 | goto clear_and_return; |
| 329 | } |
| 330 | |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 331 | if((info->xmit_cnt <= 0) || info->tty->stopped) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | /* That's peculiar... TX ints off */ |
| 333 | uart->ustcnt &= ~USTCNT_TX_INTR_MASK; |
| 334 | goto clear_and_return; |
| 335 | } |
| 336 | |
| 337 | /* Send char */ |
| 338 | uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; |
| 339 | info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); |
| 340 | info->xmit_cnt--; |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if(info->xmit_cnt <= 0) { |
| 343 | /* All done for now... TX ints off */ |
| 344 | uart->ustcnt &= ~USTCNT_TX_INTR_MASK; |
| 345 | goto clear_and_return; |
| 346 | } |
| 347 | |
| 348 | clear_and_return: |
| 349 | /* Clear interrupt (should be auto)*/ |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * This is the serial driver's generic interrupt routine |
| 355 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 356 | irqreturn_t rs_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Alan Cox | 25db8ad | 2008-08-19 20:49:40 -0700 | [diff] [blame] | 358 | struct m68k_serial *info = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | m68328_uart *uart; |
| 360 | unsigned short rx; |
| 361 | unsigned short tx; |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | uart = &uart_addr[info->line]; |
| 364 | rx = uart->urx.w; |
| 365 | |
| 366 | #ifdef USE_INTS |
| 367 | tx = uart->utx.w; |
| 368 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 369 | if (rx & URX_DATA_READY) receive_chars(info, rx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (tx & UTX_TX_AVAIL) transmit_chars(info); |
| 371 | #else |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 372 | receive_chars(info, rx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | #endif |
| 374 | return IRQ_HANDLED; |
| 375 | } |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static int startup(struct m68k_serial * info) |
| 378 | { |
| 379 | m68328_uart *uart = &uart_addr[info->line]; |
| 380 | unsigned long flags; |
| 381 | |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 382 | if (info->tport.flags & ASYNC_INITIALIZED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | |
| 385 | if (!info->xmit_buf) { |
| 386 | info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL); |
| 387 | if (!info->xmit_buf) |
| 388 | return -ENOMEM; |
| 389 | } |
| 390 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 391 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * Clear the FIFO buffers and disable them |
| 395 | * (they will be reenabled in change_speed()) |
| 396 | */ |
| 397 | |
| 398 | uart->ustcnt = USTCNT_UEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN; |
| 400 | (void)uart->urx.w; |
| 401 | |
| 402 | /* |
| 403 | * Finally, enable sequencing and interrupts |
| 404 | */ |
| 405 | #ifdef USE_INTS |
| 406 | uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | |
| 407 | USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK; |
| 408 | #else |
| 409 | uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK; |
| 410 | #endif |
| 411 | |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 412 | if (info->tty) |
| 413 | clear_bit(TTY_IO_ERROR, &info->tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; |
| 415 | |
| 416 | /* |
| 417 | * and set the speed of the serial port |
| 418 | */ |
| 419 | |
| 420 | change_speed(info); |
| 421 | |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 422 | info->tport.flags |= ASYNC_INITIALIZED; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 423 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * This routine will shutdown a serial port; interrupts are disabled, and |
| 429 | * DTR is dropped if the hangup on close termio flag is on. |
| 430 | */ |
| 431 | static void shutdown(struct m68k_serial * info) |
| 432 | { |
| 433 | m68328_uart *uart = &uart_addr[info->line]; |
| 434 | unsigned long flags; |
| 435 | |
| 436 | uart->ustcnt = 0; /* All off! */ |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 437 | if (!(info->tport.flags & ASYNC_INITIALIZED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return; |
| 439 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 440 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | if (info->xmit_buf) { |
| 443 | free_page((unsigned long) info->xmit_buf); |
| 444 | info->xmit_buf = 0; |
| 445 | } |
| 446 | |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 447 | if (info->tty) |
| 448 | set_bit(TTY_IO_ERROR, &info->tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 450 | info->tport.flags &= ~ASYNC_INITIALIZED; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 451 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | struct { |
| 455 | int divisor, prescale; |
| 456 | } |
| 457 | #ifndef CONFIG_M68VZ328 |
| 458 | hw_baud_table[18] = { |
| 459 | {0,0}, /* 0 */ |
| 460 | {0,0}, /* 50 */ |
| 461 | {0,0}, /* 75 */ |
| 462 | {0,0}, /* 110 */ |
| 463 | {0,0}, /* 134 */ |
| 464 | {0,0}, /* 150 */ |
| 465 | {0,0}, /* 200 */ |
| 466 | {7,0x26}, /* 300 */ |
| 467 | {6,0x26}, /* 600 */ |
| 468 | {5,0x26}, /* 1200 */ |
| 469 | {0,0}, /* 1800 */ |
| 470 | {4,0x26}, /* 2400 */ |
| 471 | {3,0x26}, /* 4800 */ |
| 472 | {2,0x26}, /* 9600 */ |
| 473 | {1,0x26}, /* 19200 */ |
| 474 | {0,0x26}, /* 38400 */ |
| 475 | {1,0x38}, /* 57600 */ |
| 476 | {0,0x38}, /* 115200 */ |
| 477 | }; |
| 478 | #else |
| 479 | hw_baud_table[18] = { |
| 480 | {0,0}, /* 0 */ |
| 481 | {0,0}, /* 50 */ |
| 482 | {0,0}, /* 75 */ |
| 483 | {0,0}, /* 110 */ |
| 484 | {0,0}, /* 134 */ |
| 485 | {0,0}, /* 150 */ |
| 486 | {0,0}, /* 200 */ |
| 487 | {0,0}, /* 300 */ |
| 488 | {7,0x26}, /* 600 */ |
| 489 | {6,0x26}, /* 1200 */ |
| 490 | {0,0}, /* 1800 */ |
| 491 | {5,0x26}, /* 2400 */ |
| 492 | {4,0x26}, /* 4800 */ |
| 493 | {3,0x26}, /* 9600 */ |
| 494 | {2,0x26}, /* 19200 */ |
| 495 | {1,0x26}, /* 38400 */ |
| 496 | {0,0x26}, /* 57600 */ |
| 497 | {1,0x38}, /* 115200 */ |
| 498 | }; |
| 499 | #endif |
| 500 | /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */ |
| 501 | |
| 502 | /* |
| 503 | * This routine is called to set the UART divisor registers to match |
| 504 | * the specified baud rate for a serial port. |
| 505 | */ |
| 506 | static void change_speed(struct m68k_serial *info) |
| 507 | { |
| 508 | m68328_uart *uart = &uart_addr[info->line]; |
| 509 | unsigned short port; |
| 510 | unsigned short ustcnt; |
| 511 | unsigned cflag; |
| 512 | int i; |
| 513 | |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 514 | if (!info->tty || !info->tty->termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return; |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 516 | cflag = info->tty->termios->c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | if (!(port = info->port)) |
| 518 | return; |
| 519 | |
| 520 | ustcnt = uart->ustcnt; |
| 521 | uart->ustcnt = ustcnt & ~USTCNT_TXEN; |
| 522 | |
| 523 | i = cflag & CBAUD; |
| 524 | if (i & CBAUDEX) { |
| 525 | i = (i & ~CBAUDEX) + B38400; |
| 526 | } |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | |
| 529 | PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); |
| 530 | |
| 531 | ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7); |
| 532 | |
| 533 | if ((cflag & CSIZE) == CS8) |
| 534 | ustcnt |= USTCNT_8_7; |
| 535 | |
| 536 | if (cflag & CSTOPB) |
| 537 | ustcnt |= USTCNT_STOP; |
| 538 | |
| 539 | if (cflag & PARENB) |
| 540 | ustcnt |= USTCNT_PARITYEN; |
| 541 | if (cflag & PARODD) |
| 542 | ustcnt |= USTCNT_ODD_EVEN; |
| 543 | |
| 544 | #ifdef CONFIG_SERIAL_68328_RTS_CTS |
| 545 | if (cflag & CRTSCTS) { |
| 546 | uart->utx.w &= ~ UTX_NOCTS; |
| 547 | } else { |
| 548 | uart->utx.w |= UTX_NOCTS; |
| 549 | } |
| 550 | #endif |
| 551 | |
| 552 | ustcnt |= USTCNT_TXEN; |
| 553 | |
| 554 | uart->ustcnt = ustcnt; |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Fair output driver allows a process to speak. |
| 560 | */ |
| 561 | static void rs_fair_output(void) |
| 562 | { |
| 563 | int left; /* Output no more than that */ |
| 564 | unsigned long flags; |
| 565 | struct m68k_serial *info = &m68k_soft[0]; |
| 566 | char c; |
| 567 | |
| 568 | if (info == 0) return; |
| 569 | if (info->xmit_buf == 0) return; |
| 570 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 571 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | left = info->xmit_cnt; |
| 573 | while (left != 0) { |
| 574 | c = info->xmit_buf[info->xmit_tail]; |
| 575 | info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1); |
| 576 | info->xmit_cnt--; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 577 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | rs_put_char(c); |
| 580 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 581 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | left = min(info->xmit_cnt, left-1); |
| 583 | } |
| 584 | |
| 585 | /* Last character is being transmitted now (hopefully). */ |
| 586 | udelay(5); |
| 587 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 588 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * m68k_console_print is registered for printk. |
| 594 | */ |
| 595 | void console_print_68328(const char *p) |
| 596 | { |
| 597 | char c; |
| 598 | |
| 599 | while((c=*(p++)) != 0) { |
| 600 | if(c == '\n') |
| 601 | rs_put_char('\r'); |
| 602 | rs_put_char(c); |
| 603 | } |
| 604 | |
| 605 | /* Comment this if you want to have a strict interrupt-driven output */ |
| 606 | rs_fair_output(); |
| 607 | |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | static void rs_set_ldisc(struct tty_struct *tty) |
| 612 | { |
| 613 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 614 | |
| 615 | if (serial_paranoia_check(info, tty->name, "rs_set_ldisc")) |
| 616 | return; |
| 617 | |
| 618 | info->is_cons = (tty->termios->c_line == N_TTY); |
| 619 | |
| 620 | printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off"); |
| 621 | } |
| 622 | |
| 623 | static void rs_flush_chars(struct tty_struct *tty) |
| 624 | { |
| 625 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 626 | m68328_uart *uart = &uart_addr[info->line]; |
| 627 | unsigned long flags; |
| 628 | |
| 629 | if (serial_paranoia_check(info, tty->name, "rs_flush_chars")) |
| 630 | return; |
| 631 | #ifndef USE_INTS |
| 632 | for(;;) { |
| 633 | #endif |
| 634 | |
| 635 | /* Enable transmitter */ |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 636 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped || |
| 639 | !info->xmit_buf) { |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 640 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return; |
| 642 | } |
| 643 | |
| 644 | #ifdef USE_INTS |
| 645 | uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK; |
| 646 | #else |
| 647 | uart->ustcnt |= USTCNT_TXEN; |
| 648 | #endif |
| 649 | |
| 650 | #ifdef USE_INTS |
| 651 | if (uart->utx.w & UTX_TX_AVAIL) { |
| 652 | #else |
| 653 | if (1) { |
| 654 | #endif |
| 655 | /* Send char */ |
| 656 | uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; |
| 657 | info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); |
| 658 | info->xmit_cnt--; |
| 659 | } |
| 660 | |
| 661 | #ifndef USE_INTS |
| 662 | while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5); |
| 663 | } |
| 664 | #endif |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 665 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | extern void console_printn(const char * b, int count); |
| 669 | |
| 670 | static int rs_write(struct tty_struct * tty, |
| 671 | const unsigned char *buf, int count) |
| 672 | { |
| 673 | int c, total = 0; |
| 674 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 675 | m68328_uart *uart = &uart_addr[info->line]; |
| 676 | unsigned long flags; |
| 677 | |
| 678 | if (serial_paranoia_check(info, tty->name, "rs_write")) |
| 679 | return 0; |
| 680 | |
| 681 | if (!tty || !info->xmit_buf) |
| 682 | return 0; |
| 683 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 684 | local_save_flags(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | while (1) { |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 686 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, |
| 688 | SERIAL_XMIT_SIZE - info->xmit_head)); |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 689 | local_irq_restore(flags); |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | if (c <= 0) |
| 692 | break; |
| 693 | |
| 694 | memcpy(info->xmit_buf + info->xmit_head, buf, c); |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 695 | |
| 696 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1); |
| 698 | info->xmit_cnt += c; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 699 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | buf += c; |
| 701 | count -= c; |
| 702 | total += c; |
| 703 | } |
| 704 | |
| 705 | if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) { |
| 706 | /* Enable transmitter */ |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 707 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | #ifndef USE_INTS |
| 709 | while(info->xmit_cnt) { |
| 710 | #endif |
| 711 | |
| 712 | uart->ustcnt |= USTCNT_TXEN; |
| 713 | #ifdef USE_INTS |
| 714 | uart->ustcnt |= USTCNT_TX_INTR_MASK; |
| 715 | #else |
| 716 | while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5); |
| 717 | #endif |
| 718 | if (uart->utx.w & UTX_TX_AVAIL) { |
| 719 | uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; |
| 720 | info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); |
| 721 | info->xmit_cnt--; |
| 722 | } |
| 723 | |
| 724 | #ifndef USE_INTS |
| 725 | } |
| 726 | #endif |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 727 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | return total; |
| 731 | } |
| 732 | |
| 733 | static int rs_write_room(struct tty_struct *tty) |
| 734 | { |
| 735 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 736 | int ret; |
| 737 | |
| 738 | if (serial_paranoia_check(info, tty->name, "rs_write_room")) |
| 739 | return 0; |
| 740 | ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1; |
| 741 | if (ret < 0) |
| 742 | ret = 0; |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static int rs_chars_in_buffer(struct tty_struct *tty) |
| 747 | { |
| 748 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 749 | |
| 750 | if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer")) |
| 751 | return 0; |
| 752 | return info->xmit_cnt; |
| 753 | } |
| 754 | |
| 755 | static void rs_flush_buffer(struct tty_struct *tty) |
| 756 | { |
| 757 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 758 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | if (serial_paranoia_check(info, tty->name, "rs_flush_buffer")) |
| 761 | return; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 762 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 764 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | tty_wakeup(tty); |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * ------------------------------------------------------------ |
| 770 | * rs_throttle() |
| 771 | * |
| 772 | * This routine is called by the upper-layer tty layer to signal that |
| 773 | * incoming characters should be throttled. |
| 774 | * ------------------------------------------------------------ |
| 775 | */ |
| 776 | static void rs_throttle(struct tty_struct * tty) |
| 777 | { |
| 778 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 779 | |
| 780 | if (serial_paranoia_check(info, tty->name, "rs_throttle")) |
| 781 | return; |
| 782 | |
| 783 | if (I_IXOFF(tty)) |
| 784 | info->x_char = STOP_CHAR(tty); |
| 785 | |
| 786 | /* Turn off RTS line (do this atomic) */ |
| 787 | } |
| 788 | |
| 789 | static void rs_unthrottle(struct tty_struct * tty) |
| 790 | { |
| 791 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 792 | |
| 793 | if (serial_paranoia_check(info, tty->name, "rs_unthrottle")) |
| 794 | return; |
| 795 | |
| 796 | if (I_IXOFF(tty)) { |
| 797 | if (info->x_char) |
| 798 | info->x_char = 0; |
| 799 | else |
| 800 | info->x_char = START_CHAR(tty); |
| 801 | } |
| 802 | |
| 803 | /* Assert RTS line (do this atomic) */ |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * ------------------------------------------------------------ |
| 808 | * rs_ioctl() and friends |
| 809 | * ------------------------------------------------------------ |
| 810 | */ |
| 811 | |
| 812 | static int get_serial_info(struct m68k_serial * info, |
| 813 | struct serial_struct * retinfo) |
| 814 | { |
| 815 | struct serial_struct tmp; |
| 816 | |
| 817 | if (!retinfo) |
| 818 | return -EFAULT; |
| 819 | memset(&tmp, 0, sizeof(tmp)); |
| 820 | tmp.type = info->type; |
| 821 | tmp.line = info->line; |
| 822 | tmp.port = info->port; |
| 823 | tmp.irq = info->irq; |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 824 | tmp.flags = info->tport.flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | tmp.baud_base = info->baud_base; |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 826 | tmp.close_delay = info->tport.close_delay; |
| 827 | tmp.closing_wait = info->tport.closing_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | tmp.custom_divisor = info->custom_divisor; |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 829 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 830 | return -EFAULT; |
| 831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static int set_serial_info(struct m68k_serial * info, |
| 836 | struct serial_struct * new_info) |
| 837 | { |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 838 | struct tty_port *port = &info->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | struct serial_struct new_serial; |
| 840 | struct m68k_serial old_info; |
| 841 | int retval = 0; |
| 842 | |
| 843 | if (!new_info) |
| 844 | return -EFAULT; |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 845 | if (copy_from_user(&new_serial, new_info, sizeof(new_serial))) |
| 846 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | old_info = *info; |
| 848 | |
| 849 | if (!capable(CAP_SYS_ADMIN)) { |
| 850 | if ((new_serial.baud_base != info->baud_base) || |
| 851 | (new_serial.type != info->type) || |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 852 | (new_serial.close_delay != port->close_delay) || |
Jiri Slaby | cea4b2c | 2012-04-02 13:54:35 +0200 | [diff] [blame] | 853 | ((new_serial.flags & ~ASYNC_USR_MASK) != |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 854 | (port->flags & ~ASYNC_USR_MASK))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return -EPERM; |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 856 | port->flags = ((port->flags & ~ASYNC_USR_MASK) | |
Jiri Slaby | cea4b2c | 2012-04-02 13:54:35 +0200 | [diff] [blame] | 857 | (new_serial.flags & ASYNC_USR_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | info->custom_divisor = new_serial.custom_divisor; |
| 859 | goto check_and_exit; |
| 860 | } |
| 861 | |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 862 | if (port->count > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return -EBUSY; |
| 864 | |
| 865 | /* |
| 866 | * OK, past this point, all the error checking has been done. |
| 867 | * At this point, we start making changes..... |
| 868 | */ |
| 869 | |
| 870 | info->baud_base = new_serial.baud_base; |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 871 | port->flags = ((port->flags & ~ASYNC_FLAGS) | |
Jiri Slaby | cea4b2c | 2012-04-02 13:54:35 +0200 | [diff] [blame] | 872 | (new_serial.flags & ASYNC_FLAGS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | info->type = new_serial.type; |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 874 | port->close_delay = new_serial.close_delay; |
| 875 | port->closing_wait = new_serial.closing_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
| 877 | check_and_exit: |
| 878 | retval = startup(info); |
| 879 | return retval; |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | * get_lsr_info - get line status register info |
| 884 | * |
| 885 | * Purpose: Let user call ioctl() to get info when the UART physically |
| 886 | * is emptied. On bus types like RS485, the transmitter must |
| 887 | * release the bus after transmitting. This must be done when |
| 888 | * the transmit shift register is empty, not be done when the |
| 889 | * transmit holding register is empty. This functionality |
| 890 | * allows an RS485 driver to be written in user space. |
| 891 | */ |
| 892 | static int get_lsr_info(struct m68k_serial * info, unsigned int *value) |
| 893 | { |
| 894 | #ifdef CONFIG_SERIAL_68328_RTS_CTS |
| 895 | m68328_uart *uart = &uart_addr[info->line]; |
| 896 | #endif |
| 897 | unsigned char status; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 898 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 900 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | #ifdef CONFIG_SERIAL_68328_RTS_CTS |
| 902 | status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0; |
| 903 | #else |
| 904 | status = 0; |
| 905 | #endif |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 906 | local_irq_restore(flags); |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 907 | return put_user(status, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* |
| 911 | * This routine sends a break character out the serial port. |
| 912 | */ |
Nishanth Aravamudan | 6a72c7b | 2005-06-25 14:59:33 -0700 | [diff] [blame] | 913 | static void send_break(struct m68k_serial * info, unsigned int duration) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
| 915 | m68328_uart *uart = &uart_addr[info->line]; |
| 916 | unsigned long flags; |
| 917 | if (!info->port) |
| 918 | return; |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 919 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | #ifdef USE_INTS |
| 921 | uart->utx.w |= UTX_SEND_BREAK; |
Nishanth Aravamudan | 6a72c7b | 2005-06-25 14:59:33 -0700 | [diff] [blame] | 922 | msleep_interruptible(duration); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | uart->utx.w &= ~UTX_SEND_BREAK; |
| 924 | #endif |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 925 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Alan Cox | 6caa76b | 2011-02-14 16:27:22 +0000 | [diff] [blame] | 928 | static int rs_ioctl(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | unsigned int cmd, unsigned long arg) |
| 930 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; |
| 932 | int retval; |
| 933 | |
| 934 | if (serial_paranoia_check(info, tty->name, "rs_ioctl")) |
| 935 | return -ENODEV; |
| 936 | |
| 937 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
| 938 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) && |
| 939 | (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) { |
| 940 | if (tty->flags & (1 << TTY_IO_ERROR)) |
| 941 | return -EIO; |
| 942 | } |
| 943 | |
| 944 | switch (cmd) { |
| 945 | case TCSBRK: /* SVID version: non-zero arg --> no break */ |
| 946 | retval = tty_check_change(tty); |
| 947 | if (retval) |
| 948 | return retval; |
| 949 | tty_wait_until_sent(tty, 0); |
| 950 | if (!arg) |
Nishanth Aravamudan | 6a72c7b | 2005-06-25 14:59:33 -0700 | [diff] [blame] | 951 | send_break(info, 250); /* 1/4 second */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return 0; |
| 953 | case TCSBRKP: /* support for POSIX tcsendbreak() */ |
| 954 | retval = tty_check_change(tty); |
| 955 | if (retval) |
| 956 | return retval; |
| 957 | tty_wait_until_sent(tty, 0); |
Nishanth Aravamudan | 6a72c7b | 2005-06-25 14:59:33 -0700 | [diff] [blame] | 958 | send_break(info, arg ? arg*(100) : 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | case TIOCGSERIAL: |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 961 | return get_serial_info(info, |
| 962 | (struct serial_struct *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | case TIOCSSERIAL: |
| 964 | return set_serial_info(info, |
| 965 | (struct serial_struct *) arg); |
| 966 | case TIOCSERGETLSR: /* Get line status register */ |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 967 | return get_lsr_info(info, (unsigned int *) arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | case TIOCSERGSTRUCT: |
Kulikov Vasiliy | 5d56356 | 2010-08-01 10:29:06 +0400 | [diff] [blame] | 969 | if (copy_to_user((struct m68k_serial *) arg, |
| 970 | info, sizeof(struct m68k_serial))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | default: |
| 974 | return -ENOIOCTLCMD; |
| 975 | } |
| 976 | return 0; |
| 977 | } |
| 978 | |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 979 | static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; |
| 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | change_speed(info); |
| 984 | |
| 985 | if ((old_termios->c_cflag & CRTSCTS) && |
| 986 | !(tty->termios->c_cflag & CRTSCTS)) { |
| 987 | tty->hw_stopped = 0; |
| 988 | rs_start(tty); |
| 989 | } |
| 990 | |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * ------------------------------------------------------------ |
| 995 | * rs_close() |
| 996 | * |
| 997 | * This routine is called when the serial port gets closed. First, we |
| 998 | * wait for the last remaining data to be sent. Then, we unlink its |
| 999 | * S structure from the interrupt chain if necessary, and we free |
| 1000 | * that IRQ if nothing is left in the chain. |
| 1001 | * ------------------------------------------------------------ |
| 1002 | */ |
| 1003 | static void rs_close(struct tty_struct *tty, struct file * filp) |
| 1004 | { |
| 1005 | struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1006 | struct tty_port *port = &info->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | m68328_uart *uart = &uart_addr[info->line]; |
| 1008 | unsigned long flags; |
| 1009 | |
| 1010 | if (!info || serial_paranoia_check(info, tty->name, "rs_close")) |
| 1011 | return; |
| 1012 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1013 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | if (tty_hung_up_p(filp)) { |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1016 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1020 | if ((tty->count == 1) && (port->count != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | /* |
| 1022 | * Uh, oh. tty->count is 1, which means that the tty |
| 1023 | * structure will be freed. Info->count should always |
| 1024 | * be one in these conditions. If it's greater than |
| 1025 | * one, we've got real problems, since it means the |
| 1026 | * serial port won't be shutdown. |
| 1027 | */ |
| 1028 | printk("rs_close: bad serial port count; tty->count is 1, " |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1029 | "port->count is %d\n", port->count); |
| 1030 | port->count = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1032 | if (--port->count < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | printk("rs_close: bad serial port count for ttyS%d: %d\n", |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1034 | info->line, port->count); |
| 1035 | port->count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1037 | if (port->count) { |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1038 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | return; |
| 1040 | } |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1041 | port->flags |= ASYNC_CLOSING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | /* |
| 1043 | * Now we wait for the transmit buffer to clear; and we notify |
| 1044 | * the line discipline to only process XON/XOFF characters. |
| 1045 | */ |
| 1046 | tty->closing = 1; |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 1047 | if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) |
| 1048 | tty_wait_until_sent(tty, port->closing_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | /* |
| 1050 | * At this point we stop accepting input. To do this, we |
| 1051 | * disable the receive line status interrupts, and tell the |
| 1052 | * interrupt driver to stop checking the data ready bit in the |
| 1053 | * line status register. |
| 1054 | */ |
| 1055 | |
| 1056 | uart->ustcnt &= ~USTCNT_RXEN; |
| 1057 | uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK); |
| 1058 | |
| 1059 | shutdown(info); |
Alan Cox | 978e595 | 2008-04-30 00:53:59 -0700 | [diff] [blame] | 1060 | rs_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | tty_ldisc_flush(tty); |
| 1063 | tty->closing = 0; |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 1064 | info->tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | #warning "This is not and has never been valid so fix it" |
| 1066 | #if 0 |
| 1067 | if (tty->ldisc.num != ldiscs[N_TTY].num) { |
| 1068 | if (tty->ldisc.close) |
| 1069 | (tty->ldisc.close)(tty); |
| 1070 | tty->ldisc = ldiscs[N_TTY]; |
| 1071 | tty->termios->c_line = N_TTY; |
| 1072 | if (tty->ldisc.open) |
| 1073 | (tty->ldisc.open)(tty); |
| 1074 | } |
| 1075 | #endif |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1076 | if (port->blocked_open) { |
Jiri Slaby | 4a85b1f | 2012-04-02 13:54:42 +0200 | [diff] [blame] | 1077 | if (port->close_delay) |
| 1078 | msleep_interruptible(jiffies_to_msecs(port->close_delay)); |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1079 | wake_up_interruptible(&port->open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | } |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1081 | port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1082 | wake_up_interruptible(&port->close_wait); |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1083 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * rs_hangup() --- called by tty_hangup() when a hangup is signaled. |
| 1088 | */ |
| 1089 | void rs_hangup(struct tty_struct *tty) |
| 1090 | { |
| 1091 | struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; |
| 1092 | |
| 1093 | if (serial_paranoia_check(info, tty->name, "rs_hangup")) |
| 1094 | return; |
| 1095 | |
| 1096 | rs_flush_buffer(tty); |
| 1097 | shutdown(info); |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1098 | info->tport.count = 0; |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1099 | info->tport.flags &= ~ASYNC_NORMAL_ACTIVE; |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 1100 | info->tty = NULL; |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1101 | wake_up_interruptible(&info->tport.open_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * ------------------------------------------------------------ |
| 1106 | * rs_open() and friends |
| 1107 | * ------------------------------------------------------------ |
| 1108 | */ |
| 1109 | static int block_til_ready(struct tty_struct *tty, struct file * filp, |
| 1110 | struct m68k_serial *info) |
| 1111 | { |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1112 | struct tty_port *port = &info->tport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | DECLARE_WAITQUEUE(wait, current); |
| 1114 | int retval; |
| 1115 | int do_clocal = 0; |
| 1116 | |
| 1117 | /* |
| 1118 | * If the device is in the middle of being closed, then block |
| 1119 | * until it's done, and then try again. |
| 1120 | */ |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1121 | if (port->flags & ASYNC_CLOSING) { |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1122 | interruptible_sleep_on(&port->close_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1124 | if (port->flags & ASYNC_HUP_NOTIFY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | return -EAGAIN; |
| 1126 | else |
| 1127 | return -ERESTARTSYS; |
| 1128 | #else |
| 1129 | return -EAGAIN; |
| 1130 | #endif |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * If non-blocking mode is set, or the port is not enabled, |
| 1135 | * then make the check up front and then exit. |
| 1136 | */ |
| 1137 | if ((filp->f_flags & O_NONBLOCK) || |
| 1138 | (tty->flags & (1 << TTY_IO_ERROR))) { |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1139 | port->flags |= ASYNC_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | if (tty->termios->c_cflag & CLOCAL) |
| 1144 | do_clocal = 1; |
| 1145 | |
| 1146 | /* |
| 1147 | * Block waiting for the carrier detect and the line to become |
| 1148 | * free (i.e., not in use by the callout). While we are in |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1149 | * this loop, port->count is dropped by one, so that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | * rs_close() knows when to free things. We restore it upon |
| 1151 | * exit, either normal or abnormal. |
| 1152 | */ |
| 1153 | retval = 0; |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1154 | add_wait_queue(&port->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1156 | port->count--; |
| 1157 | port->blocked_open++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | current->state = TASK_INTERRUPTIBLE; |
| 1160 | if (tty_hung_up_p(filp) || |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1161 | !(port->flags & ASYNC_INITIALIZED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | #ifdef SERIAL_DO_RESTART |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1163 | if (port->flags & ASYNC_HUP_NOTIFY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | retval = -EAGAIN; |
| 1165 | else |
| 1166 | retval = -ERESTARTSYS; |
| 1167 | #else |
| 1168 | retval = -EAGAIN; |
| 1169 | #endif |
| 1170 | break; |
| 1171 | } |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1172 | if (!(port->flags & ASYNC_CLOSING) && do_clocal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | break; |
| 1174 | if (signal_pending(current)) { |
| 1175 | retval = -ERESTARTSYS; |
| 1176 | break; |
| 1177 | } |
Arnd Bergmann | e142a31 | 2010-06-01 22:53:10 +0200 | [diff] [blame] | 1178 | tty_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | schedule(); |
Arnd Bergmann | e142a31 | 2010-06-01 22:53:10 +0200 | [diff] [blame] | 1180 | tty_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
| 1182 | current->state = TASK_RUNNING; |
Jiri Slaby | c26f011 | 2012-04-02 13:54:41 +0200 | [diff] [blame] | 1183 | remove_wait_queue(&port->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | if (!tty_hung_up_p(filp)) |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1185 | port->count++; |
| 1186 | port->blocked_open--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (retval) |
| 1189 | return retval; |
Jiri Slaby | a85dd82 | 2012-04-02 13:54:43 +0200 | [diff] [blame^] | 1190 | port->flags |= ASYNC_NORMAL_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * This routine is called whenever a serial port is opened. It |
| 1196 | * enables interrupts for a serial port, linking in its S structure into |
| 1197 | * the IRQ chain. It also performs the serial-specific |
| 1198 | * initialization for the tty structure. |
| 1199 | */ |
| 1200 | int rs_open(struct tty_struct *tty, struct file * filp) |
| 1201 | { |
| 1202 | struct m68k_serial *info; |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 1203 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Jiri Slaby | 410235f | 2012-03-05 14:52:01 +0100 | [diff] [blame] | 1205 | info = &m68k_soft[tty->index]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
| 1207 | if (serial_paranoia_check(info, tty->name, "rs_open")) |
| 1208 | return -ENODEV; |
| 1209 | |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1210 | info->tport.count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | tty->driver_data = info; |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 1212 | info->tty = tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | /* |
| 1215 | * Start up serial port |
| 1216 | */ |
| 1217 | retval = startup(info); |
| 1218 | if (retval) |
| 1219 | return retval; |
| 1220 | |
| 1221 | return block_til_ready(tty, filp, info); |
| 1222 | } |
| 1223 | |
| 1224 | /* Finally, routines used to initialize the serial driver. */ |
| 1225 | |
| 1226 | static void show_serial_version(void) |
| 1227 | { |
| 1228 | printk("MC68328 serial driver version 1.00\n"); |
| 1229 | } |
| 1230 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1231 | static const struct tty_operations rs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | .open = rs_open, |
| 1233 | .close = rs_close, |
| 1234 | .write = rs_write, |
| 1235 | .flush_chars = rs_flush_chars, |
| 1236 | .write_room = rs_write_room, |
| 1237 | .chars_in_buffer = rs_chars_in_buffer, |
| 1238 | .flush_buffer = rs_flush_buffer, |
| 1239 | .ioctl = rs_ioctl, |
| 1240 | .throttle = rs_throttle, |
| 1241 | .unthrottle = rs_unthrottle, |
| 1242 | .set_termios = rs_set_termios, |
| 1243 | .stop = rs_stop, |
| 1244 | .start = rs_start, |
| 1245 | .hangup = rs_hangup, |
| 1246 | .set_ldisc = rs_set_ldisc, |
| 1247 | }; |
| 1248 | |
| 1249 | /* rs_init inits the driver */ |
| 1250 | static int __init |
| 1251 | rs68328_init(void) |
| 1252 | { |
Jiri Slaby | 107afb7 | 2012-04-02 13:54:38 +0200 | [diff] [blame] | 1253 | unsigned long flags; |
| 1254 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | struct m68k_serial *info; |
| 1256 | |
| 1257 | serial_driver = alloc_tty_driver(NR_PORTS); |
| 1258 | if (!serial_driver) |
| 1259 | return -ENOMEM; |
| 1260 | |
| 1261 | show_serial_version(); |
| 1262 | |
| 1263 | /* Initialize the tty_driver structure */ |
| 1264 | /* SPARC: Not all of this is exactly right for us. */ |
| 1265 | |
| 1266 | serial_driver->name = "ttyS"; |
| 1267 | serial_driver->major = TTY_MAJOR; |
| 1268 | serial_driver->minor_start = 64; |
| 1269 | serial_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 1270 | serial_driver->subtype = SERIAL_TYPE_NORMAL; |
| 1271 | serial_driver->init_termios = tty_std_termios; |
| 1272 | serial_driver->init_termios.c_cflag = |
| 1273 | m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL; |
| 1274 | serial_driver->flags = TTY_DRIVER_REAL_RAW; |
| 1275 | tty_set_operations(serial_driver, &rs_ops); |
| 1276 | |
| 1277 | if (tty_register_driver(serial_driver)) { |
| 1278 | put_tty_driver(serial_driver); |
| 1279 | printk(KERN_ERR "Couldn't register serial driver\n"); |
| 1280 | return -ENOMEM; |
| 1281 | } |
| 1282 | |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1283 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
| 1285 | for(i=0;i<NR_PORTS;i++) { |
| 1286 | |
| 1287 | info = &m68k_soft[i]; |
Jiri Slaby | 8626434 | 2012-04-02 13:54:40 +0200 | [diff] [blame] | 1288 | tty_port_init(&info->tport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | info->magic = SERIAL_MAGIC; |
| 1290 | info->port = (int) &uart_addr[i]; |
Greg Ungerer | bc0c36d | 2011-02-08 21:32:36 +1000 | [diff] [blame] | 1291 | info->tty = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | info->irq = uart_irqs[i]; |
| 1293 | info->custom_divisor = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | info->x_char = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | info->line = i; |
| 1296 | info->is_cons = 1; /* Means shortcuts work */ |
| 1297 | |
| 1298 | printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line, |
| 1299 | info->port, info->irq); |
| 1300 | printk(" is a builtin MC68328 UART\n"); |
| 1301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | #ifdef CONFIG_M68VZ328 |
| 1303 | if (i > 0 ) |
| 1304 | PJSEL &= 0xCF; /* PSW enable second port output */ |
| 1305 | #endif |
| 1306 | |
| 1307 | if (request_irq(uart_irqs[i], |
| 1308 | rs_interrupt, |
Yong Zhang | 9cfb5c0 | 2011-09-22 16:59:15 +0800 | [diff] [blame] | 1309 | 0, |
Alan Cox | 25db8ad | 2008-08-19 20:49:40 -0700 | [diff] [blame] | 1310 | "M68328_UART", info)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | panic("Unable to attach 68328 serial interrupt\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
Greg Ungerer | 727dda8 | 2006-06-28 15:54:22 +1000 | [diff] [blame] | 1313 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | module_init(rs68328_init); |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | static void m68328_set_baud(void) |
| 1322 | { |
| 1323 | unsigned short ustcnt; |
| 1324 | int i; |
| 1325 | |
| 1326 | ustcnt = USTCNT; |
| 1327 | USTCNT = ustcnt & ~USTCNT_TXEN; |
| 1328 | |
| 1329 | again: |
Thiago Farina | 8b505ca | 2009-12-09 12:31:30 -0800 | [diff] [blame] | 1330 | for (i = 0; i < ARRAY_SIZE(baud_table); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | if (baud_table[i] == m68328_console_baud) |
| 1332 | break; |
Thiago Farina | 8b505ca | 2009-12-09 12:31:30 -0800 | [diff] [blame] | 1333 | if (i >= ARRAY_SIZE(baud_table)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | m68328_console_baud = 9600; |
| 1335 | goto again; |
| 1336 | } |
| 1337 | |
| 1338 | UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | |
| 1339 | PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); |
| 1340 | ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7); |
| 1341 | ustcnt |= USTCNT_8_7; |
| 1342 | ustcnt |= USTCNT_TXEN; |
| 1343 | USTCNT = ustcnt; |
| 1344 | m68328_console_initted = 1; |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | int m68328_console_setup(struct console *cp, char *arg) |
| 1350 | { |
| 1351 | int i, n = CONSOLE_BAUD_RATE; |
| 1352 | |
| 1353 | if (!cp) |
| 1354 | return(-1); |
| 1355 | |
| 1356 | if (arg) |
| 1357 | n = simple_strtoul(arg,NULL,0); |
| 1358 | |
Thiago Farina | 8b505ca | 2009-12-09 12:31:30 -0800 | [diff] [blame] | 1359 | for (i = 0; i < ARRAY_SIZE(baud_table); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (baud_table[i] == n) |
| 1361 | break; |
Greg Ungerer | e9a137c | 2010-05-24 14:32:55 -0700 | [diff] [blame] | 1362 | if (i < ARRAY_SIZE(baud_table)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | m68328_console_baud = n; |
| 1364 | m68328_console_cbaud = 0; |
| 1365 | if (i > 15) { |
| 1366 | m68328_console_cbaud |= CBAUDEX; |
| 1367 | i -= 15; |
| 1368 | } |
| 1369 | m68328_console_cbaud |= i; |
| 1370 | } |
| 1371 | |
| 1372 | m68328_set_baud(); /* make sure baud rate changes */ |
| 1373 | return(0); |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | static struct tty_driver *m68328_console_device(struct console *c, int *index) |
| 1378 | { |
| 1379 | *index = c->index; |
| 1380 | return serial_driver; |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | void m68328_console_write (struct console *co, const char *str, |
| 1385 | unsigned int count) |
| 1386 | { |
| 1387 | if (!m68328_console_initted) |
| 1388 | m68328_set_baud(); |
| 1389 | while (count--) { |
| 1390 | if (*str == '\n') |
| 1391 | rs_put_char('\r'); |
| 1392 | rs_put_char( *str++ ); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | static struct console m68328_driver = { |
| 1398 | .name = "ttyS", |
| 1399 | .write = m68328_console_write, |
| 1400 | .device = m68328_console_device, |
| 1401 | .setup = m68328_console_setup, |
| 1402 | .flags = CON_PRINTBUFFER, |
| 1403 | .index = -1, |
| 1404 | }; |
| 1405 | |
| 1406 | |
| 1407 | static int __init m68328_console_init(void) |
| 1408 | { |
| 1409 | register_console(&m68328_driver); |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | console_initcall(m68328_console_init); |