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