com_hpcio.c revision 1.8 1 1.8 christos /* $NetBSD: com_hpcio.c,v 1.8 2005/12/11 12:17:33 christos 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.7 lukem
32 1.7 lukem #include <sys/cdefs.h>
33 1.8 christos __KERNEL_RCSID(0, "$NetBSD: com_hpcio.c,v 1.8 2005/12/11 12:17:33 christos Exp $");
34 1.1 takemura
35 1.1 takemura #include "opt_kgdb.h"
36 1.1 takemura
37 1.1 takemura #include <sys/param.h>
38 1.1 takemura #include <sys/systm.h>
39 1.1 takemura #include <sys/device.h>
40 1.1 takemura #include <sys/reboot.h>
41 1.1 takemura #include <sys/termios.h>
42 1.1 takemura
43 1.1 takemura #include <machine/intr.h>
44 1.1 takemura #include <machine/bus.h>
45 1.1 takemura #include <machine/platid.h>
46 1.1 takemura #include <machine/platid_mask.h>
47 1.1 takemura
48 1.1 takemura #include <hpcmips/dev/com_hpciovar.h>
49 1.1 takemura
50 1.1 takemura #include <dev/hpc/hpciovar.h>
51 1.1 takemura #include <dev/ic/comvar.h>
52 1.1 takemura #include <dev/ic/comreg.h>
53 1.1 takemura
54 1.1 takemura #include "locators.h"
55 1.1 takemura
56 1.1 takemura #define COM_HPCIODEBUG
57 1.1 takemura #ifdef COM_HPCIODEBUG
58 1.1 takemura int com_hpcio_debug = 0;
59 1.1 takemura #define DPRINTF(arg...) do { if (com_hpcio_debug) printf(arg); } while (0)
60 1.1 takemura #else
61 1.1 takemura #define DPRINTF(arg...) do {} while (0)
62 1.1 takemura #endif
63 1.1 takemura
64 1.1 takemura #define COM_HPCIO_BYTE_ALIGNMENT 0
65 1.1 takemura #define COM_HPCIO_HALFWORD_ALIGNMENT 1
66 1.1 takemura
67 1.1 takemura struct com_hpcio_softc {
68 1.1 takemura struct com_softc hsc_com;
69 1.1 takemura struct bus_space_tag hsc_iot;
70 1.1 takemura struct hpcio_chip *hsc_hc;
71 1.1 takemura };
72 1.1 takemura static struct bus_space_tag com_hpcio_cniotx;
73 1.1 takemura static bus_space_tag_t com_hpcio_cniot = &com_hpcio_cniotx;
74 1.1 takemura static int com_hpcio_cniobase;
75 1.1 takemura
76 1.1 takemura static int com_hpcio_probe(struct device *, struct cfdata *, void *);
77 1.1 takemura static void com_hpcio_attach(struct device *, struct device *, void *);
78 1.1 takemura static int com_hpcio_common_probe(bus_space_tag_t, int, int *);
79 1.1 takemura void com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base);
80 1.1 takemura bus_space_protos(bs_notimpl);
81 1.1 takemura bus_space_protos(bs_through);
82 1.1 takemura bus_space_protos(com_hpcio);
83 1.1 takemura
84 1.5 thorpej CFATTACH_DECL(com_hpcio, sizeof(struct com_hpcio_softc),
85 1.5 thorpej com_hpcio_probe, com_hpcio_attach, NULL, NULL);
86 1.1 takemura
87 1.1 takemura struct bus_space_ops com_hpcio_bs_ops = {
88 1.1 takemura /* mapping/unmapping */
89 1.1 takemura bs_through_bs_map,
90 1.1 takemura bs_through_bs_unmap,
91 1.1 takemura bs_through_bs_subregion,
92 1.1 takemura
93 1.1 takemura /* allocation/deallocation */
94 1.1 takemura bs_through_bs_alloc,
95 1.1 takemura bs_through_bs_free,
96 1.1 takemura
97 1.1 takemura /* get kernel virtual address */
98 1.1 takemura bs_through_bs_vaddr, /* there is no linear mapping */
99 1.1 takemura
100 1.1 takemura /* Mmap bus space for user */
101 1.1 takemura bs_through_bs_mmap,
102 1.1 takemura
103 1.1 takemura /* barrier */
104 1.1 takemura bs_through_bs_barrier,
105 1.3 takemura
106 1.3 takemura /* probe */
107 1.3 takemura bs_through_bs_peek,
108 1.3 takemura bs_through_bs_poke,
109 1.1 takemura
110 1.1 takemura /* read (single) */
111 1.1 takemura com_hpcio_bs_r_1,
112 1.1 takemura bs_notimpl_bs_r_2,
113 1.1 takemura bs_notimpl_bs_r_4,
114 1.1 takemura bs_notimpl_bs_r_8,
115 1.1 takemura
116 1.1 takemura /* read multiple */
117 1.1 takemura bs_notimpl_bs_rm_1,
118 1.1 takemura bs_notimpl_bs_rm_2,
119 1.1 takemura bs_notimpl_bs_rm_4,
120 1.1 takemura bs_notimpl_bs_rm_8,
121 1.1 takemura
122 1.1 takemura /* read region */
123 1.1 takemura bs_notimpl_bs_rr_1,
124 1.1 takemura bs_notimpl_bs_rr_2,
125 1.1 takemura bs_notimpl_bs_rr_4,
126 1.1 takemura bs_notimpl_bs_rr_8,
127 1.1 takemura
128 1.1 takemura /* write (single) */
129 1.1 takemura com_hpcio_bs_w_1,
130 1.1 takemura bs_notimpl_bs_w_2,
131 1.1 takemura bs_notimpl_bs_w_4,
132 1.1 takemura bs_notimpl_bs_w_8,
133 1.1 takemura
134 1.1 takemura /* write multiple */
135 1.1 takemura com_hpcio_bs_wm_1,
136 1.1 takemura bs_notimpl_bs_wm_2,
137 1.1 takemura bs_notimpl_bs_wm_4,
138 1.1 takemura bs_notimpl_bs_wm_8,
139 1.1 takemura
140 1.1 takemura /* write region */
141 1.1 takemura bs_notimpl_bs_wr_1,
142 1.1 takemura bs_notimpl_bs_wr_2,
143 1.1 takemura bs_notimpl_bs_wr_4,
144 1.1 takemura bs_notimpl_bs_wr_8,
145 1.1 takemura
146 1.1 takemura #ifdef BUS_SPACE_HAS_REAL_STREAM_METHODS
147 1.1 takemura /* read stream (single) */
148 1.1 takemura bs_notimpl_bs_rs_1,
149 1.1 takemura bs_notimpl_bs_rs_2,
150 1.1 takemura bs_notimpl_bs_rs_4,
151 1.1 takemura bs_notimpl_bs_rs_8,
152 1.1 takemura
153 1.1 takemura /* read multiple stream */
154 1.1 takemura bs_notimpl_bs_rms_1,
155 1.1 takemura bs_notimpl_bs_rms_2,
156 1.1 takemura bs_notimpl_bs_rms_4,
157 1.1 takemura bs_notimpl_bs_rms_8,
158 1.1 takemura
159 1.1 takemura /* read region stream */
160 1.1 takemura bs_notimpl_bs_rrs_1,
161 1.1 takemura bs_notimpl_bs_rrs_2,
162 1.1 takemura bs_notimpl_bs_rrs_4,
163 1.1 takemura bs_notimpl_bs_rrs_8,
164 1.1 takemura
165 1.1 takemura /* write stream (single) */
166 1.1 takemura bs_notimpl_bs_ws_1,
167 1.1 takemura bs_notimpl_bs_ws_2,
168 1.1 takemura bs_notimpl_bs_ws_4,
169 1.1 takemura bs_notimpl_bs_ws_8,
170 1.1 takemura
171 1.1 takemura /* write multiple stream */
172 1.1 takemura bs_notimpl_bs_wms_1,
173 1.1 takemura bs_notimpl_bs_wms_2,
174 1.1 takemura bs_notimpl_bs_wms_4,
175 1.1 takemura bs_notimpl_bs_wms_8,
176 1.1 takemura
177 1.1 takemura /* write region stream */
178 1.1 takemura bs_notimpl_bs_wrs_1,
179 1.1 takemura bs_notimpl_bs_wrs_2,
180 1.1 takemura bs_notimpl_bs_wrs_4,
181 1.1 takemura bs_notimpl_bs_wrs_8,
182 1.1 takemura #endif /* BUS_SPACE_HAS_REAL_STREAM_METHODS */
183 1.1 takemura
184 1.1 takemura /* set multi */
185 1.1 takemura bs_notimpl_bs_sm_1,
186 1.1 takemura bs_notimpl_bs_sm_2,
187 1.1 takemura bs_notimpl_bs_sm_4,
188 1.1 takemura bs_notimpl_bs_sm_8,
189 1.1 takemura
190 1.1 takemura /* set region */
191 1.1 takemura bs_notimpl_bs_sr_1,
192 1.1 takemura bs_notimpl_bs_sr_2,
193 1.1 takemura bs_notimpl_bs_sr_4,
194 1.1 takemura bs_notimpl_bs_sr_8,
195 1.1 takemura
196 1.1 takemura /* copy */
197 1.1 takemura bs_notimpl_bs_c_1,
198 1.1 takemura bs_notimpl_bs_c_2,
199 1.1 takemura bs_notimpl_bs_c_4,
200 1.1 takemura bs_notimpl_bs_c_8,
201 1.1 takemura };
202 1.1 takemura
203 1.1 takemura int
204 1.1 takemura com_hpcio_cndb_attach(bus_space_tag_t iot, int iobase, int rate,
205 1.1 takemura int frequency, tcflag_t cflag, int kgdb)
206 1.1 takemura {
207 1.1 takemura int alignment;
208 1.1 takemura
209 1.1 takemura DPRINTF("com_hpcio_cndb_attach()\n");
210 1.1 takemura if (!com_hpcio_common_probe(iot, iobase, &alignment)) {
211 1.1 takemura DPRINTF("com_hpcio_cndb_attach(): probe failed\n");
212 1.1 takemura return (ENOTTY);
213 1.1 takemura }
214 1.1 takemura if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
215 1.1 takemura DPRINTF("com_hpcio_cndb_attach(): half word aligned\n");
216 1.1 takemura com_hpcio_iot_init(&com_hpcio_cniotx, iot);
217 1.1 takemura com_hpcio_cniot = &com_hpcio_cniotx;
218 1.1 takemura } else {
219 1.1 takemura com_hpcio_cniot = iot;
220 1.1 takemura }
221 1.1 takemura com_hpcio_cniobase = iobase;
222 1.1 takemura DPRINTF("com_hpcio_cndb_attach(): probe succeeded\n");
223 1.1 takemura #ifdef KGDB
224 1.1 takemura if (kgdb)
225 1.1 takemura return (com_kgdb_attach(com_hpcio_cniot, iobase, rate,
226 1.6 thorpej frequency, COM_TYPE_NORMAL, cflag));
227 1.1 takemura else
228 1.1 takemura #endif
229 1.1 takemura return (comcnattach(com_hpcio_cniot, iobase, rate,
230 1.6 thorpej frequency, COM_TYPE_NORMAL, cflag));
231 1.1 takemura }
232 1.1 takemura
233 1.1 takemura static int
234 1.1 takemura com_hpcio_common_probe(bus_space_tag_t iot, int iobase, int *alignment)
235 1.1 takemura {
236 1.1 takemura bus_space_handle_t ioh;
237 1.1 takemura static struct bus_space_tag tmpiot;
238 1.1 takemura int rv;
239 1.1 takemura
240 1.1 takemura /*
241 1.1 takemura * try byte aligned register
242 1.1 takemura */
243 1.1 takemura *alignment = COM_HPCIO_BYTE_ALIGNMENT;
244 1.2 takemura if (bus_space_map(iot, iobase, 1, 0, &ioh))
245 1.1 takemura return 0;
246 1.1 takemura rv = comprobe1(iot, ioh);
247 1.1 takemura bus_space_unmap(iot, ioh, 1);
248 1.1 takemura
249 1.1 takemura if (rv != 0)
250 1.1 takemura return (rv);
251 1.1 takemura
252 1.1 takemura /*
253 1.1 takemura * try half word aligned register
254 1.1 takemura */
255 1.1 takemura *alignment = COM_HPCIO_HALFWORD_ALIGNMENT;
256 1.1 takemura com_hpcio_iot_init(&tmpiot, iot);
257 1.2 takemura if (bus_space_map(&tmpiot, iobase, 1, 0, &ioh))
258 1.1 takemura return 0;
259 1.1 takemura rv = comprobe1(&tmpiot, ioh);
260 1.1 takemura bus_space_unmap(&tmpiot, ioh, 1);
261 1.1 takemura
262 1.1 takemura return (rv);
263 1.1 takemura }
264 1.1 takemura
265 1.1 takemura static int
266 1.1 takemura com_hpcio_probe(struct device *parent, struct cfdata *cf, void *aux)
267 1.1 takemura {
268 1.1 takemura struct hpcio_attach_args *haa = aux;
269 1.1 takemura bus_space_tag_t iot = haa->haa_iot;
270 1.1 takemura int addr, alignment;
271 1.1 takemura
272 1.1 takemura if (cf->cf_loc[HPCIOIFCF_PLATFORM] != HPCIOIFCF_PLATFORM_DEFAULT) {
273 1.1 takemura platid_mask_t mask;
274 1.1 takemura
275 1.1 takemura mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]);
276 1.1 takemura if (!platid_match(&platid, &mask))
277 1.1 takemura return (0); /* platform id didn't match */
278 1.1 takemura }
279 1.1 takemura
280 1.1 takemura if ((addr = cf->cf_loc[HPCIOIFCF_ADDR]) == HPCIOIFCF_ADDR_DEFAULT)
281 1.1 takemura return (0); /* address wasn't specified */
282 1.1 takemura
283 1.1 takemura return com_hpcio_common_probe(iot, addr, &alignment);
284 1.1 takemura }
285 1.1 takemura
286 1.1 takemura
287 1.1 takemura static void
288 1.1 takemura com_hpcio_attach(struct device *parent, struct device *self, void *aux)
289 1.1 takemura {
290 1.1 takemura struct com_hpcio_softc *hsc = (void *)self;
291 1.1 takemura struct com_softc *sc = &hsc->hsc_com;
292 1.1 takemura struct hpcio_attach_args *haa = aux;
293 1.1 takemura bus_space_tag_t iot;
294 1.1 takemura bus_space_handle_t ioh;
295 1.1 takemura int addr, port, mode, alignment, *loc;
296 1.1 takemura
297 1.1 takemura loc = sc->sc_dev.dv_cfdata->cf_loc;
298 1.1 takemura addr = loc[HPCIOIFCF_ADDR];
299 1.1 takemura printf(" addr %x", addr);
300 1.1 takemura if ((com_hpcio_cniot == haa->haa_iot ||
301 1.1 takemura com_hpcio_cniot->bs_base == haa->haa_iot) &&
302 1.1 takemura com_hpcio_cniobase == addr &&
303 1.1 takemura com_is_console(com_hpcio_cniot, addr, 0)) {
304 1.1 takemura iot = com_hpcio_cniot;
305 1.1 takemura if (com_hpcio_cniot->bs_base == haa->haa_iot)
306 1.1 takemura printf(", half word aligned");
307 1.1 takemura } else {
308 1.1 takemura com_hpcio_common_probe(haa->haa_iot, addr, &alignment);
309 1.1 takemura if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
310 1.1 takemura printf(", half word aligned");
311 1.1 takemura iot = &hsc->hsc_iot;
312 1.1 takemura com_hpcio_iot_init(iot, haa->haa_iot);
313 1.1 takemura } else {
314 1.1 takemura iot = haa->haa_iot;
315 1.1 takemura }
316 1.1 takemura }
317 1.1 takemura if (bus_space_map(iot, addr, 1, 0, &ioh)) {
318 1.1 takemura printf(": can't map bus space\n");
319 1.1 takemura return;
320 1.1 takemura }
321 1.1 takemura sc->sc_iobase = addr;
322 1.1 takemura sc->sc_iot = iot;
323 1.1 takemura sc->sc_ioh = ioh;
324 1.1 takemura
325 1.1 takemura sc->enable = NULL;
326 1.1 takemura sc->disable = NULL;
327 1.1 takemura
328 1.1 takemura sc->sc_frequency = COM_FREQ;
329 1.1 takemura com_attach_subr(sc);
330 1.1 takemura
331 1.1 takemura hsc->hsc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
332 1.1 takemura port = loc[HPCIOIFCF_PORT];
333 1.1 takemura mode = HPCIO_INTR_LEVEL | HPCIO_INTR_HIGH;
334 1.1 takemura hpcio_intr_establish(hsc->hsc_hc, port, mode, comintr, sc);
335 1.1 takemura }
336 1.1 takemura
337 1.1 takemura /*
338 1.1 takemura * bus stuff (registershalf word aligned)
339 1.1 takemura */
340 1.1 takemura void
341 1.1 takemura com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base)
342 1.1 takemura {
343 1.1 takemura
344 1.1 takemura iot->bs_base = base;
345 1.1 takemura iot->bs_ops = com_hpcio_bs_ops; /* structure assignment */
346 1.1 takemura iot->bs_ops.bs_r_1 = com_hpcio_bs_r_1;
347 1.1 takemura iot->bs_ops.bs_w_1 = com_hpcio_bs_w_1;
348 1.1 takemura iot->bs_ops.bs_wm_1 = com_hpcio_bs_wm_1;
349 1.1 takemura }
350 1.1 takemura
351 1.1 takemura u_int8_t
352 1.1 takemura com_hpcio_bs_r_1(bus_space_tag_t t, bus_space_handle_t bsh,
353 1.1 takemura bus_size_t offset)
354 1.1 takemura {
355 1.1 takemura return bus_space_read_1(t->bs_base, bsh, offset * 2);
356 1.1 takemura }
357 1.1 takemura
358 1.1 takemura void
359 1.1 takemura com_hpcio_bs_w_1(bus_space_tag_t t, bus_space_handle_t bsh,
360 1.1 takemura bus_size_t offset, u_int8_t value)
361 1.1 takemura {
362 1.1 takemura bus_space_write_1(t->bs_base, bsh, offset * 2, value);
363 1.1 takemura }
364 1.1 takemura
365 1.1 takemura void
366 1.1 takemura com_hpcio_bs_wm_1(bus_space_tag_t t, bus_space_handle_t bsh,
367 1.1 takemura bus_size_t offset, const u_int8_t *addr, bus_size_t count)
368 1.1 takemura {
369 1.1 takemura bus_space_write_multi_1(t->bs_base, bsh, offset * 2, addr, count);
370 1.1 takemura }
371