Home | History | Annotate | Line # | Download | only in tx
txcsbus.c revision 1.22.52.2
      1 /*	$NetBSD: txcsbus.c,v 1.22.52.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: txcsbus.c,v 1.22.52.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 
     39 #include <machine/intr.h>
     40 #include <machine/bus.h>
     41 #include <machine/bus_space_hpcmips.h>
     42 
     43 #include <machine/platid.h>
     44 #include <machine/platid_mask.h>
     45 
     46 #include <hpcmips/tx/tx39var.h>
     47 #include <hpcmips/tx/txcsbusvar.h>
     48 #include <hpcmips/tx/tx39biuvar.h>
     49 #include <hpcmips/tx/tx39biureg.h>
     50 
     51 #include "locators.h"
     52 
     53 /* TX39 CS mapping. (nonconfigurationable) */
     54 const struct csmap {
     55 	const char *cs_name;
     56 	paddr_t	cs_addr;
     57 	psize_t	cs_size;
     58 } __csmap[] = {
     59 	[TX39_CS0]	= {"CS0(ROM)"	, TX39_SYSADDR_CS0	,
     60 			   TX39_SYSADDR_CS_SIZE},
     61 	[TX39_CS1]	= {"CS1"	, TX39_SYSADDR_CS1	,
     62 			   TX39_SYSADDR_CS_SIZE},
     63 	[TX39_CS2]	= {"CS2"	, TX39_SYSADDR_CS2	,
     64 			   TX39_SYSADDR_CS_SIZE},
     65 	[TX39_CS3]	= {"CS3"	, TX39_SYSADDR_CS3	,
     66 			   TX39_SYSADDR_CS_SIZE},
     67 	[TX39_MCS0]	= {"MCS0"	, TX39_SYSADDR_MCS0	,
     68 			   TX39_SYSADDR_MCS_SIZE},
     69 	[TX39_MCS1]	= {"MCS1"	, TX39_SYSADDR_MCS1	,
     70 			   TX39_SYSADDR_MCS_SIZE},
     71 #ifdef TX391X
     72 	[TX39_MCS2]	= {"MCS2"	, TX39_SYSADDR_MCS2	,
     73 			   TX39_SYSADDR_MCS_SIZE},
     74 	[TX39_MCS3]	= {"MCS3"	, TX39_SYSADDR_MCS3	,
     75 			   TX39_SYSADDR_MCS_SIZE},
     76 #endif /* TX391X */
     77 	[TX39_CARD1]	= {"CARD1(io/attr)", TX39_SYSADDR_CARD1	,
     78 			   TX39_SYSADDR_CARD_SIZE},
     79 	[TX39_CARD2]	= {"CARD2(io/attr)", TX39_SYSADDR_CARD2	,
     80 			   TX39_SYSADDR_CARD_SIZE},
     81 	[TX39_CARD1MEM]	= {"CARD1(mem)"	, TX39_SYSADDR_CARD1MEM	,
     82 			   TX39_SYSADDR_CARD_SIZE},
     83 	[TX39_CARD2MEM]	= {"CARD2(mem)"	, TX39_SYSADDR_CARD2MEM	,
     84 			   TX39_SYSADDR_CARD_SIZE},
     85 	[TX39_KUCS0]	= {"KUCS0"	, TX39_SYSADDR_KUSEG_CS0,
     86 			   TX39_SYSADDR_KUCS_SIZE},
     87 	[TX39_KUCS1]	= {"KUCS1"	, TX39_SYSADDR_KUSEG_CS1,
     88 			   TX39_SYSADDR_KUCS_SIZE},
     89 	[TX39_KUCS2]	= {"KUCS2"	, TX39_SYSADDR_KUSEG_CS2,
     90 			   TX39_SYSADDR_KUCS_SIZE},
     91 	[TX39_KUCS3]	= {"KUCS3"	, TX39_SYSADDR_KUSEG_CS3,
     92 			   TX39_SYSADDR_KUCS_SIZE},
     93 };
     94 
     95 int	txcsbus_match(device_t, cfdata_t, void *);
     96 void	txcsbus_attach(device_t, device_t, void *);
     97 int	txcsbus_print(void *, const char *);
     98 int	txcsbus_search(device_t, cfdata_t, const int *, void *);
     99 
    100 struct txcsbus_softc {
    101 	tx_chipset_tag_t sc_tc;
    102 	/* chip select space tag */
    103 	struct bus_space_tag_hpcmips *sc_cst[TX39_MAXCS];
    104 	int sc_pri;
    105 };
    106 
    107 CFATTACH_DECL_NEW(txcsbus, sizeof(struct txcsbus_softc),
    108     txcsbus_match, txcsbus_attach, NULL, NULL);
    109 
    110 static bus_space_tag_t __txcsbus_alloc_cstag(struct txcsbus_softc *,
    111     struct cs_handle *);
    112 
    113 int
    114 txcsbus_match(device_t parent, cfdata_t cf, void *aux)
    115 {
    116 	struct csbus_attach_args *cba = aux;
    117 	platid_mask_t mask;
    118 
    119 	if (strcmp(cba->cba_busname, cf->cf_name))
    120 		return (0);
    121 
    122 	if (cf->cf_loc[TXCSBUSIFCF_PLATFORM] == TXCSBUSIFCF_PLATFORM_DEFAULT)
    123 		return (1);
    124 
    125 	mask = PLATID_DEREF(cf->cf_loc[TXCSBUSIFCF_PLATFORM]);
    126 	if (platid_match(&platid, &mask))
    127 		return (2);
    128 
    129 	return (0);
    130 }
    131 
    132 void
    133 txcsbus_attach(device_t parent, device_t self, void *aux)
    134 {
    135 	struct csbus_attach_args *cba = aux;
    136 	struct txcsbus_softc *sc = device_private(self);
    137 
    138 	sc->sc_tc = cba->cba_tc;
    139 	printf("\n");
    140 
    141 	/*
    142 	 *	Attach external chip.
    143 	 */
    144 	/* higher priority devices attach first */
    145 	sc->sc_pri = 2;
    146 	config_search(self, NULL,
    147 	    CFARG_SUBMATCH, txcsbus_search,
    148 	    CFARG_IATTR, "txcsbus",
    149 	    CFARG_EOL);
    150 
    151 	/* then, normal priority devices */
    152 	sc->sc_pri = 1;
    153 	config_search(self, NULL,
    154 	    CFARG_SUBMATCH, txcsbus_search,
    155 	    CFARG_IATTR, "txcsbus",
    156 	    CFARG_EOL);
    157 }
    158 
    159 int
    160 txcsbus_print(void *aux, const char *pnp)
    161 {
    162 #define PRINTIRQ(i) i, (i) / 32, (i) % 32
    163 	struct cs_attach_args *ca = aux;
    164 
    165 	if (ca->ca_csreg.cs != TXCSBUSCF_REGCS_DEFAULT) {
    166 		aprint_normal(" regcs %s %dbit %#x+%#x",
    167 		    __csmap[ca->ca_csreg.cs].cs_name,
    168 		    ca->ca_csreg.cswidth,
    169 		    ca->ca_csreg.csbase,
    170 		    ca->ca_csreg.cssize);
    171 	}
    172 
    173 	if (ca->ca_csio.cs != TXCSBUSCF_IOCS_DEFAULT) {
    174 		aprint_normal(" iocs %s %dbit %#x+%#x",
    175 		    __csmap[ca->ca_csio.cs].cs_name,
    176 		    ca->ca_csio.cswidth,
    177 		    ca->ca_csio.csbase,
    178 		    ca->ca_csio.cssize);
    179 	}
    180 
    181 	if (ca->ca_csmem.cs != TXCSBUSCF_MEMCS_DEFAULT) {
    182 		aprint_normal(" memcs %s %dbit %#x+%#x",
    183 		    __csmap[ca->ca_csmem.cs].cs_name,
    184 		    ca->ca_csmem.cswidth,
    185 		    ca->ca_csmem.csbase,
    186 		    ca->ca_csmem.cssize);
    187 	}
    188 
    189 	if (ca->ca_irq1 != TXCSBUSCF_IRQ1_DEFAULT) {
    190 		aprint_normal(" irq1 %d(%d:%d)", PRINTIRQ(ca->ca_irq1));
    191 	}
    192 
    193 	if (ca->ca_irq2 != TXCSBUSCF_IRQ2_DEFAULT) {
    194 		aprint_normal(" irq2 %d(%d:%d)", PRINTIRQ(ca->ca_irq2));
    195 	}
    196 
    197 	if (ca->ca_irq3 != TXCSBUSCF_IRQ3_DEFAULT) {
    198 		aprint_normal(" irq3 %d(%d:%d)", PRINTIRQ(ca->ca_irq3));
    199 	}
    200 
    201 	return (UNCONF);
    202 }
    203 
    204 int
    205 txcsbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    206 {
    207 	struct txcsbus_softc *sc = device_private(parent);
    208 	struct cs_attach_args ca;
    209 
    210 	ca.ca_tc		= sc->sc_tc;
    211 
    212 	ca.ca_csreg.cs		= cf->cf_loc[TXCSBUSCF_REGCS];
    213 	ca.ca_csreg.csbase	= cf->cf_loc[TXCSBUSCF_REGCSBASE];
    214 	ca.ca_csreg.cssize	= cf->cf_loc[TXCSBUSCF_REGCSSIZE];
    215 	ca.ca_csreg.cswidth	= cf->cf_loc[TXCSBUSCF_REGCSWIDTH];
    216 
    217 	if (ca.ca_csreg.cs != TXCSBUSCF_REGCS_DEFAULT) {
    218 		ca.ca_csreg.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csreg);
    219 	}
    220 
    221 	ca.ca_csio.cs		= cf->cf_loc[TXCSBUSCF_IOCS];
    222 	ca.ca_csio.csbase	= cf->cf_loc[TXCSBUSCF_IOCSBASE];
    223 	ca.ca_csio.cssize	= cf->cf_loc[TXCSBUSCF_IOCSSIZE];
    224 	ca.ca_csio.cswidth	= cf->cf_loc[TXCSBUSCF_IOCSWIDTH];
    225 
    226 	if (ca.ca_csio.cs != TXCSBUSCF_IOCS_DEFAULT) {
    227 		ca.ca_csio.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csio);
    228 	}
    229 
    230 	ca.ca_csmem.cs		= cf->cf_loc[TXCSBUSCF_MEMCS];
    231 	ca.ca_csmem.csbase	= cf->cf_loc[TXCSBUSCF_MEMCSBASE];
    232 	ca.ca_csmem.cssize	= cf->cf_loc[TXCSBUSCF_MEMCSSIZE];
    233 	ca.ca_csmem.cswidth	= cf->cf_loc[TXCSBUSCF_MEMCSWIDTH];
    234 
    235 	if (ca.ca_csmem.cs != TXCSBUSCF_MEMCS_DEFAULT) {
    236 		ca.ca_csmem.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csmem);
    237 	}
    238 
    239 	ca.ca_irq1		= cf->cf_loc[TXCSBUSCF_IRQ1];
    240 	ca.ca_irq2		= cf->cf_loc[TXCSBUSCF_IRQ2];
    241 	ca.ca_irq3		= cf->cf_loc[TXCSBUSCF_IRQ3];
    242 
    243 	if (config_match(parent, cf, &ca) == sc->sc_pri) {
    244 		config_attach(parent, cf, &ca, txcsbus_print);
    245 	}
    246 
    247 	return (0);
    248 }
    249 
    250 bus_space_tag_t
    251 __txcsbus_alloc_cstag(struct txcsbus_softc *sc, struct cs_handle *csh)
    252 {
    253 
    254 	tx_chipset_tag_t tc = sc->sc_tc;
    255 	int cs = csh->cs;
    256 	int width = csh->cswidth;
    257 	struct bus_space_tag_hpcmips *iot;
    258 	txreg_t reg;
    259 
    260  	if (!TX39_ISCS(cs) && !TX39_ISMCS(cs) && !TX39_ISCARD(cs) &&
    261 	    !TX39_ISKUCS(cs)) {
    262 		panic("txcsbus_alloc_tag: bogus chip select %d", cs);
    263 	}
    264 
    265 	/* Already setuped chip select */
    266 	if (sc->sc_cst[cs]) {
    267 		return (&sc->sc_cst[cs]->bst);
    268 	}
    269 
    270 	iot = hpcmips_alloc_bus_space_tag();
    271 	hpcmips_init_bus_space(iot, hpcmips_system_bus_space_hpcmips(),
    272 	    __csmap[cs].cs_name, __csmap[cs].cs_addr, __csmap[cs].cs_size);
    273 	sc->sc_cst[cs] = iot;
    274 
    275 	/* CS bus-width (configurationable) */
    276 	switch (width) {
    277 	default:
    278 		panic("txcsbus_alloc_tag: bogus bus width %d", width);
    279 
    280 	case 32:
    281 		if (TX39_ISCS(cs)) {
    282 			reg = tx_conf_read(tc, TX39_MEMCONFIG0_REG);
    283 			reg |= (1 << cs);
    284 			tx_conf_write(tc, TX39_MEMCONFIG0_REG, reg);
    285 		} else if(TX39_ISMCS(cs)) {
    286 #ifdef TX391X
    287 			panic("txcsbus_alloc_tag: MCS is 16bit only");
    288 #endif /* TX391X */
    289 #ifdef TX392X
    290 			reg = tx_conf_read(tc, TX39_MEMCONFIG1_REG);
    291 			reg |= ((cs == TX39_MCS0) ?
    292 			    TX39_MEMCONFIG1_MCS0_32 :
    293 			    TX39_MEMCONFIG1_MCS1_32);
    294 			tx_conf_write(tc, TX39_MEMCONFIG1_REG, reg);
    295 #endif /* TX392X */
    296 		}
    297 		break;
    298 
    299 	case 16:
    300 		if (TX39_ISCS(cs)) {
    301 			reg = tx_conf_read(tc, TX39_MEMCONFIG0_REG);
    302 			reg &= ~(1 << cs);
    303 			tx_conf_write(tc, TX39_MEMCONFIG0_REG, reg);
    304 		} else if(TX39_ISMCS(cs)) {
    305 			/* TX391X always 16bit port */
    306 #ifdef TX392X
    307 			reg = tx_conf_read(tc, TX39_MEMCONFIG1_REG);
    308 			reg &= ~((cs == TX39_MCS0) ?
    309 			    TX39_MEMCONFIG1_MCS0_32 :
    310 			    TX39_MEMCONFIG1_MCS1_32);
    311 			tx_conf_write(tc, TX39_MEMCONFIG1_REG, reg);
    312 #endif /* TX392X */
    313 		} else if (TX39_ISCARD(cs)) {
    314 			/* CARD io/attr or mem */
    315 			reg = tx_conf_read(tc, TX39_MEMCONFIG3_REG);
    316 
    317 			/* enable I/O access */
    318 			reg |= (cs == TX39_CARD1) ?
    319 			    TX39_MEMCONFIG3_CARD1IOEN :
    320 			    TX39_MEMCONFIG3_CARD2IOEN;
    321 			/* disable 8bit access */
    322 #ifdef TX392X
    323 			reg &= ~((cs == TX39_CARD1) ?
    324 			    TX39_MEMCONFIG3_CARD1_8SEL :
    325 			    TX39_MEMCONFIG3_CARD2_8SEL);
    326 #endif /* TX392X */
    327 #ifdef TX391X
    328 			reg &= ~TX39_MEMCONFIG3_PORT8SEL;
    329 #endif /* TX391X */
    330 			tx_conf_write(tc, TX39_MEMCONFIG3_REG, reg);
    331 		}
    332 		break;
    333 
    334 	case 8:
    335 		if (TX39_ISCARD(cs)) {
    336 			reg = tx_conf_read(tc, TX39_MEMCONFIG3_REG);
    337 
    338 			/* enable I/O access */
    339 			reg |= (cs == TX39_CARD1) ?
    340 			    TX39_MEMCONFIG3_CARD1IOEN :
    341 			    TX39_MEMCONFIG3_CARD2IOEN;
    342 			/* disable 8bit access */
    343 #ifdef TX392X
    344 			reg |= (cs == TX39_CARD1) ?
    345 			    TX39_MEMCONFIG3_CARD1_8SEL :
    346 			    TX39_MEMCONFIG3_CARD2_8SEL;
    347 #endif /* TX392X */
    348 #ifdef TX391X
    349 			reg |= TX39_MEMCONFIG3_PORT8SEL;
    350 #endif /* TX391X */
    351 			tx_conf_write(tc, TX39_MEMCONFIG3_REG, reg);
    352 
    353 		} else {
    354 			panic("__txcsbus_alloc_cstag: CS%d 8bit mode is"
    355 			    "not allowed", cs);
    356 		}
    357 	}
    358 
    359 	return (&iot->bst);
    360 }
    361