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