hd64461uart.c revision 1.7 1 /* $NetBSD: hd64461uart.c,v 1.7 2002/02/11 17:21:48 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/hd64461/hd64461var.h>
58 #include <hpcsh/dev/hd64461/hd64461reg.h>
59 #include <hpcsh/dev/hd64461/hd64461intcvar.h>
60 #include <hpcsh/dev/hd64461/hd64461uartvar.h>
61 #include <hpcsh/dev/hd64461/hd64461uartreg.h>
62
63 STATIC struct hd64461uart_chip {
64 struct hpcsh_bus_space __tag_body;
65 bus_space_tag_t io_tag;
66 int console;
67 } hd64461uart_chip;
68
69 struct hd64461uart_softc {
70 struct com_softc sc_com;
71
72 struct hd64461uart_chip *sc_chip;
73 enum hd64461_module_id sc_module_id;
74 };
75
76 /* boot console */
77 cdev_decl(com);
78 void comcnprobe(struct consdev *);
79 void comcninit(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 struct cfattach hd64461uart_ca = {
85 sizeof(struct hd64461uart_softc), hd64461uart_match,
86 hd64461uart_attach
87 };
88
89 STATIC void hd64461uart_init(void);
90 STATIC u_int8_t hd64461uart_read_1(void *, bus_space_handle_t, bus_size_t);
91 STATIC void hd64461uart_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 comcnprobe(struct consdev *cp)
101 {
102 int maj;
103
104 /* locate the major number */
105 for (maj = 0; maj < nchrdev; maj++)
106 if (cdevsw[maj].d_open == comopen)
107 break;
108
109 /* Initialize required fields. */
110 cp->cn_dev = makedev(maj, 0);
111 cp->cn_pri = CN_NORMAL;
112 }
113
114 void
115 comcninit(struct consdev *cp)
116 {
117
118 hd64461uart_init();
119
120 #ifdef KGDB
121 if (strcmp(kgdb_devname, "hd64461uart") == 0) {
122 if (com_kgdb_attach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED,
123 COM_FREQ, CONMODE) == 0) {
124 return;
125 }
126 }
127 #endif /* KGDB */
128 comcnattach(hd64461uart_chip.io_tag, 0x0, COMCN_SPEED, COM_FREQ,
129 CONMODE);
130
131 hd64461uart_chip.console = 1;
132 }
133
134 int
135 hd64461uart_match(struct device *parent, struct cfdata *cf, void *aux)
136 {
137 struct hd64461_attach_args *ha = aux;
138
139 return (ha->ha_module_id == HD64461_MODULE_UART);
140 }
141
142 void
143 hd64461uart_attach(struct device *parent, struct device *self, void *aux)
144 {
145 struct hd64461_attach_args *ha = aux;
146 struct hd64461uart_softc *sc = (struct hd64461uart_softc *)self;
147 struct com_softc *csc = &sc->sc_com;
148 u_int16_t r16;
149
150 sc->sc_chip = &hd64461uart_chip;
151
152 sc->sc_module_id = ha->ha_module_id;
153
154 hd64461uart_init();
155
156 csc->sc_iot = sc->sc_chip->io_tag;
157 bus_space_map(csc->sc_iot, 0, 8, 0, &csc->sc_ioh);
158 csc->sc_iobase = 0;
159 csc->sc_frequency = COM_FREQ;
160
161 /* switch port to UART */
162
163 /* supply clock */
164 r16 = hd64461_reg_read_2(HD64461_SYSSTBCR_REG16);
165 r16 &= ~HD64461_SYSSTBCR_SURTSD;
166 hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
167
168 /* sanity check */
169 if (!comprobe1(csc->sc_iot, csc->sc_ioh)) {
170 printf(": device problem. don't attach.\n");
171
172 /* stop clock */
173 r16 |= HD64461_SYSSTBCR_SURTSD;
174 hd64461_reg_write_2(HD64461_SYSSTBCR_REG16, r16);
175 return;
176 }
177
178 com_attach_subr(csc);
179
180 hd64461_intr_establish(HD64461_IRQ_UART, IST_LEVEL, IPL_TTY,
181 comintr, self);
182 }
183
184 void
185 hd64461uart_init()
186 {
187
188 if (hd64461uart_chip.io_tag)
189 return;
190
191 hd64461uart_chip.io_tag = bus_space_create(
192 &hd64461uart_chip.__tag_body, "HD64461 UART I/O",
193 HD64461_UART_REGBASE, 0); /* no extent */
194
195 /* override bus_space_read_1, bus_space_write_1 */
196 hd64461uart_chip.io_tag->hbs_r_1 = hd64461uart_read_1;
197 hd64461uart_chip.io_tag->hbs_w_1 = hd64461uart_write_1;
198 }
199
200 u_int8_t
201 hd64461uart_read_1(void *t, bus_space_handle_t h, bus_size_t ofs)
202 {
203
204 return *(__volatile__ u_int8_t *)(h + (ofs << 1));
205 }
206
207 void
208 hd64461uart_write_1(void *t, bus_space_handle_t h, bus_size_t ofs,
209 u_int8_t val)
210 {
211
212 *(__volatile__ u_int8_t *)(h + (ofs << 1)) = val;
213 }
214