[BACK]Return to ns16550.c CVS log [TXT][DIR] Up to [local] / sys / arch / jornada / stand / boot

Annotation of sys/arch/jornada/stand/boot/ns16550.c, Revision 1.1

1.1     ! nbrk        1: /*     $OpenBSD: ns16550.c,v 1.2 2006/07/29 15:01:49 kettenis Exp $    */
        !             2: /*     $NetBSD: ns16550.c,v 1.3 2005/12/24 20:07:03 perry Exp $        */
        !             3:
        !             4: /*
        !             5:  * Copyright (c) 2002 Wasabi Systems, Inc.
        !             6:  * All rights reserved.
        !             7:  *
        !             8:  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  * 3. All advertising materials mentioning features or use of this software
        !            19:  *    must display the following acknowledgement:
        !            20:  *     This product includes software developed for the NetBSD Project by
        !            21:  *     Wasabi Systems, Inc.
        !            22:  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
        !            23:  *    or promote products derived from this software without specific prior
        !            24:  *    written permission.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
        !            28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
        !            29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
        !            30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        !            31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        !            32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        !            33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        !            34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        !            35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        !            36:  * POSSIBILITY OF SUCH DAMAGE.
        !            37:  */
        !            38:
        !            39: /*
        !            40:  * This file provides console I/O routines for boards that use
        !            41:  * 16550-compatible UARTs.
        !            42:  */
        !            43:
        !            44: #include <sys/types.h>
        !            45:
        !            46: #include <dev/cons.h>
        !            47: #include <dev/ic/comreg.h>
        !            48:
        !            49: #include "libsa.h"
        !            50:
        !            51: #define        INB(x)          *((volatile uint8_t *) (CONADDR + (x)))
        !            52: #define        OUTB(x, v)      *((volatile uint8_t *) (CONADDR + (x))) = (v)
        !            53:
        !            54: #define        ISSET(t,f)      ((t) & (f))
        !            55:
        !            56: #ifndef NS16550_FREQ
        !            57: #define        NS16550_FREQ    COM_FREQ
        !            58: #endif
        !            59:
        !            60: static int
        !            61: comspeed(int speed)
        !            62: {
        !            63: #define        divrnd(n, q)    (((n)*2/(q)+1)/2)       /* divide and round off */
        !            64:
        !            65:        int x, err;
        !            66:
        !            67:        if (speed <= 0)
        !            68:                return (-1);
        !            69:        x = divrnd((NS16550_FREQ / 16), speed);
        !            70:        if (x <= 0)
        !            71:                return (-1);
        !            72:        err = divrnd((((quad_t)NS16550_FREQ) / 16) * 1000, speed * x) - 1000;
        !            73:        if (err < 0)
        !            74:                err = -err;
        !            75:        if (err > COM_TOLERANCE)
        !            76:                return (-1);
        !            77:        return (x);
        !            78: #undef divrnd
        !            79: }
        !            80:
        !            81: void
        !            82: com_probe(struct consdev *cn)
        !            83: {
        !            84:        cn->cn_pri = CN_NORMAL;
        !            85:        cn->cn_dev = makedev(12, 0);
        !            86: }
        !            87:
        !            88: void
        !            89: com_init(struct consdev *cn)
        !            90: {
        !            91:        int rate;
        !            92:
        !            93:        OUTB(com_cfcr, LCR_DLAB);
        !            94:        rate = comspeed(CONSPEED);
        !            95:        OUTB(com_dlbl, rate);
        !            96:        OUTB(com_dlbh, rate >> 8);
        !            97:        OUTB(com_cfcr, LCR_8BITS);
        !            98:        OUTB(com_mcr, MCR_DTR | MCR_RTS);
        !            99:        OUTB(com_fifo,
        !           100:            FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
        !           101:        OUTB(com_ier, 0);
        !           102: }
        !           103:
        !           104: int
        !           105: com_getc(dev_t dev)
        !           106: {
        !           107:        uint8_t stat;
        !           108:
        !           109:        if (dev & 0x80)
        !           110:                return ISSET(stat = INB(com_lsr), LSR_RXRDY);
        !           111:
        !           112:        while (!ISSET(stat = INB(com_lsr), LSR_RXRDY))
        !           113:                /* spin */ ;
        !           114:        return (INB(com_data));
        !           115: }
        !           116:
        !           117: void
        !           118: com_putc(dev_t dev, int c)
        !           119: {
        !           120:        uint8_t stat;
        !           121:        int timo;
        !           122:
        !           123:        /* Wait for any pending transmission to finish. */
        !           124:        timo = 50000;
        !           125:        while (!ISSET(stat = INB(com_lsr), LSR_TXRDY) && --timo)
        !           126:                /* spin */ ;
        !           127:
        !           128:        OUTB(com_data, c);
        !           129:
        !           130:        /* Wait for this transmission to complete. */
        !           131:        timo = 1500000;
        !           132:        while (!ISSET(stat = INB(com_lsr), LSR_TXRDY) && --timo)
        !           133:                /* spin */ ;
        !           134:
        !           135:        /* Clear any interrupts generated by this transmission. */
        !           136:        (void) INB(com_iir);
        !           137: }

CVSweb