xref: /netbsd-src/sys/arch/hpcsh/dev/hd64461/hd64461uart.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: hd64461uart.c,v 1.3 2001/07/13 16:21:39 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/reboot.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 
42 #include <sys/termios.h>
43 #include <dev/cons.h>
44 #include <sys/conf.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 
49 #include <dev/ic/comvar.h>
50 #include <dev/ic/comreg.h>
51 
52 #ifdef DEBUG
53 #include <hpcsh/hpcsh/debug.h>
54 #endif
55 
56 #include <hpcsh/dev/hd64461/hd64461var.h>
57 #include <hpcsh/dev/hd64461/hd64461reg.h>
58 #include <hpcsh/dev/hd64461/hd64461intcvar.h>
59 
60 static struct hd64461uart_chip {
61 	struct hpcsh_bus_space __tag_body;
62 	bus_space_tag_t io_tag;
63 	int console;
64 } hd64461uart_chip;
65 
66 struct hd64461uart_softc {
67 	struct com_softc sc_com;
68 
69 	struct hd64461uart_chip *sc_chip;
70 	enum hd64461_module_id sc_module_id;
71 };
72 
73 /* boot console */
74 cdev_decl(com);
75 void comcnprobe(struct consdev *);
76 void comcninit(struct consdev *);
77 
78 static int hd64461uart_match(struct device *, struct cfdata *, void *);
79 static void hd64461uart_attach(struct device *, struct device *, void *);
80 
81 struct cfattach hd64461uart_ca = {
82 	sizeof(struct hd64461uart_softc), hd64461uart_match,
83 	hd64461uart_attach
84 };
85 
86 static void hd64461uart_init(void);
87 static u_int8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
88 static void hd64461uart_write_1(void *, bus_space_handle_t, bus_size_t,
89     u_int8_t);
90 
91 #ifdef DEBUG
92 static void hd64461uart_info(struct hd64461uart_softc *);
93 #endif
94 
95 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
96 #ifndef COMCN_SPEED
97 #define COMCN_SPEED	19200
98 #endif
99 
100 void
101 comcnprobe(struct consdev *cp)
102 {
103 	int maj;
104 
105 	/* locate the major number */
106 	for (maj = 0; maj < nchrdev; maj++)
107 		if (cdevsw[maj].d_open == comopen)
108 			break;
109 
110 	/* Initialize required fields. */
111 	cp->cn_dev = makedev(maj, 0);
112 	cp->cn_pri = CN_NORMAL;
113 }
114 
115 void
116 comcninit(struct consdev *cp)
117 {
118 
119 	hd64461uart_init();
120 
121 	comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
122 	    CONMODE);
123 
124 	hd64461uart_chip.console = 1;
125 }
126 
127 static int
128 hd64461uart_match(struct device *parent, struct cfdata *cf, void *aux)
129 {
130 	struct hd64461_attach_args *ha = aux;
131 
132 	return (ha->ha_module_id == HD64461_MODULE_UART);
133 }
134 
135 static void
136 hd64461uart_attach(struct device *parent, struct device *self, void *aux)
137 {
138 	struct hd64461_attach_args *ha = aux;
139 	struct hd64461uart_softc *sc = (struct hd64461uart_softc *)self;
140 	struct com_softc *csc = &sc->sc_com;
141 	u_int16_t r16;
142 
143 	sc->sc_chip = &hd64461uart_chip;
144 
145 	sc->sc_module_id = ha->ha_module_id;
146 
147 	hd64461uart_init();
148 
149 	csc->sc_iot = sc->sc_chip->io_tag;
150 	bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
151 	csc->sc_iobase = 0;
152 	csc->sc_frequency = COM_FREQ;
153 
154 	/* switch port to UART */
155 
156 	/* supply clock */
157 	r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
158 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
159 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
160 
161 #ifdef DEBUG
162 	if (bootverbose)
163 		hd64461uart_info(sc);
164 #endif
165 	/* sanity check */
166 	if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
167 		printf(": device problem. don't attach.\n");
168 
169 		/* stop clock */
170 		r16 |= HD64461_SYSSTBCR_SURTSD;
171 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
172 		return;
173 	}
174 
175 	com_attach_subr(csc);
176 
177 	hd64461_intr_establish(HD64461_IRQ_UART, IST_LEVEL, IPL_TTY,
178 	    comintr, self);
179 }
180 
181 static void
182 hd64461uart_init()
183 {
184 
185 	if (hd64461uart_chip.io_tag)
186 		return;
187 
188 	hd64461uart_chip.io_tag = bus_space_create(
189 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
190 		HD64461_UART_REGBASE, 0); /* no extent */
191 
192 	/* override bus_space_read_1, bus_space_write_1 */
193 	hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
194 	hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
195 }
196 
197 static u_int8_t
198 hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
199 {
200 
201 	return *(volatile u_int8_t *)(h + (ofs << 1));
202 }
203 
204 static void
205 hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
206     u_int8_t val)
207 {
208 
209 	*(volatile u_int8_t *)(h + (ofs << 1)) = val;
210 }
211 
212 #ifdef DEBUG
213 static void
214 hd64461uart_info(struct hd64461uart_softc *sc)
215 {
216 	const char name[] = __FUNCTION__;
217 
218 	dbg_banner_start(name, sizeof name);
219 	dbg_banner_end();
220 }
221 #endif /* DEBUG */
222