xref: /netbsd/sys/arch/hpcsh/dev/hd64461/hd64461uart.c (revision c4a72b64)
1 /*	$NetBSD: hd64461uart.c,v 1.14 2002/10/02 15:45:20 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002 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 "opt_kgdb.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/reboot.h>
41 #include <sys/malloc.h>
42 #include <sys/device.h>
43 #include <sys/kgdb.h>
44 
45 #include <sys/termios.h>
46 #include <dev/cons.h>
47 #include <sys/conf.h>
48 
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 #include <machine/console.h>
52 
53 #include <dev/ic/comvar.h>
54 #include <dev/ic/comreg.h>
55 
56 #include <machine/debug.h>
57 
58 #include <hpcsh/dev/hd64461/hd64461var.h>
59 #include <hpcsh/dev/hd64461/hd64461reg.h>
60 #include <hpcsh/dev/hd64461/hd64461intcreg.h>
61 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
62 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
63 
64 STATIC struct hd64461uart_chip {
65 	struct hpcsh_bus_space __tag_body;
66 	bus_space_tag_t io_tag;
67 	int console;
68 } hd64461uart_chip;
69 
70 struct hd64461uart_softc {
71 	struct com_softc sc_com;
72 
73 	struct hd64461uart_chip *sc_chip;
74 	enum hd64461_module_id sc_module_id;
75 };
76 
77 /* boot console */
78 void hd64461uartcnprobe(struct consdev *);
79 void hd64461uartcninit(struct consdev *);
80 
81 STATIC int hd64461uart_match(struct device *, struct cfdata *, void *);
82 STATIC void hd64461uart_attach(struct device *, struct device *, void *);
83 
84 CFATTACH_DECL(hd64461uart, sizeof(struct hd64461uart_softc),
85     hd64461uart_match, hd64461uart_attach, NULL, NULL);
86 
87 STATIC void hd64461uart_init(void);
88 STATIC u_int8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
89 STATIC void hd64461uart_write_1(void *, bus_space_handle_t, bus_size_t,
90     u_int8_t);
91 
92 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
93 #ifndef COMCN_SPEED
94 #define COMCN_SPEED	19200
95 #endif
96 
97 void
98 hd64461uartcnprobe(struct consdev *cp)
99 {
100 	extern const struct cdevsw com_cdevsw;
101 	int maj;
102 
103 	/* locate the major number */
104 	maj = cdevsw_lookup_major(&com_cdevsw);
105 
106 	/* Initialize required fields. */
107 	cp->cn_dev = makedev(maj, 0);
108 	cp->cn_pri = CN_NORMAL;
109 }
110 
111 void
112 hd64461uartcninit(struct consdev *cp)
113 {
114 
115 	hd64461uart_init();
116 
117 	comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
118 	    CONMODE);
119 
120 	hd64461uart_chip.console = 1;
121 }
122 
123 #ifdef KGDB
124 int
125 hd64461uart_kgdb_init()
126 {
127 
128 	if (strcmp(kgdb_devname, "hd64461uart") != 0)
129 		return (1);
130 
131 	if (hd64461uart_chip.console)
132 		return (1);	/* can't share with console */
133 
134 	hd64461uart_init();
135 
136 	if (com_kgdb_attach(hd64461uart_chip.io_tag, 0x0, kgdb_rate,
137 	    COM_FREQ, CONMODE) != 0) {
138 		printf("%s: KGDB console open failed.\n", __FUNCTION__);
139 		return (1);
140 	}
141 
142 	return (0);
143 }
144 #endif /* KGDB */
145 
146 int
147 hd64461uart_match(struct device *parent, struct cfdata *cf, void *aux)
148 {
149 	struct hd64461_attach_args *ha = aux;
150 
151 	return (ha->ha_module_id == HD64461_MODULE_UART);
152 }
153 
154 void
155 hd64461uart_attach(struct device *parent, struct device *self, void *aux)
156 {
157 	struct hd64461_attach_args *ha = aux;
158 	struct hd64461uart_softc *sc = (struct hd64461uart_softc *)self;
159 	struct com_softc *csc = &sc->sc_com;
160 	u_int16_t r16;
161 
162 	sc->sc_chip = &hd64461uart_chip;
163 
164 	sc->sc_module_id = ha->ha_module_id;
165 
166 	if (!sc->sc_chip->console)
167 		hd64461uart_init();
168 
169 	csc->sc_iot = sc->sc_chip->io_tag;
170 	bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
171 	csc->sc_iobase = 0;
172 	csc->sc_frequency = COM_FREQ;
173 
174 	/* switch port to UART */
175 
176 	/* supply clock */
177 	r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
178 	r16 &= ~HD64461_SYSSTBCR_SURTSD;
179 	hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
180 
181 	/* sanity check */
182 	if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
183 		printf(": device problem. don't attach.\n");
184 
185 		/* stop clock */
186 		r16 |= HD64461_SYSSTBCR_SURTSD;
187 		hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
188 		return;
189 	}
190 
191 	com_attach_subr(csc);
192 
193 	hd6446x_intr_establish(HD64461_INTC_UART, IST_LEVEL, IPL_TTY,
194 	    comintr, self);
195 }
196 
197 void
198 hd64461uart_init()
199 {
200 
201 	if (hd64461uart_chip.io_tag)
202 		return;
203 
204 	hd64461uart_chip.io_tag = bus_space_create(
205 		&hd64461uart_chip.__tag_body, "HD64461 UART I/O",
206 		HD64461_UART_REGBASE, 0); /* no extent */
207 
208 	/* override bus_space_read_1, bus_space_write_1 */
209 	hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
210 	hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
211 }
212 
213 u_int8_t
214 hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
215 {
216 
217 	return *(__volatile__ u_int8_t *)(h + (ofs << 1));
218 }
219 
220 void
221 hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
222     u_int8_t val)
223 {
224 
225 	*(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
226 }
227