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