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