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