com_hpcio.c revision 1.6 1 /* $NetBSD: com_hpcio.c,v 1.6 2003/06/14 17:01:12 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2002 TAKEMRUA Shin. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 #include "opt_kgdb.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/reboot.h>
38 #include <sys/termios.h>
39
40 #include <machine/intr.h>
41 #include <machine/bus.h>
42 #include <machine/platid.h>
43 #include <machine/platid_mask.h>
44
45 #include <hpcmips/dev/com_hpciovar.h>
46
47 #include <dev/hpc/hpciovar.h>
48 #include <dev/ic/comvar.h>
49 #include <dev/ic/comreg.h>
50
51 #include "locators.h"
52
53 #define COM_HPCIODEBUG
54 #ifdef COM_HPCIODEBUG
55 int com_hpcio_debug = 0;
56 #define DPRINTF(arg...) do { if (com_hpcio_debug) printf(arg); } while (0)
57 #else
58 #define DPRINTF(arg...) do {} while (0)
59 #endif
60
61 #define COM_HPCIO_BYTE_ALIGNMENT 0
62 #define COM_HPCIO_HALFWORD_ALIGNMENT 1
63
64 struct com_hpcio_softc {
65 struct com_softc hsc_com;
66 struct bus_space_tag hsc_iot;
67 struct hpcio_chip *hsc_hc;
68 };
69 static struct bus_space_tag com_hpcio_cniotx;
70 static bus_space_tag_t com_hpcio_cniot = &com_hpcio_cniotx;
71 static int com_hpcio_cniobase;
72
73 static int com_hpcio_probe(struct device *, struct cfdata *, void *);
74 static void com_hpcio_attach(struct device *, struct device *, void *);
75 static int com_hpcio_common_probe(bus_space_tag_t, int, int *);
76 void com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base);
77 bus_space_protos(bs_notimpl);
78 bus_space_protos(bs_through);
79 bus_space_protos(com_hpcio);
80
81 CFATTACH_DECL(com_hpcio, sizeof(struct com_hpcio_softc),
82 com_hpcio_probe, com_hpcio_attach, NULL, NULL);
83
84 struct bus_space_ops com_hpcio_bs_ops = {
85 /* mapping/unmapping */
86 bs_through_bs_map,
87 bs_through_bs_unmap,
88 bs_through_bs_subregion,
89
90 /* allocation/deallocation */
91 bs_through_bs_alloc,
92 bs_through_bs_free,
93
94 /* get kernel virtual address */
95 bs_through_bs_vaddr, /* there is no linear mapping */
96
97 /* Mmap bus space for user */
98 bs_through_bs_mmap,
99
100 /* barrier */
101 bs_through_bs_barrier,
102
103 /* probe */
104 bs_through_bs_peek,
105 bs_through_bs_poke,
106
107 /* read (single) */
108 com_hpcio_bs_r_1,
109 bs_notimpl_bs_r_2,
110 bs_notimpl_bs_r_4,
111 bs_notimpl_bs_r_8,
112
113 /* read multiple */
114 bs_notimpl_bs_rm_1,
115 bs_notimpl_bs_rm_2,
116 bs_notimpl_bs_rm_4,
117 bs_notimpl_bs_rm_8,
118
119 /* read region */
120 bs_notimpl_bs_rr_1,
121 bs_notimpl_bs_rr_2,
122 bs_notimpl_bs_rr_4,
123 bs_notimpl_bs_rr_8,
124
125 /* write (single) */
126 com_hpcio_bs_w_1,
127 bs_notimpl_bs_w_2,
128 bs_notimpl_bs_w_4,
129 bs_notimpl_bs_w_8,
130
131 /* write multiple */
132 com_hpcio_bs_wm_1,
133 bs_notimpl_bs_wm_2,
134 bs_notimpl_bs_wm_4,
135 bs_notimpl_bs_wm_8,
136
137 /* write region */
138 bs_notimpl_bs_wr_1,
139 bs_notimpl_bs_wr_2,
140 bs_notimpl_bs_wr_4,
141 bs_notimpl_bs_wr_8,
142
143 #ifdef BUS_SPACE_HAS_REAL_STREAM_METHODS
144 /* read stream (single) */
145 bs_notimpl_bs_rs_1,
146 bs_notimpl_bs_rs_2,
147 bs_notimpl_bs_rs_4,
148 bs_notimpl_bs_rs_8,
149
150 /* read multiple stream */
151 bs_notimpl_bs_rms_1,
152 bs_notimpl_bs_rms_2,
153 bs_notimpl_bs_rms_4,
154 bs_notimpl_bs_rms_8,
155
156 /* read region stream */
157 bs_notimpl_bs_rrs_1,
158 bs_notimpl_bs_rrs_2,
159 bs_notimpl_bs_rrs_4,
160 bs_notimpl_bs_rrs_8,
161
162 /* write stream (single) */
163 bs_notimpl_bs_ws_1,
164 bs_notimpl_bs_ws_2,
165 bs_notimpl_bs_ws_4,
166 bs_notimpl_bs_ws_8,
167
168 /* write multiple stream */
169 bs_notimpl_bs_wms_1,
170 bs_notimpl_bs_wms_2,
171 bs_notimpl_bs_wms_4,
172 bs_notimpl_bs_wms_8,
173
174 /* write region stream */
175 bs_notimpl_bs_wrs_1,
176 bs_notimpl_bs_wrs_2,
177 bs_notimpl_bs_wrs_4,
178 bs_notimpl_bs_wrs_8,
179 #endif /* BUS_SPACE_HAS_REAL_STREAM_METHODS */
180
181 /* set multi */
182 bs_notimpl_bs_sm_1,
183 bs_notimpl_bs_sm_2,
184 bs_notimpl_bs_sm_4,
185 bs_notimpl_bs_sm_8,
186
187 /* set region */
188 bs_notimpl_bs_sr_1,
189 bs_notimpl_bs_sr_2,
190 bs_notimpl_bs_sr_4,
191 bs_notimpl_bs_sr_8,
192
193 /* copy */
194 bs_notimpl_bs_c_1,
195 bs_notimpl_bs_c_2,
196 bs_notimpl_bs_c_4,
197 bs_notimpl_bs_c_8,
198 };
199
200 int
201 com_hpcio_cndb_attach(bus_space_tag_t iot, int iobase, int rate,
202 int frequency, tcflag_t cflag, int kgdb)
203 {
204 int alignment;
205
206 DPRINTF("com_hpcio_cndb_attach()\n");
207 if (!com_hpcio_common_probe(iot, iobase, &alignment)) {
208 DPRINTF("com_hpcio_cndb_attach(): probe failed\n");
209 return (ENOTTY);
210 }
211 if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
212 DPRINTF("com_hpcio_cndb_attach(): half word aligned\n");
213 com_hpcio_iot_init(&com_hpcio_cniotx, iot);
214 com_hpcio_cniot = &com_hpcio_cniotx;
215 } else {
216 com_hpcio_cniot = iot;
217 }
218 com_hpcio_cniobase = iobase;
219 DPRINTF("com_hpcio_cndb_attach(): probe succeeded\n");
220 #ifdef KGDB
221 if (kgdb)
222 return (com_kgdb_attach(com_hpcio_cniot, iobase, rate,
223 frequency, COM_TYPE_NORMAL, cflag));
224 else
225 #endif
226 return (comcnattach(com_hpcio_cniot, iobase, rate,
227 frequency, COM_TYPE_NORMAL, cflag));
228 }
229
230 static int
231 com_hpcio_common_probe(bus_space_tag_t iot, int iobase, int *alignment)
232 {
233 bus_space_handle_t ioh;
234 static struct bus_space_tag tmpiot;
235 int rv;
236
237 /*
238 * try byte aligned register
239 */
240 *alignment = COM_HPCIO_BYTE_ALIGNMENT;
241 if (bus_space_map(iot, iobase, 1, 0, &ioh))
242 return 0;
243 rv = comprobe1(iot, ioh);
244 bus_space_unmap(iot, ioh, 1);
245
246 if (rv != 0)
247 return (rv);
248
249 /*
250 * try half word aligned register
251 */
252 *alignment = COM_HPCIO_HALFWORD_ALIGNMENT;
253 com_hpcio_iot_init(&tmpiot, iot);
254 if (bus_space_map(&tmpiot, iobase, 1, 0, &ioh))
255 return 0;
256 rv = comprobe1(&tmpiot, ioh);
257 bus_space_unmap(&tmpiot, ioh, 1);
258
259 return (rv);
260 }
261
262 static int
263 com_hpcio_probe(struct device *parent, struct cfdata *cf, void *aux)
264 {
265 struct hpcio_attach_args *haa = aux;
266 bus_space_tag_t iot = haa->haa_iot;
267 int addr, alignment;
268
269 if (cf->cf_loc[HPCIOIFCF_PLATFORM] != HPCIOIFCF_PLATFORM_DEFAULT) {
270 platid_mask_t mask;
271
272 mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]);
273 if (!platid_match(&platid, &mask))
274 return (0); /* platform id didn't match */
275 }
276
277 if ((addr = cf->cf_loc[HPCIOIFCF_ADDR]) == HPCIOIFCF_ADDR_DEFAULT)
278 return (0); /* address wasn't specified */
279
280 return com_hpcio_common_probe(iot, addr, &alignment);
281 }
282
283
284 static void
285 com_hpcio_attach(struct device *parent, struct device *self, void *aux)
286 {
287 struct com_hpcio_softc *hsc = (void *)self;
288 struct com_softc *sc = &hsc->hsc_com;
289 struct hpcio_attach_args *haa = aux;
290 bus_space_tag_t iot;
291 bus_space_handle_t ioh;
292 int addr, port, mode, alignment, *loc;
293
294 loc = sc->sc_dev.dv_cfdata->cf_loc;
295 addr = loc[HPCIOIFCF_ADDR];
296 printf(" addr %x", addr);
297 if ((com_hpcio_cniot == haa->haa_iot ||
298 com_hpcio_cniot->bs_base == haa->haa_iot) &&
299 com_hpcio_cniobase == addr &&
300 com_is_console(com_hpcio_cniot, addr, 0)) {
301 iot = com_hpcio_cniot;
302 if (com_hpcio_cniot->bs_base == haa->haa_iot)
303 printf(", half word aligned");
304 } else {
305 com_hpcio_common_probe(haa->haa_iot, addr, &alignment);
306 if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
307 printf(", half word aligned");
308 iot = &hsc->hsc_iot;
309 com_hpcio_iot_init(iot, haa->haa_iot);
310 } else {
311 iot = haa->haa_iot;
312 }
313 }
314 if (bus_space_map(iot, addr, 1, 0, &ioh)) {
315 printf(": can't map bus space\n");
316 return;
317 }
318 sc->sc_iobase = addr;
319 sc->sc_iot = iot;
320 sc->sc_ioh = ioh;
321
322 sc->enable = NULL;
323 sc->disable = NULL;
324
325 sc->sc_frequency = COM_FREQ;
326 com_attach_subr(sc);
327
328 hsc->hsc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
329 port = loc[HPCIOIFCF_PORT];
330 mode = HPCIO_INTR_LEVEL | HPCIO_INTR_HIGH;
331 hpcio_intr_establish(hsc->hsc_hc, port, mode, comintr, sc);
332 }
333
334 /*
335 * bus stuff (registershalf word aligned)
336 */
337 void
338 com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base)
339 {
340
341 iot->bs_base = base;
342 iot->bs_ops = com_hpcio_bs_ops; /* structure assignment */
343 iot->bs_ops.bs_r_1 = com_hpcio_bs_r_1;
344 iot->bs_ops.bs_w_1 = com_hpcio_bs_w_1;
345 iot->bs_ops.bs_wm_1 = com_hpcio_bs_wm_1;
346 }
347
348 u_int8_t
349 com_hpcio_bs_r_1(bus_space_tag_t t, bus_space_handle_t bsh,
350 bus_size_t offset)
351 {
352 return bus_space_read_1(t->bs_base, bsh, offset * 2);
353 }
354
355 void
356 com_hpcio_bs_w_1(bus_space_tag_t t, bus_space_handle_t bsh,
357 bus_size_t offset, u_int8_t value)
358 {
359 bus_space_write_1(t->bs_base, bsh, offset * 2, value);
360 }
361
362 void
363 com_hpcio_bs_wm_1(bus_space_tag_t t, bus_space_handle_t bsh,
364 bus_size_t offset, const u_int8_t *addr, bus_size_t count)
365 {
366 bus_space_write_multi_1(t->bs_base, bsh, offset * 2, addr, count);
367 }
368