Home | History | Annotate | Line # | Download | only in tx
txcsbus.c revision 1.5
      1 /*	$NetBSD: txcsbus.c,v 1.5 2001/06/14 11:09:56 uch 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #include "opt_tx39_debug.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 
     44 #include <machine/bus.h>
     45 #include <machine/intr.h>
     46 
     47 #include <machine/platid.h>
     48 #include <machine/platid_mask.h>
     49 
     50 #include <hpcmips/tx/tx39var.h>
     51 #include <hpcmips/tx/txcsbusvar.h>
     52 #include <hpcmips/tx/tx39biuvar.h>
     53 #include <hpcmips/tx/tx39biureg.h>
     54 
     55 #include "locators.h"
     56 
     57 /* TX39 CS mapping. (nonconfigurationable) */
     58 const struct csmap {
     59 	char	*cs_name;
     60 	paddr_t	cs_addr;
     61 	psize_t	cs_size;
     62 } __csmap[] = {
     63 	[TX39_CS0]	= {"CS0(ROM)"	, TX39_SYSADDR_CS0	,
     64 			   TX39_SYSADDR_CS_SIZE},
     65 	[TX39_CS1]	= {"CS1"	, TX39_SYSADDR_CS1	,
     66 			   TX39_SYSADDR_CS_SIZE},
     67 	[TX39_CS2]	= {"CS2"	, TX39_SYSADDR_CS2	,
     68 			   TX39_SYSADDR_CS_SIZE},
     69 	[TX39_CS3]	= {"CS3"	, TX39_SYSADDR_CS3	,
     70 			   TX39_SYSADDR_CS_SIZE},
     71 	[TX39_MCS0]	= {"MCS0"	, TX39_SYSADDR_MCS0	,
     72 			   TX39_SYSADDR_MCS_SIZE},
     73 	[TX39_MCS1]	= {"MCS1"	, TX39_SYSADDR_MCS1	,
     74 			   TX39_SYSADDR_MCS_SIZE},
     75 #ifdef TX391X
     76 	[TX39_MCS2]	= {"MCS2"	, TX39_SYSADDR_MCS2	,
     77 			   TX39_SYSADDR_MCS_SIZE},
     78 	[TX39_MCS3]	= {"MCS3"	, TX39_SYSADDR_MCS3	,
     79 			   TX39_SYSADDR_MCS_SIZE},
     80 #endif /* TX391X */
     81 	[TX39_CARD1]	= {"CARD1(io/attr)", TX39_SYSADDR_CARD1	,
     82 			   TX39_SYSADDR_CARD_SIZE},
     83 	[TX39_CARD2]	= {"CARD2(io/attr)", TX39_SYSADDR_CARD2	,
     84 			   TX39_SYSADDR_CARD_SIZE},
     85 	[TX39_CARD1MEM]	= {"CARD1(mem)"	, TX39_SYSADDR_CARD1MEM	,
     86 			   TX39_SYSADDR_CARD_SIZE},
     87 	[TX39_CARD2MEM]	= {"CARD2(mem)"	, TX39_SYSADDR_CARD2MEM	,
     88 			   TX39_SYSADDR_CARD_SIZE},
     89 };
     90 
     91 int	txcsbus_match(struct device *, struct cfdata *, void *);
     92 void	txcsbus_attach(struct device *, struct device *, void *);
     93 int	txcsbus_print(void *, const char *);
     94 int	txcsbus_search(struct device *, struct cfdata *, void *);
     95 
     96 struct txcsbus_softc {
     97 	struct	device sc_dev;
     98 	tx_chipset_tag_t sc_tc;
     99 	/* chip select space tag */
    100 	bus_space_tag_t sc_cst[TX39_MAXCS];
    101 };
    102 
    103 struct cfattach txcsbus_ca = {
    104 	sizeof(struct txcsbus_softc), txcsbus_match, txcsbus_attach
    105 };
    106 
    107 static bus_space_tag_t __txcsbus_alloc_cstag(struct txcsbus_softc *,
    108     struct cs_handle *);
    109 
    110 int
    111 txcsbus_match(struct device *parent, struct cfdata *cf, void *aux)
    112 {
    113 	struct csbus_attach_args *cba = aux;
    114 	platid_mask_t mask;
    115 
    116 	if (strcmp(cba->cba_busname, cf->cf_driver->cd_name))
    117 		return (0);
    118 
    119 	if (cf->cf_loc[TXCSBUSIFCF_PLATFORM] == TXCSBUSIFCF_PLATFORM_DEFAULT)
    120 		return (1);
    121 
    122 	mask = PLATID_DEREF(cf->cf_loc[TXCSBUSIFCF_PLATFORM]);
    123 	if (platid_match(&platid, &mask))
    124 		return (2);
    125 
    126 	return (0);
    127 }
    128 
    129 void
    130 txcsbus_attach(struct device *parent, struct device *self, void *aux)
    131 {
    132 	struct csbus_attach_args *cba = aux;
    133 	struct txcsbus_softc *sc = (void*)self;
    134 
    135 	sc->sc_tc = cba->cba_tc;
    136 	printf("\n");
    137 
    138 	/*
    139 	 *	Attach external chip.
    140 	 */
    141 	config_search(txcsbus_search, self, txcsbus_print);
    142 }
    143 
    144 int
    145 txcsbus_print(void *aux, const char *pnp)
    146 {
    147 #define PRINTIRQ(i) i, (i) / 32, (i) % 32
    148 	struct cs_attach_args *ca = aux;
    149 
    150 	if (ca->ca_csreg.cs != TXCSBUSCF_REGCS_DEFAULT) {
    151 		printf(" regcs %s %dbit %#x+%#x",
    152 		    __csmap[ca->ca_csreg.cs].cs_name,
    153 		    ca->ca_csreg.cswidth,
    154 		    ca->ca_csreg.csbase,
    155 		    ca->ca_csreg.cssize);
    156 	}
    157 
    158 	if (ca->ca_csio.cs != TXCSBUSCF_IOCS_DEFAULT) {
    159 		printf(" iocs %s %dbit %#x+%#x",
    160 		    __csmap[ca->ca_csio.cs].cs_name,
    161 		    ca->ca_csio.cswidth,
    162 		    ca->ca_csio.csbase,
    163 		    ca->ca_csio.cssize);
    164 	}
    165 
    166 	if (ca->ca_csmem.cs != TXCSBUSCF_MEMCS_DEFAULT) {
    167 		printf(" memcs %s %dbit %#x+%#x",
    168 		    __csmap[ca->ca_csmem.cs].cs_name,
    169 		    ca->ca_csmem.cswidth,
    170 		    ca->ca_csmem.csbase,
    171 		    ca->ca_csmem.cssize);
    172 	}
    173 
    174 	if (ca->ca_irq1 != TXCSBUSCF_IRQ1_DEFAULT) {
    175 		printf(" irq1 %d(%d:%d)", PRINTIRQ(ca->ca_irq1));
    176 	}
    177 
    178 	if (ca->ca_irq2 != TXCSBUSCF_IRQ2_DEFAULT) {
    179 		printf(" irq2 %d(%d:%d)", PRINTIRQ(ca->ca_irq2));
    180 	}
    181 
    182 	if (ca->ca_irq3 != TXCSBUSCF_IRQ3_DEFAULT) {
    183 		printf(" irq3 %d(%d:%d)", PRINTIRQ(ca->ca_irq3));
    184 	}
    185 
    186 	return (UNCONF);
    187 }
    188 
    189 int
    190 txcsbus_search(struct device *parent, struct cfdata *cf, void *aux)
    191 {
    192 	struct txcsbus_softc *sc = (void*)parent;
    193 	struct cs_attach_args ca;
    194 
    195 	ca.ca_tc		= sc->sc_tc;
    196 
    197 	ca.ca_csreg.cs		= cf->cf_loc[TXCSBUSCF_REGCS];
    198 	ca.ca_csreg.csbase	= cf->cf_loc[TXCSBUSCF_REGCSBASE];
    199 	ca.ca_csreg.cssize	= cf->cf_loc[TXCSBUSCF_REGCSSIZE];
    200 	ca.ca_csreg.cswidth	= cf->cf_loc[TXCSBUSCF_REGCSWIDTH];
    201 
    202 	if (ca.ca_csreg.cs != TXCSBUSCF_REGCS_DEFAULT) {
    203 		ca.ca_csreg.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csreg);
    204 	}
    205 
    206 	ca.ca_csio.cs		= cf->cf_loc[TXCSBUSCF_IOCS];
    207 	ca.ca_csio.csbase	= cf->cf_loc[TXCSBUSCF_IOCSBASE];
    208 	ca.ca_csio.cssize	= cf->cf_loc[TXCSBUSCF_IOCSSIZE];
    209 	ca.ca_csio.cswidth	= cf->cf_loc[TXCSBUSCF_IOCSWIDTH];
    210 
    211 	if (ca.ca_csio.cs != TXCSBUSCF_IOCS_DEFAULT) {
    212 		ca.ca_csio.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csio);
    213 	}
    214 
    215 	ca.ca_csmem.cs		= cf->cf_loc[TXCSBUSCF_MEMCS];
    216 	ca.ca_csmem.csbase	= cf->cf_loc[TXCSBUSCF_MEMCSBASE];
    217 	ca.ca_csmem.cssize	= cf->cf_loc[TXCSBUSCF_MEMCSSIZE];
    218 	ca.ca_csmem.cswidth	= cf->cf_loc[TXCSBUSCF_MEMCSWIDTH];
    219 
    220 	if (ca.ca_csmem.cs != TXCSBUSCF_MEMCS_DEFAULT) {
    221 		ca.ca_csmem.cstag = __txcsbus_alloc_cstag(sc, &ca.ca_csmem);
    222 	}
    223 
    224 	ca.ca_irq1		= cf->cf_loc[TXCSBUSCF_IRQ1];
    225 	ca.ca_irq2		= cf->cf_loc[TXCSBUSCF_IRQ2];
    226 	ca.ca_irq3		= cf->cf_loc[TXCSBUSCF_IRQ3];
    227 
    228 	if ((*cf->cf_attach->ca_match)(parent, cf, &ca)) {
    229 		config_attach(parent, cf, &ca, txcsbus_print);
    230 	}
    231 
    232 	return (0);
    233 }
    234 
    235 bus_space_tag_t
    236 __txcsbus_alloc_cstag(struct txcsbus_softc *sc, struct cs_handle *csh)
    237 {
    238 
    239 	tx_chipset_tag_t tc = sc->sc_tc;
    240 	int cs = csh->cs;
    241 	int width = csh->cswidth;
    242 	bus_space_tag_t iot;
    243 	txreg_t reg;
    244 
    245  	if (!TX39_ISCS(cs) && !TX39_ISMCS(cs) && !TX39_ISCARD(cs)) {
    246 		panic("txcsbus_alloc_tag: bogus chip select %d\n", cs);
    247 	}
    248 
    249 	/* Already setuped chip select */
    250 	if (sc->sc_cst[cs]) {
    251 		return (sc->sc_cst[cs]);
    252 	}
    253 
    254 	iot = hpcmips_alloc_bus_space_tag();
    255 	sc->sc_cst[cs] = iot;
    256 
    257 	iot->t_base = __csmap[cs].cs_addr;
    258 	iot->t_size = __csmap[cs].cs_size;
    259 	strcpy(iot->t_name , __csmap[cs].cs_name);
    260 
    261 	/* CS bus-width (configurationable) */
    262 	switch (width) {
    263 	default:
    264 		panic("txcsbus_alloc_tag: bogus bus width %d\n", width);
    265 
    266 	case 32:
    267 		if (TX39_ISCS(cs)) {
    268 			reg = tx_conf_read(tc, TX39_MEMCONFIG0_REG);
    269 			reg |= (1 << cs);
    270 			tx_conf_write(tc, TX39_MEMCONFIG0_REG, reg);
    271 		} else if(TX39_ISMCS(cs)) {
    272 #ifdef TX391X
    273 			panic("txcsbus_alloc_tag: MCS is 16bit only");
    274 #endif /* TX391X */
    275 #ifdef TX392X
    276 			reg = tx_conf_read(tc, TX39_MEMCONFIG1_REG);
    277 			reg |= ((cs == TX39_MCS0) ?
    278 			    TX39_MEMCONFIG1_MCS0_32 :
    279 			    TX39_MEMCONFIG1_MCS1_32);
    280 			tx_conf_write(tc, TX39_MEMCONFIG1_REG, reg);
    281 #endif /* TX392X */
    282 		}
    283 		break;
    284 
    285 	case 16:
    286 		if (TX39_ISCS(cs)) {
    287 			reg = tx_conf_read(tc, TX39_MEMCONFIG0_REG);
    288 			reg &= ~(1 << cs);
    289 			tx_conf_write(tc, TX39_MEMCONFIG0_REG, reg);
    290 		} else if(TX39_ISMCS(cs)) {
    291 			/* TX391X always 16bit port */
    292 #ifdef TX392X
    293 			reg = tx_conf_read(tc, TX39_MEMCONFIG1_REG);
    294 			reg &= ~((cs == TX39_MCS0) ?
    295 			    TX39_MEMCONFIG1_MCS0_32 :
    296 			    TX39_MEMCONFIG1_MCS1_32);
    297 			tx_conf_write(tc, TX39_MEMCONFIG1_REG, reg);
    298 #endif /* TX392X */
    299 		} else {
    300 			/* CARD io/attr or mem */
    301 			reg = tx_conf_read(tc, TX39_MEMCONFIG3_REG);
    302 
    303 			/* enable I/O access */
    304 			reg |= (cs == TX39_CARD1) ?
    305 			    TX39_MEMCONFIG3_CARD1IOEN :
    306 			    TX39_MEMCONFIG3_CARD2IOEN;
    307 			/* disable 8bit access */
    308 #ifdef TX392X
    309 			reg &= ~((cs == TX39_CARD1) ?
    310 			    TX39_MEMCONFIG3_CARD1_8SEL :
    311 			    TX39_MEMCONFIG3_CARD2_8SEL);
    312 #endif /* TX392X */
    313 #ifdef TX391X
    314 			reg &= ~TX39_MEMCONFIG3_PORT8SEL;
    315 #endif /* TX391X */
    316 			tx_conf_write(tc, TX39_MEMCONFIG3_REG, reg);
    317 		}
    318 		break;
    319 
    320 	case 8:
    321 		if (TX39_ISCARD(cs)) {
    322 			reg = tx_conf_read(tc, TX39_MEMCONFIG3_REG);
    323 
    324 			/* enable I/O access */
    325 			reg |= (cs == TX39_CARD1) ?
    326 			    TX39_MEMCONFIG3_CARD1IOEN :
    327 			    TX39_MEMCONFIG3_CARD2IOEN;
    328 			/* disable 8bit access */
    329 #ifdef TX392X
    330 			reg |= (cs == TX39_CARD1) ?
    331 			    TX39_MEMCONFIG3_CARD1_8SEL :
    332 			    TX39_MEMCONFIG3_CARD2_8SEL;
    333 #endif /* TX392X */
    334 #ifdef TX391X
    335 			reg |= TX39_MEMCONFIG3_PORT8SEL;
    336 #endif /* TX391X */
    337 			tx_conf_write(tc, TX39_MEMCONFIG3_REG, reg);
    338 
    339 		} else {
    340 			panic("__txcsbus_alloc_cstag: CS%d 8bit mode is"
    341 			    "not allowed", cs);
    342 		}
    343 	}
    344 
    345 	hpcmips_init_bus_space_extent(iot);
    346 
    347 	return (iot);
    348 }
    349