Home | History | Annotate | Line # | Download | only in dev
plum.c revision 1.18.2.2
      1 /*	$NetBSD: plum.c,v 1.18.2.2 2021/03/20 20:49:43 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: plum.c,v 1.18.2.2 2021/03/20 20:49:43 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kmem.h>
     39 
     40 #include <machine/bus.h>
     41 #include <machine/intr.h>
     42 
     43 #include <hpcmips/tx/tx39var.h>
     44 #include <hpcmips/tx/txcsbusvar.h>
     45 #include <hpcmips/dev/plumvar.h>
     46 #include <hpcmips/dev/plumreg.h>
     47 
     48 int plum_match(device_t, cfdata_t, void *);
     49 void plum_attach(device_t, device_t, void *);
     50 int plum_print(void *, const char *);
     51 int plum_search(device_t, cfdata_t, const int *, void *);
     52 
     53 struct plum_softc {
     54 	plum_chipset_tag_t	sc_pc;
     55 	bus_space_tag_t		sc_csregt;
     56 	bus_space_tag_t		sc_csiot;
     57 	bus_space_tag_t		sc_csmemt;
     58 	int			sc_irq;
     59 	int			sc_pri;
     60 };
     61 
     62 CFATTACH_DECL_NEW(plum, sizeof(struct plum_softc),
     63     plum_match, plum_attach, NULL, NULL);
     64 
     65 plumreg_t plum_idcheck(bus_space_tag_t);
     66 
     67 int
     68 plum_match(device_t parent, cfdata_t cf, void *aux)
     69 {
     70 	struct cs_attach_args *ca = aux;
     71 
     72 	switch (plum_idcheck(ca->ca_csreg.cstag)) {
     73 	default:
     74 		return (0);
     75 	case PLUM2_1:
     76 	case PLUM2_2:
     77 		/* nothing */;
     78 	}
     79 
     80 	return (1);
     81 }
     82 
     83 void
     84 plum_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct cs_attach_args *ca = aux;
     87 	struct plum_softc *sc = device_private(self);
     88 	plumreg_t reg;
     89 
     90 	sc->sc_csregt	= ca->ca_csreg.cstag;
     91 	sc->sc_csiot	= ca->ca_csio.cstag;
     92 	sc->sc_csmemt	= ca->ca_csmem.cstag;
     93 	sc->sc_irq	= ca->ca_irq1;
     94 
     95 	switch (reg = plum_idcheck(sc->sc_csregt)) {
     96 	default:
     97 		printf(": unknown revision %#x\n", reg);
     98 		return;
     99 	case PLUM2_1:
    100 		printf(": Plum2 #1\n");
    101 		break;
    102 	case PLUM2_2:
    103 		printf(": Plum2 #2\n");
    104 		break;
    105 	}
    106 	sc->sc_pc = kmem_zalloc(sizeof(struct plum_chipset_tag), KM_SLEEP);
    107 	sc->sc_pc->pc_tc = ca->ca_tc;
    108 
    109 	/* Attach Plum devices */
    110 	/*
    111 	 * interrupt, power/clock module is used by other plum module.
    112 	 * attach first.
    113 	 */
    114 	sc->sc_pri = 2;
    115 	config_search(self, NULL,
    116 	    CFARG_SUBMATCH, plum_search,
    117 	    CFARG_IATTR, "plumif",
    118 	    CFARG_EOL);
    119 
    120 	/*
    121 	 * Other plum module.
    122 	 */
    123 	sc->sc_pri = 1;
    124 	config_search(self, NULL,
    125 	    CFARG_SUBMATCH, plum_search,
    126 	    CFARG_IATTR, "plumif",
    127 	    CFARG_EOL);
    128 }
    129 
    130 plumreg_t
    131 plum_idcheck(bus_space_tag_t regt)
    132 {
    133 	bus_space_handle_t regh;
    134 	plumreg_t reg;
    135 
    136 	if (bus_space_map(regt, PLUM_ID_REGBASE,
    137 	    PLUM_ID_REGSIZE, 0, &regh)) {
    138 		printf("ID register map failed\n");
    139 		return (0);
    140 	}
    141 	reg = plum_conf_read(regt, regh, PLUM_ID_REG);
    142 	bus_space_unmap(regt, regh, PLUM_ID_REGSIZE);
    143 
    144 	return (reg);
    145 }
    146 
    147 int
    148 plum_print(void *aux, const char *pnp)
    149 {
    150 
    151 	return (pnp ? QUIET : UNCONF);
    152 }
    153 
    154 int
    155 plum_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    156 {
    157 	struct plum_softc *sc = device_private(parent);
    158 	struct plum_attach_args pa;
    159 
    160 	pa.pa_pc	= sc->sc_pc;
    161 	pa.pa_regt	= sc->sc_csregt;
    162 	pa.pa_iot	= sc->sc_csiot;
    163 	pa.pa_memt	= sc->sc_csmemt;
    164 	pa.pa_irq	= sc->sc_irq;
    165 
    166 	if (config_match(parent, cf, &pa) == sc->sc_pri) {
    167 		config_attach(parent, cf, &pa, plum_print);
    168 	}
    169 
    170 	return 0;
    171 }
    172 
    173 plumreg_t
    174 plum_conf_read(bus_space_tag_t t, bus_space_handle_t h, int o)
    175 {
    176 	plumreg_t reg;
    177 
    178 	reg = bus_space_read_4(t, h, o);
    179 
    180 	return (reg);
    181 }
    182 
    183 void
    184 plum_conf_write(bus_space_tag_t t, bus_space_handle_t h, int o, plumreg_t v)
    185 {
    186 
    187 	bus_space_write_4(t, h, o, v);
    188 }
    189 
    190 void
    191 plum_conf_register_intr(plum_chipset_tag_t t, void *intrt)
    192 {
    193 
    194 	if (t->pc_intrt) {
    195 		panic("duplicate intrt");
    196 	}
    197 
    198 	t->pc_intrt = intrt;
    199 }
    200 
    201 void
    202 plum_conf_register_power(plum_chipset_tag_t t, void *powert)
    203 {
    204 
    205 	if (t->pc_powert) {
    206 		panic("duplicate powert");
    207 	}
    208 
    209 	t->pc_powert = powert;
    210 }
    211