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