xref: /netbsd/sys/arch/hpcsh/dev/hd64465/hd64465uart.c (revision c4a72b64)
1 /*	$NetBSD: hd64465uart.c,v 1.8 2002/10/02 15:45:21 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/hd64465/hd64465var.h>
59 #include <hpcsh/dev/hd64465/hd64465intcreg.h>
60 #include <hpcsh/dev/hd64465/hd64465uartvar.h>
61 #include <hpcsh/dev/hd64465/hd64465uartreg.h>
62 
63 STATIC struct hd64465uart_chip {
64 	struct hpcsh_bus_space __tag_body;
65 	bus_space_tag_t io_tag;
66 	int console;
67 } hd64465uart_chip;
68 
69 struct hd64465uart_softc {
70 	struct com_softc sc_com;
71 
72 	struct hd64465uart_chip *sc_chip;
73 	enum hd64465_module_id sc_module_id;
74 };
75 
76 /* boot console */
77 void hd64465uartcnprobe(struct consdev *);
78 void hd64465uartcninit(struct consdev *);
79 
80 STATIC int hd64465uart_match(struct device *, struct cfdata *, void *);
81 STATIC void hd64465uart_attach(struct device *, struct device *, void *);
82 
83 CFATTACH_DECL(hd64465uart, sizeof(struct hd64465uart_softc),
84     hd64465uart_match, hd64465uart_attach, NULL, NULL);
85 
86 STATIC void hd64465uart_init(void);
87 STATIC u_int8_t hd64465uart_read_1(void *, bus_space_handle_t, bus_size_t);
88 STATIC void hd64465uart_write_1(void *, bus_space_handle_t, bus_size_t,
89     u_int8_t);
90 
91 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
92 #ifndef COMCN_SPEED
93 #define COMCN_SPEED	19200
94 #endif
95 
96 void
97 hd64465uartcnprobe(struct consdev *cp)
98 {
99 	extern struct cdevsw com_cdevsw;
100 	int maj;
101 
102 	/* locate the major number */
103 	maj = cdevsw_lookup_major(&com_cdevsw);
104 
105 	/* Initialize required fields. */
106 	cp->cn_dev = makedev(maj, 0);
107 	cp->cn_pri = CN_NORMAL;
108 }
109 
110 void
111 hd64465uartcninit(struct consdev *cp)
112 {
113 
114 	hd64465uart_init();
115 
116 	comcnattach(hd64465uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
117 	    CONMODE);
118 
119 	hd64465uart_chip.console = 1;
120 }
121 
122 #ifdef KGDB
123 int
124 hd64465uart_kgdb_init()
125 {
126 
127 	if (strcmp(kgdb_devname, "hd64465uart") != 0)
128 		return (1);
129 
130 	if (hd64465uart_chip.console)
131 		return (1);	/* can't share with console */
132 
133 	hd64465uart_init();
134 
135 	if (com_kgdb_attach(hd64465uart_chip.io_tag, 0x0, kgdb_rate,
136 	    COM_FREQ, CONMODE) != 0) {
137 		printf("%s: KGDB console open failed.\n", __FUNCTION__);
138 		return (1);
139 	}
140 
141 	return (0);
142 }
143 #endif /* KGDB */
144 
145 int
146 hd64465uart_match(struct device *parent, struct cfdata *cf, void *aux)
147 {
148 	struct hd64465_attach_args *ha = aux;
149 
150 	return (ha->ha_module_id == HD64465_MODULE_UART);
151 }
152 
153 void
154 hd64465uart_attach(struct device *parent, struct device *self, void *aux)
155 {
156 	struct hd64465_attach_args *ha = aux;
157 	struct hd64465uart_softc *sc = (struct hd64465uart_softc *)self;
158 	struct com_softc *csc = &sc->sc_com;
159 
160 	sc->sc_chip = &hd64465uart_chip;
161 
162 	sc->sc_module_id = ha->ha_module_id;
163 
164 	if (!sc->sc_chip->console)
165 		hd64465uart_init();
166 
167 	csc->sc_iot = sc->sc_chip->io_tag;
168 	bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
169 	csc->sc_iobase = 0;
170 	csc->sc_frequency = COM_FREQ;
171 
172 	/* supply clock XXX notyet */
173 
174 	/* sanity check */
175 	if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
176 		printf(": device problem. don't attach.\n");
177 
178 		/* stop clock XXX notyet */
179 		return;
180 	}
181 
182 	com_attach_subr(csc);
183 
184 	/* register interrupt handler */
185 	hd64465_intr_establish(HD64465_UART, IST_LEVEL, IPL_TTY,
186 	    comintr, self);
187 }
188 
189 void
190 hd64465uart_init()
191 {
192 
193 	if (hd64465uart_chip.io_tag)
194 		return;
195 
196 	hd64465uart_chip.io_tag = bus_space_create(
197 		&hd64465uart_chip.__tag_body, "HD64465 UART I/O",
198 		0xb0008000, 0); /* no extent */
199 
200 	/* override bus_space_read_1, bus_space_write_1 */
201 	hd64465uart_chip.io_tag->hbs_r_1 = hd64465uart_read_1;
202 	hd64465uart_chip.io_tag->hbs_w_1 = hd64465uart_write_1;
203 }
204 
205 u_int8_t
206 hd64465uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
207 {
208 
209 	return *(__volatile__ u_int8_t *)(h + (ofs << 1));
210 }
211 
212 void
213 hd64465uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
214     u_int8_t val)
215 {
216 
217 	*(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
218 }
219