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