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