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