hd64465uart.c revision 1.1 1 /* $NetBSD: hd64465uart.c,v 1.1 2002/02/11 17:27:16 uch 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
44 #include <sys/termios.h>
45 #include <dev/cons.h>
46 #include <sys/conf.h>
47
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50 #include <machine/console.h>
51
52 #include <dev/ic/comvar.h>
53 #include <dev/ic/comreg.h>
54
55 #include <machine/debug.h>
56
57 #include <hpcsh/dev/hd64465/hd64465var.h>
58 #include <hpcsh/dev/hd64465/hd64465uartvar.h>
59 #include <hpcsh/dev/hd64465/hd64465uartreg.h>
60
61 STATIC struct hd64465uart_chip {
62 struct hpcsh_bus_space __tag_body;
63 bus_space_tag_t io_tag;
64 int console;
65 } hd64465uart_chip;
66
67 struct hd64465uart_softc {
68 struct com_softc sc_com;
69
70 struct hd64465uart_chip *sc_chip;
71 enum hd64465_module_id sc_module_id;
72 };
73
74 /* boot console */
75 cdev_decl(com);
76 void comcnprobe(struct consdev *);
77 void comcninit(struct consdev *);
78
79 STATIC int hd64465uart_match(struct device *, struct cfdata *, void *);
80 STATIC void hd64465uart_attach(struct device *, struct device *, void *);
81
82 struct cfattach hd64465uart_ca = {
83 sizeof(struct hd64465uart_softc), hd64465uart_match,
84 hd64465uart_attach
85 };
86
87 STATIC void hd64465uart_init(void);
88 STATIC u_int8_t hd64465uart_read_1(void *, bus_space_handle_t, bus_size_t);
89 STATIC void hd64465uart_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 comcnprobe(struct consdev *cp)
99 {
100 int maj;
101
102 /* locate the major number */
103 for (maj = 0; maj < nchrdev; maj++)
104 if (cdevsw[maj].d_open == comopen)
105 break;
106
107 /* Initialize required fields. */
108 cp->cn_dev = makedev(maj, 0);
109 cp->cn_pri = CN_NORMAL;
110 }
111
112 void
113 comcninit(struct consdev *cp)
114 {
115
116 hd64465uart_init();
117
118 comcnattach(hd64465uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
119 CONMODE);
120
121 hd64465uart_chip.console = 1;
122 }
123
124 int
125 hd64465uart_match(struct device *parent, struct cfdata *cf, void *aux)
126 {
127 struct hd64465_attach_args *ha = aux;
128
129 return (ha->ha_module_id == HD64465_MODULE_UART);
130 }
131
132 void
133 hd64465uart_attach(struct device *parent, struct device *self, void *aux)
134 {
135 struct hd64465_attach_args *ha = aux;
136 struct hd64465uart_softc *sc = (struct hd64465uart_softc *)self;
137 struct com_softc *csc = &sc->sc_com;
138
139 sc->sc_chip = &hd64465uart_chip;
140
141 sc->sc_module_id = ha->ha_module_id;
142
143 hd64465uart_init();
144
145 csc->sc_iot = sc->sc_chip->io_tag;
146 bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
147 csc->sc_iobase = 0;
148 csc->sc_frequency = COM_FREQ;
149
150 /* supply clock XXX notyet */
151
152 /* sanity check */
153 if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
154 printf(": device problem. don't attach.\n");
155
156 /* stop clock XXX notyet */
157 return;
158 }
159
160 com_attach_subr(csc);
161
162 /* register interrupt handler */
163 hd64465_intr_establish(HD64465_IRQ_UART, IST_EDGE, IPL_TTY,
164 comintr, self);
165 }
166
167 void
168 hd64465uart_init()
169 {
170
171 if (hd64465uart_chip.io_tag)
172 return;
173
174 hd64465uart_chip.io_tag = bus_space_create(
175 &hd64465uart_chip.__tag_body, "HD64465 UART I/O",
176 0xb0008000, 0); /* no extent */
177
178 /* override bus_space_read_1, bus_space_write_1 */
179 hd64465uart_chip.io_tag->hbs_r_1 = hd64465uart_read_1;
180 hd64465uart_chip.io_tag->hbs_w_1 = hd64465uart_write_1;
181 }
182
183 u_int8_t
184 hd64465uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
185 {
186
187 return *(__volatile__ u_int8_t *)(h + (ofs << 1));
188 }
189
190 void
191 hd64465uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
192 u_int8_t val)
193 {
194
195 *(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
196 }
197