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