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