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