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