Home | History | Annotate | Line # | Download | only in dev
arpci.c revision 1.1
      1  1.1  matt /*	$NetBSD: arpci.c,v 1.1 2011/07/07 05:06:44 matt Exp $	*/
      2  1.1  matt /*-
      3  1.1  matt  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4  1.1  matt  * All rights reserved.
      5  1.1  matt  *
      6  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  matt  * by Matt Thomas of 3am Software Foundry.
      8  1.1  matt  *
      9  1.1  matt  * Redistribution and use in source and binary forms, with or without
     10  1.1  matt  * modification, are permitted provided that the following conditions
     11  1.1  matt  * are met:
     12  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     13  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     14  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  matt  *    documentation and/or other materials provided with the distribution.
     17  1.1  matt  *
     18  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1  matt  */
     30  1.1  matt 
     31  1.1  matt #include <sys/cdefs.h>
     32  1.1  matt 
     33  1.1  matt __KERNEL_RCSID(0, "$NetBSD: arpci.c,v 1.1 2011/07/07 05:06:44 matt Exp $");
     34  1.1  matt 
     35  1.1  matt #include <sys/param.h>
     36  1.1  matt #include <sys/bus.h>
     37  1.1  matt #include <sys/device.h>
     38  1.1  matt 
     39  1.1  matt #include <dev/pci/pcivar.h>
     40  1.1  matt 
     41  1.1  matt #include <mips/atheros/include/arbusvar.h>
     42  1.1  matt #include <mips/atheros/include/ar9344reg.h>
     43  1.1  matt 
     44  1.1  matt #define	PCI_CMD_CFG_READ	0xa
     45  1.1  matt #define	PCI_CMD_CFG_WRITE	0xb
     46  1.1  matt 
     47  1.1  matt struct arpci_softc {
     48  1.1  matt 	device_t sc_dev;
     49  1.1  matt 	bus_dma_tag_t sc_dmat;
     50  1.1  matt 	bus_space_tag_t sc_bst;
     51  1.1  matt 	bus_space_handle_t sc_bsh;
     52  1.1  matt 	struct mips_bus_space sc_memt;
     53  1.1  matt 	struct mips_pci_chipset sc_pc;
     54  1.1  matt 	u_int sc_pba_flags;
     55  1.1  matt };
     56  1.1  matt 
     57  1.1  matt static void arpci_bus_mem_init(bus_space_tag_t, void *);
     58  1.1  matt 
     59  1.1  matt static void
     60  1.1  matt arpci_attach_hook(device_t parent, device_t self,
     61  1.1  matt     struct pcibus_attach_args *pba)
     62  1.1  matt {
     63  1.1  matt }
     64  1.1  matt 
     65  1.1  matt static int
     66  1.1  matt arpci_bus_maxdevs(void *v, int busno)
     67  1.1  matt {
     68  1.1  matt 	//struct arpci_softc * const sc = v;
     69  1.1  matt 
     70  1.1  matt 	if (busno == 0)
     71  1.1  matt 		return 22;
     72  1.1  matt 
     73  1.1  matt 	return 32;
     74  1.1  matt }
     75  1.1  matt 
     76  1.1  matt static pcitag_t
     77  1.1  matt arpci_make_tag(void *v, int bus, int dev, int func)
     78  1.1  matt {
     79  1.1  matt 	if (bus == 0 && dev == 0) {
     80  1.1  matt 		/*
     81  1.1  matt 		 * Local access
     82  1.1  matt 		 */
     83  1.1  matt 		return (func << 8);
     84  1.1  matt 	}
     85  1.1  matt 
     86  1.1  matt 	if (bus == 0 && dev < 21) {
     87  1.1  matt 		/*
     88  1.1  matt 		 * Type 0 can only access 21 (32 - 11) devices starting at			 * device 0 (0 is needed for inbound transactions).
     89  1.1  matt 		 * AD[11:32] encodes the idsel for the transaction
     90  1.1  matt 		 *	(only one bit can be set).
     91  1.1  matt 		 * AD[8:11] contains function
     92  1.1  matt 		 * AD[2:7] contains the register offset.
     93  1.1  matt 		 * AD[0:1] must be zero.
     94  1.1  matt 		 */
     95  1.1  matt 		return (1 << (dev + 11)) | (func << 8);
     96  1.1  matt 	}
     97  1.1  matt 
     98  1.1  matt 	/*
     99  1.1  matt 	 * Type 1 Confugration Transaction.
    100  1.1  matt 	 */
    101  1.1  matt 	return (bus << 16) | (dev << 11) | (func << 8) | 1;
    102  1.1  matt }
    103  1.1  matt 
    104  1.1  matt static void
    105  1.1  matt arpci_decompose_tag(void *v, pcitag_t tag, int *busp, int *devp, int *funcp)
    106  1.1  matt {
    107  1.1  matt 	if (tag & 1) {
    108  1.1  matt 		if (busp)
    109  1.1  matt 			*busp = (tag >> 16) & 255;
    110  1.1  matt 		if (devp)
    111  1.1  matt 			*devp = (tag >> 11) & 31;
    112  1.1  matt 	} else {
    113  1.1  matt 		if (busp)
    114  1.1  matt 			*busp = 0;
    115  1.1  matt 		if (devp) {
    116  1.1  matt 			if (tag & ~0x7ff) {
    117  1.1  matt 				*devp = ffs(tag >> 11) - 1;
    118  1.1  matt 			} else {
    119  1.1  matt 				*devp = 0;
    120  1.1  matt 			}
    121  1.1  matt 		}
    122  1.1  matt 	}
    123  1.1  matt 	if (funcp)
    124  1.1  matt 		*funcp = (tag >> 8) & 7;
    125  1.1  matt }
    126  1.1  matt 
    127  1.1  matt static pcireg_t
    128  1.1  matt arpci_conf_read(void *v, pcitag_t tag, int reg)
    129  1.1  matt {
    130  1.1  matt 	struct arpci_softc * const sc = v;
    131  1.1  matt 	pcireg_t rv = 0xffffffff;
    132  1.1  matt 
    133  1.1  matt 	if ((tag & 0x00ff0001) == 1) {
    134  1.1  matt 		KASSERT(((tag >> 11) & 31) > 20);
    135  1.1  matt 		/*
    136  1.1  matt 		 * This was a type 0 transaction for a device > 20 which
    137  1.1  matt 		 * we can't support.
    138  1.1  matt 		 */
    139  1.1  matt 		return rv;
    140  1.1  matt 	}
    141  1.1  matt 
    142  1.1  matt 	tag |= reg & -4;
    143  1.1  matt 
    144  1.1  matt #if 0
    145  1.1  matt 	bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR);
    146  1.1  matt 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR,
    147  1.1  matt 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AR7100_PCI_ERROR) & 3);
    148  1.1  matt #endif
    149  1.1  matt 
    150  1.1  matt 	bus_space_handle_t addr = sc->sc_bsh;
    151  1.1  matt 	if ((tag & ~0x7fe) == 0) {
    152  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    153  1.1  matt 		    AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_READ | tag);
    154  1.1  matt 		addr += AR7100_PCI_LCL_CFG_RDATA;
    155  1.1  matt 		printf("%s: tag %#lx ", __func__, tag);
    156  1.1  matt 	} else {
    157  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    158  1.1  matt 		    AR7100_PCI_CFG_ADDR, tag);
    159  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    160  1.1  matt 		    AR7100_PCI_CFG_CMD, PCI_CMD_CFG_READ);
    161  1.1  matt 		addr += AR7100_PCI_CFG_RDATA;
    162  1.1  matt 		printf("%s: AD[0:31] 0x%08lx: ", __func__, tag);
    163  1.1  matt 	}
    164  1.1  matt 
    165  1.1  matt 	rv = kfetch_32((void *)addr, 0xffffffff);
    166  1.1  matt 	printf("%#x\n", rv);
    167  1.1  matt 
    168  1.1  matt 	return rv;
    169  1.1  matt }
    170  1.1  matt 
    171  1.1  matt static void
    172  1.1  matt arpci_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
    173  1.1  matt {
    174  1.1  matt 	struct arpci_softc * const sc = v;
    175  1.1  matt 
    176  1.1  matt 	if ((tag & 0x00ff0001) == 1) {
    177  1.1  matt 		KASSERT(((tag >> 11) & 31) > 20);
    178  1.1  matt 		/*
    179  1.1  matt 		 * This was a type 0 transaction for a device > 20 which
    180  1.1  matt 		 * we can't support.
    181  1.1  matt 		 */
    182  1.1  matt 		return;
    183  1.1  matt 	}
    184  1.1  matt 
    185  1.1  matt 	tag |= reg & -4;
    186  1.1  matt 
    187  1.1  matt 	if ((tag & ~0x7fe) == 0) {
    188  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    189  1.1  matt 		    AR7100_PCI_LCL_CFG_CMD, AR7100_PCI_LCL_CFG_CMD_WRITE | tag);
    190  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    191  1.1  matt 		    AR7100_PCI_LCL_CFG_WDATA, data);
    192  1.1  matt 	} else {
    193  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    194  1.1  matt 		    AR7100_PCI_CFG_ADDR, tag);
    195  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    196  1.1  matt 		    AR7100_PCI_CFG_CMD, PCI_CMD_CFG_WRITE);
    197  1.1  matt 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    198  1.1  matt 		    AR7100_PCI_CFG_WDATA, data);
    199  1.1  matt 	}
    200  1.1  matt }
    201  1.1  matt 
    202  1.1  matt static int
    203  1.1  matt arpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    204  1.1  matt {
    205  1.1  matt 	return EINVAL;
    206  1.1  matt }
    207  1.1  matt 
    208  1.1  matt static const char *
    209  1.1  matt arpci_intr_string(void *v, pci_intr_handle_t ih)
    210  1.1  matt {
    211  1.1  matt 	return NULL;
    212  1.1  matt }
    213  1.1  matt 
    214  1.1  matt static const struct evcnt *
    215  1.1  matt arpci_intr_evcnt(void *v, pci_intr_handle_t ih)
    216  1.1  matt {
    217  1.1  matt 	return NULL;
    218  1.1  matt }
    219  1.1  matt 
    220  1.1  matt static void *
    221  1.1  matt arpci_intr_establish(void *v, pci_intr_handle_t ih,
    222  1.1  matt 	int ipl, int (*func)(void *), void *arg)
    223  1.1  matt {
    224  1.1  matt 	return NULL;
    225  1.1  matt }
    226  1.1  matt 
    227  1.1  matt static void
    228  1.1  matt arpci_intr_disestablish(void *v, void *cookie)
    229  1.1  matt {
    230  1.1  matt }
    231  1.1  matt 
    232  1.1  matt static void
    233  1.1  matt arpci_conf_interrupt(void *v, int bus, int dev, int func, int swiz, int *ilinep)
    234  1.1  matt {
    235  1.1  matt }
    236  1.1  matt 
    237  1.1  matt static void
    238  1.1  matt arpci_chipset_init(struct arpci_softc *sc)
    239  1.1  matt {
    240  1.1  matt 	pci_chipset_tag_t pc = &sc->sc_pc;
    241  1.1  matt 
    242  1.1  matt 	pc->pc_conf_v =			sc;
    243  1.1  matt 	pc->pc_attach_hook =		arpci_attach_hook;
    244  1.1  matt 	pc->pc_bus_maxdevs =		arpci_bus_maxdevs;
    245  1.1  matt 	pc->pc_make_tag =		arpci_make_tag;
    246  1.1  matt 	pc->pc_decompose_tag =		arpci_decompose_tag;
    247  1.1  matt 	pc->pc_conf_read =		arpci_conf_read;
    248  1.1  matt 	pc->pc_conf_write =		arpci_conf_write;
    249  1.1  matt 
    250  1.1  matt 	pc->pc_intr_v =			sc;
    251  1.1  matt 	pc->pc_intr_map =		arpci_intr_map;
    252  1.1  matt 	pc->pc_intr_string =		arpci_intr_string;
    253  1.1  matt 	pc->pc_intr_evcnt =		arpci_intr_evcnt;
    254  1.1  matt 	pc->pc_intr_establish =		arpci_intr_establish;
    255  1.1  matt 	pc->pc_intr_disestablish =	arpci_intr_disestablish;
    256  1.1  matt 
    257  1.1  matt 	pc->pc_conf_interrupt =		arpci_conf_interrupt;
    258  1.1  matt 
    259  1.1  matt #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
    260  1.1  matt 	//pc->pc_pciide_compat_intr_establish = arpci_pciide_compat_intr_establish;
    261  1.1  matt #endif
    262  1.1  matt }
    263  1.1  matt 
    264  1.1  matt static int
    265  1.1  matt arpci_match(device_t parent, cfdata_t cf, void *aux)
    266  1.1  matt {
    267  1.1  matt 	struct arbus_attach_args * const aa = aux;
    268  1.1  matt 	bus_space_handle_t bsh;
    269  1.1  matt 
    270  1.1  matt         if (strcmp(aa->aa_name, cf->cf_name) != 0)
    271  1.1  matt 		return 0;
    272  1.1  matt 
    273  1.1  matt 	if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &bsh))
    274  1.1  matt 		return 0;
    275  1.1  matt 
    276  1.1  matt 	bus_space_unmap(aa->aa_bst, bsh, aa->aa_size);
    277  1.1  matt 
    278  1.1  matt 	return 1;
    279  1.1  matt }
    280  1.1  matt 
    281  1.1  matt static void
    282  1.1  matt arpci_attach(device_t parent, device_t self, void *aux)
    283  1.1  matt {
    284  1.1  matt 	struct arbus_attach_args * const aa = aux;
    285  1.1  matt 	struct arpci_softc * const sc = device_private(self);
    286  1.1  matt 
    287  1.1  matt 	sc->sc_dev = self;
    288  1.1  matt 	sc->sc_bst = aa->aa_bst;
    289  1.1  matt 	sc->sc_dmat = aa->aa_dmat;
    290  1.1  matt 
    291  1.1  matt 	if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0,
    292  1.1  matt 		    &sc->sc_bsh)) {
    293  1.1  matt 		aprint_error(": failed to map registers\n");
    294  1.1  matt 		return;
    295  1.1  matt 	}
    296  1.1  matt 
    297  1.1  matt 	aprint_normal("\n");
    298  1.1  matt 	arpci_bus_mem_init(&sc->sc_memt, sc);
    299  1.1  matt 	arpci_chipset_init(sc);
    300  1.1  matt 
    301  1.1  matt 	sc->sc_pba_flags |= PCI_FLAGS_MEM_OKAY;
    302  1.1  matt 
    303  1.1  matt 	struct pcibus_attach_args pba;
    304  1.1  matt 	memset(&pba, 0, sizeof(pba));
    305  1.1  matt 
    306  1.1  matt 	pba.pba_flags = sc->sc_pba_flags;
    307  1.1  matt 	if (pba.pba_flags & PCI_FLAGS_MEM_OKAY)
    308  1.1  matt 		pba.pba_memt = &sc->sc_memt;
    309  1.1  matt 	pba.pba_dmat = aa->aa_dmat;
    310  1.1  matt 	pba.pba_pc = &sc->sc_pc;
    311  1.1  matt 	pba.pba_bus = 0;
    312  1.1  matt 
    313  1.1  matt 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    314  1.1  matt }
    315  1.1  matt 
    316  1.1  matt CFATTACH_DECL_NEW(arpci, sizeof(struct arpci_softc),
    317  1.1  matt     arpci_match, arpci_attach, NULL, NULL);
    318  1.1  matt 
    319  1.1  matt #define CHIP			arpci
    320  1.1  matt #define CHIP_LITTLE_ENDIAN	/* defined */
    321  1.1  matt #define CHIP_MEM		/* defined */
    322  1.1  matt #define CHIP_EXTENT		/* defined */
    323  1.1  matt #define	CHIP_EX_MALLOC_SAFE(v)	true
    324  1.1  matt #define CHIP_W1_BUS_START(v)	0x10000000UL
    325  1.1  matt #define CHIP_W1_BUS_END(v)	0x16ffffffUL
    326  1.1  matt #define CHIP_W1_SYS_START(v)	CHIP_W1_BUS_START(v)
    327  1.1  matt #define CHIP_W1_SYS_END(v)	CHIP_W1_BUS_END(v)
    328  1.1  matt 
    329  1.1  matt #include <mips/mips/bus_space_alignstride_chipdep.c>
    330