1 /* $NetBSD: ns16550.c,v 1.5 2022/09/05 14:14:42 tsutsui 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 #ifdef NS16550_AX4
50 #define INB(x) *((volatile uint8_t *) (CONADDR + ((x) * 4)))
51 #define OUTB(x, v) *((volatile uint8_t *) (CONADDR + ((x) * 4))) = (v)
52 #else
53 #define INB(x) *((volatile uint8_t *) (CONADDR + (x)))
54 #define OUTB(x, v) *((volatile uint8_t *) (CONADDR + (x))) = (v)
55 #endif
56
57 #ifndef NS16550_FREQ
58 #define NS16550_FREQ COM_FREQ
59 #endif
60
61 static int
comspeed(int speed)62 comspeed(int speed)
63 {
64 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
65
66 int x, err;
67
68 if (speed <= 0)
69 return (-1);
70 x = divrnd((NS16550_FREQ / 16), speed);
71 if (x <= 0)
72 return (-1);
73 err = divrnd((((quad_t)NS16550_FREQ) / 16) * 1000, speed * x) - 1000;
74 if (err < 0)
75 err = -err;
76 if (err > COM_TOLERANCE)
77 return (-1);
78 return (x);
79 #undef divrnd
80 }
81
82 void
cons_init(void)83 cons_init(void)
84 {
85 int rate;
86
87 OUTB(com_cfcr, LCR_DLAB);
88 rate = comspeed(CONSPEED);
89 OUTB(com_dlbl, rate);
90 OUTB(com_dlbh, rate >> 8);
91 OUTB(com_cfcr, LCR_8BITS);
92 OUTB(com_mcr, MCR_DTR | MCR_RTS);
93 OUTB(com_fifo,
94 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
95 OUTB(com_ier, 0);
96 }
97
98 int
getchar(void)99 getchar(void)
100 {
101 uint8_t stat;
102
103 while (!ISSET(stat = INB(com_lsr), LSR_RXRDY))
104 /* spin */ ;
105 return (INB(com_data));
106 }
107
108 static void
iputchar(int c)109 iputchar(int c)
110 {
111 uint8_t stat;
112 int timo;
113
114 /* Wait for any pending transmission to finish. */
115 timo = 50000;
116 while (!ISSET(stat = INB(com_lsr), LSR_TXRDY) && --timo)
117 /* spin */ ;
118
119 OUTB(com_data, c);
120
121 /* Wait for this transmission to complete. */
122 timo = 1500000;
123 while (!ISSET(stat = INB(com_lsr), LSR_TXRDY) && --timo)
124 /* spin */ ;
125
126 /* Clear any interrupts generated by this transmission. */
127 (void) INB(com_iir);
128 }
129
130 void
putchar(int c)131 putchar(int c)
132 {
133
134 if (c == '\n')
135 iputchar('\r');
136 iputchar(c);
137 }
138